Skip to content
Quantoros

Return each signup month (YYYY-MM), the number of new customers that month, and the running total of customers so far…

SQLLevel: IntermediateBonus drills (mixed difficulty)

Return each signup month (YYYY-MM), the number of new customers that month, and the running total of customers so far (running_customers), in chronological order.

Table definitions (from the source exercise):

CREATE TABLE customers(customer_id INTEGER PRIMARY KEY, name TEXT, city TEXT, signup_date TEXT, credit_score INTEGER);
Hints
  • Define one output row and the source-table keys.
  • Check duplicates, NULLs and the order of filtering and aggregation.

Answer

Approach: Monthly counts in a CTE, then SUM(cnt) OVER (ORDER BY month) for the running total.

WITH monthly AS (
  SELECT DATE_FORMAT(signup_date, '%Y-%m') AS m, COUNT(*) AS new_customers
  FROM customers
  GROUP BY m
)
SELECT m, new_customers,
       SUM(new_customers) OVER (ORDER BY m) AS running_customers
FROM monthly
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