← All SQL interview questions
Order lines with product and category names
MediumJoins — Inner JoinAmazonFlipkart
An operations analyst needs a readable list of the bigger order lines, with the product and category names instead of IDs.
Join order_items, products and categories. Return order_id, product_name, category_name, quantity and line_total (quantity * unit_price) for lines whose line_total is 2000 or more. Order by line_total descending, then item_id.
Sample data
Table: categories
4 row(s)
| category_idINT | category_nameTEXT |
|---|---|
| 1 | Electronics |
| 2 | Home |
| 3 | Fashion |
| 4 | Grocery |
Table: products
8 row(s)
| product_idINT | product_nameTEXT | category_idINT |
|---|---|---|
| 101 | Wireless Earbuds | 1 |
| 102 | Phone Case | 1 |
| 103 | Air Fryer | 2 |
| 104 | Bedsheet Set | 2 |
| 105 | Running Shoes | 3 |
| 106 | Basmati Rice 5kg | 4 |
| 107 | Desk Lamp | 2 |
| 108 | Tote Bag | 3 |
Table: order_items
14 row(s) · showing first 8
| item_idINT | order_idINT | product_idINT | quantityINT | unit_priceNUMERIC(10,2) |
|---|---|---|---|---|
| 1 | 9001 | 101 | 1 | 2499.00 |
| 2 | 9001 | 102 | 2 | 299.00 |
| 3 | 9002 | 103 | 1 | 5999.00 |
| 4 | 9002 | 106 | 2 | 640.00 |
| 5 | 9003 | 104 | 2 | 1199.00 |
| 6 | 9003 | 107 | 1 | 899.00 |
| 7 | 9004 | 105 | 1 | 3299.00 |
| 8 | 9004 | 108 | 3 | 449.00 |
categories
- category_idINT
- category_nameTEXT
products
- product_idINT
- product_nameTEXT
- category_idINT
order_items
- item_idINT
- order_idINT
- product_idINT
- quantityINT
- unit_priceNUMERIC(10,2)
- Step 1: Start from `order_items` and join `products` on `product_id`.
- Step 2: Chain a second join from `products` to `categories` on `category_id`; each `JOIN` adds one table to the running result.
- Step 3: `WHERE` cannot reference the `line_total` alias, so the expression is repeated; `ORDER BY` can use the alias.
SELECT oi.order_id,
p.product_name,
c.category_name,
oi.quantity,
oi.quantity * oi.unit_price AS line_total
FROM order_items oi
JOIN products p ON p.product_id = oi.product_id
JOIN categories c ON c.category_id = p.category_id
WHERE oi.quantity * oi.unit_price >= 2000
ORDER BY line_total DESC, oi.item_id;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