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
What is shared lock?
What is statement level trigger?
How does using a separate hard drive for several database objects improves performance right away?
What are the triggers in sql?
What causes index fragmentation?
We need to perform what steps in the following order to work with a cursor?
What will be query used to get the list of triggers in a database?
What are click through reports?
What is the rdl file?
Explain about Views?
Does transparent data encryption provide encryption when transmitting data across the network?
What is Extended user-defined?
what are different types of backups available in sql server? : Sql server database administration
Which database stores information about replication?
What is ddl and dml commands?