Загрузка...

Same Table, Same Filter, DIFFERENT COUNT?! (COUNT(*) vs COUNT(column))

Same table, same filter, but different counts?! In this SQL tutorial, we see why COUNT(*), COUNT(bonus) and COUNT(1) can give different results, and how NULL values change what the COUNT function really does.​​

Your manager asks: “How many customers got a bonus?”

You run one SQL query and get 100.
You tweak just one part of the COUNT function and now you get 60.
Same table. Same filter. Different count.

In this video, using a simple Employees table, we break down the difference between:

COUNT(*)
COUNT(column) (like COUNT(bonus))
COUNT(1)

…and understand why they don’t always behave the way we think.​​

What you’ll learn in this video:

How COUNT(*) answers: “How many rows do I have?” (counts all rows in the result)

How COUNT(column) counts only rows where that column is NOT NULL

Why COUNT(bonus) can be smaller than COUNT(*) on the same table

How COUNT(1) usually behaves like COUNT(*) in modern databases

A simple mental model to never get confused by COUNT again in reports or interviews​

We’ll walk through:

Building a small Employees table with some NULL bonus values

Running COUNT(*), COUNT(bonus), and COUNT(1) on that data

A grouped example by city to show totals vs “has bonus” counts

An interview-style question: “COUNT(*) = 100 but COUNT(col) = 60 – why?”​​

This video is for:
SQL beginners who keep getting unexpected counts
Data analysts and data engineers preparing for interviews
Anyone confused about COUNT, NULL, and what’s actually being counted in their queries​

If this helped you, subscribe to SQLWithAsif for more SQL traps, interview questions, and practical examples that make you job-ready.

Also check out my previous video on NOT IN vs NOT EXISTS – another classic SQL trap where a query returns 0 rows because of NULL.

#sql #sqltutorial #sqlforbeginners #sqlcount #countstar #count1 #sqlnull #dataanalytics

Видео Same Table, Same Filter, DIFFERENT COUNT?! (COUNT(*) vs COUNT(column)) канала SQLWithAsif
Яндекс.Метрика
Все заметки Новая заметка Страницу в заметки
Страницу в закладки Мои закладки
На информационно-развлекательном портале SALDA.WS применяются cookie-файлы. Нажимая кнопку Принять, вы подтверждаете свое согласие на их использование.
О CookiesНапомнить позжеПринять