SQL Keywords

SQL FILTER

What is the SQL FILTER clause?

Adds a row-level WHERE predicate to an aggregate or window function so only qualifying rows contribute to the calculation.
Sign up to get up to date news on SQL keywords
Welcome to the Galaxy, Guardian!
You'll be receiving a confirmation email

Follow us on twitter :)
Oops! Something went wrong while submitting the form.

Compatible dialects for SQL FILTER: Supported: PostgreSQL 9.4+, SQLite 3.30+, DuckDB, Amazon Redshift, Trino/Presto, Vertica, MariaDB 10.3+ (partial). Not supported: MySQL 8.x, SQL Server, Oracle, older SQLite, older PostgreSQL versions.

SQL FILTER Full Explanation

FILTER is a clause defined in the SQL standard that lets you attach a Boolean predicate directly to an aggregate or window function. Instead of applying the condition in a separate WHERE or CASE expression, you write the predicate once inside FILTER (WHERE …). Only rows that satisfy the predicate are passed to the function; other rows in the result set are ignored for that specific calculation. This improves readability, prevents duplicated CASE logic, and can eliminate extra scans when the database optimizer can evaluate all aggregates in a single pass.FILTER works with any aggregate function (SUM, COUNT, AVG, array_agg, json_agg, percentile_cont, etc.) and with most window functions. It is evaluated after the FROM, WHERE, and GROUP BY clauses but before HAVING, making it logically equivalent to wrapping the input expression in CASE WHEN predicate THEN value END. Because the clause lives inside each function call, you can apply different predicates to multiple aggregates in the same SELECT without writing multiple CASE statements or subqueries.Key caveats:- Not all database engines implement FILTER. Where unsupported, you must fall back to CASE expressions.- The condition must appear inside parentheses immediately after the keyword WHERE.- FILTER cannot reference column aliases created in the same SELECT list.- For GROUP BY queries, FILTER applies within each group.- For window functions, FILTER applies within the window frame after PARTITION BY and ORDER BY have been processed.

SQL FILTER Syntax

<aggregate_function>( [expression] )
  FILTER ( WHERE <condition> )
  [OVER ( window_clause )]

SQL FILTER Parameters

  • aggregate_function (text) - Any built-in or user defined aggregate or window function.
  • expression (any) - Value fed into the aggregate (optional for COUNT(*)).
  • condition (Boolean) - Row-level predicate that determines participation.

Example Queries Using SQL FILTER

-- Compare total, paid, and failed orders in one pass
SELECT
  COUNT(*)                               AS total_orders,
  COUNT(*) FILTER (WHERE status = 'paid')   AS paid_orders,
  COUNT(*) FILTER (WHERE status = 'failed') AS failed_orders,
  SUM(amount) FILTER (WHERE status = 'paid') AS paid_revenue
FROM orders;

-- Windowed clicks per user, counting only click events
SELECT
  user_id,
  event_time,
  COUNT(*) FILTER (WHERE event_type = 'click')
      OVER (PARTITION BY user_id) AS clicks_per_user
FROM events;

Expected Output Using SQL FILTER

  • Each aggregate returns a single scalar per group or per row (for window functions) that includes only rows meeting its FILTER predicate
  • Other rows are disregarded for that function but remain visible to the query overall

Use Cases with SQL FILTER

  • Producing multiple conditional aggregates in a single SELECT without repetitive CASE clauses
  • Building KPI dashboards that compare totals across categories
  • Calculating conditional window metrics such as active_days_per_user
  • Replacing separate subqueries used solely to filter individual aggregates

Common Mistakes with SQL FILTER

  • Using FILTER in a database that does not support it, leading to syntax errors
  • Forgetting the WHERE keyword inside the parentheses
  • Referencing column aliases inside the FILTER predicate
  • Assuming FILTER replaces the outer WHERE clause; it only affects the specific function

Related Topics

CASE WHEN, GROUP BY, HAVING, WINDOW FUNCTIONS, DISTINCT, KEEP (Oracle)

First Introduced In

SQL:2003 standard; first implemented in PostgreSQL 9.4

Frequently Asked Questions

What is the SQL FILTER clause?

FILTER adds an inline WHERE predicate to an aggregate or window function, restricting which rows the function processes.

How is FILTER different from HAVING?

HAVING filters whole groups after aggregation. FILTER filters rows going into a specific function, letting different aggregates use different predicates in the same query.

Can I replace CASE WHEN with FILTER?

Yes. `SUM(CASE WHEN status = 'paid' THEN amount END)` is equivalent to `SUM(amount) FILTER (WHERE status = 'paid')` and is usually easier to read.

What happens if my database lacks FILTER support?

Rewrite the logic with CASE expressions or separate subqueries. Example: `SUM(CASE WHEN condition THEN value END)`.

Sign up to get up to date news on SQL keywords
Welcome to the Galaxy, Guardian!
You'll be receiving a confirmation email

Follow us on twitter :)
Oops! Something went wrong while submitting the form.
Trusted by top engineers on high-velocity teams
Aryeo Logo
Assort Health
Curri
Rubie Logo
Bauhealth Logo
Truvideo Logo

Check out other commonly used SQL Keywords!