SUMIF in SQL
SUMIF adds up the values that meet one condition; in SQL you do the same with SUM and a WHERE clause.
The Excel version
=SUMIF(B:B, "West", C:C)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 SUM(amount) AS west_sales
FROM sales
WHERE region = 'West';How it works
- WHERE region = 'West' is the criteria range and criterion.
- SUM(amount) is the sum range, the column whose values are added up.
- To get a total for every region at once, as a table of SUMIFs would, group instead: SELECT region, SUM(amount) FROM sales GROUP BY region.
Where the results differ from Excel
- Excel's SUMIF is case-insensitive and accepts wildcards like West*; in SQL use ILIKE or LIKE 'West%' for the same effect.
- With no matching rows SUMIF returns 0, while SQL's SUM returns null; use COALESCE(SUM(amount), 0).
- Criteria such as ">100" are strings in Excel; in SQL they are ordinary comparisons: WHERE amount > 100.
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.
- What are total sales for the West region?
- Give me total sales for each region.