LEFT, RIGHT and FULL joins
SQL · 15 questions
Short notes
Key idea
A LEFT JOIN keeps every row of the left table, matched or not. Where nothing matches, the right table's columns come back as NULL. That makes it the tool for "who has none?" questions.
Syntax
SELECT c.name, o.order_id
FROM customers c
LEFT JOIN orders o ON o.customer_id = c.customer_id;
RIGHT JOINis the mirror image; most people just swap the tables.FULL OUTER JOINkeeps unmatched rows from both sides.
Example
Customers who never ordered:
SELECT c.name
FROM customers c
LEFT JOIN orders o ON o.customer_id = c.customer_id
WHERE o.order_id IS NULL;
Traps
- A
WHEREon the right table undoes the left join.WHERE o.status = 'paid'drops the NULL rows. Put that condition in theONclause instead. COUNT(*)counts unmatched rows as 1. UseCOUNT(o.order_id)to get 0 for customers with no orders.- Use
COALESCE(SUM(o.amount), 0)to show 0 instead of NULL.
Want the full lesson with step-by-step examples, a project and a certificate? SQL course, ₹120 →
- Question 1easy
customers LEFT JOIN orders: which customers appear in the result? - Question 2medium
40 customers placed 80 orders between them, and 9 customers never ordered. How many rows does this return?
SELECT COUNT(*) FROM customers c LEFT JOIN orders o ON o.customer_id = c.customer_id;
- Question 3hard
Customer 11 never placed an order. What does this return?
SELECT COUNT(*) - COUNT(o.order_id) FROM customers c LEFT JOIN orders o ON o.customer_id = c.customer_id WHERE c.customer_id = 11;
- Question 4hard
You want every customer with their delivered orders, keeping customers who have none. Which is right?
- Question 5easy
In
customers c LEFT JOIN orders o, customer 1 has no orders. What iso.amounton customer 1's row?
LEFT, RIGHT and FULL joins
LEFT, RIGHT and FULL joins
LEFT, RIGHT and FULL joins
LEFT, RIGHT and FULL joins
LEFT, RIGHT and FULL joins
10 more questions on this topic, with hints, and tests
Sign up free for 21 days of every question and test.