How to Filter Data Using PostgreSQL WHERE Clause

In PostgreSQL, the WHERE clause is used to filter the result set retrieved by the UPDATE, SELECT, or DELETE query. Using the WHERE clause, you can specify single or various conditions based on which the data will be filtered. Only those records will be added to the result set that satisfies the given criteria, and the rest of the records will be eliminated.

This post will teach you how to use the WHERE clause to filter the data of a result set retrieved by any query. So, let’s begin!

How to Filter Data Using PostgreSQL WHERE Clause?

The logical and comparison operators can be used within the WHERE clause to specify one or more conditions. The WHERE clause must be specified before the GROUP BY clause, ORDER BY clause, and HAVING clause. As shown in the syntax below, it must be placed right after the FROM clause:

SELECT col_list
FROM tab_name
WHERE condition | conditions
GROUP BY col_list
HAVING condition 
ORDER BY ASC | DESC;

Note: In the above syntax, the GROUP BY clause, HAVING clause, and ORDER BY clause are optional. In the above snippet, we specify all these clauses just to show you the order in which these clauses will be used.

Example 1: Filter Articles Whose id is Less Than 6

We have created a table named article_details, whose details are listed in the below snippet:

SELECT * FROM article_details;
img

Execute the below query to filter the articles with article_id less than 6:

SELECT article_id, article_title, published_date
FROM article_details
WHERE article_id < 6;
img

The output shows that the WHERE clause filters the result set based on the specified condition.

Example 2: Filter Data Based on Two Conditions

In this example, we will show you how to specify two conditions in the WHERE clause using logical operators:

SELECT article_id, article_title, published_date
FROM article_details
WHERE article_id >= 3 AND article_id <= 9;

The above-specified query will filter the articles between the article_id greater than or equal to three but less than or equal to nine:

img

The output shows that the WHERE clause filters the data based on the specified conditions.

Example 3: Filter Data Between Two Values

Let’s use the WHERE clause to filter the articles between article id 4 to 9:

SELECT article_id, article_title, published_date
FROM article_details
WHERE article_id BETWEEN 4 AND 9;
img

The output snippet verifies that article details have been fetched between the specified ranges.

This way, you can use any comparison or logical operator within the WHERE clause to specify a condition.

Conclusion

In PostgreSQL, the WHERE clause allows us to filter the result set retrieved by the UPDATE, SELECT, or DELETE query. The WHERE clause filters the data based on a specific condition or several conditions. Only those records will be added to the result set that satisfies the given criteria, and the rest of the records will be eliminated. This write-up explained how to filter the data in PostgreSQL using the WHERE clause.