postgresql Common Table Expressions in SELECT Queries


Example

Common table expressions support extracting portions of larger queries. For example:

WITH sales AS (
  SELECT 
    orders.ordered_at,
    orders.user_id,
    SUM(orders.amount) AS total
  FROM orders
  GROUP BY orders.ordered_at, orders.user_id
)
SELECT 
  sales.ordered_at,
  sales.total,
  users.name
FROM sales 
JOIN users USING (user_id)