Skip to content
engineering workbench.by StudyPort Academy
Learning path
Foundations18 min

Joins in Depth: Inner, Left, and Dropped Rows

Understand the idea

Why the wrong join type silently deletes records from your dashboards without throwing an error.

The idea

In relational schemas, related data lives in separate tables. The SQL operator lets you combine rows from two or more tables based on a shared column (usually linking a to a ).

The two most common joins are:

  1. INNER JOIN: Keeps only rows that match in BOTH tables. If a user has never placed an order, an INNER JOIN silently leaves that user out of the results.
  2. LEFT JOIN: Keeps EVERY row from the left table, even if there are no matching rows on the right. Any missing right-side data is filled with .

Silently dropped rows are one of the most common surprises for beginners. In our database, user Dave (id 4) has placed zero orders. An INNER JOIN between users and orders silently drops Dave from the output. In a user dashboard or customer export, Dave completely vanishes. Using a LEFT JOIN guarantees that Dave remains visible in the result set with order columns populated as NULL.

What changes

SQL Join Types Compared
Join TypeRows ReturnedBehavior When No Match Exists
INNER JOINOnly rows with matching keys in BOTH tablesNon-matching rows from both tables are discarded
LEFT JOINALL rows from left table + matches from rightRight-side columns are filled with NULL
RIGHT JOINALL rows from right table + matches from leftLeft-side columns are filled with NULL
FULL OUTER JOINALL rows from both tables combinedFills NULL on whichever side lacks a corresponding match

The command

INNER JOIN dropping Dave vs LEFT JOIN preserving Dave

3s timeout · one stateless run. Node and Express use Piston when that instance accepts the job (public EMKC execute is whitelist-only; set PISTON_URL or PISTON_TOKEN). Otherwise a local Node process. MongoDB uses a fresh in-memory server per run, then it is stopped.

QUERY VISUALIZER

Before the run · edit the code, then press Run to see what changes.

Source tables appear here when available; successful runs show their captured data.

Match rule: IDs match and the user lives in New York. Bob and Dave have no matching right row; order 3 has no matching left row. Choose a join, then Run.

Compare all four joins

3s timeout · one stateless run. Node and Express use Piston when that instance accepts the job (public EMKC execute is whitelist-only; set PISTON_URL or PISTON_TOKEN). Otherwise a local Node process. MongoDB uses a fresh in-memory server per run, then it is stopped.

QUERY VISUALIZER

Before the run · edit the code, then press Run to see what changes.

Source tables appear here when available; successful runs show their captured data.

The mistake

Check the result

Run the example to see the INNER JOIN output 4 rows (omitting Dave entirely), while the LEFT JOIN outputs 5 rows, preserving Dave with a NULL order_id.

Guided challenge

Choose the right join type

Update the function to return 'left' when needAllUsers is true, and 'inner' when false.

Choose the right join type

3s timeout · one stateless run. Node and Express use Piston when that instance accepts the job (public EMKC execute is whitelist-only; set PISTON_URL or PISTON_TOKEN). Otherwise a local Node process. MongoDB uses a fresh in-memory server per run, then it is stopped.

EXPECTED RESULT

left
inner

Hints 0/1

    Use all hints or make 3 failed checks (0/3).

    Free exploration

    Change this example and run it again. Each run is a new process. Nothing is saved.

    Your variation

    3s timeout · one stateless run. Node and Express use Piston when that instance accepts the job (public EMKC execute is whitelist-only; set PISTON_URL or PISTON_TOKEN). Otherwise a local Node process. MongoDB uses a fresh in-memory server per run, then it is stopped.

    Quiz

    A quick check for understanding. Retry as often as you like; your learning path stays open.

    01Why does an INNER JOIN between users and orders exclude users who have placed zero orders?
    02How does a LEFT JOIN represent columns from the right table when no matching record exists?

    Ready to call this one understood?

    Mark this lesson complete to earn 100 XP. You can always revisit it.

    Sign in to save progress
    Next lesson