{"id":5013,"date":"2024-08-19T13:05:27","date_gmt":"2024-08-19T13:05:27","guid":{"rendered":"https:\/\/www.aegissofttech.com\/insights\/?p=5013"},"modified":"2025-10-02T06:55:37","modified_gmt":"2025-10-02T06:55:37","slug":"postgresql-filter-and-where-clause","status":"publish","type":"post","link":"https:\/\/www.aegissofttech.com\/insights\/postgresql-filter-and-where-clause\/","title":{"rendered":"PostgreSQL Filter and WHERE Clause"},"content":{"rendered":"\n<p>The terms FILTER and WHERE are used in PostgreSQL to further refine data selection, but they have different purposes and can be used in different contexts. Let\u2019s explore both keywords in detail, understanding their unique use cases with code examples.<\/p>\n<h2><a name=\"setting-up-a-database-on-Docker\"><\/a>1. Setting up a database on Docker<\/h2>\n<p>Creating a database is time-consuming but with <a href=\"https:\/\/www.docker.com\/\" target=\"_blank\" rel=\"noopener\">Docker<\/a>, it becomes easy. To understand how to install Docker on Windows OS, you can view the video provided at this <a href=\"https:\/\/www.youtube.com\/watch?v=S7NVloq0EBc\" target=\"_blank\" rel=\"noopener\">link<\/a>. After completing the task, launch the terminal and execute the command below to configure and start postgresql.<\/p>\n<pre>-- Remember to change the password \u2013\ndocker run -d -p 5432:5432 -e POSTGRES_PASSWORD=your_password --name postgres postgres\n-- command to stop the Postgres docker container --\ndocker stop postgres\n-- command to remove the Postgres docker container --\ndocker rm postgres\n<\/pre>\n<p>Don&#8217;t forget to input your preferred password. Assuming everything goes smoothly, the postgresql database server will be operational on port number 5432, allowing you to connect using the <a href=\"https:\/\/dbeaver.io\/\" target=\"_blank\" rel=\"noopener\">Dbeaver<\/a> GUI tool.<\/p>\n<h2><a name=\"postgresql-where-keyword\"><\/a>2. The WHERE Keyword<\/h2>\n<p>The <code>WHERE<\/code> keyword filters table rows based on specified conditions. It is a crucial clause in SQL that is used in <code>SELECT<\/code>, <code>UPDATE<\/code>, <code>DELETE<\/code>, and other SQL statements to specify the rows affected by the query. The format for this clause is the following:<\/p>\n<pre>SELECT column1, column2, ... FROM table_name WHERE condition;\n<\/pre>\n<h3>2.1 Example<\/h3>\n<p>Consider a table named <code>employees<\/code> with the following structure:<\/p>\n<pre>CREATE TABLE employees (\n  id SERIAL PRIMARY KEY, \n  name VARCHAR(100), \n  age INT, \n  department VARCHAR(50)\n);\n<\/pre>\n<p>To find out the employees whose age is greater than 30, we can use the following <code>WHERE<\/code> clause:<\/p>\n<pre>SELECT \n  * \nFROM \n  employees \nWHERE \n  age &gt; 30;\n<\/pre>\n<h3>2.2 Code Breakdown<\/h3>\n<ul>\n<li><code>SELECT * FROM employees<\/code>: This query selects all columns from the <code>employees<\/code> table.<\/li>\n<li><code>WHERE age &gt; 30<\/code>: This filter in the select query fetches only those rows where the <code>age<\/code> column is greater than 30.<\/li>\n<\/ul>\n<h2><a name=\"postgresql-filter-keyword\"><\/a>3. The FILTER Keyword<\/h2>\n<p>The <code>FILTER<\/code> keyword has a role in aggregate functions as it filters rows before the function is executed. It is commonly used in combination with <code>GROUP BY<\/code> clause to compute aggregates that meet specific criteria. The syntax is shown in the following manner:<\/p>\n<pre>SELECT aggregate_function(column) FROM table_name WHERE condition FILTER (WHERE filter_condition);\n<\/pre>\n<h3>3.1 Example<\/h3>\n<p>Using the <code>employees<\/code> table created above, we will find the average age of employees who work in the &#8216;IT&#8217; department:<\/p>\n<pre>SELECT \n  AVG(age) FILTER (\n    WHERE \n      department = 'IT'\n  ) AS avg_age_it \nFROM \n  employees;\n<\/pre>\n<h3>3.2 Code Breakdown<\/h3>\n<ul>\n<li><code>SELECT AVG(age)<\/code>: This keyword calculates the average of the <code>age<\/code> column.<\/li>\n<li><code>FILTER (WHERE department = 'IT')<\/code>: This filters in the query fetches those rows where the <code>department<\/code> is &#8216;IT&#8217; before calculating the average.<\/li>\n<li><code>AS avg_age_it<\/code>: This keyword gives an alias name to the resulting column, <code>avg_age_it<\/code>.<\/li>\n<\/ul>\n<h2><a name=\"using-in-application-setup\"><\/a>4. Using in Application Development<\/h2>\n<p>These days most people focus on using such queries in application development. Here is a snippet of how the above SQL queries can be used in Spring <a href=\"https:\/\/www.aegissofttech.com\/java-application-development-services.html\">Java application development services<\/a>.<\/p>\n<pre>import org.springframework.data.jpa.repository.JpaRepository;\nimport org.springframework.data.jpa.repository.Query;\nimport org.springframework.stereotype.Repository;\nimport java.util.List;\n@Repository\npublic interface EmployeesDao extends JpaRepository&lt;Employee, Integer&gt; {\n    @Query(nativeQuery = true, value = \"SELECT * FROM employees WHERE age &gt; ?1\")\n    List&lt;Employee&gt; getEmployeesByAge(int age);\n    @Query(nativeQuery = true, value = \"SELECT AVG(age) FILTER (WHERE department = 'IT') AS avg_age_it FROM employees;\")\n    int getAverageAgeIT();\n}\n<\/pre>\n<p>In this example, we have created a Spring dao interface and used the native SQL approach to do the query operations.<\/p>\n<h2><a name=\"postgresql-filter-keyword\"><\/a>5. Comparison and Conclusion<\/h2>\n<p>The PostgreSQL keywords <code>WHERE<\/code> and <code>FILTER<\/code> play important roles in data querying, yet they fulfill different functions:<\/p>\n<ul>\n<li><code>WHERE<\/code> clause is used to narrow down rows in standard queries, affecting which rows are selected or manipulated<\/li>\n<li><code>FILTER<\/code> clause is used with aggregate functions to define conditions that determine which rows are considered in the aggregate calculation.<\/li>\n<\/ul>\n<p>Understanding the differences and applications of <code>WHERE<\/code> and <code>FILTER<\/code> keywords enhances the accuracy and efficiency of queries in PostgreSQL.<\/p>\n<blockquote>\n<p>Want to learn more about Spring? Don\u2019t miss our related blog: <a href=\"https:\/\/www.aegissofttech.com\/insights\/kafka-baggage-amp-webflux-migration-to-spring-boot\/\"><b>Kafka migration to Spring Boot<\/b><\/a><\/p>\n<\/blockquote>\n\n<p>\u00a0<\/p>\n","protected":false},"excerpt":{"rendered":" ","protected":false},"author":12,"featured_media":5018,"comment_status":"open","ping_status":"closed","sticky":false,"template":"","format":"standard","meta":{"_acf_changed":false,"site-sidebar-layout":"default","site-content-layout":"","ast-site-content-layout":"default","site-content-style":"default","site-sidebar-style":"default","ast-global-header-display":"","ast-banner-title-visibility":"","ast-main-header-display":"","ast-hfb-above-header-display":"","ast-hfb-below-header-display":"","ast-hfb-mobile-header-display":"","site-post-title":"","ast-breadcrumbs-content":"","ast-featured-img":"","footer-sml-layout":"","ast-disable-related-posts":"","theme-transparent-header-meta":"","adv-header-id-meta":"","stick-header-meta":"","header-above-stick-meta":"","header-main-stick-meta":"","header-below-stick-meta":"","astra-migrate-meta-layouts":"default","ast-page-background-enabled":"default","ast-page-background-meta":{"desktop":{"background-color":"var(--ast-global-color-4)","background-image":"","background-repeat":"repeat","background-position":"center center","background-size":"auto","background-attachment":"scroll","background-type":"","background-media":"","overlay-type":"","overlay-color":"","overlay-opacity":"","overlay-gradient":""},"tablet":{"background-color":"","background-image":"","background-repeat":"repeat","background-position":"center center","background-size":"auto","background-attachment":"scroll","background-type":"","background-media":"","overlay-type":"","overlay-color":"","overlay-opacity":"","overlay-gradient":""},"mobile":{"background-color":"","background-image":"","background-repeat":"repeat","background-position":"center center","background-size":"auto","background-attachment":"scroll","background-type":"","background-media":"","overlay-type":"","overlay-color":"","overlay-opacity":"","overlay-gradient":""}},"ast-content-background-meta":{"desktop":{"background-color":"var(--ast-global-color-5)","background-image":"","background-repeat":"repeat","background-position":"center center","background-size":"auto","background-attachment":"scroll","background-type":"","background-media":"","overlay-type":"","overlay-color":"","overlay-opacity":"","overlay-gradient":""},"tablet":{"background-color":"var(--ast-global-color-5)","background-image":"","background-repeat":"repeat","background-position":"center center","background-size":"auto","background-attachment":"scroll","background-type":"","background-media":"","overlay-type":"","overlay-color":"","overlay-opacity":"","overlay-gradient":""},"mobile":{"background-color":"var(--ast-global-color-5)","background-image":"","background-repeat":"repeat","background-position":"center center","background-size":"auto","background-attachment":"scroll","background-type":"","background-media":"","overlay-type":"","overlay-color":"","overlay-opacity":"","overlay-gradient":""}},"footnotes":""},"categories":[12],"tags":[908,909],"class_list":["post-5013","post","type-post","status-publish","format-standard","has-post-thumbnail","hentry","category-java","tag-postgresql","tag-where-clause"],"acf":[],"_links":{"self":[{"href":"https:\/\/www.aegissofttech.com\/insights\/wp-json\/wp\/v2\/posts\/5013","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/www.aegissofttech.com\/insights\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/www.aegissofttech.com\/insights\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/www.aegissofttech.com\/insights\/wp-json\/wp\/v2\/users\/12"}],"replies":[{"embeddable":true,"href":"https:\/\/www.aegissofttech.com\/insights\/wp-json\/wp\/v2\/comments?post=5013"}],"version-history":[{"count":11,"href":"https:\/\/www.aegissofttech.com\/insights\/wp-json\/wp\/v2\/posts\/5013\/revisions"}],"predecessor-version":[{"id":14806,"href":"https:\/\/www.aegissofttech.com\/insights\/wp-json\/wp\/v2\/posts\/5013\/revisions\/14806"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/www.aegissofttech.com\/insights\/wp-json\/wp\/v2\/media\/5018"}],"wp:attachment":[{"href":"https:\/\/www.aegissofttech.com\/insights\/wp-json\/wp\/v2\/media?parent=5013"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/www.aegissofttech.com\/insights\/wp-json\/wp\/v2\/categories?post=5013"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/www.aegissofttech.com\/insights\/wp-json\/wp\/v2\/tags?post=5013"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}