Topic 9 ยท Databases ยท 9.4

SELECT ... FROM ... WHERE Explained Simply

WHERE adds a condition to the query so that only matching records appear in the output.

Invitation

What if you only want some of the records?

SELECT chooses the fields. FROM chooses the table. WHERE adds a condition.

The database checks each record and only returns the ones that match that condition.

WHERE filters the records.
How WHERE changes the output
Whole Students table
โ†“
WHERE Age = 15
โ†“
Only matching records remain
Big Idea

Read the query as one simple sentence

SQL QueryPlain English
SELECT Name
FROM Students
WHERE Age = 15;
SELECT NameChoose the Name field.
FROM StudentsUse the Students table.
WHERE Age = 15Keep only records where Age is 15.
OUTPUTShow the names that remain.
Plain English: โ€œShow me the names of students who are 15.โ€
How It Works

Keep the same three-step pattern

The map stays the same. Only the query becomes more precise.

1. DATABASEStudents table
โ†’
2. QUERYSELECT Name FROM Students WHERE Age = 15
โ†’
3. OUTPUTAlice and Chen
Database โ†’ Query โ†’ Filtered Output.
Worked Example

Follow the database, query and output

1. Database

StudentIDNameAgeHouse
101Alice15Blue
102Ben16Green
103Chen15Blue

2. SQL Query

SQLFilter
SELECT Name
FROM Students
WHERE Age = 15;

3. Output

Alice
Chen
Why is Ben missing? His Age is 16, so his record does not match the WHERE condition.
Another Example

Text values need quotation marks

SQL QueryMeaning
SELECT Name
FROM Students
WHERE House = "Blue";

Query Meaning

Choose the Name field from Students, but keep only records where House is Blue.

Output

Alice
Chen
Numbers do not need quotation marks. Text values do.
Exam Tip

Apply the WHERE condition before writing the output

1Find the selected fields.
2Test each record against WHERE.
3Write only matching records.
Exam rule: WHERE filters the records.
Common Mistake

Do not include records that fail the condition

โœ— Incorrect output

Alice Ben Chen

This ignores WHERE Age = 15.

โœ“ Correct output

Alice Chen

Only matching records are returned.

Remember: SELECT controls the columns. WHERE controls the rows.
Summary

Three keywords, three clear jobs

SELECTChooses the fields.
FROMChooses the table.
WHEREFilters the records.
Final rule: SELECT columns. WHERE rows.