How to Write a Common Table Expression in BigQuery

Common table expressions (CTEs) help you break up complex queries in BigQuery. Here's a simple query to illustrate how to write a CTE:

with beta_users as (
  select *
  from users
  where beta is true
)

select events.*
from events
inner join beta_users on beta_users.id = events.user_id;

You can see a real example of using CTEs to generate a time series to avoid gaps in data.

Previous

Next

Ready for a modern SQL editor?