2.3 · SELECT & WHERE
Goal: read data effectively using SELECT and filter rows with WHERE.
SELECT — pick columns
sql
SELECT * FROM Student; -- every column
SELECT name, class_id FROM Student; -- specific columns
SELECT name AS pupil_name FROM Student; -- alias1
2
3
2
3
WHERE — filter rows
sql
SELECT * FROM Student WHERE class_id = 'F.4A';
SELECT * FROM Student WHERE dob >= '2008-01-01';
SELECT * FROM Score WHERE subject = 'ICT' AND score >= 80;
SELECT * FROM Student WHERE name LIKE 'A%';1
2
3
4
2
3
4
Comparison operators
| Operator | Meaning | Example |
|---|---|---|
= | Equal | score = 100 |
<> | Not equal (standard SQL) | class_id <> 'F.4B' |
> < >= <= | Comparisons | score >= 50 |
BETWEEN a AND b | Inclusive range | score BETWEEN 70 AND 90 |
IN (...) | Member of list | class_id IN ('F.4A','F.4C') |
LIKE pattern | Pattern match | name LIKE 'A%' |
IS NULL / IS NOT NULL | Null test | email IS NULL |
Logical operators
| Operator | Meaning |
|---|---|
AND | Both conditions true |
OR | At least one true |
NOT | Negation |
Combine with parentheses for clarity:
sql
SELECT *
FROM Student
WHERE (class_id = 'F.4A' AND dob >= '2008-01-01')
OR class_id = 'F.4C';1
2
3
4
2
3
4
Worked examples
List all F.4A students
sql
SELECT student_id, name FROM Student WHERE class_id = 'F.4A';1
ICT scores above 80
sql
SELECT s.name, sc.score
FROM Student s, Score sc -- (use JOIN in 2.8)
WHERE s.student_id = sc.student_id
AND sc.subject = 'ICT'
AND sc.score > 80;1
2
3
4
5
2
3
4
5
Students born in 2007
sql
SELECT name, dob FROM Student WHERE dob BETWEEN '2007-01-01' AND '2007-12-31';
SELECT name, dob FROM Student WHERE YEAR(dob) = 2007; -- simpler with YEAR()1
2
2
Names starting with A or B
sql
SELECT name FROM Student WHERE name LIKE 'A%' OR name LIKE 'B%';1
How to predict output by hand
- FROM — write down all rows of the table.
- WHERE — cross out rows that fail the condition.
- SELECT — keep only requested columns.
This three-step trace earns marks on "what is the output" questions.
Common student mistakes
- Comparing with
=for NULL (useIS NULL). - Single quotes for strings, double quotes for identifiers in standard SQL (most DBMS accept either, but be consistent).
- Forgetting
ANDbetween conditions (WHERE a=1 b=2→ syntax error).
Exam-style question
Q (5 marks): Write SQL queries against
Student(student_id, name, class_id, dob)andScore(student_id, subject, score):(a) List names of F.4A students. (b) List subjects where any student scored 100. (c) List students born between 2007-01-01 and 2008-12-31 whose name starts with 'A'.
Sample answer:
sql
-- (a)
SELECT name FROM Student WHERE class_id = 'F.4A';
-- (b)
SELECT DISTINCT subject FROM Score WHERE score = 100;
-- (c)
SELECT name, dob
FROM Student
WHERE dob BETWEEN '2007-01-01' AND '2008-12-31'
AND name LIKE 'A%';1
2
3
4
5
6
7
8
9
10
11
2
3
4
5
6
7
8
9
10
11
Key takeaways
- SELECT picks columns; WHERE filters rows.
- Use the right operator:
=,BETWEEN,IN,LIKE,IS NULL. - AND / OR / NOT combine conditions.
➡️ Next: 2.4 Operators & LIKE / IN