Welcome to Module 4 of Introduction to SQL for Data Analysis.
In the previous module, you learned how to retrieve information from a database
using the SELECT statement.
Now, imagine you are managing a school database with 500 students. Your teacher asks you to find only students who are in JSS 2. Do you need to display all 500 students? No!
You can use SQL to find exactly the information you need. This process is called filtering data.
Data filtering means selecting only the records that meet a particular condition. Instead of displaying everything in a database, you tell SQL what you want to see.
For example, you may want to:
SQL uses a command called WHERE to perform these tasks.
The WHERE clause tells SQL the condition that a record must meet
before it is displayed.
SELECT column_name
FROM table_name
WHERE condition;
Let's look at an example.
Imagine we have a table called Students.
| student_id | student_name | age | class | state |
|---|---|---|---|---|
| 1 | Samuel | 13 | JSS 2 | Oyo |
| 2 | Mary | 12 | JSS 1 | Lagos |
| 3 | Peter | 14 | JSS 3 | Ogun |
| 4 | Esther | 13 | JSS 2 | Osun |
| 5 | David | 12 | JSS 1 | Oyo |
To find students who are 13 years old, write:
SELECT *
FROM Students
WHERE age = 13;
SQL will display Samuel and Esther because they are the students whose ages are equal to 13.
Comparison operators help SQL compare values. They allow you to search for records using conditions such as equal to, greater than or less than.
| Operator | Meaning | Example |
|---|---|---|
| = | Equal to | age = 13 |
| != | Not equal to | age != 13 |
| <> | Not equal to | age <> 13 |
| > | Greater than | age > 12 |
| < | Less than | age < 14 |
| >= | Greater than or equal to | age >= 13 |
| <= | Less than or equal to | age <= 13 |
Find students older than 12.
SELECT *
FROM Students
WHERE age > 12;
Find students younger than 14.
SELECT *
FROM Students
WHERE age < 14;
Find students who are not 13 years old.
SELECT *
FROM Students
WHERE age != 13;
Sometimes, you may need more than one condition.
The AND operator allows you to combine conditions.
All conditions must be true for a record to appear.
For example, find students who are 13 years old and belong to JSS 2.
SELECT *
FROM Students
WHERE age = 13 AND class = 'JSS 2';
SQL will only display students who satisfy both conditions.
Imagine a supermarket wants to find products that cost more than โฆ1,000 and belong to the Food category.
SELECT *
FROM Products
WHERE price > 1000 AND category = 'Food';
The OR operator allows you to search for records that satisfy
at least one of the conditions.
For example, find students who are in JSS 1 or JSS 2.
SELECT *
FROM Students
WHERE class = 'JSS 1' OR class = 'JSS 2';
A student will appear if the student belongs to either of the two classes.
Find customers who live in Ibadan or Lagos.
SELECT *
FROM Customers
WHERE city = 'Ibadan' OR city = 'Lagos';
The NOT operator reverses a condition. It helps you find
records that do not meet a particular condition.
For example, find students who are not in JSS 2.
SELECT *
FROM Students
WHERE NOT class = 'JSS 2';
You can also write:
SELECT *
FROM Students
WHERE class != 'JSS 2';
The BETWEEN operator helps you find values within a particular
range. The starting and ending values are included.
For example, find students whose ages are between 12 and 14.
SELECT *
FROM Students
WHERE age BETWEEN 12 AND 14;
This query includes students aged 12, 13 and 14.
Find products with prices between โฆ1,000 and โฆ5,000.
SELECT *
FROM Products
WHERE price BETWEEN 1000 AND 5000;
The IN operator makes it easier to search for several possible
values in one column.
For example, find students in JSS 1, JSS 2 or JSS 3.
SELECT *
FROM Students
WHERE class IN ('JSS 1', 'JSS 2', 'JSS 3');
This is shorter than writing several conditions using OR.
Compare the two approaches:
SELECT *
FROM Students
WHERE class = 'JSS 1'
OR class = 'JSS 2'
OR class = 'JSS 3';
Both queries can return the same results.
The LIKE operator helps you search for text that matches a
particular pattern.
It commonly uses two special characters:
% represents zero or more characters._ represents exactly one character.SELECT *
FROM Students
WHERE student_name LIKE 'S%';
This finds names that begin with the letter S, such as Samuel.
SELECT *
FROM Students
WHERE student_name LIKE '%y';
This can find names such as Mary.
SELECT *
FROM Students
WHERE student_name LIKE '%e%';
This finds names containing the letter E anywhere in the name.
SELECT *
FROM Students
WHERE student_name LIKE '_ary';
The underscore represents one character before "ary". For example, this pattern can match Mary.
Sometimes, a database contains records with missing information.
In SQL, a missing or unknown value can be represented by NULL.
Imagine a Students table where some students have not provided their email addresses.
To find students whose email addresses are missing, use:
SELECT *
FROM Students
WHERE email IS NULL;
To find students who have provided their email addresses, use:
SELECT *
FROM Students
WHERE email IS NOT NULL;
Important: Do not use email = NULL to search
for missing values. Use IS NULL or IS NOT NULL.
You can combine multiple SQL operators to answer more detailed questions.
Find students who are at least 13 years old and belong to JSS 2 or JSS 3.
SELECT *
FROM Students
WHERE age >= 13
AND class IN ('JSS 2', 'JSS 3');
Find products that cost between โฆ1,000 and โฆ10,000 but are not in the Drinks category.
SELECT *
FROM Products
WHERE price BETWEEN 1000 AND 10000
AND category != 'Drinks';
When combining AND and OR, use parentheses
when necessary to make your intended conditions clear.
SELECT *
FROM Students
WHERE age = 13
AND (class = 'JSS 2' OR class = 'JSS 3');
| Mistake | Correct Approach |
|---|---|
| WHERE age == 13 | WHERE age = 13 |
| WHERE class = JSS 2 | WHERE class = 'JSS 2' |
| WHERE email = NULL | WHERE email IS NULL |
| Using AND when either condition should match | Use OR |
| Forgetting the WHERE condition | Check the condition before running the query |
Remember these rules:
AND when all conditions must be true.OR when at least one condition can be true.IS NULL to find missing values.Imagine you are helping a supermarket in Ibadan analyse its product database. The database contains a table called Products.
Assume the table has these columns:
Write SQL queries to answer the following questions:
-- 1. Products above โฆ2,000
SELECT *
FROM Products
WHERE price > 2000;
-- 2. Products in the Food category
SELECT *
FROM Products
WHERE category = 'Food';
-- 3. Products between โฆ1,000 and โฆ5,000
SELECT *
FROM Products
WHERE price BETWEEN 1000 AND 5000;
-- 4. Food or Drinks products
SELECT *
FROM Products
WHERE category IN ('Food', 'Drinks');
-- 5. Products with quantity below 10
SELECT *
FROM Products
WHERE quantity < 10;
-- 6. Products beginning with R
SELECT *
FROM Products
WHERE product_name LIKE 'R%';
-- 7. Products outside Electronics
SELECT *
FROM Products
WHERE category != 'Electronics';
-- 8. Products with missing category
SELECT *
FROM Products
WHERE category IS NULL;
In this module, you have learned how to filter and search database records using SQL.
WHERE filters records based on conditions.AND combines conditions that must all be true.OR allows either condition to be true.NOT excludes matching conditions.BETWEEN searches within a range.IN searches for multiple possible values.LIKE searches for matching text patterns.IS NULL finds missing values.You now know how to search through thousands of records and find only the information you need. This is one of the most important skills in data analysis.
Question 1: Which SQL clause is used to filter records?
Question 2: Which operator means greater than?
Question 3: Which operator requires all conditions to be true?
Question 4: Which operator searches for values within a range?
Question 5: Which SQL condition finds missing email addresses?
Question 6: Which symbol represents zero or more characters in a LIKE pattern?
Question 7: What does the IN operator help you do?
Question 8: Which query finds students who are 13 years old?
Question 9: Which operator excludes records that match a condition?
Question 10: Which pattern finds names beginning with S?
In the next module, you will learn how to arrange database records in a useful
order using ORDER BY, sort data from smallest to largest, arrange
names alphabetically, and limit the number of records displayed using
LIMIT.
CRMNuggets โ Learn. Practice. Get Certified.