SELECT *
FROM customers
WHERE city = 'Pune'
OR city = 'Mumbai';
This returns customers from either Pune or Mumbai.
🔹 14. AND vs OR
Consider:
WHERE age > 30
AND city = 'Pune'
A customer must satisfy both conditions.
But:
WHERE age > 30
OR city = 'Pune'
A customer only needs to satisfy one or both conditions.
This difference is extremely important.
🔹 15. NOT
NOT reverses a condition.
Example:
SELECT *
FROM customers
WHERE NOT city = 'Pune';
This returns customers who aren't from Pune.
You can also commonly write:
SELECT *
FROM customers
WHERE city <> 'Pune';
🔹 16. Combining AND and OR
You can combine multiple logical operators.
Example:
SELECT *
FROM employees
WHERE department = 'Finance'
AND salary > 60000;
Another example:
SELECT *
FROM employees
WHERE department = 'Finance'
OR department = 'Analytics'
AND salary > 60000;
When conditions become complex, use parentheses to make your intended logic explicit.
For example:
SELECT *
FROM employees
WHERE
(department = 'Finance' OR department = 'Analytics')
AND salary > 60000;
This means:
Employees from Finance or Analytics who earn more than 60,000.
🔹 17. Why Parentheses Matter
Consider:
WHERE city = 'Pune'
OR city = 'Mumbai'
AND age > 30
SQL's logical evaluation rules can make this behave differently from what a beginner might expect.
A safer and clearer version is:
WHERE
(city = 'Pune' OR city = 'Mumbai')
AND age > 30;
This clearly communicates the intended logic.
Best practice:
Use parentheses whenever combining AND and OR in a complex condition.
🔹 18. WHERE with Dates
You can also filter dates.
Example:
SELECT *
FROM orders
WHERE order_date >= '2026-01-01';
This retrieves orders on or after January 1, 2026.
Another example:
SELECT *
FROM orders
WHERE order_date < '2026-07-01';
This retrieves orders before July 1, 2026.
Date syntax can vary slightly across database systems, so always consider the SQL dialect you're using.
🔹 19. Filtering a Date Range
Suppose you want orders during a particular period.
You can use:
SELECT *
FROM orders
WHERE order_date >= '2026-01-01'
AND order_date < '2026-04-01';
This retrieves orders from January through March.
Using a half-open range like this is particularly useful when working with timestamps because it avoids accidentally excluding records with time components.
🔹 20. BETWEEN
SQL provides BETWEEN for range filtering.
Example:
SELECT *
FROM products
WHERE price BETWEEN 1000 AND 5000;
BETWEEN is inclusive of both boundaries in standard SQL.
So this includes:
1000
and:
5000
as well as values between them.
🔹 21. BETWEEN with Dates
Example:
SELECT *
FROM orders
WHERE order_date BETWEEN '2026-01-01' AND '2026-01-31';
For a date-only column, this can be useful.
However, if order_date contains timestamps, using:
order_date >= '2026-01-01'
AND order_date < '2026-02-01'
is often safer because it includes the entire final day regardless of the timestamp.
🔹 22. IN Operator
Suppose you want customers from:
Pune
Mumbai
Delhi
You could write:
SELECT *
FROM customers
WHERE city = 'Pune'
OR city = 'Mumbai'
OR city = 'Delhi';