PostgreSQL WHERE
The PostgreSQL WHERE clause is used with SELECT, INSERT, UPDATE and DELETE statements to return the result only when the condition is satisfied.
Syntax:
WHERE conditions;
Example 1: Selecting specific fields from a table.
Employment table:
ID | STATE | RATE |
1 | A | 60 |
2 | B | 70 |
3 | C | 65 |
4 | D | 80 |
5 | E | 78 |
Query:
SELECT * FROM “EMPLOYMENT” WHERE “RATE” > 60; |
Output:
ID | STATE | RATE |
2 | B | 70 |
3 | C | 65 |
4 | D | 80 |
5 | E | 78 |
Example 2: Using WHERE clause with AND condition.
Employment table:
ID | STATE | RATE |
1 | A | 60 |
2 | B | 70 |
3 | C | 65 |
4 | D | 80 |
5 | E | 78 |
Query:
SELECT * FROM “EMPLOYMENT” WHERE “RATE” > 60 AND “ID” < 4; |
Output:
ID | STATE | RATE |
2 | B | 70 |
3 | C | 65 |
Example 3: Using WHERE clause with OR condition.
Employment table:
ID | STATE | RATE |
1 | A | 60 |
2 | B | 70 |
3 | C | 65 |
4 | D | 80 |
5 | E | 78 |
Query:
SELECT * FROM “EMPLOYMENT” WHERE “RATE” > 60 OR “ID” < 4; |
Output:
ID | STATE | RATE |
1 | A | 60 |
2 | B | 70 |
3 | C | 65 |
4 | D | 80 |
5 | E | 78 |
Example 4: Using WHERE clause with both AND and OR condition.
Employment table:
ID | STATE | RATE |
1 | A | 60 |
2 | B | 70 |
3 | C | 65 |
4 | D | 80 |
5 | E | 78 |
Query:
SELECT * FROM “EMPLOYMENT” WHERE (“RATE” > 60 AND “STATE” = ‘C’) OR “ID” > 4; |
Output:
ID | STATE | RATE |
3 | C | 65 |
5 | E | 78 |