SQL Challenger
0 of 250 complete

Lessons

0 of 250 complete

Saved in this browser

Lesson 145 of 250

Easy

Find the end of each signup month

Your task

Return customer_id and month_end in YYYY-MM-DD form for each signup date. Sort by customer ID.

The day before the start of next month is the last day of the current month.

Sample data

Table:customers
idnamesignup_datesource
1Avery2025-01-02organic
2Casey2025-01-05email
3Jordan2025-01-12organic
4Morgan2025-02-01email
5Riley2025-02-10social
6Sam2025-02-20organic
7Taylor2025-03-01social
8Devon2025-02-07email

Expected output

customer_idmonth_end
12025-01-31
22025-01-31
32025-01-31
42025-02-28
52025-02-28
62025-02-28
72025-03-31
82025-02-28
Show a hint

Use DATE with the modifiers 'start of month', '+1 month', and '-1 day'.

Dates, text, and JSON guide

Work with dates as ISO text

The receipt dates use YYYY-MM-DD, so their text order matches calendar order. Use an inclusive start and exclusive end for periods. This query returns receipts in the first quarter, without including April 1.

SELECT id, sold_on, amount
FROM receipts
WHERE sold_on >= '2025-01-01'
  AND sold_on < '2025-04-01';

Normalize values before comparing them

The receipt channel values vary in capitalization and spacing. TRIM removes surrounding spaces and LOWER handles these ASCII names. Use the same normalization when grouping values that represent the same category.

SELECT LOWER(TRIM(channel)) AS channel, COUNT(*) AS receipts
FROM receipts
GROUP BY LOWER(TRIM(channel));

Extract a field or expand an array

JSON_EXTRACT reads a value at a path, such as the currency code. JSON_EACH turns an array into rows with a value column. After expansion, receipt IDs can repeat and a receipt with no tags contributes no rows.

SELECT r.id, JSON_EXTRACT(r.metadata, '$.currency') AS currency,
       tag.value AS tag
FROM receipts AS r
CROSS JOIN JSON_EACH(r.metadata, '$.tags') AS tag;
SQLiteCtrl / ⌘ + Enter

Press Control+Space for SQL suggestions. Use the arrow keys to select an option and Enter to insert it.

Results

Loading SQLite…