PostgreSQL - WHERE Clause

PostgreSQL Tutorial

The PostgreSQL WHERE clause is used to specify a condition while fetching the data from single table or joining with multiple tables.

If the given condition is satisfied, only then it returns specific value from the table. You can filter out rows that you don't want included in the result-set by using the WHERE clause.

The WHERE clause not only is used in SELECT statement, but it is also used in UPDATE, DELETE statement, etc. which we would examine in subsequent chapters.

Syntax

The basic syntax of SELECT statement with WHERE clause is as follows:

You can specify a search_condition using comparison or logical operators. like >, <, =, LIKE, NOT, etc. Below examples would make this concept clear.

Example:

Consider the table COMPANY having records as follows:

Here are simple examples showing usage of PostgreSQL Logical Operators. Following SELECT statement will list down all the records where AGE is greater than or equal to 25 AND salary is greater than or equal to 65000.00:

Above PostgreSQL statement will produce the following result:

Following SELECT statement lists down all the records where AGE is greater than or equal to 25 OR salary is greater than or equal to 65000.00:

Above PostgreSQL statement will produce the following result:

Following SELECT statement lists down all the records where AGE is not NULL which means all the records because none of the record is having AGE equal

to NULL:

Above PostgreSQL statement will produce the following result:

Following SELECT statement lists down all the records where NAME starts with 'Pa', does not matter what comes after 'Pa'.

Above PostgreSQL statement will produce the following result:

Following SELECT statement lists down all the records where AGE value is either 25 or 27:

Above PostgreSQL statement will produce the following result:

Following SELECT statement lists down all the records where AGE value is neither 25 nor 27:

Above PostgreSQL statement will produce the following result:

Following SELECT statement lists down all the records where AGE value is in BETWEEN 25 AND 27:

Above PostgreSQL statement will produce the following result:

Following SELECT statement makes use of SQL subquery where subquery finds all the records with AGE field having SALARY > 65000 and later WHERE clause is being used along with EXISTS operator to list down all the records where AGE from the outside query exists in the result returned by sub-query:

Above PostgreSQL statement will produce the following result:

Following SELECT statement makes use of SQL subquery where subquery finds all the records with AGE field having SALARY > 65000 and later WHERE clause is being used along with > operator to list down all the records where AGE from outside query is greater than the age in the result returned by sub-query:

Above PostgreSQL statement will produce the following result:

Source: www.tutorialspoint.com

Category: Forex

Similar articles: