SQL COUNT Column
Counting a specific column only counts its non-NULL values — this is where it diverges from COUNT(*).
What & Why
COUNT(column_name) counts only the rows where that column has an actual, non-NULL value. Rows where it's NULL are skipped entirely.
See How It Works
BUSINESS QUESTION
Count only product feedback rows that contain an NPS score.
| id | user_id | feature_id | rating | nps_score | comment | created_at |
|---|---|---|---|---|---|---|
| 1 | 101 | 1 | 5 | 9 | Clear and useful | 2024-01-25 12:15:00+00 |
| 2 | 102 | 2 | 4 | NULL | Export worked well | 2024-02-20 15:40:00+00 |
| 3 | 103 | 1 | 4 | 7 | Helpful setup flow | 2024-03-18 09:05:00+00 |
| 4 | 104 | 3 | 3 | NULL | Needs clearer timing | 2024-04-08 17:30:00+00 |
EXAMPLE QUERY
SELECT COUNT(nps_score) AS scored_feedback
FROM product.feedback;RESULT — NULL scores are skipped
| scored_feedback |
|---|
| 2 |
Two of the four product.feedback rows contain an nps_score.
Now You Try
Practice this concept
Marketing wants total leads, qualified leads, and converted leads in one summary row.
Available schema
marketingPrefix tables with marketing.table_name.
total_leadsqualified_leadsconverted_leadsmarketing.leads| Column | Type |
|---|---|
| id | integer |
| campaign_id | integer |
| text | |
| created_at | timestamp with time zone |
| qualified_at | timestamp with time zone |
| converted_at | timestamp with time zone |
| lead_score | integer |
| source | text |
| country | text |
| archive_status | text |
query.sql
Sign up free to try it on a real business scenario