CHAPTER 15
Keeping all right table rows — even when there is no match.
🌟 Think of it this way: You want all orders, even if some customers are missing. RIGHT JOIN keeps all rows from the right table (orders). In SQLite, we achieve this by reversing the tables and using LEFT JOIN.
Returns all orders, including those that may not have matching customers (simulated using LEFT JOIN).
SQL
SELECT o.order_id, c.name, o.total FROM orders o LEFT JOIN customers c ON c.customer_id = o.customer_id ORDER BY o.order_id;
Find orders that do NOT have matching customers using LEFT JOIN + IS NULL.
SQL
SELECT o.order_id FROM orders o LEFT JOIN customers c ON c.customer_id = o.customer_id WHERE c.customer_id IS NULL;
✅ Pro Tip: RIGHT JOIN is rarely used in practice. Most developers prefer LEFT JOIN because it is more readable and widely supported.
| customer_id | name | city | country |
|---|---|---|---|
| 1 | Ananya | Hyderabad | India |
| 2 | Rohan | Bangalore | India |
| 3 | Sam | Mumbai | India |
| 4 | Lisa | London | UK |
| 5 | Ravi | Delhi | India |
| order_id | customer_id | order_date | total | status |
|---|---|---|---|---|
| 1 | 1 | 2024-01-15 | 79999 | completed |
| 2 | 2 | 2024-01-20 | 3498 | completed |
| 3 | 1 | 2024-02-10 | 65000 | completed |
| 4 | 3 | 2024-02-14 | 499 | pending |
| 5 | 2 | 2024-03-01 | 248 | completed |
| 6 | 5 | 2024-03-15 | 2999 | shipped |
| 7 | 1 | 2024-04-05 | 149 | completed |