Which expression would you use to filter orders within the last 30 days relative to today?

Study for the SQL Basics Test. Improve your knowledge with multiple choice questions and detailed explanations. Prepare effectively to master SQL concepts!

Multiple Choice

Which expression would you use to filter orders within the last 30 days relative to today?

Explanation:
The key idea is to define a rolling 30-day window based on today and keep only orders that fall within that window. Using the current date and subtracting 30 days gives a clear cutoff date. By comparing order_date to that cutoff with a strict greater-than, you select orders that occurred after that cutoff, i.e., within the most recent 30 days up to today. Using CURRENT_DATE helps keep the boundary simple and avoids time components that can complicate date comparisons if order_date is a date (or a timestamp that will implicitly cast). It also ensures the window stays fixed to 30 days, not to calendar months. Other approaches either introduce a time component (NOW()) which can affect boundary behavior, or subtract a calendar month (ADD_MONTHS) which isn’t the same as 30 days, since month lengths vary. The chosen expression thus cleanly and reliably captures the intended 30-day period.

The key idea is to define a rolling 30-day window based on today and keep only orders that fall within that window. Using the current date and subtracting 30 days gives a clear cutoff date. By comparing order_date to that cutoff with a strict greater-than, you select orders that occurred after that cutoff, i.e., within the most recent 30 days up to today. Using CURRENT_DATE helps keep the boundary simple and avoids time components that can complicate date comparisons if order_date is a date (or a timestamp that will implicitly cast). It also ensures the window stays fixed to 30 days, not to calendar months.

Other approaches either introduce a time component (NOW()) which can affect boundary behavior, or subtract a calendar month (ADD_MONTHS) which isn’t the same as 30 days, since month lengths vary. The chosen expression thus cleanly and reliably captures the intended 30-day period.

Subscribe

Get the latest from Passetra

You can unsubscribe at any time. Read our privacy policy