Module 1: SQL Foundations45 min

Logical Operators: AND, OR, NOT

Progress Tracking

Log in to save this lesson and continue from where you left off.

Log in

Combining Conditions

A single WHERE condition gets you pretty far, but real questions usually need more:

  • Find employees in HR who earn over 80,000
  • Show me orders from March or April
  • Get everyone except the Admin department

That's where AND, OR, and NOT come in. They let you combine multiple conditions into one filter.

AND: Both Conditions Must Be True

SQL
SELECT *
FROM orders
WHERE order_details = 'Coat'
  AND total_order_cost > 100;
Table: techcorp_workforce
idfirst_namelast_namedepartmentsalaryphone_numberjoining_date
1SarahMitchellHR95000555-01012021-03-15
2MichaelChenHR88000555-01022022-06-01
3EmilyRodriguezHR825002021-09-20
4DavidParkHR80000555-01042023-01-10
5LisaThompsonHR650002021-04-05
1
Filter by Department and Salary

Find HR employees earning over 80000 from `techcorp_workforce`.

Tables: techcorp_workforce

Both conditions must be true for a row to be included.

Chaining Multiple ANDs

You can chain as many ANDs as you need. Try adding a third condition — what happens if you also filter for employees who joined after January 1, 2022?

2
Chain Three AND Conditions

Find Admin employees earning at least 80,000 who joined after January 1, 2022.

Tables: techcorp_workforce

Each AND adds another filter, narrowing your results.

Churro Activity Date

Table: los_angeles_restaurant_health_inspections
serial_numberactivity_datefacility_namescoregradeservice_codeservice_descriptionemployee_idfacility_addressfacility_cityfacility_idfacility_statefacility_zipowner_idowner_namepe_descriptionprogram_element_peprogram_nameprogram_statusrecord_id
DAQHRSETQ2017-06-08MARGARITAS CAFE93A1ROUTINE INSPECTIONEE00000065026 S CRENSHAW BLVDLOS ANGELESFA0023656CA90043OW0004133BAZAN, ASCENCIONRESTAURANT (61-150) SEATS HIGH RISK1638MARGARITAS CAFEACTIVEPR0011718
DA2GQRJOS2017-03-07LAS MOLENDERAS97A1ROUTINE INSPECTIONEE00009972635 WHITTIER BLVDLOS ANGELESFA0160416CA90023OW0125379MARISOL FEREGRINORESTAURANT (0-30) SEATS HIGH RISK1632LAS MOLENDERASINACTIVEPR0148504
DAMQTA46T2016-03-22SANDRA'S TAMALES93A1ROUTINE INSPECTIONEE00010495390 WHITTIER BLVDLOS ANGELESFA0171769CA90022-4032OW0178828SANDRA'S TAMALES INC.RESTAURANT (0-30) SEATS MODERATE RISK1631SANDRA'S TAMALESACTIVEPR0164225
DAXMBTIRZ2018-02-12CAFE GRATITUDE97A1ROUTINE INSPECTIONEE0000828639 N LARCHMONT BLVD STE #102LOS ANGELESFA0058921CA90004OW0005704CAFE GRATITUDE LARCHMONT LLCRESTAURANT (61-150) SEATS HIGH RISK1638CAFE GRATITUDEACTIVEPR0019854
DAK8TBMS02015-09-10THE WAFFLE90A1ROUTINE INSPECTIONEE00007096255 W SUNSET BLVD STE #105LOS ANGELESFA0051830CA90028OW0035796THE WAFFLE, LLCRESTAURANT (61-150) SEATS HIGH RISK1638THE WAFFLEACTIVEPR0010922
3
Churro Activity Date
View solution

Find the inspection date and risk category (`pe_description`) of facilities named 'STREET CHURROS' that received a score below 95.

Tables: los_angeles_restaurant_health_inspections
SQL
SELECT
  first_name,
  last_name,
  department
FROM techcorp_workforce
WHERE department = 'HR'
  OR department = 'Admin';

A row only needs to match one condition to be included. HR employees are in, Admin employees are in, Engineering employees are out.

Use IN for Multiple Values on the Same Column

When checking the same column for multiple values, IN (covered next lesson) is often cleaner than chaining ORs.

Lyft Driver Wages

Table: lyft_drivers
indexstart_dateend_dateyearly_salary
02018-04-0248303
12018-05-3067973
22015-04-0556685
32015-01-0851320
42017-03-0967507
4
Lyft Driver Wages
View solution

Find all Lyft drivers who earn either equal to or less than 30k USD or equal to or more than 70k USD. Output all details related to retrieved records.

Tables: lyft_drivers

NOT: Exclude Matches

NOT reverses a condition. It gives you everything that doesn't match.

SQL
SELECT
  first_name,
  last_name,
  department
FROM techcorp_workforce
WHERE NOT department = 'Admin';

This is equivalent to:

SQL
WHERE department <> 'Admin'

Both work. Use whichever reads more naturally for your situation.

NOT Is Most Useful with IN, BETWEEN, and LIKE

NOT is more powerful when combined with other operators. For example: WHERE NOT department IN ('Admin', 'HR')

Try It

5
Exclude a Department

Find all employees who are not in the HR department. Try it using NOT first, then rewrite using <>.

Tables: techcorp_workforce

Combining AND and OR

This is where things get tricky. When you mix AND and OR, SQL evaluates AND first. This can lead to unexpected results.

Let's say you want employees in HR or Engineering, but only those earning over 80,000. You might write:

SQL
-- This doesn't do what you think!
SELECT
  first_name,
  department,
  salary
FROM techcorp_workforce
WHERE department = 'HR'
  OR department = 'Engineering'
  AND salary > 80000;

Because AND is evaluated first, SQL reads this as:

SQL
WHERE department = 'HR'
  OR (department = 'Engineering' AND salary > 80000)

So you get ALL HR employees (regardless of salary) plus Engineering employees earning over 80,000. Not what you wanted.

Use Parentheses to Control Order

6
Fix Operator Precedence with Parentheses

Find employees in HR or Engineering who earn more than 80,000. Use parentheses to make sure the salary filter applies to both departments.

Tables: techcorp_workforce

The corrected version groups the OR conditions first, then applies the salary filter. 

When in Doubt, Use Parentheses

Parentheses make your intent clear and prevent bugs. Even if they're not strictly necessary, they help whoever reads your code next — including future you.

High Earners in Support Departments

Table: techcorp_workforce
idfirst_namelast_namedepartmentsalaryphone_numberjoining_date
1SarahMitchellHR95000555-01012021-03-15
2MichaelChenHR88000555-01022022-06-01
3EmilyRodriguezHR825002021-09-20
4DavidParkHR80000555-01042023-01-10
5LisaThompsonHR650002021-04-05
7
High Earners in Support Departments
View solution

The HR team is reviewing compensation packages for employees in support functions. They want to identify high earners in the HR and Admin departments for a salary benchmarking study. Find all employees who earn more than $80,000 and work in either the HR or Admin department. Return `first name`, `last name`, `department`, and `salary`.

Tables: techcorp_workforce

Key Takeaways

  • AND requires all conditions to be true.
  • OR requires at least one condition to be true.
  • NOT excludes rows that match the condition.
  • AND is evaluated before OR — use parentheses to override.
  • When mixing AND and OR, always use parentheses to be safe.

What's Next

Chaining ORs for the same column gets tedious. Writing department = 'HR' OR department = 'Account' OR department = 'Admin' is painful. In the next lesson, you'll learn cleaner ways to filter: LIKE for pattern matching, IN for multiple values, and BETWEEN for ranges.