Skip to main content
Beginner Step-by-Step Tutorial 11 min read PostgreSQL 16 / MySQL 8

SQL Joins & Composite B-Tree Indexes Explained with EXPLAIN

Write fast relational queries: understand JOIN semantics without accidental NULL filtering and design composite indexes using the leftmost-prefix rule.

Written by Ravi Shankar Mishra (Founder & Lead Systems Architect, MockAttempt Academy) Reviewed by MockAttempt Engineering Editorial Desk
Last updated: Verification Policy

Learning Objectives

  • Avoid accidentally converting a `LEFT JOIN` into an `INNER JOIN` via `WHERE` filters on nullable columns.
  • Order columns inside a composite B-Tree index (`(equality_col, range_or_sort_col)`) for Index-Only / Index Scans.

Prerequisites

  • Basic `SELECT ... FROM ... WHERE` relational table familiarity

INNER vs LEFT JOIN: The Silent WHERE Trap

INNER JOIN keeps rows matching on both sides, whereas LEFT JOIN preserves every row from the left table and fills right-side columns with NULL when unmatched. If you then write WHERE right_table.status = 'published', the database drops all `NULL` rows after joining — silently flattening your query back into an inner join! Move right-table conditions inside the ON clause instead.

Composite B-Tree Index Column Order Matters

A composite index on (learning_category_id, status, published_at) behaves like a phone book sorted first by category, second by status, and third by date. Queries filtering on learning_category_id or (learning_category_id, status) jump straight via the B-Tree, whereas filtering solely on `published_at` often falls back to a sequential scan.

Apply indexing directly in Spring Data JPA Performance and Java / Python Backend Career Programs.

Practical Programming Examples

Leftmost Composite Index + Safe LEFT JOIN Filter (PostgreSQL 16)

Count published articles per active category (including categories with zero articles yet) while leveraging a composite index.

PostgreSQL 16 / MySQL 8 / SQLite
CREATE INDEX idx_articles_cat_status_pub
    ON learning_articles (learning_category_id, status, published_at);

SELECT
    c.slug,
    COUNT(a.id) AS published_count
FROM learning_categories AS c
LEFT JOIN learning_articles AS a
    ON a.learning_category_id = c.id
    AND a.status = 'published'
WHERE c.is_active = TRUE
GROUP BY c.id, c.slug
ORDER BY c.sort_order ASC;
Expected Output
Categories with 0 matching published articles still appear cleanly with `published_count = 0`.
Step-by-Step Execution
  1. Keeping `a.status = 'published'` inside `LEFT JOIN ... ON` preserves unmatched categories cleanly.
  2. The `(learning_category_id, status, published_at)` composite index matches equality predicates in leftmost order.

Common Mistakes to Avoid

  • Adding standalone indexes blindly on every column (slows `INSERT`/`UPDATE` writes without helping multi-predicate reads).
  • Wrapping indexed columns inside functions (`WHERE LOWER(email) = ...` or `YEAR(created_at) = 2026`), disabling B-Tree index usage unless using range bounds or expression indexes.

Production Best Practices

  • Place equality columns before range/sort columns (`=` before `>`, `<`, `BETWEEN`) when ordering composite indexes.
  • Inspect actual execution paths using `EXPLAIN (ANALYZE, BUFFERS)` in PostgreSQL or `EXPLAIN` in MySQL.

Technical Interview Relevance

One of the highest-frequency backend & full-stack SQL interview topics across service and product companies.

Practice & Self-Check Questions

Given index `(a, b, c)`, can a query filtering solely on `WHERE b = 10 AND c = 20` seek directly into the B-Tree index?

Generally no (without index skip-scan optimizations), because B-Tree keys are ordered primarily by leading column `a`.

Optional Next Step

Have Questions About Learning SQL & Databases Structured Cohorts?

Request the JBCP syllabus & weekend batch schedule from an Academic Counselor.

View JBCP Details →
Learn. Practise. Prove. Get Career-Ready.

Ready to Build Your IT Career?

Explore industry-led programmes and develop practical skills through training, assessments and interview preparation.