top of page

Welcome
to NumpyNinja Blogs

NumpyNinja: Blogs. Demystifying Tech,

One Blog at a Time.
Millions of views. 

CTE Or Subquery? Choosing The Right Tool In SQL

May 25, 2025
3 min read

Updated: Jun 5, 2025



SQL is a powerful language for querying relational databases.  Whether we are a data analyst or a business intelligence professional, writing readable and efficient queries is key to extracting meaningful insights from data.


As the databases grow more complex, so do the queries required to analyze them. A simple SQL statement is not enough to express complex logic. This is where Subqueries and CTEs (Common Table Expressions) come into play. These tools help us break down complex tasks into simpler and more manageable steps by improving clarity and performance.


In this blog, let’s explore:

  • What Subqueries and CTEs are.

  • How they differ in syntax and behavior.

  • When to use each effectively. And

  • Practical examples that show their real-world use.


What are Subqueries and CTEs?


Subqueries


A subquery is a query written inside another query. We can think of it as a mini query that helps us fetch immediate results needed by the outer query. Subqueries are commonly used in SELECT, WHERE or FROM clauses.  Let’s see an example of the syntax:


Here, we are finding all patients who are getting treated by a Cardiologist. The subquery locates all the DoctorIDs from the Doctors table whose specialty is ‘Cardiology’. The outer query then filters the patients based on these DoctorIDs.


Subqueries work well for simple filtering or value comparisions, but if we nest them too deeply, they can quickly get messy and hard to follow.


Common Table Expressions (CTEs)


A CTE is a temporary result set that we define at the beginning of our query using the WITH keyword. It helps us organize complex logic into clean and readable chunks. And we can even reuse them later in the query as subqueries. Let’s see an example of the syntax:



Here, we are trying to find all patients who are treated by Cardiologists and are older than the average age of such patients. This CTE is building a virtual table called Cardio_Patients. It pulls Patient_Name and Age from the Patients table.  Then it filters patients whose DoctorID is in the list of Doctors who have a specialty in Cardiology (via a subquery).


So here, we are combining a CTE with a subquery to keep logic modular and readable. Now that we have a clean list of Cardiology patients, we select those whose age is above the average age in that group. This uses a scalar subquery to calculate AVG(Age) from the same CTE.


CTEs are easier to debug and often preferred in complex queries, especially when working with Joins or recursive logic. 


Now, let’s see when to use each one effectively.


Use Subqueries:

  • When dealing with a quick filter or calculation. If our logic is short and sweet, a subquery can keep it compact. Especially, in a WHERE or SELECT clause.

  • When we don’t need to reuse the result. Subqueries are one-time use. If we are not planning to reference the logic again, a subquery is a good fit.

  • When we want to avoid overcomplicating things. For small queries, subquery avoids the overhead of naming a CTE unnecessarily.


Use CTEs:

  • When the query is long and has multiple steps. CTEs make large queries readable by letting us build logic step-by-step.

  • When we need to reuse the result in the same query. Since CTEs can be referenced multiple times, they are perfect when the same immediate result is used more than once.

  • When we want to use recursion. Only CTEs can handle recursive queries.

  • When we care about readability and collaboration. If we are writing SQL that others will read, CTEs make our intent clearer.


With a good understanding of both, we can write queries that are not just correct, but also clean, readable and scalable.

 
 

+1 (302) 200-8320

NumPy_Ninja_Logo (1).png

Numpy Ninja Inc. 8 The Grn Ste A Dover, DE 19901

© Copyright 2025 by Numpy Ninja Inc.

  • Twitter
  • LinkedIn
bottom of page