Subqueries, CTEs and Analytical Thinking in SQL

Data Analyst 10 min min read Updated: Mar 07, 2026
Subqueries, CTEs and Analytical Thinking in SQL
Topic 4 of 4

Think of this chapter as a classroom explanation written in simple language, with the goal of making the topic practical instead of theoretical.

Chapter Overview

Subqueries and common table expressions help break a complex problem into smaller steps. This is useful when a business question has layers, such as finding the top customer in each city or comparing current sales with monthly averages.

Why CTEs Help

A CTE makes the logic easier to read. Instead of one long unreadable query, you can build a temporary named result and then query it.

SQL Example

WITH monthly_sales AS (
  SELECT DATE_FORMAT(order_date, '%Y-%m') AS month,
         SUM(revenue) AS total_revenue
  FROM orders
  GROUP BY DATE_FORMAT(order_date, '%Y-%m')
)
SELECT *
FROM monthly_sales
WHERE total_revenue > 100000;

Learning Tip

When SQL looks complex, solve the problem in stages. Analysts who can structure thinking clearly usually write better queries.

Key Takeaways

  • Go beyond basics with layered SQL logic that mirrors real business questions.
  • This chapter belongs to SQL for Data Analysts and is written in a simple student-friendly style.
  • Practice with SQL queries with simple table examples to build confidence faster.

What to Do After This Chapter

Revise the main terms, recreate the example on your own, and move to the next lesson only after you can explain the idea in your own words.

Previous tutorial

Get Newsletter

Subscibe to our newsletter and we will notify you about the newest updates on Edugators