Skip to content
Quantoros

For 2026 orders: return each month (YYYY-MM) and the percent of orders with discount 0, rounded to 1 decimal, in month…

SQLLevel: IntermediateConditional aggregation and pivots

For 2026 orders: return each month (YYYY-MM) and the percent of orders with discount > 0, rounded to 1 decimal, in month order.

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: Conditional count over total count, grouped by DATE_FORMAT month.

SELECT DATE_FORMAT(order_date, '%Y-%m') AS m,
       ROUND(100.0 * SUM(CASE WHEN discount > 0 THEN 1 ELSE 0 END) / COUNT(*), 1) AS pct_discounted
FROM orders
WHERE YEAR(order_date) = 2026
GROUP BY m
ORDER BY m;

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