QA Interview Hub · Knowledge base · Quizzes · Українська

SQL › JOIN

How does INNER JOIN work?

INNER JOIN returns only the pairs of rows for which the ON condition is true, and everything without a pair silently disappears from the result. A NULL in the key matches nothing, not even another NULL, so such a row disappears too. That is why the row count after a JOIN is compared with the source table, and missing pairs are found with an anti-join.

SQL · JOIN

The table has 4 orders worth 140 and the JOIN report shows 100. Which order vanished?

A tester reconciles revenue: the sum over the orders table is 140, while the report that adds the buyer's email through a JOIN shows 100. The query has no error and the database did not fail. INNER JOIN simply dropped everything for which it found no pair. This lesson is about what exactly INNER JOIN keeps, why a missing row gives no signal and how to turn that into a data integrity check.

How it works and why. INNER JOIN returns the intersection: only pairs for which the ON condition is true. The word INNER can be omitted, because JOIN is inner by default. The design makes sense when the link is mandatory: an order without a buyer should not appear in a report about buyers. The silence is also the weak spot: SQL does not treat a vanished row as an event, so there is no warning or error.

The ON condition is true only when both sides are known and equal. NULL never equals anything, not even another NULL (the comparison gives UNKNOWN and JOIN takes only true). So a row with user_id = NULL finds no pair anywhere and vanishes the same way as a row that points to a user that does not exist.

Example: the sum that does not add up. The table has four orders: 100 (Anna, 50), 101 (Anna, 30), 102 (Ivan, 20) and 103 (a guest order, no user_id, 40). The initial query:

-- revenue by email: 100, although orders hold 140
SELECT SUM(o.total)
FROM orders o
JOIN users u ON u.id = o.user_id;

It returns 100 instead of 140. To find the cause, do not guess: compare the counts before and after the join, and then output the rows that are missing:

SELECT COUNT(*) AS all_orders,
       COUNT(user_id) AS with_user
FROM orders;
-- illustration: 4 and 3

SELECT id, total
FROM orders
WHERE user_id IS NULL
   OR NOT EXISTS (SELECT 1 FROM users u WHERE u.id = orders.user_id);

The gap between 4 and 3 says that one order has no user. The second query names it: 103. Two conclusions here. For a report "revenue by buyer" the result 100 may be right, since a guest order belongs to no buyer, but only if that is known and agreed. If by the requirement an order must have a user, order 103 is a data defect, and INNER JOIN hid it.

What INNER JOIN dropped

orders
iduser_idtotal
100150
101130
102220
103NULL40 lost
JOIN result
orderemailtotal
100anna@test.com50
101anna@test.com30
102ivan@test.com20
Figure 1. A NULL key has no pair, so order 103 vanishes without any error: the sum is 100 instead of 140.

Limits and alternatives. In a chain of several INNER JOINs the loss accumulates: a row must pass every link.

-- an INNER JOIN chain: one missing link drops the row
SELECT o.id, u.email, p.amount
FROM orders o
JOIN users u    ON u.id = o.user_id
JOIN payments p ON p.order_id = o.id;

Here an order with no payment vanishes even if the buyer exists. So when you need "everything from one table and, if present, data from another", take LEFT JOIN (the next lesson). When you only need to prove that a related record exists, without multiplying rows, EXISTS fits. When losing rows is undesirable, the best fix is a foreign key and NOT NULL in the schema: then an "order with no user" cannot be created at all, and INNER JOIN has nothing to drop. The table order in an INNER JOIN does not affect the result; the optimiser picks where to start.

Common mistakes.

  • Using INNER JOIN for the question "what is missing": whatever has no pair just disappears from the result.
  • Not comparing the row count after a JOIN with the source table: the loss goes unnoticed, the sum looks plausible.
  • Assuming a NULL key joins with a NULL in another table: it never matches.
  • Building a long INNER JOIN chain in a report "about everything": each link quietly filters rows.
  • Adding an INNER JOIN to a working query without recomputing the result: the report "lost weight" for no visible reason.

Check yourself

orders has 1000 rows, user_id is filled in all of them, and users lacks one user referenced by 5 orders. How many rows does an INNER JOIN with users return and how do you detect it without browsing the table?

Answer

995 rows. The orders vanished with no error. Detect it by comparison: SELECT COUNT(*) FROM orders against COUNT(*) after the JOIN, and then an anti-join (LEFT JOIN … WHERE u.id IS NULL) will show those 5 orders.

Why do rows with user_id = NULL in orders and id = NULL (if there were such) in users not join under ON u.id = o.user_id?

Answer

The comparison NULL = NULL gives UNKNOWN, not true, and INNER JOIN keeps only pairs with a true condition. So NULL does not match even NULL. If you need NULLs treated as equal, PostgreSQL has IS NOT DISTINCT FROM, but for keys that is almost always a sign of a problem in the data.

In an interview. Junior: INNER JOIN returns only rows that have a pair in both tables, the rest disappear. So if there are fewer records after a JOIN than expected, I check whether some records have no pair: it may be a data problem. Middle/Senior: The ON condition takes only true pairs and NULL equals nothing, so a row with a NULL key drops silently. I treat each link of an INNER JOIN chain as a filter. I compare COUNT(*) before and after and look for the missing rows with an anti-join. In the schema I back the link with a foreign key and NOT NULL.

More articles in this chapter