CodeBlanks Logo Icon CodeBlanks

Summary - CTE

Write a multi-step report with multiple CTEs.

Summary - CTE

A CTE is a temporary result defined with WITH. It improves readability and allows multiple and chained use.

WITH active AS (
  SELECT * FROM customers WHERE status = 'active'
)
SELECT * FROM active;

Summary CTE query.

WITH defines a temporary result set (CTE) named active; the query after it uses it like a table. All columns are selected from active.

Practice Summary - CTE

Summary: define and use the active CTE.

Starter code

query.sql
-- summary CTE
WITH

Sample solution

query.sql
WITH active AS (
  SELECT * FROM customers WHERE status = 'active'
)
SELECT * FROM active;

Expected output

id  name   status
1   Ayşe   active

Questions and answers

Review the lesson questions together with their answers. To test yourself, solve the lesson interactively.

1 What starts a CTE?

  • SELECT
  • WITH Correct answer
  • FROM
  • WHERE

Explanation: A CTE starts with WITH.

2 Summary - CTE: match the terms

Term Meaning
CTE temporary table
WITH definition
multiple comma
JOIN join

All lessons in Advanced