Welcome to Module 4 of Basic Queries! In the previous module, you learned how to use the SELECT statement to retrieve information from a database.
But what happens when a database contains 10,000 student records and you only want to find students who are 13 years old? Do you need to display all 10,000 records before finding them?
No! SQL has a special keyword called WHERE that helps you find specific information by setting conditions.
In this module, you will learn how to filter records, use comparison operators, combine conditions, and search for specific information inside database tables.
The WHERE clause is an SQL keyword used to filter records based on specific conditions.
Think of it this way: Imagine your school has 500 students, but your teacher only wants to see students in JSS 2. Instead of displaying every student, you can use WHERE to find only the students in JSS 2.
Example:
SELECT * FROM Students
WHERE Class = 'JSS 2';
This query tells the database to display only students whose class is JSS 2.
Let's understand the query:
The WHERE clause makes it easier to find exactly what you need without displaying unnecessary information.
Comparison operators help us compare values in a database. They allow us to find records that are equal to, greater than, less than, or different from a particular value.
Here are some common SQL comparison operators:
| Operator | Meaning | Example |
|---|---|---|
| = | Equal to | Age = 13 |
| > | Greater than | Age > 13 |
| < | Less than | Age < 13 |
| >= | Greater than or equal to | Age >= 13 |
| <= | Less than or equal to | Age <= 13 |
| <> | Not equal to | Age <> 13 |
SELECT * FROM Students
WHERE Age = 13;
SELECT * FROM Students
WHERE Age > 13;
SELECT * FROM Students
WHERE Age < 13;
SELECT * FROM Students
WHERE Age >= 13;
SELECT * FROM Students
WHERE Age <> 13;
Remember: Text values are usually placed inside single quotation marks, while numbers are normally written without quotation marks.
Sometimes, you may need to check more than one condition at the same time. SQL provides AND and OR operators to help you do this.
The AND operator is used when all the conditions must be true before a record is displayed.
Imagine you want to find students who are 13 years old AND are in JSS 2. Both conditions must be true.
SELECT * FROM Students
WHERE Age = 13 AND Class = 'JSS 2';
The database will display only students who satisfy both conditions.
The OR operator is used when at least one of the conditions must be true.
Imagine you want to find students who are either in JSS 1 OR JSS 2.
SELECT * FROM Students
WHERE Class = 'JSS 1' OR Class = 'JSS 2';
The database will display students who belong to either class.
| Operator | How It Works |
|---|---|
| AND | All conditions must be true. |
| OR | At least one condition must be true. |
Easy way to remember: AND means all conditions must match, while OR means any one of the conditions can match.
The NOT operator is used to reverse a condition. It helps you find records that do not match a particular condition.
Imagine you want to find all students who are NOT in JSS 2.
SELECT * FROM Students
WHERE NOT Class = 'JSS 2';
This query displays students who belong to other classes.
You can also write the same query like this:
SELECT * FROM Students
WHERE Class <> 'JSS 2';
Both examples look for students whose class is different from JSS 2.
Another example:
Imagine a supermarket wants to display products that are not in the Drinks category.
SELECT * FROM Products
WHERE NOT Category = 'Drinks';
The database will display products from categories other than Drinks.
Database tables contain different types of information. Some information is made up of numbers, while other information contains words and letters.
SQL allows you to filter both types of information.
Numeric data includes values such as age, price, quantity, and examination scores.
Example: Find students who scored 80 marks.
SELECT * FROM Results
WHERE Score = 80;
Example: Find products that cost more than โฆ5,000.
SELECT * FROM Products
WHERE Price > 5000;
Text data includes names, addresses, cities, product categories, and other information containing letters.
Example: Find students from Ibadan.
SELECT * FROM Students
WHERE City = 'Ibadan';
Example: Find customers named Mary.
SELECT * FROM Customers
WHERE Name = 'Mary';
Notice that text values are enclosed in single quotation marks.
SQL provides special operators that make filtering easier. Three useful operators are BETWEEN, IN, and LIKE.
BETWEEN is used to find values within a specified range. The starting and ending values are included.
Imagine you want to find students whose examination scores are between 50 and 80.
SELECT * FROM Results
WHERE Score BETWEEN 50 AND 80;
This displays students whose scores are 50, 60, 70, 80, or any other value within that range.
Another example: Find products priced between โฆ2,000 and โฆ10,000.
SELECT * FROM Products
WHERE Price BETWEEN 2000 AND 10000;
The IN operator helps you search for several possible values in one column without writing many OR conditions.
Imagine you want to find students in JSS 1, JSS 2, or JSS 3.
SELECT * FROM Students
WHERE Class IN ('JSS 1', 'JSS 2', 'JSS 3');
This displays students whose class matches any of the values in the list.
Another example: Find customers from Ibadan, Lagos, or Abuja.
SELECT * FROM Customers
WHERE City IN ('Ibadan', 'Lagos', 'Abuja');
The LIKE operator is used to search for a particular pattern in text. It is especially useful when you do not know the complete name or word.
For example, imagine you want to find all students whose names begin with the letter A.
SELECT * FROM Students
WHERE Name LIKE 'A%';
The percent sign (%) is a special wildcard that represents zero or more characters.
Here are some examples:
| Pattern | Meaning | Example Matches |
|---|---|---|
| 'A%' | Starts with A | Aisha, Amaka |
| '%a' | Ends with a | Fatima, Aisha |
| '%an%' | Contains an | Daniel, Anthony |
| 'J%' | Starts with J | John, James |
There is another wildcard called the underscore (_). It represents exactly one character.
Example:
SELECT * FROM Students
WHERE Name LIKE 'J_hn';
This pattern can match a four-character name such as John, where the underscore represents the missing character.
You are now working as a junior database assistant at Bright Future Academy in Ibadan.
The school administrator has provided you with this Students table:
| ID | Name | Age | Class | City |
|---|---|---|---|---|
| 201 | John | 13 | JSS 2 | Ibadan |
| 202 | Grace | 12 | JSS 1 | Lagos |
| 203 | Michael | 14 | JSS 3 | Abuja |
| 204 | Aisha | 13 | JSS 2 | Ibadan |
| 205 | David | 12 | JSS 1 | Lagos |
| 206 | Amaka | 14 | JSS 3 | Enugu |
SELECT * FROM Students
WHERE Age = 13;
SELECT * FROM Students
WHERE City = 'Ibadan';
SELECT * FROM Students
WHERE Class = 'JSS 2' AND Age = 13;
SELECT * FROM Students
WHERE Class <> 'JSS 1';
SELECT * FROM Students
WHERE Name LIKE 'A%';
Imagine you are helping a supermarket in Ibadan manage its products. The supermarket has a database table called Products.
The table contains these columns:
Write SQL queries to perform the following tasks:
The WHERE clause is very important when updating or deleting records. Without the correct condition, you may accidentally change or delete many records instead of just one.
Example of a dangerous command:
DELETE FROM Students;
This command deletes all records from the Students table.
A safer example:
DELETE FROM Students
WHERE StudentID = 201;
This command deletes only the record matching StudentID 201, assuming that StudentID identifies one student.
Always double-check your WHERE conditions before running UPDATE or DELETE commands.
In this module, you have learned:
Answer the following questions to test your understanding.
You have successfully completed Module 4 of Basic Queries! You can now filter database records and find specific information using SQL conditions and operators.
Keep practising. You are gradually developing the skills of a database professional!
Module 5: Sorting and Limiting Query Results
In the next module, you will learn how to arrange information from smallest to largest, largest to smallest, and display only the number of records you need.
CRMNuggets โ Learn. Practice. Get Certified.