Filtering Data
The heart of SQL — WHERE, comparison and logical operators, operator precedence, BETWEEN, IN, LIKE, REGEXP, NULL handling, LIMIT/OFFSET and logical execution order.
#Introduction
Spring Boot APIs, JPA queries, Hibernate, reports, dashboards, admin portals — everything revolves around filtering data.
#Sample Table
We'll use this employees table throughout the chapter:
| id | name | department | salary | city | manager_id |
|---|---|---|---|---|---|
| 1 | Rahul | IT | 70000 | Bangalore | 10 |
| 2 | Amit | HR | 50000 | Delhi | NULL |
| 3 | Neha | IT | 90000 | Mumbai | 10 |
| 4 | Raj | Finance | 60000 | Bangalore | 20 |
| 5 | Ankit | IT | 75000 | Chennai | 10 |
#WHERE Clause
Without WHERE, SELECT * FROM employees; returns all employees. With it:
SELECT *
FROM employees
WHERE department = 'IT';Rahul
Neha
AnkitThink Like Java
WHERE salary > 50000if (employee.getSalary() > 50000) {
// include employee
}A WHERE clause is essentially a filter condition.
#Comparison Operators
| Operator | Meaning |
|---|---|
= | Equal |
!= or <> | Not equal |
> | Greater than |
>= | Greater than or equal |
< | Less than |
<= | Less than or equal |
SELECT *
FROM employees
WHERE salary > 70000;Neha
Ankit#Logical Operators
AND — All Conditions Must Be True
SELECT *
FROM employees
WHERE department = 'IT'
AND salary > 70000;Neha
AnkitJava equivalent: if (department.equals("IT") && salary > 70000).
OR — Either Condition Can Be True
SELECT *
FROM employees
WHERE city = 'Delhi'
OR city = 'Mumbai';Amit
NehaJava equivalent: if (city.equals("Delhi") || city.equals("Mumbai")).
NOT — Reverse the Condition
SELECT *
FROM employees
WHERE NOT department = 'HR';Everyone except HR employees.
Combining AND and OR
SELECT *
FROM employees
WHERE department = 'IT'
AND city = 'Bangalore'; -- Rahul
SELECT *
FROM employees
WHERE department = 'IT'
OR salary > 80000; -- Rahul, Neha, AnkitParentheses Matter
-- ❌ Ambiguous
SELECT *
FROM employees
WHERE department = 'IT'
OR city = 'Delhi'
AND salary > 70000;SQL evaluates AND before OR, so this means IT OR (Delhi AND salary > 70000).
-- ✅ Correct
SELECT *
FROM employees
WHERE (department = 'IT' OR city = 'Delhi')
AND salary > 70000;#BETWEEN
Instead of salary >= 50000 AND salary <= 70000:
SELECT *
FROM employees
WHERE salary BETWEEN 50000 AND 70000;Much cleaner — and inclusive on both ends.
Dates
SELECT *
FROM orders
WHERE order_date BETWEEN '2026-01-01' AND '2026-01-31';Very common in reporting.
#IN and NOT IN
Instead of city = 'Delhi' OR city = 'Mumbai' OR city = 'Bangalore':
SELECT *
FROM employees
WHERE city IN ('Delhi', 'Mumbai', 'Bangalore');
SELECT *
FROM employees
WHERE city NOT IN ('Delhi', 'Mumbai');Cleaner and easier to maintain.
#LIKE
Pattern matching with wildcards:
| Symbol | Meaning |
|---|---|
% | Zero or more characters |
_ | Exactly one character |
SELECT *
FROM employees
WHERE name LIKE 'R%'; -- starts with R: Rahul, Raj| Pattern | Meaning |
|---|---|
LIKE 'R%' | Starts with R |
LIKE '%a' | Ends with a |
LIKE '%ah%' | Contains "ah" |
LIKE '_a%' | Second character is "a" (Rahul, Raj, Ram) |
LIKE '_a_' | Exactly three characters, "a" in the middle (Raj, Ram — not Rahul) |
#REGEXP
Much more powerful than LIKE:
| Pattern | Meaning |
|---|---|
WHERE name REGEXP '^R' | Starts with R |
WHERE name REGEXP 'a
|