PostgreSQL WHERE Clause: A Beginner's Guide
Learn how the PostgreSQL WHERE clause filters rows using comparisons, logical operators and practical examples.
ADVERTISEMENT
The WHERE clause is one of the first SQL concepts you should learn. It allows you to filter rows and return only the records that satisfy a condition.
Basic WHERE Example
SELECT id, name, city
FROM customers
WHERE city = 'Delhi';
The query returns only customers whose city is Delhi.
Comparison Operators
You can use operators such as =, <>, >, <, >= and <=.
SELECT *
FROM products
WHERE price > 1000;
Using AND and OR
SELECT *
FROM employees
WHERE department = 'IT'
AND salary > 50000;
AND requires both conditions to be true. OR allows either condition to match.
Using IN
SELECT *
FROM customers
WHERE city IN ('Delhi', 'Mumbai', 'Pune');
Using LIKE
SELECT *
FROM customers
WHERE name LIKE 'A%';
This finds names beginning with the letter A.
Handling NULL
NULL should be checked with IS NULL or IS NOT NULL rather than using the normal equality operator.
SELECT *
FROM employees
WHERE manager_id IS NULL;
Practical tip: Start with simple WHERE conditions, then combine operators as your queries become more complex.
Final Thoughts
A strong understanding of WHERE makes filtering, reporting and troubleshooting much easier in PostgreSQL and SQL in general.