QUERYINGBEGINNERlesson-05

SELECT & Filtering with WHERE

🕑 32 min
📃 5 sections
🎓 Querying
SELECT is the most written SQL statement in the world. WHERE is what gives it power — turning a query that returns millions of rows into one that returns exactly what you need. Master these two commands and you'll handle the majority of real-world SQL work.

SELECT Basics

SQL
-- All columns
SELECT * FROM students;

-- Specific columns
SELECT name, age, grade FROM students;

-- Column aliases
SELECT name AS student_name, age AS years_old FROM students;

-- Calculated column
SELECT name, age, age + 1 AS next_year_age FROM students;

-- Remove duplicates
SELECT DISTINCT grade FROM students;

-- Concatenate columns
SELECT CONCAT(name, ' (Grade: ', grade, ')') AS label FROM students;

WHERE Comparison Operators

SQL
SELECT * FROM students WHERE age = 17;
SELECT * FROM students WHERE age != 17;    -- or <> 17
SELECT * FROM students WHERE age > 16;
SELECT * FROM students WHERE age >= 17;
SELECT * FROM students WHERE age BETWEEN 15 AND 18;  -- inclusive
SELECT * FROM students WHERE grade IN ('A', 'A+');
SELECT * FROM students WHERE grade NOT IN ('F', 'D');

LIKE Pattern Matching

SQL
-- % = any number of characters
SELECT * FROM students WHERE name LIKE 'A%';       -- starts with A
SELECT * FROM students WHERE email LIKE '%gmail%'; -- contains gmail
SELECT * FROM students WHERE name LIKE '%an';      -- ends with 'an'

-- _ = exactly one character
SELECT * FROM students WHERE name LIKE '___a%';    -- 3 chars then 'a'

-- Case insensitive in MySQL by default
-- Use BINARY for case sensitive:
SELECT * FROM students WHERE BINARY name LIKE 'sara%';

NULL Checks and Logical Operators

SQL
-- NULL checks (always use IS NULL, never = NULL)
SELECT * FROM students WHERE email IS NULL;
SELECT * FROM students WHERE email IS NOT NULL;

-- AND, OR, NOT
SELECT * FROM students WHERE grade = 'A' AND age >= 17;
SELECT * FROM students WHERE grade = 'A' OR grade = 'A+';
SELECT * FROM students WHERE NOT (grade = 'F' OR grade = 'D');

-- Operator precedence: AND before OR — use parentheses!
SELECT * FROM students
WHERE (grade = 'A' OR grade = 'A+')
  AND age >= 16
  AND email IS NOT NULL;
ℹ️
NULL Comparison Rule
NULL = NULL is always FALSE in SQL. You must use IS NULL to check for missing values. This trips up even experienced developers.

ORDER BY, LIMIT, DISTINCT Combined

SQL
-- Sort alphabetically
SELECT name, grade FROM students ORDER BY grade ASC, name ASC;

-- Top 3 oldest students
SELECT name, age FROM students ORDER BY age DESC LIMIT 3;

-- Unique grades
SELECT DISTINCT grade FROM students ORDER BY grade;

-- Complex query combining multiple clauses
SELECT name, age, grade
FROM students
WHERE age BETWEEN 16 AND 18
  AND email IS NOT NULL
ORDER BY grade ASC, age DESC
LIMIT 10;
SQL Execution Order
FROM
WHERE
SELECT
ORDER BY
LIMIT
🎤 Knowledge Check
1. What does % do in LIKE?
% is a wildcard that matches zero or more characters. Use _ to match exactly one character.
2. How do you check for missing values in SQL?
Always use IS NULL — the = operator always returns FALSE when comparing to NULL because NULL means "unknown."
3. What does SELECT DISTINCT do?
SELECT DISTINCT removes duplicate rows from the result set — useful for finding unique values like all the different grades in a class.
4. Which finds students aged 15 to 18 inclusive?
BETWEEN is inclusive — BETWEEN 15 AND 18 includes both 15 and 18. Note: "age > 14 AND age < 19" also works but is less readable.
5. What is wrong with: WHERE email = NULL?
= NULL never returns true in SQL because NULL represents an unknown value. Use IS NULL or IS NOT NULL instead.