PostgreSQL

Conditionals

The WHERE clause is used to specify conditions of the queried result. It can be used with AND, OR and NOT for more complex conditionals.

The AND and OR operators are used for multiple conditions.

The NOT operator is used to specify when a condition is not true.

Syntax

SELECT <columns> FROM <table-name> WHERE <condition>

Example #1

SELECT name, founded, hq FROM Companies WHERE hq='United States'; 
Result from Example #1
Name Founded HQ
Walmart 1962 United States
Disney 1923 United States
PayPal 1998 United States

Example #2

SELECT name, founded, hq FROM Companies 
WHERE founded=1998 OR hq='Canada'; 
Result from Example #2
Name Founded HQ
PayPal 1998 United States
Blackberry 1984 Canada

Example #3

SELECT name, founded, hq FROM Companies 
WHERE hq='United States' AND 
(founded='1923' OR founded='1998'); 
Result from Example #3
Name Founded HQ
Disney 1923 United States
PayPal 1998 United States

Example #4

SELECT name, founded, hq FROM Companies 
WHERE hq='United States'
ORDER BY founded; 
Result from Example #4
Name Founded HQ
Disney 1923 United States
Walmart 1962 United States
PayPal 1998 United States

Example #5

SELECT name, founded, hq FROM Companies 
WHERE NOT hq='United States'; 
Result from Example #5
Name Founded HQ
Blackberry 1984 Canada