← All SQL interview questions
Match every order to its customer
MediumJoins — Inner JoinPayPalRazorpay
The finance team reconciles a payment export against the customer master. Orders whose customer record was purged should not appear.
Join orders_ledger to customer_master and return order_id, customer_name, segment and amount for orders where the customer still exists.
Sample data
Table: customer_master
10 row(s) · showing first 8
| customer_idINT | customer_nameTEXT | segmentTEXT |
|---|---|---|
| 11 | Ira Bansal | Retail |
| 12 | Devansh Rao | Retail |
| 13 | Nikhil Sarin | SMB |
| 14 | Mahima Sethi | Enterprise |
| 15 | Yusuf Ali | SMB |
| 16 | Prerna Iyer | Enterprise |
| 17 | Sagar Naik | Retail |
| 18 | Rhea Kapoor | SMB |
Table: orders_ledger
14 row(s) · showing first 8
| order_idINT | customer_idINT | order_dateDATE | amountNUMERIC(10,2) |
|---|---|---|---|
| 5001 | 11 | 2026-01-05 | 2400.00 |
| 5002 | 12 | 2026-01-08 | 5100.50 |
| 5003 | 99 | 2026-01-11 | 1800.00 |
| 5004 | 13 | 2026-01-15 | 12200.00 |
| 5005 | 14 | 2026-01-19 | 7650.00 |
| 5006 | 15 | 2026-01-22 | 980.00 |
| 5007 | 98 | 2026-01-25 | 4300.00 |
| 5008 | 16 | 2026-02-01 | 18900.00 |
customer_master
- customer_idINT
- customer_nameTEXT
- segmentTEXT
orders_ledger
- order_idINT
- customer_idINT
- order_dateDATE
- amountNUMERIC(10,2)
- Step 1: The two tables are linked by `customer_id`, which is the primary key on the master and a foreign key on the ledger.
- Step 2: Use `INNER JOIN` because rows without a surviving customer record must be dropped.
- Step 3: The join condition `c.customer_id = o.customer_id` automatically discards orders with unmatched IDs (99, 98) and the order whose `customer_id` is `NULL`, since `NULL` never equals anything.
- Step 4: Alias the tables (`o`, `c`) so the projected columns are unambiguous, and select the four requested fields.
SELECT o.order_id, c.customer_name, c.segment, o.amount
FROM orders_ledger AS o
INNER JOIN customer_master AS c
ON c.customer_id = o.customer_id;Learn the concept first
Stuck on Joins — Inner Join? These free SQL lessons teach it step by step.
More Joins — Inner Join questions
Practising alone only goes so far — our live cohorts pair SQL depth with dashboards, agentic analytics projects and mock interviews.
Explore the program