Foundations › SQL › Practice bank
Practice bankSQL Practice Bank
20 exercises from real analyst work, three levels.
0 / 20
Easy Exercise 1 — Show Electronics products sorted by price descending: name and price.
WHERE then ORDER BY price DESC.
Easy Exercise 2 — Show customers per city, most first then by city: city and n.
GROUP BY city with COUNT(*).
Easy Exercise 3 — What share of all orders is completed? Show a percentage with one decimal as completed_pct.
SUM(CASE ...) divided by COUNT(*).
Easy Exercise 4 — Show cities with more than 2 customers lacking an email: city and n.
WHERE email IS NULL then GROUP BY and HAVING.
Easy Exercise 5 — Show the average price per category rounded to 2 decimals, ascending: category and avg_price.
ROUND(AVG(price), 2).
Medium Exercise 6 — Show revenue per month in 2026 from completed orders, sorted by month: month and revenue.
Join orders to order_items then GROUP BY month.
Medium Exercise 7 — Show the 5 best-selling products by quantity, ties by name: name and units.
SUM(oi.quantity) with a JOIN on products.
Medium Exercise 8 — Show orders per status and its share of the total with one decimal: status, n and pct.
A scalar subquery for the total inside SELECT.
Medium Exercise 9 — Show the number of customers who never ordered per city: city and n.
LEFT JOIN, o.id IS NULL, then GROUP BY.
Medium Exercise 10 — Show products not sold in 2026 (they may have sold earlier): name.
NOT EXISTS joined to orders with the date filter.
Medium Exercise 11 — Show revenue and profit per category (quantity × (price − cost)): category, revenue and profit.
cost lives in the products table.
Medium Exercise 12 — Show average order value per city (completed orders) = city revenue ÷ its order count, 2 decimals: city and avg_order_value.
COUNT(DISTINCT o.id) for the order count.
Medium Exercise 13 — Show employees paid more than their manager: name and salary.
SELF JOIN then compare e.salary with m.salary.
Medium Exercise 14 — Show each department's employee count and average salary rounded to a whole number (departments with no employees still appear): department, employees and avg_salary.
LEFT JOIN from departments.
Hard Exercise 15 — Show customers with 9 or more orders with first and last order dates: customer_id, first_order, last_order and n.
MIN, MAX and COUNT with HAVING.
Hard Exercise 16 — Show each category's share of total revenue as a percentage with one decimal: category and share_pct.
A subquery for total revenue.
Hard Exercise 17 — Show orders with more than 2 items and a total above 3000: order_id, items and total.
HAVING with two aggregate conditions.
Hard Exercise 18 — Pivot: revenue per category (completed orders) split across 2025 and 2026 in two columns: category, y2025 and y2026.
SUM(CASE ...) for each year.
Hard Exercise 19 — How many customers have both completed and cancelled orders? Name it n.
INTERSECT in a subquery then COUNT.
Hard Exercise 20 — Segment customers by total spend on completed orders: VIP if spend is 25000 or more, Regular if 8000 or more, otherwise Low. Show the customers per segment: segment and n.
Compute each customer's spend in a subquery with CASE, then count segments.
Want an expert to do it for you?
Gomaa Intelligence turns your data into clear reports and decisions. Tell us your problem and we will reply.