Syntax

SQL Common Table Expression Syntax

In SQL, common table expression: names a temporary result set with WITH — WITH recent AS ( SELECT * FROM orders WHERE created_at > now() - interval '7 days' ) SELECT * FROM recent;

Quick Answer

Syntax
WITH recent AS ( SELECT * FROM orders WHERE created_at > now() - interval '7 days' ) SELECT * FROM recent;
SQLSyntax

Common Table Expression

Code example
WITH recent AS ( SELECT * FROM orders WHERE created_at > now() - interval '7 days' ) SELECT * FROM recent;

Names a temporary result set with WITH.

Learn More

How to common table expression in SQL

  1. Open your SQL file or editor.
  2. Copy the example, then replace its sample names and values for your code.

SQL Common Table Expression Syntax

Use WITH recent AS ( SELECT * FROM orders WHERE created_at > now() - interval '7 days' ) SELECT * FROM recent;.

Related Syntax References

Frequently asked questions

What is the SQL syntax for common table expression?

Use this pattern: WITH recent AS ( SELECT * FROM orders WHERE created_at > now() - interval '7 days' ) SELECT * FROM recent;

Can I copy this SQL code example?

Yes. Use Copy code and adapt the example to your program.

Is this SQL syntax different on Mac or Windows?

No. The language syntax is the same across operating systems.

Official source · How we verify