Lesson 2 / 10

SELECT, WHERE and Filtering Rows

SELECT *
FROM students;   -- every column, every row

* means “all columns.” It’s convenient while exploring a table you don’t know yet, but in real application code it’s considered bad practice — naming the exact columns you need makes a query faster, and protects your code from breaking silently if someone adds a new column to the table later.

Filtering with WHERE

SELECT name, score
FROM students
WHERE score >= 75;

Combining conditions

SELECT name
FROM students
WHERE course = 'SQL' AND score >= 90;

SELECT name
FROM students
WHERE course = 'Python' OR course = 'JavaScript';

AND requires every condition to be true for a row to be included; OR only requires at least one. Mixing both in the same query without parentheses can produce surprising results, since AND is evaluated before OR — when in doubt, use parentheses to make the grouping explicit, exactly like in a math expression.

Pattern matching and ranges

SELECT name FROM students WHERE name LIKE 'Y%';       -- starts with Y
SELECT name FROM students WHERE name LIKE '%a%';      -- contains an 'a' anywhere
SELECT name FROM students WHERE score BETWEEN 70 AND 89;
SELECT name FROM students WHERE course IN ('SQL', 'Python');

% in a LIKE pattern matches any sequence of characters (including none), while _ matches exactly one character. BETWEEN is inclusive on both ends, and IN is shorthand for a chain of OR comparisons against the same column — much easier to read than course = 'SQL' OR course = 'Python' once you have more than two options.

Checking for missing data

SELECT name FROM students WHERE score IS NULL;
SELECT name FROM students WHERE score IS NOT NULL;

A common beginner mistake is writing WHERE score = NULL, which never matches anything — NULL represents “unknown,” and by definition nothing can be compared equal to an unknown value. Always use IS NULL / IS NOT NULL instead.