Unit 3, Checking AI-written SQL · Checking text matches
Checking AI-written SQL: text that only looks the same
Check the AI: how many sign-ups from Denver?
AI assistants usually compare text with =, which only matches values typed exactly alike. Before trusting a count or filter on typed-in text, look at the distinct values in the column. If the same thing is written several ways, the query has to clean the values before comparing them.
SELECT DISTINCT city
FROM signups
ORDER BY city;Review the AI's query
Someone asked an AI assistant: How many newsletter sign-ups came from Denver? It wrote this query:
SELECT COUNT(*) AS denver_signups
FROM signups
WHERE city = 'Denver';
Does it answer the question?
- Yes, it answers the question
- No: it misses Denver sign-ups typed with extra spaces or different capitals
- No: COUNT(*) also counts sign-ups with no city
Show the answer
No: it misses Denver sign-ups typed with extra spaces or different capitals. An = comparison matches the text exactly. ' Denver', 'DENVER' and 'denver ' are all Denver to a person, but not to SQL, so the AI's count of 2 misses three of the five Denver sign-ups. Nothing errors and 2 looks plausible, which is why text matches deserve a look at the raw values.
Then fix it
Now fix the AI's query so it counts every Denver sign-up, however the city was typed. Keep the column name denver_signups.
Tables you will use: signups
Try it in your browserFree, no sign-up. Your query is checked as you go.