Private archive copy · Unofficial site · Content copyright belongs to Runoob Tutorial and the original authors · View Original

PostgreSQL WHERE Clause

PostgreSQL WHERE Clause

In PostgreSQL, when we need to query data from a single table or multiple tables based on specified conditions, we can add a WHERE clause to the SELECT statement to filter out the data we do not need.

The WHERE clause can be used not only in SELECT statements, but also in UPDATE, DELETE, and other statements.

Syntax

The following is the general syntax for using a WHERE clause in a SELECT statement to read data from a database:

SELECT column1, column2, columnN

FROM table_name

WHERE [condition1]

We can use comparison operators or logical operators in the WHERE clause, such as >, <, =, LIKE, NOT, etc.

Create the COMPANY table (Download the COMPANY SQL file ), with the following data:

runoobdb# select * from COMPANY;

 id | name  | age | address   | salary

----+-------+-----+-----------+--------

  1 | Paul  |  32 | California|  20000

  2 | Allen |  25 | Texas     |  15000

  3 | Teddy |  23 | Norway    |  20000

  4 | Mark  |  25 | Rich-Mond |  65000

  5 | David |  27 | Texas     |  85000

  6 | Kim   |  22 | South-Hall|  45000

  7 | James |  24 | Houston   |  10000

(7 rows)

In the following examples we use logical operators to read data from the table.

AND

Find the AGE (age) field greater than or equal to 25, and SALARY (salary) field greater than or equal to 65000:

runoobdb=# SELECT * FROM COMPANY WHERE AGE >= 25 AND SALARY >= 65000;

 id | name  | age |  address   | salary

----+-------+-----+------------+--------

  4 | Mark  |  25 | Rich-Mond  |  65000

  5 | David |  27 | Texas      |  85000

(2 rows)

OR

Find the AGE (age) field greater than or equal to 25, or SALARY (salary) field greater than or equal to 65000:

runoobdb=# SELECT * FROM COMPANY WHERE AGE >= 25 OR SALARY >= 65000;

id | name  | age | address     | salary

----+-------+-----+-------------+--------

  1 | Paul  |  32 | California  |  20000

  2 | Allen |  25 | Texas       |  15000

  4 | Mark  |  25 | Rich-Mond   |  65000

  5 | David |  27 | Texas       |  85000

(4 rows)

NOT NULL

find records in the company table where AGE (age) field is not empty:

runoobdb=#  SELECT * FROM COMPANY WHERE AGE IS NOT NULL;

  id | name  | age | address    | salary

 ----+-------+-----+------------+--------

   1 | Paul  |  32 | California |  20000

   2 | Allen |  25 | Texas      |  15000

   3 | Teddy |  23 | Norway     |  20000

   4 | Mark  |  25 | Rich-Mond  |  65000

   5 | David |  27 | Texas      |  85000

   6 | Kim   |  22 | South-Hall |  45000

   7 | James |  24 | Houston    |  10000

(7 rows)

LIKE

find in the COMPANY table NAME (name) field starting with Pa:

runoobdb=# SELECT * FROM COMPANY WHERE NAME LIKE 'Pa%';

id | name | age |address    | salary

----+------+-----+-----------+--------

  1 | Paul |  32 | California|  20000

IN

The following SELECT statement lists data where the AGE (age) field is 25 or 27:

runoobdb=# SELECT * FROM COMPANY WHERE AGE IN ( 25, 27 );

 id | name  | age | address    | salary

----+-------+-----+------------+--------

  2 | Allen |  25 | Texas      |  15000

  4 | Mark  |  25 | Rich-Mond  |  65000

  5 | David |  27 | Texas      |  85000

(3 rows)

NOT IN

The following SELECT statement lists data where the AGE (age) field is not 25 or 27:

runoobdb=# SELECT * FROM COMPANY WHERE AGE NOT IN ( 25, 27 );

 id | name  | age | address    | salary

----+-------+-----+------------+--------

  1 | Paul  |  32 | California |  20000

  3 | Teddy |  23 | Norway     |  20000

  6 | Kim   |  22 | South-Hall |  45000

  7 | James |  24 | Houston    |  10000

(4 rows)

BETWEEN

The following SELECT statement lists data where the AGE (age) field is between 25 and 27:

runoobdb=# SELECT * FROM COMPANY WHERE AGE BETWEEN 25 AND 27;

 id | name  | age | address    | salary

----+-------+-----+------------+--------

  2 | Allen |  25 | Texas      |  15000

  4 | Mark  |  25 | Rich-Mond  |  65000

  5 | David |  27 | Texas      |  85000

(3 rows)

Subquery

The following SELECT statement uses a SQL subquery; the subquery reads the SALARY (salary) field data greater than 65000, and then uses the EXISTS operator to determine whether it returns any rows; if rows are returned, all AGE (age) fields are read.

runoobdb=# SELECT AGE FROM COMPANY

        WHERE EXISTS (SELECT AGE FROM COMPANY WHERE SALARY > 65000);

 age

-----

  32

  25

  23

  25

  27

  22

  24

(7 rows)

The following SELECT statement also uses a SQL subquery; the subquery reads the SALARY (salary) field greater than 65000 AGE (age) field data, and then uses the > operator to query data greater than that AGE (age) field data:

runoobdb=# SELECT * FROM COMPANY

        WHERE AGE > (SELECT AGE FROM COMPANY WHERE SALARY > 65000);

 id | name | age | address    | salary

----+------+-----+------------+--------

  1 | Paul |  32 | California |  20000