Hiring Partners
Segilola Resources Operating LimitedTaviatech TechnologiesFCMBMTN NigeriaAdvans La Fayette Microfinance BankAboki AfricaFirst Bank of NigeriaOPay
โ† Introduction to SQL for Data Analysis ยท Lesson 5 of 9

Module Four

๐Ÿ“– Every lesson in this course is free to read right here, no account needed. Create a free account to track your progress, take the exam, and earn your certificate.
5
Module 4: Filtering and Searching Data Using SQL** for the *Introduction to SQL for Data Analysis* course. This module builds on Module 3 and introduces the `WHERE` clause and SQL operators using simple examples a 13-year-old can understand.

Module 4: Filtering and Searching Data Using SQL

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.


1. What Is Data Filtering?

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:

  • Find students who are 13 years old.
  • Find products that cost less than โ‚ฆ5,000.
  • Find customers from Ibadan.
  • Find employees earning more than โ‚ฆ100,000.
  • Find students who belong to JSS 2.

SQL uses a command called WHERE to perform these tasks.


2. Understanding the WHERE Clause

The WHERE clause tells SQL the condition that a record must meet before it is displayed.

Basic Syntax

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
1Samuel13JSS 2Oyo
2Mary12JSS 1Lagos
3Peter14JSS 3Ogun
4Esther13JSS 2Osun
5David12JSS 1Oyo

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.


3. SQL Comparison Operators

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 toage = 13
!=Not equal toage != 13
<>Not equal toage <> 13
>Greater thanage > 12
<Less thanage < 14
>=Greater than or equal toage >= 13
<=Less than or equal toage <= 13

Example 1: Greater Than

Find students older than 12.

SELECT *
FROM Students
WHERE age > 12;

Example 2: Less Than

Find students younger than 14.

SELECT *
FROM Students
WHERE age < 14;

Example 3: Not Equal To

Find students who are not 13 years old.

SELECT *
FROM Students
WHERE age != 13;

4. Using AND to Combine Conditions

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.

Another Example

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';

5. Using OR to Find Different Conditions

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.

Another Example

Find customers who live in Ibadan or Lagos.

SELECT *
FROM Customers
WHERE city = 'Ibadan' OR city = 'Lagos';

6. Using NOT to Exclude Records

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';

7. Using BETWEEN to Search Within a Range

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.

Supermarket Example

Find products with prices between โ‚ฆ1,000 and โ‚ฆ5,000.

SELECT *
FROM Products
WHERE price BETWEEN 1000 AND 5000;

8. Using IN to Search for Multiple Values

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.


9. Using LIKE to Search for Text

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.

Example 1: Names Beginning with S

SELECT *
FROM Students
WHERE student_name LIKE 'S%';

This finds names that begin with the letter S, such as Samuel.

Example 2: Names Ending with Y

SELECT *
FROM Students
WHERE student_name LIKE '%y';

This can find names such as Mary.

Example 3: Names Containing the Letter E

SELECT *
FROM Students
WHERE student_name LIKE '%e%';

This finds names containing the letter E anywhere in the name.

Example 4: Using the Underscore

SELECT *
FROM Students
WHERE student_name LIKE '_ary';

The underscore represents one character before "ary". For example, this pattern can match Mary.


10. Searching for Missing Values Using IS NULL

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.


11. Combining Different Filtering Techniques

You can combine multiple SQL operators to answer more detailed questions.

Example 1

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');

Example 2

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');

12. Common SQL Filtering Mistakes

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:

  • Use single quotation marks around text values.
  • Numbers usually do not need quotation marks.
  • Use AND when all conditions must be true.
  • Use OR when at least one condition can be true.
  • Use IS NULL to find missing values.
  • Always check your conditions before executing a query that changes data.

13. Practical Exercise

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:

  • product_id
  • product_name
  • price
  • category
  • quantity

Write SQL queries to answer the following questions:

  1. Find all products that cost more than โ‚ฆ2,000.
  2. Find products in the Food category.
  3. Find products that cost between โ‚ฆ1,000 and โ‚ฆ5,000.
  4. Find products in the Food or Drinks categories.
  5. Find products with a quantity of less than 10.
  6. Find products whose names begin with the letter R.
  7. Find products that are not in the Electronics category.
  8. Find products whose category information is missing.

Sample Answers

-- 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;

14. Module Summary

In this module, you have learned how to filter and search database records using SQL.

  • WHERE filters records based on conditions.
  • Comparison operators compare values.
  • 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.


15. Knowledge Check

Question 1: Which SQL clause is used to filter records?

  1. ORDER BY
  2. WHERE
  3. SELECT
  4. CREATE

Question 2: Which operator means greater than?

  1. <
  2. =
  3. >
  4. !=

Question 3: Which operator requires all conditions to be true?

  1. OR
  2. AND
  3. NOT
  4. LIKE

Question 4: Which operator searches for values within a range?

  1. BETWEEN
  2. LIKE
  3. IN
  4. NOT

Question 5: Which SQL condition finds missing email addresses?

  1. email = NULL
  2. email == NULL
  3. email IS NULL
  4. email LIKE NULL

Question 6: Which symbol represents zero or more characters in a LIKE pattern?

  1. _
  2. %
  3. #
  4. @

Question 7: What does the IN operator help you do?

  1. Create a database
  2. Search for multiple possible values
  3. Delete a table
  4. Change a column name

Question 8: Which query finds students who are 13 years old?

  1. SELECT * FROM Students WHERE age = 13;
  2. SELECT * FROM Students WHERE age > 13;
  3. SELECT * FROM Students WHERE age < 13;
  4. SELECT age FROM 13;

Question 9: Which operator excludes records that match a condition?

  1. AND
  2. OR
  3. NOT
  4. BETWEEN

Question 10: Which pattern finds names beginning with S?

  1. '%S'
  2. 'S%'
  3. '_S'
  4. '%S%'

Coming Up in Module 5

Sorting and Organising Query Results

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.

๐Ÿ† Get Certified

๐Ÿ”’

Earn this certificate

Every lesson is already free to read. Sign up, finish the lessons, pass the exam (โ‚ฆ2,000 or 20,000 coins), and get a verified certificate with your name on it.

๐ŸŽ“ Sign Up to Get Certified
๐Ÿ› ๏ธ Practice Tools
Hands-on simulators & labs - subscription required.
โ†’
๐ŸŽฏ Internship Tasks
Real-world tasks to build your portfolio - unlock them with a subscription.
โ†’