JournalSQLSubqueries and CTEs2 min

For Those Who Do Not Know WITH

First, we must understand that any query we write produces its own result table; that resulting table might not directly match the stored tables in the database, as we will see in this explanation.

  • WITH
  • result table
  • subquery

Introduction

First, we must understand that any query we write produces its own result table; that resulting table might not directly match the stored tables in the database, as we will see in this explanation. We also need to understand how subqueries work: For Those Who Do Not Know Subquery We use WITH (also called a CTE, short for Common Table Expression) to store temporary tables from the query we write.

Why Do We Use WITH?

We use the WITH clause to make writing queries easier for ourselves and easier to read (if the query is long). Its ideal use case is when we find that a subquery we are writing will be repeated in the same form in the outer query—in that case, we save a lot of execution time.

Example

Suppose we have an employees table, and we want to retrieve the employee's name and salary. We write:

SELECT name, salary
FROM employees

If I ask you to get the employee with the highest salary, you write:

SELECT name, salary
FROM employees
ORDER BY salary DESC
LIMIT 1

Notice that I sorted the salaries using ORDER BY and wrote DESC to sort from highest to lowest (descending order), and I wrote LIMIT 1 to make the output consist of just one row (the highest-paid employee).

Using WITH

What if more than one employee earns the same highest salary? In that case, LIMIT 1 won't be accurate. If we want to write a subquery instead of using LIMIT 1 to get all employees who earn the highest salary, we write:

WITH t1 AS (SELECT name, salary
            FROM employees)

SELECT *
FROM t1 AS emp1
WHERE emp1.salary = (SELECT MAX(emp2.salary) FROM t1 AS emp2)

Instead of querying specific columns directly from employees repeatedly, I selected the two columns I need (name and salary) and stored them in a temporary table named t1 so I can query the columns I need from t1.

Notice that the main query execution is in the last 3 lines:

SELECT *
FROM t1 AS emp1
WHERE emp1.salary = (SELECT MAX(emp2.salary) FROM t1 AS emp2)

What if we want to store more than one temporary table using WITH? Let's apply the same example in another way:

WITH t1 AS (SELECT name, salary
            FROM employees),
t2 AS (SELECT salary
       FROM employees)

SELECT *
FROM t1
WHERE t1.salary = (SELECT MAX(t2.salary) FROM t2)

Notice that when I defined the second temporary table t2, I didn't need to write WITH again; instead, I used a comma , to separate the two temporary tables t1 and t2.

Summary

  • We use WITH so our code stays clean and easier to read.
  • We use WITH to optimize our query and make it run faster when a subquery repeats itself in the main query.

If you want to know the difference between a subquery and WITH in terms of speed, check out this topic: WITH-queries vs. Subqueries