SQL QuestSQL Interview Questions › Subqueries & CTEs

Subquery in FROM (Derived Table)

MediumFreeQuerying BasicsSubqueries & CTEsJoinsAggregation & GroupingConditional Logic

A derived table is a subquery in the FROM clause — it creates a temporary result you can query like a regular table. First, create a derived table that counts orders per customer. Then JOIN it with customers to show name, membership, order_count, and whether they're above_average ('Yes' if order_count > average orders per customer, 'No' otherwise). Sort by order_count descending, then name ascending for ties.

Solve it in the browser editor →

Runs on SQLite in your browser, graded against the expected result, no signup. A wrong answer gets a diagnosis, not just "incorrect".

Schema

orders

order_idcustomer_idproductcategoryquantitypricetotalorder_datecountrystatus
11Laptop ProElectronics11299.991299.992024-01-15USAcompleted
22Wireless MouseElectronics249.9999.982024-01-16Canadacompleted
33Office ChairFurniture1349.99349.992024-01-17USAcompleted

customers

customer_idnameemailsignup_datemembershiptotal_orders
1John Smithjohn.smith@email.com2023-01-15Gold15
2Emma Wilsonemma.wilson@email.com2023-03-20Silver8
3Michael Brownmichael.brown@email.com2023-02-10Gold12

Expected output: Alice | Gold | 5 | Yes

Hint

FROM (SELECT customer_id, COUNT(*) AS order_count FROM orders GROUP BY customer_id) o JOIN customers c ON o.customer_id = c.customer_id. The subquery in parentheses IS the derived table.

Concepts

SELECT Subquery Derived Table JOIN GROUP BY CASE

Practise the topic: SQL practice questions · CTE practice · JOIN practice · GROUP BY exercises · CASE WHEN practice

In these company practice sets

Snowflake

A SQL Quest challenge matched to patterns reported for these companies — not a question any of them has published.

Related questions

Department Roster with GROUP_CONCATMedium · FreeConsistent Director AnalysisMedium · FreeBelow Department AverageMedium · FreeHighest Total Salary Budget DepartmentMedium · FreeFare Imputation AnalysisMedium · FreeUNION ALL Dedup: Cross-Dataset SearchMedium · Free

Where would this cost you points in an interview?

Ten questions, no signup: a Skillmap across nine SQL skills and the one to fix first.

Take the readiness test