Counting according to query
ML System Design practice on Codemia
Design recommenders, ranking systems and training pipelines the way ML interviews actually ask for them, with worked solutions.
Introduction
Counting according to a query usually means returning how many records match a condition rather than returning the records themselves. In SQL, that is usually a COUNT(...) query with WHERE, and in application code the main concerns are correctness, parameterization, and performance on large tables.
Basic Counting with COUNT(*)
The simplest form counts all rows that satisfy a condition:
This returns one number, not the matching rows. That makes it useful for dashboards, pagination, monitoring, and summaries.
If you need the count for a user-specific filter:
That is the standard pattern for "count according to query."
Count by Group, Not Just Globally
Often the question is not "how many rows total?" but "how many rows per category?" Use GROUP BY:
This returns one row per distinct status, which is useful for analytics and reporting.
You can add filters first:
That is still counting according to a query, just at a grouped level instead of a single number. If you need unique values only, COUNT(DISTINCT customer_id) is another common variation.
Use Parameters in Application Code
If the count condition depends on user input, parameterize it instead of concatenating SQL strings.
Example in Python with SQLite:
This is safer and clearer than building the SQL by hand.
Think About Performance on Large Tables
Counting sounds cheap, but on large datasets it can still be expensive. Performance depends on:
- indexes on filtered columns
- whether the query can avoid scanning unnecessary rows
- grouping complexity
- concurrent load on the database
If counting by status or customer_id is common, indexes on those columns often help. On the other hand, counting with a broad filter on a very large table may still require substantial work.
In reporting systems, repeated counts are sometimes pre-aggregated into summary tables rather than recalculated live on every request.
Common Pitfalls
The biggest mistake is using SELECT * and counting rows in application code when the database could return the count directly. That wastes bandwidth and CPU.
Another issue is forgetting that COUNT(column_name) ignores NULL values, while COUNT(*) counts rows regardless of nullability. Choose deliberately based on what you want counted.
Developers also often build count queries by string concatenation from user input. That creates unnecessary SQL injection risk when parameterized queries are easy to use.
Finally, if a count query is slow, check indexes and execution plans before assuming the database is at fault. The query shape often explains the delay.
Summary
- Use
COUNT(*)withWHEREto count rows that match a query. - Use
GROUP BYwhen you need counts per category. - Parameterize count queries in application code.
- Be aware that
COUNT(column)andCOUNT(*)are not the same. - Indexing and query design matter when counts run on large tables.
Related reading
- Counting all entries with KSQL
- Counting Number of messages stored in a kafka topic
- Counting unique values in a column in pandas dataframe like in Qlik?
- Create a custom Tensorflow histogram summary
- CQL3 How to retrieve the TTL when there is only a primary key?
- Create a Cumulative Sum Column in MySQL
- Counting alternating numbers in an array
- counting boolean parenthesizations implementation

System Design Fundamentals
Build a strong foundation in designing scalable, reliable distributed systems.
View the courseTrack what you have practised
A free account saves your progress, solutions and study plan across every problem on Codemia.
ML System Design practice on Codemia
Design recommenders, ranking systems and training pipelines the way ML interviews actually ask for them, with worked solutions.