DataBolt

All Lessons

Introduction to SQL
SQL Lesson 1: Your Sample Database
SQL Lesson 2: SELECT — Reading data
SQL Lesson 3: WHERE — Filtering rows
SQL Lesson 4: AND, OR, NOT
SQL Lesson 5: BETWEEN, IN, LIKE
SQL Lesson 6: NULL — The Mystery Value
SQL Lesson 7: ORDER BY
SQL Lesson 8: LIMIT & OFFSET
SQL Lesson 9: Aggregate Functions
SQL Lesson 10: GROUP BY
SQL Lesson 11: HAVING
SQL Lesson 12: INNER JOINs
SQL Lesson 13: LEFT JOINs
SQL Lesson 14: RIGHT JOINs
SQL Lesson 15: SELF JOINs
SQL Lesson 16: UNION JOINs
SQL Lesson 17: Joining Multiple Tables
SQL Lesson 18: Subqueries
SQL Lesson 19: CTEs (WITH)
SQL Lesson 20: CASE Statements
SQL Lesson 21: Window Functions
SQL Lesson 22: String Functions
SQL Lesson 23: Date & Time Functions
SQL Lesson 24: INSERT, UPDATE, DELETE
SQL Lesson 25: CREATE TABLE & DDL
SQL Lesson 26: Indexes & Performance
SQL Lesson 27: Transactions & ACID
SQL Lesson 28: SQL Execution Order
SQL Lesson 29: 50 Practice Problems

CHAPTER 5

AND, OR, NOT

Combining multiple conditions into one filter.

IN THIS CHAPTER

  • AND: both conditions must be true
  • OR: at least one condition must be true
  • NOT: invert a condition
  • Parentheses to control evaluation order

IT employees earning > 70,000

SELECT name, department, salary 
FROM employees
WHERE department = 'IT' AND salary > 70000;

IT or HR employees

SELECT name, department 
FROM employees
WHERE department = 'IT' OR department = 'HR';

5.3 - NOT

Everyone except Salses

SELECT name, department 
FROM employees
WHERE NOT department = 'Sales';

5.4 - Parentheses - Always Be Explicit

❌ Common Mistake: AND has higher precedence than OR, just like multiplication vs addition in maths. WHERE a OR b AND c is evaluated as WHERE a OR (b AND c), which is almost never what you intended. Always use parentheses

Sales employees, OR IT employees earning > 80,000

SELECT name, department, salary 
FROM employees
WHERE department = 'Sales'
      OR (department = 'IT' AND salary > 80000);

Exercise 👇

Exercise:

Tasks

1.👉Find employees who are in IT and have salary greater than 70000.
2.Find employees who are in HR or Sales.
3.Find employees who have salary less than 60000 or work in IT.
4.Find employees who are not in Sales department.
5.Find employees who do have a manager (i.e., manager_id is NOT NULL)
6.Find employees who: are in IT or HR, and salary greater than 60000, and not in Sales
Stuck? Read this task's