PostgreSQL – IN Operator

An ‘IN’ clause is used to specify 1 or more values in the WHERE clause.

Syntax:

SELECT * FROM table_name WHERE column_name IN ('value1','value2','value3',...);

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

Below is the SQL query to return all rows from the employee table. Returning all rows does not require a WHERE clause.

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
109 tom        young     sales      55000

Now, if you need to specify more than one department in the ‘WHERE’ clause, ‘IN’ clause can be used as shown below:

SELECT * FROM employee WHERE department IN ('sales','accounts');

id first_name last_name department salary
--- --------- --------- ---------- -------
101 bryan      walker    sales      50000
102 alex       carpenter accounts   55000
103 sam        white     sales      60000
104 john       barry     sales      54000
105 bill       wood      accounts   65000
109 tom        young     sales      55000