Golgappa.net | Golgappa.org | BagIndia.net | BodyIndia.Com | CabIndia.net | CarsBikes.net | CarsBikes.org | CashIndia.net | ConsumerIndia.net | CookingIndia.net | DataIndia.net | DealIndia.net | EmailIndia.net | FirstTablet.com | FirstTourist.com | ForsaleIndia.net | IndiaBody.Com | IndiaCab.net | IndiaCash.net | IndiaModel.net | KidForum.net | OfficeIndia.net | PaysIndia.com | RestaurantIndia.net | RestaurantsIndia.net | SaleForum.net | SellForum.net | SoldIndia.com | StarIndia.net | TomatoCab.com | TomatoCabs.com | TownIndia.com
Interested to Buy Any Domain ? << Click Here >> for more details...


In performance wise distinct is good or group by is good?
eg:select name from emp group by name;
select distinct name from emp;

Answers were Sorted based on User's Feedback



In performance wise distinct is good or group by is good? eg:select name from emp group by name; ..

Answer / madhu

This question is asked many times to me. What is difference
between DISTINCT and GROUP BY?

A DISTINCT and GROUP BY usually generate the same query
plan, so performance should be the same across both query
constructs. GROUP BY should be used to apply aggregate
operators to each group. If all you need is to remove
duplicates then use DISTINCT. If you are using sub-queries
execution plan for that query varies so in that case you
need to check the execution plan before making decision of
which is faster.

Example of DISTINCT:

SELECT DISTINCT Employee, Rank
FROM Employees

Example of GROUP BY:

SELECT Employee, Rank
FROM Employees
GROUP BY Employee, Rank

Is This Answer Correct ?    14 Yes 3 No

In performance wise distinct is good or group by is good? eg:select name from emp group by name; ..

Answer / ghorban adabi

I have test DISTINCT and GROUP BY query on a table in my
site db with 7800 record. the results that turned GROUP BY
was faster than DISTINCT!!!
GROUP BY result -> Query took 0.0094 sec
DISTINCT result -> Query took 0.0133 sec

Is This Answer Correct ?    5 Yes 0 No

In performance wise distinct is good or group by is good? eg:select name from emp group by name; ..

Answer / selvaraj.v

In SQL Server 2000:
-------------------
A DISTINCT and GROUP BY usually generate the same query
plan, so performance should be the same across both query
constructs. GROUP BY should be used to apply aggregate
operators to each group. If all you need is to remove
duplicates then use DISTINCT. If you are using sub-queries
execution plan for that query varies so in that case you
need to check the execution plan before making decision of
which is faster.

Example of DISTINCT:
--------------------
Query:

select DISTINCT Book_Title,COUNT(*) from bookdetails

Answer:

Server: Msg 8118, Level 16, State 1, Line 1
Column 'bookdetails.Book_Title' is invalid in the select
list because it is not contained in an aggregate function
and there is no GROUP BY clause.

Example of Group By:
--------------------
Query:

select Book_Title,COUNT(*) from bookdetails group by
Book_Title

Answer:

ASP 1
C 1
C++ 1
Oracle 1
SQL Server 1
VB.Net 3
Visual Bsic 1

In this Answer, the VB.NET is Duplicate, it having this
table in 3 times.

Is This Answer Correct ?    5 Yes 3 No

In performance wise distinct is good or group by is good? eg:select name from emp group by name; ..

Answer / anil sharma

Group apply before where clause and distinct apply after
where clause.
So group by is good.

Is This Answer Correct ?    4 Yes 10 No

In performance wise distinct is good or group by is good? eg:select name from emp group by name; ..

Answer / hitesh kumar vyas s

Performance wise Distinct is more effective than group by.
Since in group by it has to group and then provide the
result but this is not the case in distinct

Is This Answer Correct ?    3 Yes 11 No

Post New Answer

More SQL Server Interview Questions

How to get a list of columns using the "sys.columns" view in ms sql server?

0 Answers  


Hi all, can any one please tell me the difference between sql server 2008 and orace 9i

0 Answers   HP,


Explain log shipping?

0 Answers  


How many partitions a clustered index has by default in sql server 2012?

0 Answers  


Can we use where and having clause together?

0 Answers  


i want to join two queries....and i want to run them as one after another that is want output of first query then second , then again output of first query then second and so on...

2 Answers  


What is measure group, measure? : sql server analysis services, ssas

0 Answers  


Explain the steps to create and execute a user-defined function in the sql server?

0 Answers  


SYNTAX FOR VIEWS WITH EXAMPLE HOW TO LINK TWO TABLES

1 Answers   Microsoft,


What is the STUFF function and how does it differ from the REPLACE function?

2 Answers  


1.What is the deferece between DBMS & RDBMS? 2.How can you trouble shoot? If u will get error while installing sql server? 3.How can u trouble shoot slow running query? 4.how can u trouble log shipping errors? 5.why do we use merge replication instead of T.log replication?

7 Answers   Microsoft,


what is an extended stored procedure? Can you instantiate a com object by using t-sql? : Sql server database administration

0 Answers  


Categories