postgresql Common Table Expressions (WITH) Common Table Expressions in SELECT Queries

Help us to keep this website almost Ad Free! It takes only 10 seconds of your time:
> Step 1: Go view our video on YouTube: EF Core Bulk Extensions
> Step 2: And Like the video. BONUS: You can also share it!

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)


Got any postgresql Question?