AI Hints
Homeโ€บSQL Tutorialโ€บWorking with Dates
SQL ยท Lesson 30 of 126

Practice: Busy Seasons

Dates in real questions.

โฑ 2 min readโ–ถ Video at 42:06๐ŸŽ“ Free
Watch this lesson in the SQL courseWatch on YouTube โ†—Full SQL course

Helen wants to plan staff for next Christmas. How many real orders came in last December? A real order means one that wasn't cancelled.

Practice: Busy Seasons
From the video: Practice: Busy Seasons
  • Planning for Christmas
  • Orders last December
  • Not counting cancellations

December orders

We combine a date range with a status test. Between the first and the thirty first of December, and status is not cancelled. Run it. Eighty eight real orders in December. That's one of Helen's busier months, so she'll want extra help.

query.sqlsql
SELECT COUNT(*) AS december_orders
FROM orders
WHERE order_date BETWEEN '2025-12-01' AND '2025-12-31'
  AND status <> 'cancelled';
Output
โ”Œโ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”
โ”‚ december_orders โ”‚
โ”œโ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”ค
โ”‚ 88              โ”‚
โ””โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”˜

Returns in February

Your turn. Find every order from February twenty twenty five that was returned. Show the order ID, the date, and the status. Pause and try it. Here's one solution. The month is zero two, the year is twenty twenty five, and the status is returned.

โœ๏ธ Exercise: Returns in February

Orders from February 2025 with status 'returned'

Show hint

strftime('%m', ...) = '02' and strftime('%Y', ...) = '2025'

Show solution
solution.sqlsql
SELECT order_id, order_date, status
FROM orders
WHERE strftime('%m', order_date) = '02'
  AND strftime('%Y', order_date) = '2025'
  AND status = 'returned';
Output
โ”Œโ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”ฌโ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”ฌโ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”
โ”‚ order_id โ”‚ order_date โ”‚  status  โ”‚
โ”œโ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”ผโ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”ผโ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”ค
โ”‚ 2071     โ”‚ 2025-02-09 โ”‚ returned โ”‚
โ”‚ 2152     โ”‚ 2025-02-14 โ”‚ returned โ”‚
โ”‚ 2546     โ”‚ 2025-02-26 โ”‚ returned โ”‚
โ””โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”ดโ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”ดโ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”˜

Quick check

Quick check. Which of these finds orders in March twenty twenty five? Pause and choose. The answer is C. Both ends of the range are included, so the first and the thirty first of March are counted. Option A would miss most of the month.

Quick check
Orders placed in March 2025

Summary

Dates are no longer scary. You can filter, split, add, and count them. You've finished the functions module! Next, the moment SQL becomes truly powerful. Counting and adding up.

Key takeaways
  • Date ranges with BETWEEN
  • Parts with strftime
  • Combine with AND
Next up: Counting Rows โ€” COUNT