Skip to content
Quantoros

Return two columns in one row: weekendorders and weekdayorders (counts) across all orders. Table definitions (from the…

SQLLevel: IntermediateConditional aggregation and pivots

Return two columns in one row: weekend_orders and weekday_orders (counts) across all orders.

Table definitions (from the source exercise):

CREATE TABLE orders(order_id INTEGER PRIMARY KEY, customer_id INTEGER, order_date TEXT, amount REAL, rep_id INTEGER, discount REAL);
Hints
  • Define one output row and the source-table keys.
  • Check duplicates, NULLs and the order of filtering and aggregation.

Answer

Approach: DAYNAME(order_date) IN (Saturday, Sunday) inside conditional sums.

SELECT SUM(CASE WHEN DAYNAME(order_date) IN ('Saturday','Sunday') THEN 1 ELSE 0 END) AS weekend_orders,
       SUM(CASE WHEN DAYNAME(order_date) IN ('Saturday','Sunday') THEN 0 ELSE 1 END) AS weekday_orders
FROM orders;

The source uses SQLite with a MySQL compatibility layer. Date and string functions are dialect dependent; the query is preserved as published.

Source and licence: SQL Practice Lab — Ebrahim Sharifi · MIT

Related questions