Unit 4, Checking AI-written SQL · Checking date filters
Checking AI-written SQL: which March?
Check the AI: how many orders in March?
When you ask about a month, an AI assistant often filters on the month alone. That is only right if the data covers a single year. Before trusting a date filter, check the range of dates in the table with MIN and MAX, and make sure the filter pins down the year too.
SELECT MIN(order_date) AS first_order,
MAX(order_date) AS last_order
FROM orders;Review the AI's query
Someone asked an AI assistant: How many orders were placed in March 2026? It wrote this query:
SELECT COUNT(*) AS march_orders
FROM orders
WHERE EXTRACT(MONTH FROM order_date) = 3;
Does it answer the question?
- Yes, it answers the question
- No: it counts March of every year, not just 2026
- No: EXTRACT gives text, so it should be compared with '03'
Show the answer
No: it counts March of every year, not just 2026. The filter checks the month only. The data runs from March 2025 to November 2026, so the AI's count of 8 includes an order from March 2025. The right answer is 7. The query runs and the number looks reasonable, which is why a missing year is easy to overlook.
Then fix it
Now fix the AI's query so it counts only orders placed in March 2026. Keep the column name march_orders.
Tables you will use: orders
Try it in your browserFree, no sign-up. Your query is checked as you go.