Adventures in Machine Learning

Streamline Your SQL Queries with Common Table Expressions

to CTEs:

If you work with databases, you may already know what a query is. It is a way to extract data from a table and display it in a meaningful way.

But have you heard about CTEs? CTEs stand for Common Table Expressions, and they are a powerful tool to create temporary data sets within your query.

Defining CTEs:

A CTE allows you to create a temporary table that is only accessible within the scope of the query. It is defined using the WITH clause followed by the expression name and the SELECT statement that will populate it.

The result of the SELECT is then accessible as if it were a table. The temporary data set is discarded once the query has finished executing.

The syntax for CTEs:

To define a CTE, start with the WITH keyword and follow it with the expression’s name. Then, list the columns that will populate that temporary table.

These columns are defined in parentheses. The SELECT keyword follows this, and it is used to retrieve the data that will populate the temporary data set.

Finally, add your actual query code. Remember, the temporary table is only visible within the query.

Example 1: Calculating average salary and salary difference:

Let’s say you have a salary table in your database with all your employees’ salaries. You could use a CTE to find the average salary and the salary difference of each employee compared to the average.

Here’s how:

WITH salaries_cte (salary, emp_id)



SELECT salary, emp_id

FROM salaries


SELECT emp_id, salary,

(salary – AVG(salary) OVER()) as salary_difference, AVG(salary) OVER() as average_salary

FROM salaries_cte;

In this query, we started by defining our salaries_cte table that includes the employee ID and salary. Then, we used the SELECT statement to query from this temporary table and calculate the salary difference compared to the average and the average salary itself.

Example 2: Finding the largest single-day sale:

Imagine you have a sales table with the date and the amount of each sale. You could use a CTE to find the largest single-day sale that your company has ever made.

Here’s how:

WITH sales_cte (sale_amount, sale_date)



SELECT sale_amount, sale_date

FROM sales


SELECT MAX(sale_amount) as largest_sale, sale_date

FROM sales_cte

GROUP BY sale_date

ORDER BY largest_sale DESC


In this query, we defined a temporary table sales_cte that includes the sale amount and the sale date. We then queried from this table to find the highest sale amount and its corresponding date.

We used GROUP BY on the sale_date column to group the data by day, and ORDER BY and LIMIT to return only the highest sale amount and date.


CTEs may seem complicated at first glance, but their potential is enormous.

By creating temporary data sets within your query, you can simplify and streamline your code, making it more efficient and organized. By understanding how CTEs work and studying examples, you can start using them in your queries to become a more productive and effective data analyst or developer.

Common table expressions (CTEs) are a powerful tool for creating temporary data sets within your queries. They allow for more efficient and organized code, and can simplify complex business queries.

By understanding the syntax and examples of CTEs, you can start using them in your own work to become a more productive and effective data analyst or developer. Overall, CTEs are an important concept to grasp for anyone working with databases.

Popular Posts