Practical Exercises
Time to combine everything. These exercises use the ShopDB sample from earlier lessons. Try each one before opening the solution.
On this page · 5 min · Advanced
On this page
- In simple words
- Learning objectives
- Explanation
- Code example
- Step by step
- Real-world use
- Common mistakes
- Best practices
- Remember this
- Practice exercise
- Mini quiz
- Related topics
- My notes
- Reading time
- 5 min
- Difficulty
- Advanced
- Updated
- Oct 27, 2026
Related tutorials
In simple words#
Like solving previous years’ exam papers: practising real queries is what makes SQL stick.
- Read the question
- Identify tables, filters and grouping.
- Build step by step
- FROM → WHERE → GROUP BY → SELECT → ORDER BY.
- Check results
- Verify counts with a simple query.
Learning objectives#
- Combine SELECT, joins and grouping to answer business questions
- Use window functions in realistic reports
- Modify data safely
- Build confidence for interviews
Explanation#
Each exercise is a question a manager might ask. Read it, decide which tables you need, then build the query one clause at a time.
Check your result against the expected output. If it differs, run parts of the query separately to see where it goes wrong.
Exercises: (1) revenue per category from delivered orders; (2) each customer’s most recent order; (3) products never ordered; (4) monthly revenue; (5) safely increase Furniture prices by 5% inside a transaction.
Code example: Worked exercise 1 — revenue per category (delivered orders only)#
SELECT p.Category, SUM(oi.Qty * oi.UnitPrice) AS Revenue
FROM OrderItems AS oi
JOIN Orders AS o ON o.OrderId = oi.OrderId
JOIN Products AS p ON p.ProductId = oi.ProductId
WHERE o.Status = 'Delivered'
GROUP BY p.Category
ORDER BY Revenue DESC;
Category Revenue Electronics 56400.00 Furniture 12000.00
This example is illustrative. Copy or download it and run it in your own environment; the expected output is shown above.
Step-by-step explanation#
- Start from OrderItems because revenue lives on order lines.
- Join Orders to filter by status and Products to get the category.
- Only orders 101 and 102 are delivered: 55,000 + 1,400 Electronics and 12,000 Furniture.
- Group by category and sort by revenue.
Real-world use case#
These are the same patterns used in real sales dashboards and in SQL interview tests.
Common mistakes#
Revenue must include quantity.
Status is a row condition, so it belongs in WHERE.
Best practices#
- Write the FROM and JOINs first
- Add WHERE, then GROUP BY, then SELECT columns
- Verify totals with a small manual calculation
- Save useful queries as views
Remember this#
Read the question → find tables → filters → grouping → sorting.
Check results with a quick COUNT.
Practice daily — SQL is learned by writing it.
Practice exercise#
Your task: Exercise 2: return each customer’s most recent order (customer name, order id, order date).
Need a hint?
Show solution
WITH Ranked AS (
SELECT c.Name, o.OrderId, o.OrderDate,
ROW_NUMBER() OVER (PARTITION BY c.CustomerId ORDER BY o.OrderDate DESC) AS rn
FROM Customers AS c JOIN Orders AS o ON o.CustomerId = c.CustomerId)
SELECT Name, OrderId, OrderDate FROM Ranked WHERE rn = 1;
-- Asha 103 2026-02-10 | Ravi 102 2026-01-07 | Meera 104 2026-02-12
Mini quiz#
Which query pattern finds products that were never ordered?
Want more? Practise all SQL Server MCQs · SQL Server interview questions
My notes#
Saved in this browser.