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
sqlSELECT column1, column2FROM table_nameWHERE condition;
WHEREfilters rows based on a condition
Example Dataset
Table: employees
Using WHERE
Example 1: Single Condition
sqlSELECT * FROM employeesWHERE department = 'IT';
Returns only employees in the IT department.
Example 2: Numeric Condition
sqlSELECT name, salary FROM employeesWHERE salary > 50000;
Returns employees with salary greater than 50000.
Using AND
AND is used when multiple conditions must be true.
sqlSELECT * FROM employeesWHERE 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.
sqlSELECT * FROM employeesWHERE department = 'HR' OR salary > 65000;
Returns employees who:
- are in HR
- or have salary greater than 65000
Combining AND and OR
sqlSELECT * FROM employeesWHERE department = 'IT' AND (salary > 50000 OR salary < 56000);
Use parentheses to control logic and avoid incorrect results.
Key Concepts
WHEREfilters rowsANDrequires all conditions to be trueORrequires 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.
Salary > 55000
Retrieve employees with salary greater than 55000.
HR with salary > 40000
Retrieve employees from HR with salary greater than 40000.
HR or high salary
Retrieve employees who are in HR or have salary greater than 65000.
IT between 50000-60000
Retrieve employees from IT with salary between 50000 and 60000 (inclusive logic using AND).
Not Finance
Retrieve names of employees who are not in the Finance department.
Low salary or IT
Retrieve employees where salary is less than 45000 or department is IT.