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

Sql studio em braces a variety of capabilities, but will I need them all? Is there a simpler product ? : sql server management studio

521


How to verify the port number of the sql server?

560


A trigger can reference objects outside the current database? State true or false.

553


What is explicit mode in sql server?

535


Where views are stored in sql server?

560






How to insert data with null values?

587


Tell me what is the difference between locking and multi-versioning?

579


What is a DBMS, query, SQL?

575


What are the differences in Clustering in SQL Server 2005 and 2008 or 2008 R2?

593


What is mean by dml?

539


What is the most common type of join?

531


What stored by the model?

552


How many types of dimensions are there and what are they? : sql server analysis services, ssas

564


What do you mean by cardinality?

494


Which autogrowth database setting is good?

617