SQL Tutorial · Revision notes
SQL Joins & Subqueries
A JOIN combines rows from two or more tables using a related column. A subquery is a query placed inside another query. Together they let us answer questions that need data from many tables.
Quick revision
- INNER JOIN = only matching rows.
- LEFT JOIN = all left rows + matches. RIGHT JOIN = all right rows + matches. FULL JOIN = everything.
- LEFT JOIN + WHERE right.key IS NULL finds rows with no match.
- CROSS JOIN gives rows A x rows B. SELF JOIN uses the same table twice with aliases.
- A subquery is a query in brackets. A normal one runs once.
- A correlated subquery uses the outer row and runs per row.
- IN checks a list. EXISTS checks if any row exists. > ANY = greater than minimum. > ALL = greater than maximum.
- Prefer NOT EXISTS over NOT IN when NULL can appear.