Reading a Query Before You Run It
After this lesson, you will be able to:
- Read a query clause by clause and say in one plain sentence what it asks
- Explain what NULL means and why a plain equals test never matches it
- Predict which rows a not-equal test drops when some values are missing
- Use a row count and a small hand check to test a query result
- Apply a short checklist to SQL written by an AI tool before you trust it
- Spot the words that change data, so you know when a query is not just a question
#Read It Like a Sentence
When you get a query from someone else, do not start by running it. Read it first, in the order a database would act it out. Ask four small questions, one clause at a time.
- FROM: which table does it start with?
- WHERE: which rows does it keep? What exactly is the test?
- SELECT: which columns will I see?
- ORDER BY and LIMIT: in what order, and how many rows?
Try it on this query.
SELECT name, hire_date
FROM employees
WHERE hire_date >= '2021-01-01'
ORDER BY hire_date DESC
LIMIT 3;
LIMIT 3 cuts.Do: Compare the three names you predicted with the result. Then changeLIMIT 3toLIMIT 4.Notice: Jack Taylor, Grace Lee and Eve Brown come first. With LIMIT 4, David Wilson (2021-02-10) appears as the fourth row.Why: The testhire_date >= '2021-01-01'keeps four rows. LIMIT 3 was cutting the last one, and a reading would have told you that before you ran anything.
A colleague sends: SELECT name FROM employees WHERE department = 'Sales' ORDER BY salary DESC LIMIT 1; What does it ask for?
In one sentence: read each clause in order, say the query as one plain sentence, and predict a few rows by hand before you run it.
#The Missing Value Trap
manager_id column of the employees table. It holds the id of a person's manager. Three people, Alice, Carol and Frank, have no manager listed. In a database, an empty cell like that is called NULL, which means "no value here". It does not mean zero and it does not mean an empty word. It means the fact is missing or does not apply.| name | manager_id |
|---|---|
| Alice Johnson | NULL |
| Bob Smith | 1 |
| Carol Davis | NULL |
| David Wilson | 1 |
| Eve Brown | 3 |
| Frank Miller | NULL |
| Grace Lee | 6 |
| Henry Chen | 1 |
| Iris Patel | 3 |
| Jack Taylor | 6 |
WHERE manager_id != 1, where != means "is not equal to". Before running it, act it out. Go down the ten rows, and for each one ask whether the test is true.- Bob: 1 is not different from 1, so the test is false. Dropped.
- Eve: 3 is different from 1, so it is true. Kept.
- Grace and Jack: 6 is different from 1, true. Kept.
- Iris: 3, true. Kept.
- David and Henry: 1, false. Dropped.
- Alice, Carol and Frank: the manager is NULL. Is "no value" different from 1? The database cannot say yes or no, because it has no value to compare. It treats the answer as unknown, and
WHEREkeeps a row only when the test is true. Unknown is not true, so these three are dropped too.
The result is four rows: Eve, Grace, Iris and Jack. Many people expect seven, because the three people with no manager surely do not report to manager 1. The database does not guess.
How many rows does SELECT name FROM employees WHERE manager_id != 1; return?
Do: Run it and count the rows. Then change the last line toWHERE manager_id = NULL;and run again. Then change it toWHERE manager_id IS NULL;.Notice: The first run gives 4 rows: Eve, Grace, Iris and Jack. The= NULLrun gives 0 rows with no error. TheIS NULLrun gives 3 rows: Alice, Carol and Frank.Why: A missing value is never equal to anything, not even to another missing value, so= NULLis never true. The special phraseIS NULLis the test that can find missing values.
To get the seven people you probably wanted, say both parts out loud: "not under manager 1, or with no manager".
SELECT name, manager_id
FROM employees
WHERE manager_id != 1 OR manager_id IS NULL;
That returns seven rows: Alice, Carol, Eve, Frank, Grace, Iris and Jack.
Which query finds the people who have no manager?
In one sentence: NULL means no value, an equals or not-equals test on it is never true, and IS NULL is the only test that finds it.
#Check the Count
A result can look tidy and be wrong. Two cheap checks catch many problems. Neither needs any new SQL, only a little arithmetic you can do with the table in view.
department = 'engineering' with a small e finds nothing, because text is compared letter for letter. Run it and see. Other databases may treat capitals differently, so test your own.Do: Run the query as it is. Then change 'engineering' to 'Engineering' and run again.Notice: The first run gives 0 rows and no error message. The second gives 4 rows: Alice, Bob, David and Henry.Why: A different capital letter is a different word to the database. No error only means the database could follow your words, not that they matched anything.
WHERE department = 'Engineering' AND salary > 90000 include him? Yes. Would it include Bob, who earns 85000? No. If the result contradicts a row you can check by eye, the query is wrong, however clean it looks.In one sentence: guess the count first, treat zero rows as a clue, and test one row you can check by eye.
#Checking SQL That an AI Tool Wrote
Many people now ask a chatbot to write SQL from a plain question. This is a real time-saver, and it is also where this lesson pays off. An AI tool writes a query that looks right. It does not run your database or know your data. Read its work the way you read a colleague's: carefully, and before you trust it.
Here is a plain question: "Who are the top 3 paid people in Engineering?" Suppose a tool answers with this.
SELECT name, salary
FROM employees
WHERE department = 'Engineering'
ORDER BY salary
LIMIT 3;
DESC, so the sort goes smallest first. LIMIT 3 then keeps the three smallest. This query returns the three lowest paid engineers, and not the three highest.Act it out. The four engineers by salary are Bob 85000, David 92000, Alice 95000 and Henry 98000. Smallest first and keep three gives Bob, David and Alice. Henry, the top earner, is missing, and the answer looks completely reasonable.
Do: Run it. Then addDESCaftersalaryin the ORDER BY line and run again.Notice: Without DESC you get Bob 85000, David 92000 and Alice 95000. With DESC you get Henry 98000, Alice 95000 and David 92000.Why: ORDER BY sorts smallest first unless told otherwise, and LIMIT keeps the start of the sorted list. One missing word flipped the question the query answers.
Use this short checklist on any query you did not write.
- Does it only ask? Check for words that change data (the next section).
- Right table and columns? Compare the names with the real table.
- Does the filter say what the question says? Check each test: greater or at least, text spelled and capitalised as stored, and what happens to empty cells.
- Do the sort and limit match the words "top", "latest", "first"? Top usually needs DESC.
- Does one hand check agree? Pick a row you can verify and see whether the result agrees.
None of this needs you to write the query yourself. It needs you to read it, which is a smaller skill and one you now have.
In one sentence: treat an AI-written query as a draft, read it with the checklist, and check one result by hand.
#Words That Change Data
Every query so far only asked. Some queries do not ask. They change what is stored, and you cannot take that back by pressing Run again. A few words give them away.
| Word | What it does |
|---|---|
| INSERT | adds new rows |
| UPDATE | changes values in existing rows |
| DELETE | removes rows |
| DROP | removes a whole table |
| ALTER | changes the shape of a table |
If a query contains one of these words, stop and read twice. Here is what a missing filter can do:
UPDATE employees SET salary = 0;
SELECT queries and nothing can be changed by mistake.You are handed this query to run on your company's database: DELETE FROM orders WHERE status = 'pending'; What should you do first?
In one sentence: INSERT, UPDATE, DELETE, DROP and ALTER change data, so a query with one of them is read twice and tried on a copy before it touches real work.
#Where to Go Next
You can now read a query, spot the missing-value trap, and check a result before you trust it.
- Next: Your First SQL Query walks through SELECT, columns and LIMIT in more detail, with the other sample tables.
- Then: Filtering & Sorting Data covers AND, OR, NOT and sorting properly.
- Later: INSERT, UPDATE, DELETE: Modifying Data teaches the words from the last section, with transactions as a safety net.
In one sentence: carry the reading routine and the checklist into every lesson that follows.
The mark for a missing value in a cell. It is not zero and not an empty word.
The test that finds rows whose cell is missing. A plain equals test never matches NULL.
The result of testing a missing value. WHERE keeps a row only when the test is true, so unknown rows are dropped.
Saying a query as one plain sentence, clause by clause, before running it.
How many rows a result has. A quick guess of the count before running is a cheap check.
Short for descending. It makes ORDER BY put the biggest value first.
Permission to look at data with SELECT but not to change it.
#Key Takeaways
- Read a query clause by clause, in the order a database acts it out, and say it as one plain sentence before you run it
- NULL means no value. An equals or not-equals test on it is never true, so rows quietly drop out, and IS NULL is the test that finds them
- Guess the row count first, treat zero rows as a clue, and check one row you can verify by eye
- An AI-written query is a draft. Check the filter, the sort direction and the limit against the words of the question
- INSERT, UPDATE, DELETE, DROP and ALTER change data, so read those twice and try them on a copy
#Quick Check
A query has WHERE manager_id != 1 and the manager_id column has some NULL cells. What happens to the NULL rows?