PostgreSQL Tutorial PostgreSQL Advanced PostgreSQL Database Account Management PostgreSQL References
PostgreSQL Tutorial PostgreSQL Advanced PostgreSQL Database Account Management PostgreSQL References

PostgreSQL - WHERE Clause



The PostgreSQL WHERE Clause is used to specify condition(s) in a query. It can be used to specify condition(s) while fetching data from a table, joining two tables, updating records in a table, inserting records in a table or deleting records from a table.

Syntax

The syntax for using WHERE Clause in PostgreSQL is given below:

SELECT column1, column2, ...
FROM table_name
WHERE condition(s);

To specify condition in a query, comparison or logical operators like <, >, =, LIKE, IN, NOT, NULL etc. are used.

Example:

Consider a database containing a table called Employee with the following records:

EmpIDNameCityAgeSalary
1JohnLondon253000
2MarryNew York242750
3JoParis272800
4KimAmsterdam303100
5RameshNew Delhi283000
6HuangBeijing282800

  • To fetch Name, Age and Salary data from Employee table where Salary is greater than 2800, the query is:

    SELECT Name, Age, Salary 
    FROM Employee
    WHERE Salary > 2800;
    

    This will produce the result as shown below:

    NameAgeSalary
    John253000
    Kim303100
    Ramesh283000
  • To specify multiple conditions logical operators are used, for example - To fetch data from the Employee table where Salary is greater than 2800 and Age is less than 30, the PostgreSQL AND operator is used and the query will be:

    SELECT * FROM Employee
    WHERE Salary > 2800 AND Age < 30;
    

    This result of the following code will be:

    EmpIDNameCityAgeSalary
    1JohnLondon253000
    5RameshNew Delhi283000