FILTER in SQL

The FILTER function (or AutoFilter) shows only the rows that meet a condition; in SQL the condition goes in a WHERE clause.

The Excel version

=FILTER(A2:C100, C2:C100>1000)

The SQL equivalent

This example assumes: sales(id bigint, region text, amount numeric, sale_date date). It is written in PostgreSQL syntax, and your table and column names will differ from these.

SELECT id, region, amount, sale_date
FROM sales
WHERE amount > 1000
  AND region IN ('West', 'East')
ORDER BY sale_date;

How it works

  • WHERE keeps only rows where the condition is true, like the include argument of FILTER.
  • IN (...) matches any of several values, which is what ticking several items in an AutoFilter list does.
  • Combine conditions with AND and OR, using parentheses when you mix them.

Where the results differ from Excel

  • FILTER returns an array that updates in the sheet; a SQL result is a fresh snapshot each time you run it.
  • Rows where the condition is null (a blank amount) are excluded, not shown, which can hide data you expected to see.
  • FILTER's if-empty argument has no SQL equivalent; an empty result is simply zero rows.

Skip the query: ask in plain English

If you would rather not write SQL, type the question. SchemaChat turns it into SQL against your connected database or uploaded file, runs it, and shows the SQL and the figures it used so you can check the answer before you share it. Check that the result matches how your spreadsheet defines the number.

  • Show West and East sales over 1,000.
  • Which orders in the last 7 days were over 500?

Frequently asked questions

What is the SQL equivalent of FILTER?
The FILTER function (or AutoFilter) shows only the rows that meet a condition; in SQL the condition goes in a WHERE clause.
How does the SQL version of FILTER work?
WHERE keeps only rows where the condition is true, like the include argument of FILTER. IN (...) matches any of several values, which is what ticking several items in an AutoFilter list does. Combine conditions with AND and OR, using parentheses when you mix them.
Why can SQL give a different result from FILTER?
FILTER returns an array that updates in the sheet; a SQL result is a fresh snapshot each time you run it.