Chapter 5 · JOINs
SQL JOINs Practice
Real data is split across tables: customers in one, orders in another, products in a third. JOINs put them back together using the keys that link them, such as orders.customer_id = customers.id.
The key decision is which rows to keep. INNER JOIN keeps only matches; LEFT JOIN keeps every row from the left table, so you can find customers with no orders or products that never sold.
Start practising75 exercises · 10 easy, 63 medium, 2 hard
Key ideas
- INNER JOIN
- Rows that match in both tables. Unmatched rows disappear.
- LEFT JOIN
- Every row from the left table; columns from the right are NULL when there is no match.
- Anti-join
- LEFT JOIN … WHERE right.id IS NULL finds rows with no match (customers who never ordered).
- Self join
- Join a table to itself, e.g. to compare rows within the same table.
Example: Customers with the most orders (including those with none)
SELECT c.first_name, c.last_name,
COUNT(o.id) AS orders
FROM customers c
LEFT JOIN orders o ON o.customer_id = c.id
GROUP BY c.id, c.first_name, c.last_name
ORDER BY orders DESC
LIMIT 5;LEFT JOIN keeps customers without orders, and COUNT(o.id) counts them as 0 instead of 1.
In interviews
Expect "find customers who never placed an order". Show the LEFT JOIN + IS NULL pattern, then mention NOT EXISTS as an alternative.
All 75 JOINs exercises
Ordered from easiest to hardest. Each one runs on a real database with instant feedback.
- 01Order Details with Customer NameecommerceEasy
- 02Products with Category NamesecommerceEasy
- 03Rarely Ordered ProductsecommerceEasy
- 04Orders with Customer NamesecommerceEasy
- 05Number of Customers Per Branch CityfinanceEasy
- 06Total Interest Paid by Loan TypefinanceEasy
- 07Customers with Any Frozen AccountfinanceEasy
- 08High-Value Savings Account HoldersfinanceEasy
- 09Patient Visits with Doctor NameshealthcareEasy
- 10Visits That Had Abnormal Lab ResultshealthcareEasy
- 11Customers Who Never OrderedecommerceMedium
- 12Order Items with Full DetailsecommerceMedium
- 13Orders with Payment and Shipping StatusecommerceMedium
- 14Customer Order SummaryecommerceMedium
- 15Products Never ReviewedecommerceMedium
- 16Total Revenue per Product CategoryecommerceMedium
- 17Products with Average Rating and Review CountecommerceMedium
- 18Customers in the Same CityecommerceMedium
- 19Orders with Shipping DelaysecommerceMedium
- 20Delivered Orders Without Completed PaymentecommerceMedium
- 21Customer Order History with NamesecommerceMedium
- 22Order Line Items with Product DetailsecommerceMedium
- 23Product Catalog with Category NamesecommerceMedium
- 24Orders with Shipping StatusecommerceMedium
- 25Detailed Review ReportecommerceMedium
- 26Customer Account ListfinanceMedium
- 27Customers with No AccountsfinanceMedium
- 28Transaction Details with Customer InfofinanceMedium
- 29Cards with Account InformationfinanceMedium
- 30Loan Payments with Customer NamesfinanceMedium
- 31Total Balance Per CustomerfinanceMedium
- 32Accounts with Card CountfinanceMedium
- 33Customers in Branch CitiesfinanceMedium
- 34Customers with Both Checking and SavingsfinanceMedium
- 35Average Transaction Amount Per CustomerfinanceMedium
- 36Credit Card Utilization RatefinanceMedium
- 37Expired Cards with Customer InfofinanceMedium
- 38Number of Cards Per CustomerfinanceMedium
- 39Customer First and Last Transaction DatesfinanceMedium
- 40Customer Pairs in the Same CityfinanceMedium
- 41Customers with Active Loans but No PaymentsfinanceMedium
- 42Customer Card Details ReportfinanceMedium
- 43Each Loan with Its Most Recent PaymentfinanceMedium
- 44Accounts That Have No Cards IssuedfinanceMedium
- 45Customer Total Balance with Branch InfofinanceMedium
- 46Customer Transaction SummaryfinanceMedium
- 47Loan Payment Progress TrackerfinanceMedium
- 48Average Balance by Branch CityfinanceMedium
- 49Customers Who Opened Accounts on the Same DayfinanceMedium
- 50Visits with Patient NameshealthcareMedium
- 51Doctors with Department NameshealthcareMedium
- 52Patients with Insurance DetailshealthcareMedium
- 53Visit Details with PrescriptionshealthcareMedium
- 54Visit Count per DoctorhealthcareMedium
- 55Billing Records with Patient InfohealthcareMedium
- 56Lab Results with Visit and Patient InfohealthcareMedium
- 57Revenue per DepartmenthealthcareMedium
- 58Number of Unique Patients per DoctorhealthcareMedium
- 59Total Billing by Insurance Plan TypehealthcareMedium
- 60Overdue Bills with Patient Contact InfohealthcareMedium
- 61Number of Lab Tests per VisithealthcareMedium
- 62Doctors with the Fewest VisitshealthcareMedium
- 63Doctor Pairs in the Same DepartmenthealthcareMedium
- 64Patient Out-of-Pocket vs Insurance CoveragehealthcareMedium
- 65Complete Visit Detail ReporthealthcareMedium
- 66Prescriptions with Patient, Doctor, and Diagnosis DetailshealthcareMedium
- 67Patient Visit History with Doctor DetailshealthcareMedium
- 68Average Prescriptions per Visit by DoctorhealthcareMedium
- 69Billing Breakdown by Insurance Plan TypehealthcareMedium
- 70Department Visit Completion RatehealthcareMedium
- 71Patient Out-of-Pocket Exceeding Insurance CoveragehealthcareMedium
- 72Patients with Visits Within 30 Days of a Previous VisithealthcareMedium
- 73Patients on Multiple Concurrent MedicationshealthcareMedium
- 74Customers Who Reviewed Their PurchasesecommerceHard
- 75Customers Who Live in a Branch CityfinanceHard