Home All Groups Group Topic Archive Search About

Differences between DISTINCT and GROUP BY?

Author
2 Dec 2005 9:35 PM
Nicolas Verhaeghe - White Echo
What is the difference between the two statements, besides the syntax? And
in general what are the differences between the DISTINCT and the GROUP BY
methods? -- Thanks

select field
from table
group by field

select distinct field
from table

Author
2 Dec 2005 9:39 PM
Adam Machanic
DISTINCT is for distinctness.

GROUP BY is for aggregation.  It just so happens that part of the
aggregation process is distinctness of all non-aggregated columns, which is
why you see the same behavior when you use GROUP BY with no aggregation,
compared with DISTINCT.


--
Adam Machanic
Pro SQL Server 2005, available now
http://www.apress.com/book/bookDisplay.html?bID=457
--


Show quote
"Nicolas Verhaeghe - White Echo" <nospam_nicolas@whiteecho.com_nospam> wrote
in message news:4390be2d$0$3756$39cecf19@news.twtelecom.net...
> What is the difference between the two statements, besides the syntax? And
> in general what are the differences between the DISTINCT and the GROUP BY
> methods? -- Thanks
>
> select field
> from table
> group by field
>
> select distinct field
> from table
>
Author
6 Dec 2005 12:00 AM
Conor Cunningham [MS]
GROUP BY without any aggregate columns is effectively the same as DISTINCT
on the same column set.

Specifically these are the same as far as SQL Server 2005 is concerned.
They return the same results.  They will be optimized with the same level of
support in pretty much every case.

SELECT DISTINCT A, B, C FROM Table;
and
SELECT A, B, C FROM Table GROUP BY A, B, C

Show quote
"Adam Machanic" <amachanic@hotmail._removetoemail_.com> wrote in message
news:eK61kj49FHA.3308@TK2MSFTNGP11.phx.gbl...
> DISTINCT is for distinctness.
>
> GROUP BY is for aggregation.  It just so happens that part of the
> aggregation process is distinctness of all non-aggregated columns, which
> is why you see the same behavior when you use GROUP BY with no
> aggregation, compared with DISTINCT.
>
>
> --
> Adam Machanic
> Pro SQL Server 2005, available now
> http://www.apress.com/book/bookDisplay.html?bID=457
> --
>
>
> "Nicolas Verhaeghe - White Echo" <nospam_nicolas@whiteecho.com_nospam>
> wrote in message news:4390be2d$0$3756$39cecf19@news.twtelecom.net...
>> What is the difference between the two statements, besides the syntax?
>> And in general what are the differences between the DISTINCT and the
>> GROUP BY methods? -- Thanks
>>
>> select field
>> from table
>> group by field
>>
>> select distinct field
>> from table
>>
>
>
Author
12 Dec 2005 5:00 PM
Nicolas Verhaeghe
Thanks. My habit is to throw in "DISTINCT" when I see it is going to
duplicate the results (or that it is already doing it), it's fast and
efficient.

I use GROUP BY when I need to aggregate as you say.

Although Crystal is doing all of my grouping and counting now.

Show quote
> DISTINCT is for distinctness.
>
> GROUP BY is for aggregation.  It just so happens that part of the
> aggregation process is distinctness of all non-aggregated columns, which
> is why you see the same behavior when you use GROUP BY with no
> aggregation, compared with DISTINCT.
>
>
> --
> Adam Machanic
> Pro SQL Server 2005, available now
> http://www.apress.com/book/bookDisplay.html?bID=457
> --
>
>
> "Nicolas Verhaeghe - White Echo" <nospam_nicolas@whiteecho.com_nospam>
> wrote in message news:4390be2d$0$3756$39cecf19@news.twtelecom.net...
>> What is the difference between the two statements, besides the syntax?
>> And in general what are the differences between the DISTINCT and the
>> GROUP BY methods? -- Thanks
>>
>> select field
>> from table
>> group by field
>>
>> select distinct field
>> from table
>>
>
>
Author
12 Dec 2005 9:26 PM
Gert-Jan Strik
Hmm, when I see a query that returns duplicates, my first reaction is to
inspect the query, because if this happens the query is usually
incorrect. My first reaction is not to throw in a DISTINCT...

Gert-Jan


Nicolas Verhaeghe wrote:
Show quote
>
> Thanks. My habit is to throw in "DISTINCT" when I see it is going to
> duplicate the results (or that it is already doing it), it's fast and
> efficient.
>
> I use GROUP BY when I need to aggregate as you say.
>
> Although Crystal is doing all of my grouping and counting now.
>
> > DISTINCT is for distinctness.
> >
> > GROUP BY is for aggregation.  It just so happens that part of the
> > aggregation process is distinctness of all non-aggregated columns, which
> > is why you see the same behavior when you use GROUP BY with no
> > aggregation, compared with DISTINCT.
> >
> >
> > --
> > Adam Machanic
> > Pro SQL Server 2005, available now
> > http://www.apress.com/book/bookDisplay.html?bID=457
> > --
> >
> >
> > "Nicolas Verhaeghe - White Echo" <nospam_nicolas@whiteecho.com_nospam>
> > wrote in message news:4390be2d$0$3756$39cecf19@news.twtelecom.net...
> >> What is the difference between the two statements, besides the syntax?
> >> And in general what are the differences between the DISTINCT and the
> >> GROUP BY methods? -- Thanks
> >>
> >> select field
> >> from table
> >> group by field
> >>
> >> select distinct field
> >> from table
> >>
> >
> >

AddThis Social Bookmark Button