SQL LEFT JOIN Flashcards: ON vs WHERE

Predict which rows survive a SQL LEFT JOIN, choose ON or WHERE, and count matches without losing rows with zero matches.

ಈ ಡೆಕ್ ಬಗ್ಗೆ

Practice SQL LEFT JOIN with short, PostgreSQL-checked examples. Given a query and a few rows, predict which IDs appear or what count you get. Given a reporting requirement, choose where the filter goes and write the join or missing-match test. Debug queries that drop rows, mistake a missing value for a missing match, or report one match when there are zero.

Start with what counts as a match and how many rows a join produces. Then alternate result prediction, filter placement, missing-match tests, and counting so each new scenario makes you recall the rule again. The deck stays with two-table queries so you can isolate filter placement. Multi-join fanout, alternative subquery patterns, and query tuning are outside its scope.

The cards and fictional examples were written independently and checked against PostgreSQL's table expression documentation. The cover was generated with AI.

ಈ ಡೆಕ್‌ನ ಕಾರ್ಡ್‌ಗಳು

  1. ಕಾರ್ಡ್ 1

    ಪ್ರಶ್ನೆ

    A left row has no right row that satisfies the ON condition. What does the LEFT JOIN produce for that left row?

    ಉತ್ತರ

    One row with the original left values and NULL in every right-side column.

  2. ಕಾರ್ಡ್ 2

    ಪ್ರಶ್ನೆ

    A LEFT JOIN uses a.code = b.code in its ON clause. Do two NULL codes match?

    ಉತ್ತರ

    No. NULL = NULL evaluates to unknown, and a pair matches only when ON evaluates to true.

  3. ಕಾರ್ಡ್ 3

    ಪ್ರಶ್ನೆ

    A café has three bookings that satisfy a LEFT JOIN condition. How many joined rows does that café contribute before any filtering or grouping?

    ಉತ್ತರ

    Three rows, one per matching booking.

  4. ಕಾರ್ಡ್ 4

    ಪ್ರಶ್ನೆ

    Show every greenhouse and attach only sensors whose status is 'working'. Put the status condition in ON or WHERE?

    ಉತ್ತರ

    Put it in ON alongside the key match. A greenhouse with zero working sensors still appears with NULL sensor columns.

    SELECT g.id, s.id AS sensor_id
    FROM greenhouses AS g
    LEFT JOIN sensors AS s
      ON s.greenhouse_id = g.id AND s.status = 'working';
    
  5. ಕಾರ್ಡ್ 5

    ಪ್ರಶ್ನೆ

    A LEFT JOIN to deliveries AS d exposes d.id (the primary key) and d.note (nullable). Which column can you test for NULL to detect a missing delivery?

    ಉತ್ತರ

    d.id. Every real delivery has a non-null primary key, so WHERE d.id IS NULL selects the rows with no match.

  6. ಕಾರ್ಡ್ 6

    ಪ್ರಶ್ನೆ

    Which result of a SQL WHERE condition keeps a row: true, false, or unknown (NULL)?

    ಉತ್ತರ

    True. False and unknown both discard the row.

  7. ಕಾರ್ಡ್ 7

    ಪ್ರಶ್ನೆ

    Hubs 4 and 6 are left-joined to parcels on hub ID. Parcels 51 and 52 belong to hub 4, and hub 6 has none. Which (hub_id, parcel_id) pairs does the join produce?

    ಉತ್ತರ

    (4, 51), (4, 52), and (6, NULL). Hub 4 has one row per match. Hub 6 gets one row with a null parcel ID.

  8. ಕಾರ್ಡ್ 8

    ಪ್ರಶ್ನೆ

    Orchards 1, 2, and 3 have these inspections: 31 passed at orchard 1, 32 failed at orchard 2, and orchard 3 has none. Which (orchard_id, inspection_id) pairs survive?

    SELECT o.id, i.id
    FROM orchards AS o
    LEFT JOIN inspections AS i ON i.orchard_id = o.id
    WHERE i.result = 'passed';
    

    ಉತ್ತರ

    (1, 31) is the only pair. Orchard 2 fails the filter, and orchard 3 has a null result, so the comparison is unknown.

  9. ಕಾರ್ಡ್ 9

    ಪ್ರಶ್ನೆ

    A report left-joins bins to items, groups by bin ID, and selects COUNT(*). What count does an empty bin get?

    ಉತ್ತರ

    1. The join supplies one row for the bin with null item columns. COUNT(*) counts that row.

  10. ಕಾರ್ಡ್ 10

    ಪ್ರಶ್ನೆ

    The report should show only open studios, including any open studio with no rentals. Where should s.is_open go in a query starting from studios AS s and left-joining rentals?

    ಉತ್ತರ

    Put s.is_open in WHERE. It filters the studios in the final result and keeps open studios with no rental.

    SELECT s.id, r.id AS rental_id
    FROM studios AS s
    LEFT JOIN rentals AS r ON r.studio_id = s.id
    WHERE s.is_open;
    
  11. ಕಾರ್ಡ್ 11

    ಪ್ರಶ್ನೆ

    The query below joins trails to checkpoints. Checkpoint ID is the primary key. Add a clause to return only trails with no checkpoints.

    SELECT t.id
    FROM trails AS t
    LEFT JOIN checkpoints AS c ON c.trail_id = t.id
    

    ಉತ್ತರ

    WHERE c.id IS NULL.

  12. ಕಾರ್ಡ್ 12

    ಪ್ರಶ್ನೆ

    Plots 1, 2, and 3 have these samples: sample 91 for plot 1 is approved, sample 92 for plot 2 is pending, and plot 3 has none. Which (plot_id, sample_id) pairs does the query return?

    SELECT p.id, s.id
    FROM plots AS p
    LEFT JOIN samples AS s
      ON s.plot_id = p.id AND s.status = 'approved';
    

    ಉತ್ತರ

    (1, 91), (2, NULL), and (3, NULL). Plots 2 and 3 both have zero samples that qualify as matches.

  13. ಕಾರ್ಡ್ 13

    ಪ್ರಶ್ನೆ

    A route with no notices disappears after a LEFT JOIN followed by WHERE n.state <> 'archived'. Why does that route disappear?

    ಉತ್ತರ

    Its notice state is NULL. The comparison evaluates to unknown, which WHERE discards.

  14. ಕಾರ್ಡ್ 14

    ಪ್ರಶ್ನೆ

    A report left-joins shelves to boxes and groups by shelf ID. b.id is the box primary key. Which count expression returns zero for an empty shelf?

    ಉತ್ತರ

    COUNT(b.id). It counts non-null box IDs, so the row for an empty shelf contributes zero.

  15. ಕಾರ್ಡ್ 15

    ಪ್ರಶ್ನೆ

    Studio 1 is open and has rental 41. Studio 2 is closed and has rental 42. Studio 3 is open and has no rental. Which (studio_id, rental_id) pairs does the query return?

    SELECT s.id, r.id
    FROM studios AS s
    LEFT JOIN rentals AS r
      ON r.studio_id = s.id AND s.is_open;
    

    ಉತ್ತರ

    (1, 41), (2, NULL), and (3, NULL). The ON test prevents a rental from matching studio 2. The studio itself still appears.

  16. ಕಾರ್ಡ್ 16

    ಪ್ರಶ್ನೆ

    Room 1 has a booking with a null comment. Room 2 has no bookings. After a LEFT JOIN to bookings AS b, which room IDs pass WHERE b.comment IS NULL?

    ಉತ್ತರ

    Both 1 and 2. Room 1 has a real booking with a null comment. Room 2 gets null booking columns from the join.

  17. ಕಾರ್ಡ್ 17

    ಪ್ರಶ್ನೆ

    Pumps 1 and 2 have these check records: pump 1 has a failed check, and pump 2 has no checks. Which pump IDs survive this query? c.id is the check primary key.

    SELECT p.id
    FROM pumps AS p
    LEFT JOIN pump_checks AS c ON c.pump_id = p.id
    WHERE c.status = 'passed' OR c.id IS NULL;
    

    ಉತ್ತರ

    Only pump 2. Pump 1 already matched a real check, so the join supplied no row with a null check ID. The failed check is then discarded by WHERE.

  18. ಕಾರ್ಡ್ 18

    ಪ್ರಶ್ನೆ

    Lockers are left-joined to assignments on a.locker_id = l.id. The assignment table enforces UNIQUE (locker_id). How many rows does the join produce per locker?

    ಉತ್ತರ

    Exactly one. At most one assignment can match each locker ID, and a locker with no assignment gets one row with null assignment columns.

  19. ಕಾರ್ಡ್ 19

    ಪ್ರಶ್ನೆ

    A bay has two shipment rows after a LEFT JOIN. Both have a shipment ID, and only one has a tracking code. What does COUNT(s.tracking_code) return for that bay?

    ಉತ್ತರ

    1. It counts rows with a tracking code. COUNT(s.id) would count both shipments.

  20. ಕಾರ್ಡ್ 20

    ಪ್ರಶ್ನೆ

    Find labs with no completed calibration, including labs with only pending calibrations. c.id is the calibration primary key. How should you write the join and filter?

    ಉತ್ತರ

    Match completed calibrations in ON, then test c.id IS NULL in WHERE.

    SELECT l.id
    FROM labs AS l
    LEFT JOIN calibrations AS c
      ON c.lab_id = l.id AND c.status = 'completed'
    WHERE c.id IS NULL;
    
  21. ಕಾರ್ಡ್ 21

    ಪ್ರಶ್ನೆ

    Return one row per approved repair on a bicycle. Omit bicycles with no approved repair. Which join expresses that requirement directly?

    ಉತ್ತರ

    INNER JOIN with approval as part of the match condition.

    SELECT b.id, r.id AS repair_id
    FROM bicycles AS b
    INNER JOIN repairs AS r
      ON r.bicycle_id = b.id AND r.status = 'approved';
    
  22. ಕಾರ್ಡ್ 22

    ಪ್ರಶ್ನೆ

    A LEFT JOIN matches visit 71 to visitor 1. The visit's status is NULL. Does WHERE v.status = 'confirmed' OR v.status IS NULL keep this real visit?

    ಉತ್ತರ

    Yes. The IS NULL part is true for this visit's stored status. The same test is true for the null status supplied for a visitor with no match.

  23. ಕಾರ್ಡ್ 23

    ಪ್ರಶ್ನೆ

    Show only active classes, with any in-person sessions attached. Active classes with zero in-person sessions must still appear. Where do the two filters belong?

    ಉತ್ತರ

    Put the session format in ON and the class activity filter in WHERE.

    SELECT c.id, s.id AS session_id
    FROM classes AS c
    LEFT JOIN class_sessions AS s
      ON s.class_id = c.id AND s.format = 'in-person'
    WHERE c.is_active;
    
  24. ಕಾರ್ಡ್ 24

    ಪ್ರಶ್ನೆ

    Stages 1, 2, and 3 have these bookings: stage 1 has one paid and one unpaid, stage 2 has only one unpaid, and stage 3 has none. What paid count does each stage get? b.id is the booking primary key.

    SELECT s.id, COUNT(b.id)
    FROM stages AS s
    LEFT JOIN stage_bookings AS b
      ON b.stage_id = s.id AND b.status = 'paid'
    GROUP BY s.id;
    

    ಉತ್ತರ

    Stage 1 gets 1. Stages 2 and 3 each get 0. Both stages with no paid match still have a group, and the null booking ID contributes zero.

  25. ಕಾರ್ಡ್ 25

    ಪ್ರಶ್ನೆ

    To find rows with no matched label after a LEFT JOIN, use WHERE l.id = NULL or WHERE l.id IS NULL? l.id is the label primary key.

    ಉತ್ತರ

    Use WHERE l.id IS NULL. Equality with NULL evaluates to unknown, so WHERE l.id = NULL keeps no rows.

  26. ಕಾರ್ಡ್ 26

    ಪ್ರಶ್ನೆ

    A “keep every pump” report is tested with one pump that has a passing check and one that has no checks. c.id is the check primary key. What case can expose the bug in a proposed fix that uses WHERE c.status = 'passed' OR c.id IS NULL after joining on pump ID?

    ಉತ್ತರ

    Add a pump with a failed check and no passing checks. Its real failed match is filtered out, so the proposed fix drops the pump.

Wooden tray with three slots, two holding green and amber tiles and one empty.

26 ಕಾರ್ಡ್‌ಗಳು

SQL LEFT JOIN Flashcards: ON vs WHERE

ಈ ಡೆಕ್ ಅನ್ನು ಉಚಿತವಾಗಿ ಅಧ್ಯಯನ ಮಾಡಿ

ಅಧ್ಯಯನ ಆರಂಭಿಸಲು Nibomo ತೆರೆಯುತ್ತದೆ.

ಹೋಲುವ ಡೆಕ್‌ಗಳು