sqladvanced

Upsert - Technique 42

Insert or update rows

sql
WITH recent_orders AS (
  SELECT customer_id, SUM(total) AS spend
  FROM orders
  WHERE created_at >= now() - interval '30 days'
  GROUP BY customer_id
)
SELECT customer_id, spend
FROM recent_orders
ORDER BY spend DESC
LIMIT 10;

Use Cases

  • database operations
  • data management

Tags

Related Snippets

Similar patterns you can reuse in the same workflow.