Lesson 4 of 4 20 min read
Basic filtering and a mini SQL exercise
Combine conditions with AND, OR and NOT, use IN, BETWEEN, LIKE and IS NULL, and solve a set of SQL challenges.
In this lesson you will
- Combine conditions with AND, OR, NOT and parentheses
- Filter with IN, BETWEEN, LIKE and IS NULL
- Translate plain-language questions into correct SQL
Combining conditions
SELECT name, city, marks
FROM students
WHERE marks >= 80 AND city <> 'Mumbai';| name | city | marks |
|---|---|---|
| Asha Menon | Kochi | 88 |
| Meera Nair | Kochi | 95 |
ANDrequires both conditions.ORrequires at least one.NOTreverses a condition.
AND is evaluated before OR, so use parentheses to make your intention explicit:
-- Students in year 1 who are from Kochi or Bengaluru
SELECT name
FROM students
WHERE year = 1 AND (city = 'Kochi' OR city = 'Bengaluru');
-- Meera Nair, Kiran RaoWithout the parentheses the query would mean "year 1 students from Kochi, or any student from Bengaluru", which also returns Arjun Das.
AND: both sides must be true (contains year = 1, OR: at least one side must be true (contains city = 'Kochi', city = 'Bengaluru'))
IN: matching a list
SELECT name, city
FROM students
WHERE city IN ('Chennai', 'Bengaluru');
-- Ravi Kumar, Arjun Das, Kiran RaoIN is shorter and clearer than several OR conditions. NOT IN excludes the listed values.
BETWEEN: ranges
SELECT name, marks
FROM students
WHERE marks BETWEEN 70 AND 85;
-- Ravi Kumar (72), Priya Shah (81), Kiran Rao (77)BETWEEN is inclusive: it equals marks >= 70 AND marks <= 85. It works with numbers and dates.
LIKE: text patterns
| Pattern | Matches |
|---|---|
'A%' | Text starting with A |
'%a' | Text ending with a |
'%ra%' | Text containing "ra" anywhere |
'_iran%' | Any single character, then "iran", then anything |
SELECT name
FROM students
WHERE name LIKE 'A%';
-- Asha Menon, Arjun Das% matches any number of characters (including none) and _ matches exactly one. In PostgreSQL, LIKE is case-sensitive; ILIKE ignores case.
IS NULL: missing values
Because NULL means unknown, email = NULL is never true. Use IS NULL or IS NOT NULL:
SELECT name
FROM students
WHERE email IS NULL;
-- Kiran RaoMini SQL exercise
Try each challenge on your own before reading the solution. All use the students table.
Challenge 1. List the names of second- and third-year students with marks of at least 70, highest marks first.
SELECT name, marks
FROM students
WHERE year IN (2, 3) AND marks >= 70
ORDER BY marks DESC;
-- Asha Menon (88), Priya Shah (81), Ravi Kumar (72)Challenge 2. Find students whose surname starts with N or S. Surnames come after the space in name.
SELECT name
FROM students
WHERE name LIKE '% N%' OR name LIKE '% S%';
-- Meera Nair, Priya ShahChallenge 3. Show the student with the lowest marks who has an email address on record.
SELECT name, marks
FROM students
WHERE email IS NOT NULL
ORDER BY marks ASC
LIMIT 1;
-- Arjun Das (64)Hands-on exercise
Write and test these queries against the students table, predicting each result before you run it: (1) students not from Kochi with marks below 80; (2) students whose name contains the letters "ar" in any position, ignoring case; (3) first-year students or students with marks above 90, using parentheses to make the logic clear; (4) the number of students with no email recorded, using SELECT COUNT(*). For each query, write one sentence explaining why each returned row matches.
Knowledge check
3 questions • 70% to pass • Practice only, no certificate
Key concepts
- AND needs every condition, OR needs at least one, and NOT reverses; use parentheses because AND binds before OR.
- IN matches any value in a list; BETWEEN is an inclusive range.
- LIKE uses % for any characters and _ for one character; ILIKE ignores case in PostgreSQL.
- Test for missing values with IS NULL and IS NOT NULL, never with = NULL.
Optional next step: DevCrest Academy
PostgreSQL, Distributed SQL & Database Internals
Want to go deeper? The academy's PostgreSQL course moves from these fundamentals to query tuning, indexing, transactions and distributed SQL with hands-on labs.
- Intermediate
- 6 weeks • 45 hours
- Online Live
- From ₹7,500 per student
Fees and batch dates are sample data pending confirmation
Or keep learning for free with Programming Fundamentals.
