PostgreSQL – WHERE Clause

A WHERE clause is used in a Select statement to filter the results based on conditions specified in the ‘WHERE’ clause.

Consider an employee table as shown below:

idfirst_namelast_namedepartmentsalary
101bryanwalkersales50,000
102alexcarpenteraccounts55,000
103samwhitesales60,000
104johnbarrysales54,000
105billwoodaccounts65,000
106roberthopkinssupport70,000
107davidleesupport72,000
108jacksearssupport65,000
109tomyoungsales55,000
employee

Below is the SQL query to return all rows from the employee table.

SELECT * FROM employee;

id first_name last_name department salary
--- --------- --------- ---------- -------
101 bryan      walker    sales      50000
102 alex       carpenter accounts   55000
.
.
.

Let’s assume you need to view the details of only the employees who work in the ‘sales’ department. To filter the rows, you would use the ‘WHERE’ clause as shown below:

SELECT * FROM employee WHERE department='sales';

id first_name last_name department salary
--- --------- --------- ---------- -------
101 bryan      walker    sales      50000
103 sam        white     sales      60000
104 john       barry     sales      54000

See also  PostgreSQL - Get table size