# Select and Filter Data - WHERE Clause and Comparison Operators
The WHERE clause in SQL is used to filter records from a table, returning only those that meet a specific condition.
# Basics of the WHERE Clause
The WHERE clause follows the syntax:
|
|
# Comparison Operators
There are many different types of operators that can be used in our condition to filter what rows we get back based on the data they contain.
The first type are the comparison operators. These are used to compare values in a column with other values or expressions. These include:
=
: Equal to<
>
or!
=
: Not equal to>
: Greater than<
: Less than>
=
: Greater than or equal to<
=
: Less than or equal to
For example, assume we have a table Employees
with the following data:
EmployeeID | Name | Age | Department |
---|---|---|---|
1 | John Doe | 28 | Marketing |
2 | Jane Doe | 32 | HR |
3 | Alex Ray | 45 | IT |
4 | Sara Ali | 30 | Finance |
To get details of employees from the ‘Marketing’ department we could write:
|
|
# Expected Output:
EmployeeID | Name | Age | Department |
---|---|---|---|
1 | John Doe | 28 | Marketing |
If we wanted to find all employees who are not in the ‘HR’ department we could write:
|
|
# Expected Output:
EmployeeID | Name | Age | Department |
---|---|---|---|
1 | John Doe | 28 | Marketing |
3 | Alex Ray | 45 | IT |
4 | Sara Ali | 30 | Finance |
If we wanted to see information for employees over the age of 30 we could write:
|
|
# Expected Output:
EmployeeID | Name | Age | Department |
---|---|---|---|
2 | Jane Doe | 32 | HR |
3 | Alex Ray | 45 | IT |
# Practice Questions
Write a query to select all customers who are from ‘Germany’.
Write a query to select all customers whose contact age is less than or equal to 20.
# Lessons
- SQL - W2 Select and Filter Data - Introduction
- SQL - W2 Select and Filter Data - Creating a Database
- SQL - W2 Select and Filter Data - Running Scripts
- SQL - W2 Select and Filter Data - SELECT Statement
- SQL - W2 Select and Filter Data - WHERE Clause and Comparison Operators
- Next: SQL - W2 Select and Filter Data - Logical Operators
- SQL - W2 Select and Filter Data - ORDER BY
- SQL - W2 Select and Filter Data - SQL Comments
- SQL - W2 Select and Filter Data - Note on Semicolons
- SQL - W2 Select and Filter Data - Practice Assignment