Lesson 1 of 4

Progress: 0/3 exercises solved (0%)

Solve all exercises below to unlock the next lesson.

AND, OR, NOT

Lesson Description

Combine multiple conditions in a WHERE clause.

Easy Project

Mini project: Turn this lesson into a real-world query artifact by writing one clean business report query and validating output quality.

AND, OR, NOT — combining conditions

You can combine multiple conditions in WHERE using AND, OR, and NOT.

AND — both conditions must be true

SELECT first_name, salary
FROM employees
WHERE department_id = 60 AND salary > 5000;

OR — at least one condition must be true

SELECT first_name, department_id
FROM employees
WHERE department_id = 60 OR department_id = 80;

NOT — invert a condition

SELECT first_name, department_id
FROM employees
WHERE NOT department_id = 90;

Operator precedence

NOT binds tightest, then AND, then OR. This can cause surprises:

-- WARNING: this means department = 60 AND (salary > 5000 OR department = 80)
WHERE department_id = 60 AND salary > 5000 OR department_id = 80

Use parentheses to make intent explicit:

WHERE (department_id = 60 OR department_id = 80)
  AND salary > 5000

Tips

  • Always use parentheses when mixing AND and OR — it prevents logic bugs and aids readability.
  • NOT can be used with IN, BETWEEN, LIKE, and EXISTS (covered in later lessons).

Learning context

This lesson is part of Filtering & Logic. Focus on the core idea in AND, OR, NOT, then validate with deliberate practice.

What to master

  • Understand clause order and intent
  • Write deterministic result queries
  • Use clear aliases and readable structure

Common mistakes

  • Skipping ORDER BY in final outputs
  • Using joins before validating base rows
  • Not checking row counts after changes

High-level strategy

  • Start simple, then layer complexity
  • Run often and compare expected shape
  • Refactor with CTEs when logic grows

Task ladder

  1. Baseline query: get a correct first output.
  2. Add one complication: edge case, join, filter, or ranking rule.
  3. Re-run and refine until output is stable and explainable.

Transparent data checks

  • Check row count before and after each major clause.
  • Validate one manual sample row for correctness.
  • Confirm final ordering and column naming for trust.

Retention loop

  • Run the solution once.
  • Rewrite from memory with one variation.
  • Explain the logic in plain English in one sentence.

Logical reasoning for commands

WHERE

Why: Limits rows to only the business-relevant subset.

Memory cue: Filter early to reduce noise.

ORDER BY

Why: Makes output deterministic and reviewable.

Memory cue: No ORDER BY means no guaranteed row order.

AS alias

Why: Makes output columns readable for teams and reports.

Memory cue: If the name is clear, the query is easier to trust.

Concept check

Quick check: AND, OR, NOT

3 quick questions. One at a time. Instant score at the end.

Exercise 1

IT or Sales employees earning > $6000

Enterprise Workforce Analyticseasy

Return first_name, last_name, and salary for employees in IT (department 60) or Sales (department 80) earning more than 6000.

Run a query to see results here.
Exercise 2

Exec department or very high earners

Enterprise Workforce Analyticseasy

Return first_name, last_name, department_id, and salary for employees who are either in department 90 or earn more than 12000.

Run a query to see results here.
Exercise 3

Customers NOT from the USA

Commerce Operations Intelligenceeasy

From the store sandbox, return first_name, last_name, and country for customers whose country is not 'USA', ordered by country, then last_name.

Run a query to see results here.

Lesson 1 of 4

0/3 solved (0%)

Finish this lesson to unlock next.