Excel COUNTIF for SQL people: COUNT with WHERE, explained with a worked example

If you already think in SQL, Excel's COUNTIF family is the same idea with different syntax. Here is the mapping, one worked example, and the mistakes that cost me the most time.
The data
Six orders in A1:C7, columns Order, City, Status. The criteria live in E2:E4 so the formulas stay short and you can change a filter without editing a formula.
SQL to Excel
| What you want | SQL | Excel |
|---|---|---|
| Rows where city is Cairo | SELECT COUNT(*) FROM orders WHERE city = 'Cairo' | =COUNTIF(B2:B7,E2) |
| Cairo and status Done | ... WHERE city = 'Cairo' AND status = 'Done' | =COUNTIFS(B2:B7,E2,C2:C7,E3) |
| Status not Cancelled | ... WHERE status <> 'Cancelled' | =COUNTIF(C2:C7,"<>"&E4) |
With the sample data the results are 3, 2 and 4. Cairo has orders 1001, 1003 and 1005. Cairo and Done leaves 1001 and 1003. Everything except Cancelled leaves 1001, 1003, 1004 and 1006.
Rules that behave like WHERE
-
COUNTIFtakes one range and one condition. ForANDacross columns useCOUNTIFS, which takes range and condition pairs. - There is no
ORinside oneCOUNTIF. Add two calls:=COUNTIF(B2:B7,"Cairo")+COUNTIF(B2:B7,"Aswan"). - Comparison operators go inside quotes and join the cell with
&:">"&E2,"<>"&E4. - Wildcards work on text:
"Al*"matches everything starting with Al,"?????"matches exactly five characters. Escape a real*with~. - Matching is case-insensitive, like many default SQL collations.
Mistakes and fixes
- Count is 0 but the value is there. There is a trailing space in the cell, like
"Cairo ". Fix:=TRIM(B2)in a helper column, paste as values. - Numbers stored as text.
COUNTIF(A:A,5)matches both5and"5", but a comparison like">3"skips text. Fix: convert with Text to Columns orVALUE(). - Ranges of different size in
COUNTIFS. All ranges must have the same shape or you get#VALUE!. Fix: make every range cover the same rows. - Semicolon locale. In some regional settings the argument separator is
;, not,. If a pasted formula errors, swap them. - Counting blanks.
COUNTIF(A2:A100,"")counts empty cells and cells with an empty string,"<>"counts non-empty ones. Fix: pick the one that matches your intent and test with a known blank.
Why bother
A single COUNTIFS on a few thousand rows is faster to write and easier to audit than a pivot table when you only need one number. When you need many numbers, a pivot table or a query is the better tool.
Data is made up. Screenshot is a real LibreOffice render of the sample sheet.
If you want a guided path after this, the Microsoft Excel course from beginner to advanced on Udemy covers lookups, pivot tables and charts with practice files. This is an affiliate link: I earn a commission if you buy through it, at no extra cost to you. Free routes work too.