Skip to main content
PostgreSQL & Redis Engineering Predict Output P0 Easy

COUNT(*) vs COUNT(col) and WHERE vs HAVING

Given rows (dept='eng', bonus=100), (dept='eng', bonus=NULL), (dept='ops', bonus=NULL), predict the result.

Official Reference

Code Snippet

SELECT dept, COUNT(*) AS total, COUNT(bonus) AS paid
FROM staff
GROUP BY dept
HAVING COUNT(*) >= 1;

Expected Output & Explanation

eng | 2 | 1
ops | 1 | 0

COUNT(*) counts every row in the group including NULLs; COUNT(col) ignores NULL values in col. WHERE filters individual rows before grouping, while HAVING filters grouped aggregates after GROUP BY.

Follow-up Interview Question · Can you filter a single aggregate cleanly with COUNT(*) FILTER (WHERE bonus > 0) in Postgres?

Module Context & References

SQL & PostgreSQL · Query behavior & trap drills Predict query results for NULL logic, JOIN filters, aggregates and window functions
Personal Notes · Markdown

Technologies & Interview Topics Covered

Switch between short-term interview tracks and long-term 80/20 engineering depth to practice JavaScript internals, React rendering and hooks, timed frontend machine coding UI challenges, MeetaPro FG300, Blind 75 & NeetCode 150 DSA patterns, Python & FastAPI APIs, PostgreSQL query optimization & MVCC, Redis caching, Applied AI RAG pipelines & evaluations, and distributed system design.