COUNT(*) vs COUNT(column): Why Missing Values Change a SQL Result
Two count queries can give different results on the same table without either query being broken. The difference is what you asked…
Sometimes you need individual rows and a total beside each one. A SQL window function can calculate across related rows while keeping the row detail. Here is a small example you can practise before a Malta data interview.
Imagine a work_log table with three rows: Support, Ada, 2 hours; Support, Ben, 3 hours; and Data, Cara, 4 hours. These are invented names and values for learning.
SELECT team, person, hours,
SUM(hours) OVER (PARTITION BY team) AS team_hours
FROM work_log;
Ada’s row shows 2 individual hours and a team total of 5. Ben’s row shows 3 and 5. Cara’s row shows 4 and 4. There are still three result rows. A normal grouped query by team would instead produce one row per team. The PostgreSQL tutorial explains this distinction.
Add a fourth invented row: Support, Dan, 1 hour. Write down the expected team total for each Support row. It should now be 6, while each person keeps their own hours. This exercise checks whether you understand the partition rather than only recognising OVER in a query.
The team total is repeated for the team’s rows. Adding that displayed column across every row would count it repeatedly. Decide what one output row represents before using it in a report.
For an interview, explain why you need both detail and a total. That business reason makes the technical choice easier to follow.
Explore software, cloud and data roles that fit your skills and ambitions.
Explore opportunitiesTwo count queries can give different results on the same table without either query being broken. The difference is what you asked…
Data job titles can overlap. When reading vacancies in Malta, look at the daily responsibilities before deciding which role fits. The same…
“Strong SQL” can mean many things. In a Malta data interview, a clear example helps an employer understand the kind of work…