Complete the code to start a Common Table Expression (CTE) named recent_orders.
WITH [1] AS (SELECT * FROM orders WHERE order_date > '2024-01-01') SELECT * FROM recent_orders;
The CTE name must match the name used later in the query. Here, recent_orders is the correct CTE name.
Complete the code to select customer_id and total from the CTE named recent_orders.
WITH recent_orders AS (SELECT customer_id, SUM(amount) AS total FROM orders GROUP BY customer_id) SELECT [1] FROM recent_orders;To get both customer_id and total columns, list them separated by a comma.
Fix the error in the CTE definition by completing the code correctly.
WITH recent_orders AS (SELECT customer_id, SUM(amount) [1] total FROM orders GROUP BY customer_id) SELECT * FROM recent_orders;In SQL, to rename a column or expression, use AS followed by the alias.
Fill both blanks to create a CTE named top_customers that selects customers with total orders above 1000.
WITH [1] AS (SELECT customer_id, SUM(amount) AS total FROM orders GROUP BY customer_id HAVING total [2] 1000) SELECT * FROM top_customers;
The CTE must be named top_customers to match the SELECT. The HAVING clause uses '>' to filter totals above 1000.
Fill all three blanks to create a CTE named recent_customers that selects customer_id and count of orders after 2024-01-01, filtering counts greater than or equal to 5.
WITH [1] AS (SELECT customer_id, COUNT(*) AS order_count FROM orders WHERE order_date [2] '2024-01-01' GROUP BY customer_id HAVING order_count [3] 5) SELECT * FROM recent_customers;
The CTE name must be recent_customers. The WHERE clause uses '>' to select orders after the date. The HAVING clause uses '>=' to include customers with at least 5 orders.