SQL Interview Question - Difference between Count(*), Count(1), Count(colname) | Which is fastest

แชร์
ฝัง
  • เผยแพร่เมื่อ 19 ม.ค. 2025

ความคิดเห็น • 97

  • @LearnatKnowstar
    @LearnatKnowstar  2 ปีที่แล้ว +2

    Register at the below link to get US $100 off for Coursera Plus membership between Sep 8 - Sep 29, 2022.
    The Coursera Plus membership gets you access to unlimited courses and unlimited certifications!
    imp.i384100.net/Ke51on

  • @jfletcher12
    @jfletcher12 2 ปีที่แล้ว +45

    "There is no difference" - effectively demonstrates live that count(1) took 40x longer to execute than count(*)

    • @TimGautier
      @TimGautier 2 ปีที่แล้ว +7

      Doing a count on a table with only 10 rows is so trivial that you'll see wild variations in speed just due to other processes running on the system.

    • @VictorSzk
      @VictorSzk 2 ปีที่แล้ว

      The tables I work isually have millions of rows, its good to know these things

  • @marcelositonio2495
    @marcelositonio2495 7 หลายเดือนก่อน

    Great and clear explanation on Count(1), * plus ColumnName. Thanks!

  • @karunakaranr2473
    @karunakaranr2473 ปีที่แล้ว +1

    Nice explanation... Thank you for your time and effort.

  • @tlozwarlock
    @tlozwarlock 2 ปีที่แล้ว +50

    As a 10 year+ SQL DBA and Dev... a difference of a quarter second (0.250) versus a 7 thousandth of a second (0.007) process time in an execution plan means that count(*) for raw row count is indeed faster when you start scaling. Even running against a clustered index, count(1) is going to be marginally slower. count(*) is your winner. Imagine multiple millions of rows with a count(1) with just a PK index. I shudder at the thought.

    • @马化腾-p1e
      @马化腾-p1e 2 ปีที่แล้ว

      I have learnt count(1) is quicker than count(*), you mean that is wrong?

    • @betamvmt
      @betamvmt 2 ปีที่แล้ว

      make your own test, you'll be surprised.

    • @garylee867
      @garylee867 2 ปีที่แล้ว +4

      The time difference is just due to some fluctuation.
      The query optimizer is smart enough to figure out COUNT(*) and COUNT(1) are equivalent and execute them in exactly the same way. Besides, Taking 0.25s to scan a 60000 rows table is ridiculously slow. (I tried it on my machine as well)

    • @cyfrowymuza
      @cyfrowymuza 2 ปีที่แล้ว +6

      that's ridiculous, the plans are the same, meaning the same computations are done, meaning the time difference is because of you running it on your laptop where you run multiple different programs e.g. you are recording this video, so in essence - count(1) or count(*) perform exactly the same !

    • @СергейКиян-ш6у
      @СергейКиян-ш6у 2 ปีที่แล้ว +4

      Seems like due to cache was cold for count(1)

  • @marcosbarcellos1601
    @marcosbarcellos1601 2 ปีที่แล้ว +2

    Thank you for the explanation. I'm sharing the video with a friend. He wanted to know the syntax difference of COUNT. 😁☺️

  • @vivektarab1959
    @vivektarab1959 2 หลายเดือนก่อน

    0:47 - Every Body knows that it gets same result
    2:01 - On bigger Tables
    4:20 - * Took less time

  • @danieljust295
    @danieljust295 2 ปีที่แล้ว +3

    It all depends on the table size and it’s structure and number of CPUs.
    On my PC , table with one column and 500k rows count(*) and count(1) take approximately the same time, count(column) takes longer.
    Table with 3 columns and 500k rows - count(*) is slightly faster than count(1) , count(column) takes double time.
    The timing may be different for different version of database. I am sure we would see significant difference in the old versions such as SQL 2008. Count(1), count(*) and count(column) fetched rows differently and this caused the execution variation.

  • @RajatKumar-rf4xi
    @RajatKumar-rf4xi 3 ปีที่แล้ว +5

    Same count questions has been asked in 24th April 2021 in tiger analytics

  • @gpcgpc810
    @gpcgpc810 2 ปีที่แล้ว

    Thanks for sharing, but I don't see you show Count(colname) cost how many sec.
    Did I miss something?

  • @praveenshetty7688
    @praveenshetty7688 2 ปีที่แล้ว

    Hi, Can you make video on long running stored procedures. Daily its running fyn but one day its taking long time so how we can debug it

  • @saleembasha2918
    @saleembasha2918 2 ปีที่แล้ว +6

    It's like soo coool explanations, you are making everything so easy by perfect usage of all clauses, functions etc., of SQ language

  • @rprasad8271
    @rprasad8271 3 ปีที่แล้ว +3

    Good Stuff, All are very useful. Plz provide more videos...

  • @miguelpetrarca5540
    @miguelpetrarca5540 3 ปีที่แล้ว +6

    I have seen/heard select count(*) from tableA returns count of all rows in table regardless of whether a row has all null or some null values, on the other hard select count(columnA) from tableA returns the number of records that do not have null in columnA. But what if we use a group by? I have seen no info on this. are they equivalent?

    • @RakeshKumar-dq3db
      @RakeshKumar-dq3db 2 ปีที่แล้ว +1

      I have the same doubt

    • @borus_editz1259
      @borus_editz1259 2 ปีที่แล้ว +1

      @@RakeshKumar-dq3db yes me too

    • @nareshsmily226
      @nareshsmily226 2 ปีที่แล้ว

      Group by will elimates the duplicates it will fetch only unique records from coloumns

    • @JoaoPinela
      @JoaoPinela 2 ปีที่แล้ว +2

      @miguel Petrarca , if you use a group by with a count (columnname) , the resulting aggregation Will only account for non-null records

  • @tugrularslan542
    @tugrularslan542 ปีที่แล้ว

    So great explaination. Thank a lot

  • @ashutoshpandey1452
    @ashutoshpandey1452 2 ปีที่แล้ว

    You're an amazing teacher.

  • @designanddecor8964
    @designanddecor8964 2 ปีที่แล้ว

    Thank you very much for the wonderful explanation.

  • @Foodie-Techie
    @Foodie-Techie 2 ปีที่แล้ว

    Well explained.Appreciate

  • @jacksparrowcapitan3232
    @jacksparrowcapitan3232 3 ปีที่แล้ว +5

    Great content !!!!

  • @Shinnra
    @Shinnra 2 ปีที่แล้ว

    Yestarday I had some clases and a question similar to this one was asked to the professor, I understood your answer way better than his, thank you!

  • @arpangupta2162
    @arpangupta2162 ปีที่แล้ว

    count(*) was 40 times faster than count(1) in the example.
    this is a big difference when we work in production.
    just like hbase and hive

  • @AsifHussain-ul4ss
    @AsifHussain-ul4ss 2 ปีที่แล้ว

    Thank you for the class ❤️

  • @aritrabanerjee645
    @aritrabanerjee645 2 ปีที่แล้ว

    if we have NULL in all columns and do COUNT(*), will that row be counted?

    • @LearnatKnowstar
      @LearnatKnowstar  2 ปีที่แล้ว

      Yes, Count(*) will count null values.

  • @avishekdas9557
    @avishekdas9557 2 ปีที่แล้ว

    Very good explanation, thank you!!

  • @kunalkawale4829
    @kunalkawale4829 2 ปีที่แล้ว

    If we say select distinct(column )will the null value row will also be shown? Since it is distinct.

    • @MrGaurav331
      @MrGaurav331 ปีที่แล้ว

      yes it would show you NULL also

  • @kierasher1
    @kierasher1 2 ปีที่แล้ว +2

    I've been writing SQL for a long time, and I like to think I'm very good at it. Never in my life have I even considered typing "count(1)", and I clicked on this in a big WTF moment. Not gonna lie. My time was wasted.

  • @tableaututorialstips71
    @tableaututorialstips71 2 ปีที่แล้ว

    Thanks for sharing valueble videos mam :),Liked,shared,subscribed :)

  • @sbharathiraja8671
    @sbharathiraja8671 ปีที่แล้ว

    Its helpful for me

  • @kovalensue
    @kovalensue 2 ปีที่แล้ว +6

    It's funny that in the Oracle database is count(1) and count(*) exactly the same... They have the same execution plan. 🙂

  • @USA_to_Asia_in_2024
    @USA_to_Asia_in_2024 6 หลายเดือนก่อน

    Thank you.

  • @mohitgupta7145
    @mohitgupta7145 2 ปีที่แล้ว

    When do we use count(1) in our queries and what's it's purpose

  • @sanc7753
    @sanc7753 3 ปีที่แล้ว +2

    Waiting for another interesting video

    • @LearnatKnowstar
      @LearnatKnowstar  3 ปีที่แล้ว +1

      Thank you. More videos coming soon!

  • @mohammadyounos2011
    @mohammadyounos2011 2 ปีที่แล้ว

    The way ur explanation is good. I need a query which explain sales aging by 6 months qty totals from table. Could u please help.

    • @LearnatKnowstar
      @LearnatKnowstar  2 ปีที่แล้ว +1

      Thank you

    • @LearnatKnowstar
      @LearnatKnowstar  2 ปีที่แล้ว +1

      These are a few videos on sales. Please see if these help.
      th-cam.com/video/rGapnGwEWIU/w-d-xo.html

    • @LearnatKnowstar
      @LearnatKnowstar  2 ปีที่แล้ว +1

      th-cam.com/video/CDGwVXknZXI/w-d-xo.html

  • @scottt1412
    @scottt1412 2 ปีที่แล้ว

    my interview question for count would be:. How would I perform a count on a table with 30 billion rows? (Hint, bigint comes into play).

    • @LearnatKnowstar
      @LearnatKnowstar  2 ปีที่แล้ว

      That is a great question. Please post your approach as well for this scenario.

    • @TimGautier
      @TimGautier 2 ปีที่แล้ว +3

      I deal with tables that big on a regular basis. If I need to know the unfiltered count (roughly), I'll check the table stats, not run a count query. :)

    • @scottt1412
      @scottt1412 2 ปีที่แล้ว

      @@TimGautier that's a good one.. the stats will be ultra fast too. The one issue is if your stats are somewhat stale they could be considerably off, in which case count_big works well on those very large tables.
      Side comment, If you're doing something with columnstore indexes then your counts and aggregates will be much much faster too, albeit CPU intensive sometimes.

  • @joseignaciodiazgonzalez6769
    @joseignaciodiazgonzalez6769 2 ปีที่แล้ว +4

    wow... the execution performance depends of many many things... on a local database so small as that one table youre working on you will never have a good sample... on remote databases with millions of rows those queries will differ significantly

  • @Mike-mc5ll
    @Mike-mc5ll 2 ปีที่แล้ว

    Would there be any difference in prequel?

    • @jeisonsanchez4842
      @jeisonsanchez4842 2 ปีที่แล้ว

      In prequel, the time is the same but the count is negative.

  • @souravsubham8575
    @souravsubham8575 2 ปีที่แล้ว +1

    If count (1) and count (*) are same in every aspect.. what is the need of count(1)?

    • @LearnatKnowstar
      @LearnatKnowstar  2 ปีที่แล้ว

      You are right. Both serve the same purpose. It is just a way of writing.

    • @omkarbobade1115
      @omkarbobade1115 2 ปีที่แล้ว +1

      Person who have lot of time can use the count(1) 😅

  • @MalClarke
    @MalClarke 2 ปีที่แล้ว +5

    This video is misleading. If you add a value between the parentheses it will assign that values to every row and return the count. It will ALWAYS be slower than count *. They will return the same result but are NOT THE SAME. Your timing demonstrates that

  • @madhuchowdarytalluri7739
    @madhuchowdarytalluri7739 3 ปีที่แล้ว +3

    very good explanation, everyone can easily understand.

  • @visaalakshiselvaraj5572
    @visaalakshiselvaraj5572 3 ปีที่แล้ว +1

    Wonderful ❤️

  • @nicolas3palacios805
    @nicolas3palacios805 2 ปีที่แล้ว

    Thanks

  • @Beyondhorizons-i5z
    @Beyondhorizons-i5z 3 หลายเดือนก่อน

    Great

  • @shirishnamdeo1775
    @shirishnamdeo1775 2 ปีที่แล้ว +1

    Wrong, You executed then count(*) two times, intially it took 5 seconds.. check your video yourself.

    • @newsun9382
      @newsun9382 2 ปีที่แล้ว

      You could have put it in a softer way. BTW, what's you channel? Do you have one? Indians should learn gratitude.

    • @shirishnamdeo1775
      @shirishnamdeo1775 2 ปีที่แล้ว +4

      @@newsun9382 No i dont have channels.
      And btw dont bring this to off topic, with me being Indian.
      We are commenting on the technical topic.
      Keep Nation, And other topics off the thread.

    • @newsun9382
      @newsun9382 2 ปีที่แล้ว

      @@shirishnamdeo1775 No I will not keep nation, nationality out of any thing. Indians, thought they form bulk of the IT world, are net consumers of such videos and articles. They are very good at criticising though.
      If you take out the nationality part, all I said was to be more polite and encouraging. We dont have to be @$$ #0l#$ all the time. Take a break sometimes.

  • @ltorres321
    @ltorres321 2 ปีที่แล้ว +7

    Is no here going to call this fraud video? She says no difference between * and 1 yet the * had .007 and 1 had .275 ? That is a huge significance. I would take this course for free 0 trust…

  • @data_home
    @data_home 2 ปีที่แล้ว +1

    SQL-in sadə yolla izahı

  • @badprogrammerr162
    @badprogrammerr162 2 ปีที่แล้ว

    Now compare SUM(1) vs COUNT(4) 😂

  • @shabareesharyan
    @shabareesharyan ปีที่แล้ว

    please charge your laptop its giving my anxiety

  • @HansenTandi
    @HansenTandi 2 ปีที่แล้ว +1

    Count(1) can be faster than count(*) if column 1 is a primary key column. Cmiiw

  • @sylvesterkamau2051
    @sylvesterkamau2051 2 ปีที่แล้ว +1

    Count(*) is always faster than count(1)

  • @adreskorespondencyjny1551
    @adreskorespondencyjny1551 2 ปีที่แล้ว

    calculating efficiency on 10rows database? aha, what pronouns do you use?