CodingNeed.

3 · Connect and build · 8 MIN

Connect related data

Connect related data.

JOIN matches rows using the ON condition. INNER JOIN keeps matching rows. LEFT JOIN also keeps unmatched left rows, with NULL for missing right-side values. Multiple right-side matches multiply output rows.

Think of a spreadsheet: select columns, keep relevant rows, and summarize the result.

Read the example

SELECT e.id, c.label FROM expenses e JOIN (SELECT 'food' AS category, 'Meals' AS label) c ON e.category = c.category ORDER BY e.id;
Check the expected output
[[1,"Meals"],[3,"Meals"]]

Your challenge

Return every expense id with its matching label, or null if no label matches. Use the derived label table shown in the example.

Common trap

An inner join silently drops unmatched rows.

Next lesson: Project · Spending report