Last Updated : 12 Jul, 2025
The PostgreSQL WHERE clause is a critical component of SQL queries, allowing users to filter records based on specified conditions. In this tutorial, we'll explore how the WHERE clause works in PostgreSQL, its integration with the SELECT statement, and various examples.
By using the WHERE clause, we can retrieve only the rows that meet our specific criteria, making it an essential tool for data manipulation and querying in PostgreSQL. Whether we are working with simple conditions or complex comparisons by mastering the WHERE clause can significantly improve query efficiency.
PostgreSQL WHERE ClauseWHERE
clause is used to filter rows in a SQL query based on specific conditions. It ensures that only rows that meet the given criteria are included in the query result. WHERE
clause can be used in SELECT
, UPDATE
, DELETE
and other SQL statements to control which records are affected or returned.Syntax:
SELECT column1, column2, ...
FROM table_name
WHERE condition;
Explanation:
SELECT column1, column2, ...
: Specifies the columns that you want to retrieve. You can select one or more columns from a table.FROM table_name
: Indicates the table from which the data will be selected. This is the table that contains the rows to be filtered.WHERE condition
: This is the core of the WHERE
clause, where you define the condition that must be met for rows to be included in the result set. Only rows that satisfy the condition will be returned by the query.The WHERE
clause can also be used with UPDATE
and DELETE
statements to specify rows to be updated or deleted.
Now, let us look into some of the examples of WHERE Clause in PostgreSQL to better understand the concept.
Example 1: Using WHERE Clause with the Equal (=) OperatorUsing WHERE clause with the equal (=) operator. Here we will be using the equal operator in the "customer" table of our sample database.
SELECT
last_name,
first_name
FROM
customer
WHERE
first_name = 'Kelly';
Output:
Explanation: The WHERE
clause filters the results to include only those rows where the first_name
is 'Kelly'.
Using the WHERE clause with the AND operator. Here we will be using the AND operator in the "customer" table of our sample database.
SELECT
last_name,
first_name
FROM
customer
WHERE
first_name = 'Kelly'
AND last_name = 'Knott';
Output:
Explanation: The WHERE
clause filters the results to include only those rows where both conditions (first_name = 'Kelly'
AND last_name = 'Knott'
) are met.
Using the WHERE clause with the OR operator. Here we will be using the OR operator in the "customer" table of our sample database.
SELECT
first_name,
last_name
FROM
customer
WHERE
last_name = 'Cooper' OR
first_name = 'Jo';
Output:
Explanation: The WHERE
clause filters the results to include rows where either condition (last_name = 'Cooper'
OR first_name = 'Jo'
) is met.
Using the WHERE clause with the IN operator. The IN operator is used for string matching. Here we will be using the IN operator in the "customer" table of our sample database.
SELECT
first_name,
last_name
FROM
customer
WHERE
first_name IN ('Kelly', 'Jo', ' Alexander');
Output:
Explanation: The WHERE
clause filters the results to include rows where the first_name
is either 'Kelly', 'Jo', or 'Alexander'.
Using the WHERE clause with the LIKE operator. The LIKE operator is used to find string matching a particular pattern. Here we will be using the LIKE operator in the "customer" table of our sample database.
SELECT
first_name,
last_name
FROM
customer
WHERE
first_name LIKE 'Kath%';
Output:
Explanation: The LIKE
operator with the pattern 'Kath%' filters the results to include rows where the first_name
starts with 'Kath'.
Using the WHERE clause with the BETWEEN operator. The BETWEEN operator return if a value is in the mentioned range. Here we will be using the BETWEEN operator in the "customer" table of our sample database.
SELECT
first_name,
LENGTH(first_name) name_length
FROM
customer
WHERE
first_name LIKE 'K%' AND
LENGTH(first_name) BETWEEN 3 AND 7
ORDER BY
name_length;
Output:
Explanation: The WHERE
clause filters the results to include rows where the first_name
starts with 'K' and the length of first_name
is between 3 and 7 characters.
Using the WHERE clause with the not equal operator (<>). Here we will be using the <> operator in the "customer" table of our sample database.
SELECT
first_name,
last_name
FROM
customer
WHERE
first_name LIKE 'Bra%'
AND
last_name <> 'Motley';
Output:
Explanation: The WHERE
clause filters the results to include rows where the first_name
starts with 'Bra' and the last_name
is not 'Motley'.
WHERE
clause is used to filter records returned by the SELECT
, UPDATE
, and DELETE
statements.WHERE
clause can evaluate to true, false, or unknown. It can be a single Boolean expression or a combination of expressions using logical operators.WHERE
clause is executed after the FROM
clause and before the SELECT
clause in a query.WHERE
clause if possible, as this can prevent the use of indexes.Understanding and effectively using the WHERE clause in PostgreSQL is fundamental to writing optimized SQL queries. This tutorial has covered how to apply the WHERE clause within the SELECT statement and provided practical examples of filtering data using various operators and conditions.
RetroSearch is an open source project built by @garambo | Open a GitHub Issue
Search and Browse the WWW like it's 1997 | Search results from DuckDuckGo
HTML:
3.2
| Encoding:
UTF-8
| Version:
0.7.4