SQL FOUNDATIONS:Filtering Data (WHERE, AND, OR)

Use WHERE with AND/OR to filter rows by conditions.

What is Filtering

Filtering allows you to retrieve only the rows that match specific conditions.

Instead of returning all data, you can narrow results using the WHERE clause.

Basic Syntax

sql
SELECT column1, column2
FROM table_name
WHERE condition;
  • WHERE filters rows based on a condition

Example Dataset

Table: employees

idnamedepartmentsalary
1JohnHR40000
2AliceIT60000
3BobIT55000
4EmmaFinance70000
5DavidHR45000

Using WHERE

Example 1: Single Condition

sql
SELECT * FROM employees
WHERE department = 'IT';

Returns only employees in the IT department.

Example 2: Numeric Condition

sql
SELECT name, salary FROM employees
WHERE salary > 50000;

Returns employees with salary greater than 50000.

Using AND

AND is used when multiple conditions must be true.

sql
SELECT * FROM employees
WHERE department = 'IT' AND salary > 55000;

Returns employees who:

  • belong to IT
  • and have salary greater than 55000

Using OR

OR is used when at least one condition must be true.

sql
SELECT * FROM employees
WHERE department = 'HR' OR salary > 65000;

Returns employees who:

  • are in HR
  • or have salary greater than 65000

Combining AND and OR

sql
SELECT * FROM employees
WHERE department = 'IT' AND (salary > 50000 OR salary < 56000);

Use parentheses to control logic and avoid incorrect results.

Key Concepts

  • WHERE filters rows
  • AND requires all conditions to be true
  • OR requires at least one condition to be true
  • Parentheses help control evaluation order

Filtering Data (WHERE, AND, OR) Missions

Solve exercises in sequence to unlock the next mission.

1

IT department only

Retrieve all employees from the IT department.

Solve Mission
2

Salary > 55000

Retrieve employees with salary greater than 55000.

Locked
3

HR with salary > 40000

Retrieve employees from HR with salary greater than 40000.

Locked
4

HR or high salary

Retrieve employees who are in HR or have salary greater than 65000.

Locked
5

IT between 50000-60000

Retrieve employees from IT with salary between 50000 and 60000 (inclusive logic using AND).

Locked
6

Not Finance

Retrieve names of employees who are not in the Finance department.

Locked
7

Low salary or IT

Retrieve employees where salary is less than 45000 or department is IT.

Locked