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

Answer Posted / 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



Post New Answer       View All Answers


Please Help Members By Posting Answers For Below Questions

What is the process of normalising?

604


What is sql service broker?

596


What are the joins in sql server? : sql server database administration

544


How to convert binary strings into integers in ms sql server?

565


Do you know exporting and importing utility?

541






What is the use of attributehierarchyvisible ? : sql server analysis services, ssas

582


what is checksum in sql server.........???

1211


Differentiate between SQL and ORACLE joins and write their syntax.

599


What is the difference between Triggers and Stored Procedure?

581


Working with TLogs

1459


Thanks to some maintenance being done, the sql server on a failover cluster needs to be brought down. How do you bring the sql server down?

533


when you create a database how is it stored? : Sql server database administration

499


Explain comment on transactions?

564


What is data source document?

551


Where the sql logs gets stored?

552