Udemy
Turn what you know into an opportunity and reach millions around the world.
Learn More
Your cart is empty.
Keep shopping
SQL CTEs and Subqueries: Write Clear, Maintainable Queries
New

SQL CTEs and Subqueries: Write Clear, Maintainable Queries

Break complex sales questions into clear SQL steps with CTEs, scalar subqueries and EXISTS.
Last updated 11/2026
English
English

What you'll learn

  • Compare individual orders against a benchmark using a scalar subquery.
  • Build customer lists with correlated EXISTS and handle missing matches safely with NOT EXISTS.
  • Turn a nested aggregate into named CTEs with a clear population and grain at each step.
  • Validate a buyer-spend report using control totals, boundary cases and duplicate-sensitive checks.

Course content

3 sections • 8 lectures • 37m total length
  • Define the rows behind a sales question4:46

    Trace seven checkouts through payment status and account eligibility. Reconcile 400 in paid sales to 300 in registered-buyer revenue.

  • Compare orders with a scalar subquery4:37

    Use an inner average as a single comparison value. See how the population filter and the equality boundary affect the qualifying orders.

  • Filter customers with correlated EXISTS4:55

    Trace the outer customer key into an EXISTS test and build a recipient list with one row per buyer.

  • Find missing buyers safely with NOT EXISTS4:41

    See why a NULL guest key makes NOT IN lose eligible customers. Use NOT EXISTS to recover accounts with cancellations or no orders.

Requirements

  • Basic SELECT, WHERE and GROUP BY skills. The practice kit includes a SQLite database for any SQLite-compatible editor.

Description

Which customers spend more than average? Before you write the query, you have to decide what counts as a sale, whether guest checkouts belong in the calculation, and whether each order or each buyer gets one vote in the average. In this short SQL course, you will answer that question for a small shop and trace each result back to the original receipts.


Start with a scalar subquery to find unusually large orders. Then use correlated EXISTS and NOT EXISTS to build two customer lists. A guest checkout with a missing account identifier shows exactly how NOT IN can hide customers who should appear in your report.


Next, group purchases by buyer inside a derived table. Rewrite the eligibility rule as a common table expression, inspect the rows it returns, and chain named steps into a report with a shared benchmark. Finish by adding a buyer who lands exactly on the threshold and checking your expected results for missing rows, extra copies and incorrect amounts.


Each lecture pairs readable SQL with the rows it produces. The downloadable SQLite database, setup script, worked examples, eight practice challenges and answer key let you repeat every calculation in your own editor. You should already be comfortable with SELECT, WHERE and basic aggregation. By the end, you will have a buyer-spend report you can explain, change and verify one step at a time.


AI tools assisted in preparing course artwork and instructional materials.

Who this course is for:

  • SQL learners, data analysts and BI developers who can write simple queries and want to maintain multi-step reports.