The WHERE clause in PostgreSQL is used to filter rows from a table based on specified conditions. It follows the SELECT statement and is followed by any other clauses like ORDER BY or GROUP BY clauses.
The basic syntax of the WHERE clause is as follows:
SELECT column1, column2, ...
FROM table_name
WHERE condition;
The condition is the expression that evaluates to true or false for each row in the table. Rows that evaluate to true are included in the query results, while rows that evaluate to false are excluded.
Here’s an example that demonstrates how the WHERE clause works in PostgreSQL:
Suppose we have a table called employees, which contains information about employees in a company. We want to retrieve a list of all employees whose age is greater than or equal to 30. The query would be as follows:
SELECT *
FROM employees
WHERE age >= 30;
In this example, the ’age >= 30’ condition is evaluated for each row in the employees table. Only rows that satisfy the condition are included in the query result.
So, the WHERE clause can be used to filter rows based on various conditions such as greater than or equal to, less than or equal to, not equal to, or any other logical operators. It can also be combined with AND, OR and NOT operators to form complex conditions.