How the WITH Clause Can Simplify Your SQL Queries — Ken Williams (2024)

This is a quick post to highlight how you can simplify your SQL using the WITH clause. Many of the posts on this blog use this technique to prepare queries that are easy to understand and work with.

  • Explained recursion and updated the “performance” section.

Benefits of using the WITH Clause

The WITH clause allows you to reduce joins and logically sequence your code. It does this by creating temporary tables (technically they are views) that are usable by your main SQL statement so that you can break your code into smaller and easier to understand snippets.

Syntax of the WITH Clause

NOTE
I am working with Google Analytics 4 data in BigQuery for this post, but the following will be applicable to many other database systems.

The WITH clause is a prefix for your main query. You can use it to create one or more CTEs (also known as "common table expressions". Each CTE can be given a name, and then reference in other places in the same query.

As an example, here is how you might pull the unique ID's of users who visited your site yesterday and then came back again today:

# Step 1: Get a list of users who visited the site yesterdayWITH yesterday AS ( SELECT user_pseudo_id AS yesterday_user_pseudo_id FROM `<project name>.<dataset name>.events_08012021`)# Step 2: Get a list of users who also visited the site today# by pulling today's users and then running an inner join# with those who visited yesterday.SELECT user_pseudo_idFROM `<project name>.<dataset name>.events_09012021`,yesterdayWHERE yesterday.yesterday_user_pseudo_id = user_pseudo_id

In the example above I'm defining a temporary table with the name "yesterday". You could also put a comma at the end to add other temporary tables if you choose, like this:

WITH yesterday AS ( SELECT user_pseudo_id AS yesterday_user_pseudo_id FROM `<project name>.<dataset name>.events_08012021`), last_week AS ( SELECT user_pseudo_id AS yesterday_user_pseudo_id FROM `<project name>.<dataset name>.events_02012021`)

Recursive vs. Non-recursive CTEs

The CTEs that you create using the WITH clause can be recursive (meaning that it references itself) or non-recursive. Virtually all of the CTE's created in this blog are non-recursive to keep them simple and easy to read, but recursive CTEs will declare that they are "RECURSIVE" and be given a keyword like this:

# Recursive CTE in a WITH clause# The recursive keyword in this example is "n"WITH RECURSIVE T1 AS ( (SELECT 1 AS n) UNION ALL (SELECT n + 2 FROM T1 WHERE n < 4))SELECT * FROM T1 ORDER BY n/*---* | n | +---+ | 1 | | 3 | | 5 | *---*/

So why am I teaching you about recursive CTE's if I don't use them in this blog? Well it's so you can understand how the WITH clause impacts query performance...

Performance

Recursive CTEs will be materialized in BigQuery, which means that they will be precomputed and the results will be cached. This is great for performance because they will only be executed once even if they are referenced multiple times.

The results of your non-recursive CTEs will not be materialized however. So if you reference the CTE multiple times it will be executed once for each reference.

So, the tl;dr is that recursive CTEs (which you will not find in this blog) are helpful for performance, but non-recursive CTEs are useful to make your code readable. You can read Google's post on this topic HERE

Other Resources for BigQuery and GA4

Here are a few other posts that I’ve created for using BigQuery with data from Google Analytics 4:

How the WITH Clause Can Simplify Your SQL Queries — Ken Williams (2024)

FAQs

How the WITH Clause Can Simplify Your SQL Queries — Ken Williams? ›

The WITH clause allows you to reduce joins and logically sequence your code. It does this by creating temporary tables (technically they are views) that are usable by your main SQL statement so that you can break your code into smaller and easier to understand snippets.

What is the benefit of using with clause in SQL? ›

Reduces complexity: Breaks down complex queries into smaller, easier-to-understand subqueries. Improves readability: Makes code more organized and clear by naming intermediate results. Reusability: Allows you to reuse common subqueries throughout the main query without repeating them.

What is the with clause in SQL? ›

What is SQL WITH? The SQL WITH clause, also known as a Common Table Expression (CTE), is used to create a temporary result set that can be referenced within a SELECT, INSERT, UPDATE, or DELETE statement. It makes complex queries more readable by breaking them into smaller, named, and reusable subqueries.

What does the FROM clause in an SQL query do? ›

The FROM command is used to specify which table to select or delete data from.

What is the difference between with clause and subquery? ›

The with clause, aka subquery factoring, allows you to tell us "hey, reuse this result over and over in the query". We can factor out a subquery that is used more then once and reuse it -- resulting in a perhaps "better" plan. It can also make the query overall "more readable".

Does with clause create a temporary table? ›

The WITH clause allows you to reduce joins and logically sequence your code. It does this by creating temporary tables (technically they are views) that are usable by your main SQL statement so that you can break your code into smaller and easier to understand snippets.

How to make SQL queries more efficient? ›

12 Ways to Optimize SQL Queries
  1. Use indexes effectively. ...
  2. Avoid SELECT queries. ...
  3. Reduce the use of wildcard characters. ...
  4. Use appropriate data types and layouts. ...
  5. Avoid redundant or unnecessary data retrieval. ...
  6. Use EXIST() instead of COUNT() queries. ...
  7. Avoid subqueries. ...
  8. Make use of cloud database-specific features.

Can a view be created to simplify using complex SQL queries? ›

Views are virtual tables that you can use to simplify complex queries or present data in a more user-friendly format. CREATE VIEW is the statement to create this virtual table based on your SQL query results.

What is SQL simplified? ›

Structured query language (SQL) is a programming language for storing and processing information in a relational database. A relational database stores information in tabular form, with rows and columns representing different data attributes and the various relationships between the data values.

What is the purpose of on clause in SQL? ›

The SQL ON clause is used in conjunction with JOIN statements to specify the conditions that determine how two or more tables are related or joined. It specifies the columns from each table that are used to establish the join conditions, allowing you to connect rows based on related data.

What is a clause in SQL with an example? ›

They form specific parts of a SQL statement that perform a specific function and allow us to put constraints on data. The sql clauses can help filter out the data according to the users' needs. The main clauses are SELECT, FROM, WHERE, GROUP BY, HAVING, ORDER BY, INSERT, UPDATE, DELETE, and JOIN.

How does in clause work in SQL? ›

The IN command allows you to specify multiple values in a WHERE clause. The IN operator is a shorthand for multiple OR conditions.

Why do we use using clause in SQL? ›

The USING clause specifies which columns to test for equality when two tables are joined. It can be used instead of an ON clause in the JOIN operations that have an explicit join clause.

How to use SELECT query in from clause in SQL? ›

Each SQL query statement must contain both a SELECT and a FROM clause. The combination of these two clauses determine the table columns that are searched by the query. The WHERE clause and other advanced clauses further limit data retrieval to specific table rows.

What is the purpose of having clause in a SQL SELECT query? ›

The SQL HAVING clause is combined with GROUP BY clause to restrict the groups of returned rows whose condition is TRUE. It is used to filter records that one or more columns have grouped. The HAVING clause is used instead of the WHERE clause with aggregate functions.

What are the benefits of HAVING clause in SQL? ›

The SQL HAVING clause is combined with GROUP BY clause to restrict the groups of returned rows whose condition is TRUE. It is used to filter records that one or more columns have grouped. The HAVING clause is used instead of the WHERE clause with aggregate functions.

What is the benefit of using a common table expression with a clause? ›

CTEs, like database views and derived tables, enable users to more easily write and maintain complex queries via increased readability and simplification. This reduction in complexity is achieved by deconstructing ordinarily complex queries into simple blocks to be used, and reused if necessary, in rewriting the query.

Can we use 2 with clause in SQL? ›

You can use multiple WITH statements in one SQL query to define multiple CTEs. In this article, we will explain how to define multiple CTEs in a single query. We'll also show how this approach takes the expressive power of an SQL query to the next level.

Why do we use on clause in SQL? ›

The SQL ON clause is used in conjunction with JOIN statements to specify the conditions that determine how two or more tables are related or joined. It specifies the columns from each table that are used to establish the join conditions, allowing you to connect rows based on related data.

Top Articles
Latest Posts
Article information

Author: Allyn Kozey

Last Updated:

Views: 5685

Rating: 4.2 / 5 (43 voted)

Reviews: 82% of readers found this page helpful

Author information

Name: Allyn Kozey

Birthday: 1993-12-21

Address: Suite 454 40343 Larson Union, Port Melia, TX 16164

Phone: +2456904400762

Job: Investor Administrator

Hobby: Sketching, Puzzles, Pet, Mountaineering, Skydiving, Dowsing, Sports

Introduction: My name is Allyn Kozey, I am a outstanding, colorful, adventurous, encouraging, zealous, tender, helpful person who loves writing and wants to share my knowledge and understanding with you.