DevLearningTools

LEARN · SQL · MEDIUM PRACTICE

50 Medium SQL Questions

Correlated subqueries, CASE expressions, CTEs, window functions, and real-world data cleaning. Beacon Robotics' employees from the basic set, plus its clients, orders, and products, used throughout.

New to SQL practice on this site? Start with the basics first.← Basic questions
New Tables for This Set

Beacon Robotics also sells robot products to outside clients. These tables extend the same schema the basic set used (employees/departments are unchanged). Orders and order_items aren't shown in full here, each question shows the exact rows it needs.

clients
idnameemail
1Orion Logisticsops@orionlogistics.com
2Meridian Farmsprocurement@meridianfarms.com
3Harbor Freight CoNULL
4Vertex Robotics Labscontact@vertexlabs
5Summit Manufacturingbuy@summitmfg.com
6 Bluewave Deviceshello@bluewave.io
7Bluewave Devices EastHELLO@bluewave.io
8Falcon Transithi@falcontransit.com
products
idnamecategoryprice
1SentinelDroneDrones1200
2TitanArmArms4500
3PulseSensor KitSensors300
4CoreControllerControllers150
5TrailRoverRovers2200
6ScoutBeacon GPS TagSensors80
7NovaBeacon Charging DockControllers45

orders(id, client_id, order_date, status, total_amount) · order_items(order_id, product_id, quantity, unit_price) · payments(order_id, amount, status)

Your Progress

0 / 50 marked solved

Self-reported, not auto-graded, there's no live SQL engine behind this page. Check your own query against the shown solution and expected output, then mark it yourself. Saved only in this browser.

Level 1 · Advanced Joins and Subqueries

Q1–10 · Intermediate

Correlated subqueries, NOT EXISTS, self-joins, and multi-row comparisons.

01

Above Your Own Department's Average

Find every employee earning more than the average salary of their own department.

02

Clients Who Never Ordered

Find every client who has never placed an order.

03

Each Client's Latest Order

Find the most recent order placed by each client.

04

Products Never Ordered

Find every product that has never appeared in an order.

05

A Genuinely High-Volume Client

Find every client who has placed more than five orders.

06

Out-Earning Your Own Manager

Find every employee whose salary is higher than their manager's.

07

Bought Every Product in a Category

Find every client who has purchased every product in the Rovers category (there's only one, TrailRover, so this demonstrates the pattern on the simplest possible case).

08

Orders With a Mismatched Total

Find every order where the recorded total_amount doesn't match the actual sum of its order_items.

09

Ordered in Both January and February

Find every client who placed at least one order in January 2026 and at least one in February 2026.

10

Each Client's Second Order

Find the second order ever placed by each client, by date.

Level 2 · CASE Expressions and Conditional Aggregation

Q11–20 · Intermediate

CASE for categorizing, and COUNT/SUM with CASE for splitting one query into several measures.

11

Salary Categories

Categorize every employee's salary as Low (below 30000), Medium (30000 to 60000), or High (above 60000).

12

Readable Order Status

Display each order's status code as a readable word.

13

Three Counts in One Query

Count completed, pending, and cancelled orders, all three in a single query.

14

Sales by Category

Calculate total sales for each product category, excluding cancelled orders.

15

Completion Rate

Find the percentage of completed orders out of every order ever placed.

16

Salary Grades by Threshold

Grade every employee's salary: Grade A (70000+), Grade B (50000+), Grade C (30000+), Grade D otherwise.

17

Client Tiers by Total Purchases

Categorize each client as Gold (10000+ total purchases), Silver (1000+), or Bronze (below 1000), excluding cancelled orders.

18

Monthly Sales, Completed vs Pending

Show total completed sales and total pending sales for each calendar month.

19

Experience Category by Hire Date

For every employee hired before 2023, classify them as Senior (hired before 2020), Experienced (before 2022), or Recent.

20

Successful vs Failed Payments, Per Order

For every order, total its successful payment amount and its failed payment amount separately.

Level 3 · Common Table Expressions

Q21–28 · Intermediate

Single and chained CTEs for readable multi-step queries.

21

Above the Company Average, via a CTE

Find every employee earning more than the overall company average salary, naming the average with a CTE.

22

Revenue Per Client, via a CTE

Find the total revenue generated by each client, excluding cancelled orders, highest first.

23

Top Three Clients

Find the top three clients by total purchases, using two chained CTEs.

24

Departments Above the Company Average

Find every department whose average salary is greater than the company's overall average salary, using two CTEs.

25

Multiple Orders, Same Day

Find every client who placed more than one order on the exact same date.

26

Category Sales, via Chained CTEs

Find total sales for each product category, using a product-level CTE feeding into a category-level one.

27

Above the Average for Your Hire Year

Find every employee earning more than the average salary of employees hired in the same calendar year as them.

28

Every Client, Orders or Not

List every client alongside their order count and total spent, including clients with zero orders.

Level 4 · Window Functions and Ranking

Q29–38 · Intermediate

ROW_NUMBER, DENSE_RANK, running totals, LAG/LEAD, and NTILE.

29

Ranking Salaries Within Each Department

Assign a row number to every employee based on salary, restarting for each department.

30

The Top Earner in Every Department

Find the single highest-paid employee in each department.

31

Top Two Per Department, Ties Included

Find the top two highest-paid employees in each department, including any tie at second place.

32

Running Total of Orders

Calculate Orion Logistics' running total of order amounts, in date order.

33

Previous Order Amount

For Orion Logistics, show each order's amount alongside the previous order's amount.

34

Next Order Date

For Orion Logistics, show each order's date alongside the date of the next order.

35

Distance From the Department Average

For every employee, show how far their salary is from their own department's average.

36

Four Salary Groups

Divide all ten employees into four roughly equal salary groups using NTILE.

37

Three-Order Moving Average

For Orion Logistics, calculate the moving average of order amount across the current order and the two before it.

38

Each Client's Share of Total Sales

Calculate what percentage of total company sales each client contributes, excluding cancelled orders.

Level 5 · Date, String, and Data Cleaning

Q39–44 · Intermediate

Date ranges, string pattern checks, and finding messy data.

39

Recently Acquired Clients

Assuming today is 2026-03-20, find every client whose first order was within the last 90 days.

40

Days Between Consecutive Orders

For Orion Logistics, find the number of days between each order and the one before it.

41

Invalid Email Addresses

Find every client whose email is missing entirely, or doesn't look like a real email address (no @ or no dot).

42

Extracting the Email Domain

Extract just the domain portion from every client's email address.

43

Names With Extra Spaces

Find every client whose name has leading or trailing spaces.

44

Priced Above the Category Average

Find every product priced higher than the average price within its own category.

Level 6 · Real-World Problem Solving

Q45–50 · Intermediate

Consecutive-month detection, duplicate data, and tying several techniques together.

45

Three Consecutive Months of Orders

Find every client who placed at least one order in each of three consecutive calendar months.

46

Duplicate Client Emails

Find email addresses that belong to more than one client, even if the capitalization or spacing differs.

47

Each Product's Share of Its Category

For every product, calculate what percentage of its own category's revenue it's responsible for, excluding cancelled orders.

48

Orders With Outstanding Balances

Find every order where the successfully paid amount is less than the order's total.

49

Best-Selling Product Per Category

Find the best-selling product, by quantity, within each category.

50

Clients Whose Spending Increased Month Over Month

Find every client-month where that client's total spending (excluding cancelled orders) increased compared with the previous calendar month they had any orders in.

HOW TO PRACTICE
  • Write your own query first against the tables shown, before revealing the solution.
  • A few questions deliberately use database-neutral pseudo-syntax (EXTRACT, interval subtraction); translate it to your actual database's real syntax before running it.
  • If your query differs from the shown solution but gets the same result, that's fine, especially for CTEs and window functions, more than one correct shape usually exists.
  • Use the lesson link on a question that trips you up before retrying it.
</>

Run These for Real

Create the sample tables above in a free SQL playground, then try each question yourself before checking the solution. Pick one based on which database you want to practice on.

SQLite / PostgreSQL

Fast, no server, no ads.

Open DB Fiddle ↗

MySQL / SQL Server

Switch databases with one dropdown.

Open SQL Fiddle ↗

Both open a third-party SQL playground in a new tab. External site terms and privacy policy apply.

Other Practice Levels