Learning Goals
3 min- Recognise the four parts of every query: SELECT, FROM, WHERE, semicolon.
- Pick all columns, or pick just the ones you want.
- Filter rows with
=,!=,<,>,BETWEEN,IN,LIKE. - Combine conditions with
AND,OR,NOT.
Warm-Up · Set Up
5 minTwo ways to follow along — both free, both work today:
- Browser:
sqliteonline.com— click "SQLite", paste & run. - Desktop: Install
DB Browser for SQLite(sqlitebrowser.org). Create a new database.
Run this once to set up our sample table:
CREATE TABLE students ( id INTEGER PRIMARY KEY, name TEXT, age INTEGER, class TEXT, score INTEGER ); INSERT INTO students VALUES (1, 'Aisyah', 13, 'Form 1A', 88), (2, 'Wei Jie', 14, 'Form 1A', 75), (3, 'Suresh', 13, 'Form 1B', 92), (4, 'Mei', 14, 'Form 1B', 80), (5, 'Devi', 13, 'Form 1A', 65), (6, 'Ali', 14, 'Form 1B', 70), (7, 'Anita', 13, 'Form 1A', 95), (8, 'Razif', 14, 'Form 1B', 55);
SQL is read-aloud English. SELECT name FROM students WHERE score > 80 literally says "give me the name from the students table where the score is greater than 80". Read SQL out loud — it tells you what it does.
New Concept · SELECT + WHERE
14 minThe four parts
SELECT name, score -- columns FROM students -- table WHERE score >= 80 -- filter ; -- end of statement
name score Aisyah 88 Suresh 92 Mei 80 Anita 95
Picking columns
SELECT * FROM students; -- every column SELECT name FROM students; -- only name SELECT name, class FROM students; -- two columns SELECT name AS pupil FROM students; -- rename in the result
* means "every column". Fine in a quick query; in production code, list the columns you actually need.
Comparison operators
= equal score = 90 != not equal score != 100 (also: <>) < less than age < 14 <= less or equal age <= 14 > greater than score > 80 >= greater or eq. score >= 80
Range and set
SELECT name FROM students WHERE score BETWEEN 70 AND 80; SELECT name FROM students WHERE class IN ('Form 1A'); SELECT name FROM students WHERE class IN ('Form 1A', 'Form 1B');
String matching with LIKE
SELECT name FROM students WHERE name LIKE 'A%'; -- starts with A SELECT name FROM students WHERE name LIKE '%i'; -- ends with i SELECT name FROM students WHERE name LIKE '%e%'; -- contains e SELECT name FROM students WHERE name LIKE '_ei%'; -- _ is exactly one char
% = any number of characters; _ = exactly one. LIKE is case-insensitive in SQLite by default for ASCII.
Combining conditions
-- Form 1A and high-scoring SELECT name FROM students WHERE class = 'Form 1A' AND score >= 85; -- Either young OR a top scorer SELECT name FROM students WHERE age < 14 OR score > 90; -- NOT SELECT name FROM students WHERE NOT class = 'Form 1A';
Worked Example · Honour Roll
12 minBuild a real-world query step by step. Goal: every Form 1A student aged ≤ 13 with score ≥ 80.
-- start with a sanity check: list everyone SELECT name, age, class, score FROM students; -- narrow to Form 1A SELECT name, age, score FROM students WHERE class = 'Form 1A'; -- add the age cap SELECT name, age, score FROM students WHERE class = 'Form 1A' AND age <= 13; -- add the score floor SELECT name, age, score FROM students WHERE class = 'Form 1A' AND age <= 13 AND score >= 80;
name age score Aisyah 13 88 Anita 13 95
Read the diff
Build queries incrementally: start broad, run, narrow, run again. If a clause kills too many rows, you spot it immediately. This step-by-step rhythm is how junior engineers stop guessing.
Try It Yourself
13 minWrite a query that returns every student whose name starts with A.
Hint
SELECT name FROM students WHERE name LIKE 'A%';
Return name + score for students with score 70–90 inclusive. Sort isn't required (we'll learn it tomorrow).
Hint
SELECT name, score FROM students WHERE score BETWEEN 70 AND 90;
Find every student in Form 1A OR Form 1B who is either aged 13 with score ≥ 90, or aged 14 with score ≥ 70.
Hint
SELECT name, age, class, score FROM students WHERE class IN ('Form 1A','Form 1B') AND ( (age = 13 AND score >= 90) OR (age = 14 AND score >= 70) );
Parentheses matter when mixing AND and OR. Without them, SQL evaluates AND first — which is usually not what you want.
Mini-Challenge · Detective Queries
8 minWrite one query for each:
- Names that contain an
iAND end witha. - Form 1B students who are NOT 14.
- The id, name and score of anyone scoring below 60 OR above 90.
Show one possible solution
-- 1 SELECT name FROM students WHERE name LIKE '%i%' AND name LIKE '%a'; -- 2 SELECT name FROM students WHERE class = 'Form 1B' AND age != 14; -- 3 SELECT id, name, score FROM students WHERE score < 60 OR score > 90;
Non-negotiables: each query stands alone; LIKE patterns precise; parentheses if you mix AND/OR.
Recap
3 minSELECT cols FROM table WHERE condition is the shape of nearly every query. Combine conditions with AND/OR; use parentheses when you mix them. BETWEEN, IN and LIKE make filters readable. Tomorrow we add ordering and limiting.
Vocabulary Card
- SELECT
- Choose which columns to return.
- FROM
- Choose which table to read.
- WHERE
- Choose which rows to keep.
- LIKE
- Pattern-match strings.
%= many,_= one.
Homework
4 minRun the setup SQL in sqliteonline.com, save your database file, then write 5 queries:
- Every student.
- Only Form 1A.
- Students aged 14 with score above 70.
- Students whose names contain a vowel pattern of your choice.
- Students NOT in Form 1A.
Take a screenshot of each query + result and paste them into a markdown file.
Sample answers
SELECT * FROM students; SELECT * FROM students WHERE class = 'Form 1A'; SELECT name, score FROM students WHERE age = 14 AND score > 70; SELECT name FROM students WHERE name LIKE '%ei%'; SELECT name, class FROM students WHERE class != 'Form 1A';