GeekInterview.com
  I am new, Sign me up!
 
Home Oracle
 

What is the use of COUNT with DISTINCT option

 


COUNT is a group function returns result by summarizing multiple rows. All group by value function will have the usage of DISTINCT or ALL option and so is the COUNT which uses the DISTINCT option. DISTINCT option is used to enforce uniqueness and  if combined with COUNT is used to count only unique mentioned value.


To understand this let us see an example. Consider exforsys table which has columns as empno, empname, salary, DOJ


Select * from exforsys;


EMPNO EMPNAME SALARY DOJ
----- ------- ------ ---
1000 SRI 10000 12-MAR-1978
2000 SRI 50000 13-JUN-1980
3000 SRI 60000 23-APR-1998
4000 JOHN 5000 21-MAR-1981


Select COUNT(DISTINCT empname),COUNT(empname),COUNT(*) from exforsys;


COUNT(DISTINCTEMPNAME) COUNT(EMPNAME) COUNT(*)
---------------------- -------------- --------
2                          4              4


In the above sample, COUNT(EMPNAME) returned 4 that is it counts all records of empname but COUNT(DISTINCTEMPNAME) returned output 2 because there are three records with empname SRI .



Read Next: Evolution of database systems



 
Related Topics


 

Comments


kirangsv said:

  COUNT is a group function returns result by summarizing multiple rows.
DISTINCT option is used to enforce uniqueness
January 7, 2008, 4:04 am

Jirange said:

  its great combination of uniqe and group, in one group possibilities of duplicate values to avoide the wrong count this is the best solution
September 9, 2008, 7:35 am

vermaritwik said:

  To put it into simple words:

When you want to Count a number of unique values in a set, you use COUNT with DISTINCT.

Let's say:

I have a table called BAG which is like this:

ARTICLE TYPE
HB Pencil Pencil
Black Pen Pen
White Eraser Eraser
2B Pencil Pencil
Blue Pen Pen


Now if I want to count the no. of items, I will write COUNT(ARTICLE) or COUNT(TYPE), which will give us '5'.

But if we want to see how many types of articles we have but not the exact count. We will write COUNT(DISTINCT TYPE) which will give us 3.
November 28, 2008, 2:40 pm

sagartambe2008 said:

  Hey guys remember distinct is faster than group
June 6, 2009, 3:32 am

Post Your Comment:

Members Please Login
Your Name:*
e-mail ID:(required for notification)*
Image Verification: 
 
 Subscribe    

Sponsored Links

 
About Us -  Privacy Policy -  Terms and Conditions -  Contact  

Copyright © 2005 - 2009 GeekInterview.com. All Rights Reserved

Page copy protected against web site content infringement by Copyscape