DBMS & SQL · Intermediate · Topic 2 of 4
SQL: Joins
9 questions.
Your progress0%
- 1st Visit
- 0/9
- 2nd Visit
- 0/9
- Revise
- 0/9
Overview
A join combines rows from two tables using a related column, usually a foreign key matching a primary key. INNER JOIN keeps only matching pairs; LEFT JOIN keeps every row of the left table and fills the missing right side with NULL; CROSS JOIN pairs every row with every row; and a self join compares rows within one table, such as employees and their managers. Watch the row count: joining on a non-unique column multiplies rows and can inflate sums. Signals include data spread across several tables, "including those with no orders" (left join) and "customers who never ordered" (left join with IS NULL).
SELECT c.name, COUNT(o.id) AS orders
FROM customers c
LEFT JOIN orders o ON o.customer_id = c.id
GROUP BY c.id, c.name; -- customers with no orders show 0