Gmail Calendar Documents Reader Web more »
Recently Visited Groups | Help | Sign in
Google Groups Home
SELECT DISTINCT vs GROUP BY
There are currently too many topics in this group that display first. To make this topic appear first, remove this option from another topic.
There was an error processing your request. Please try again.
flag
  9 messages - Collapse all  -  Translate all to Translated (View all originals)
The group you are posting to is a Usenet group. Messages posted to this group will make your email address visible to anyone on the Internet.
Your reply message has not been sent.
Your post was successful
 
From:
To:
Cc:
Followup To:
Add Cc | Add Followup-to | Edit Subject
Subject:
Validation:
For verification purposes please type the characters you see in the picture below or the numbers you hear by clicking the accessibility icon. Listen and type the numbers you hear
 
Brandon Stout  
View profile  
 More options Jan 15, 9:16 pm
From: Brandon Stout <brandon.st...@gmail.com>
Date: Thu, 15 Jan 2009 19:16:04 -0700
Local: Thurs, Jan 15 2009 9:16 pm
Subject: SELECT DISTINCT vs GROUP BY
I know some differences between SELECT DISTINCT vs GROUP BY in MySQL,
but I'd like to open this up as a discussion on the group.  Each can
accomplish the same thing depending on what you are doing, yet each has
it's own purpose.  I'd like to see what the group says before I say
anything myself (call it a brain teaser if you wish).  When do you use
each of these?  When do you choose one over the other?  For other
database engines, what's the equivalent (if there's any difference), and
how do the same questions apply to your favorite database?

Brandon Stout
UDBUG Admin/Founder


    Reply to author    Forward  
You must Sign in before you can post messages.
To post a message you must first join this group.
Please update your nickname on the subscription settings page before posting.
You do not have the permission required to post.
Joshua Tolley  
View profile  
 More options Jan 15, 9:52 pm
From: Joshua Tolley <eggyk...@gmail.com>
Date: Thu, 15 Jan 2009 19:52:56 -0700
Local: Thurs, Jan 15 2009 9:52 pm
Subject: Re: [udbug] SELECT DISTINCT vs GROUP BY

On Thu, Jan 15, 2009 at 07:16:04PM -0700, Brandon Stout wrote:

> I know some differences between SELECT DISTINCT vs GROUP BY in MySQL,
> but I'd like to open this up as a discussion on the group.  Each can
> accomplish the same thing depending on what you are doing, yet each has
> it's own purpose.  I'd like to see what the group says before I say
> anything myself (call it a brain teaser if you wish).  When do you use
> each of these?  When do you choose one over the other?  For other
> database engines, what's the equivalent (if there's any difference), and
> how do the same questions apply to your favorite database?

In the development version of PostgreSQL, SELECT DISTINCT is implemented
using the same logic as GROUP BY, because the GROUP BY logic is a
performance improvement over the code that was used.

- Josh

  signature.asc
< 1K Download

    Reply to author    Forward  
You must Sign in before you can post messages.
To post a message you must first join this group.
Please update your nickname on the subscription settings page before posting.
You do not have the permission required to post.
Brandon Stout  
View profile  
 More options Jan 21, 12:56 am
From: Brandon Stout <brandon.st...@gmail.com>
Date: Tue, 20 Jan 2009 22:56:33 -0700
Local: Wed, Jan 21 2009 12:56 am
Subject: Re: [udbug] SELECT DISTINCT vs GROUP BY

Does SELECT DISTINCT also sort, then, if it does the same as GROUP BY in
PostgreSQL?

Brandon Stout
UDBUG Admin


    Reply to author    Forward  
You must Sign in before you can post messages.
To post a message you must first join this group.
Please update your nickname on the subscription settings page before posting.
You do not have the permission required to post.
Brandon Stout  
View profile  
 More options Jan 21, 1:01 am
From: Brandon Stout <brandon.st...@gmail.com>
Date: Tue, 20 Jan 2009 23:01:32 -0700
Local: Wed, Jan 21 2009 1:01 am
Subject: Re: [udbug] SELECT DISTINCT vs GROUP BY

Sasha Pachev wrote:
> With MySQL, I would recommend using SELECT DISTINCT when you need just
> distinct values, and GROUP BY when you need more than that. Even
> though in a lot of cases the conceptual logic and the number of
> examined rows may end up being the same, even then the optimizer will
> breathe a sigh of relief when it notices there is no GROUP BY. So a
> lot of potentially unnecessary code will not get executed.

> Additionally, MySQL GROUP BY always orders the results. You can even
> say something like GROUP BY name ASC or GROUP BY name DESC.  DISTINCT
> does not have to. In some case the need to order will burden the
> optimizer.

> In short, keep it simple if you can.

Thank you Sasha!  I was looking mainly at the fact that GROUP BY sorts
the results while SELECT DISTINCT, plus one more I'm not so sure of.  I
wonder about the processing order since GROUP BY is in a different
location of the SELECT statement than SELECT DISTINCT is.  Any comments
on that one?  How about in other databases?  Does MS Access even have
SELECT DISTINCT?

Brandon Stout
UDBUG Admin


    Reply to author    Forward  
You must Sign in before you can post messages.
To post a message you must first join this group.
Please update your nickname on the subscription settings page before posting.
You do not have the permission required to post.
Brandon Stout  
View profile  
 More options Jan 21, 1:11 am
From: Brandon Stout <brandon.st...@gmail.com>
Date: Tue, 20 Jan 2009 23:11:24 -0700
Local: Wed, Jan 21 2009 1:11 am
Subject: Re: [udbug] Re: SELECT DISTINCT vs GROUP BY
Brandon Stout wrote:
> I was looking mainly at the fact that GROUP BY sorts the results while SELECT DISTINCT, plus...

s/SELECT DISTINCT/SELECT DISTINCT does not/

Maybe next time I'll proofread a little better...

Brandon Stout
UDBUG Admin


    Reply to author    Forward  
You must Sign in before you can post messages.
To post a message you must first join this group.
Please update your nickname on the subscription settings page before posting.
You do not have the permission required to post.
Joshua Tolley  
View profile  
 More options Jan 21, 9:30 am
From: Joshua Tolley <eggyk...@gmail.com>
Date: Wed, 21 Jan 2009 07:30:19 -0700
Local: Wed, Jan 21 2009 9:30 am
Subject: Re: [udbug] Re: SELECT DISTINCT vs GROUP BY

On Tue, Jan 20, 2009 at 10:56:33PM -0700, Brandon Stout wrote:
> Does SELECT DISTINCT also sort, then, if it does the same as GROUP BY in
> PostgreSQL?

Neither sorts, unless you explicitly tell it to with an ORDER BY clause. The
grouping algorithm used (at least most of the time) is hash-based, so there's
no sorting required.

- Josh

  signature.asc
< 1K Download

    Reply to author    Forward  
You must Sign in before you can post messages.
To post a message you must first join this group.
Please update your nickname on the subscription settings page before posting.
You do not have the permission required to post.
Brandon Stout  
View profile  
 More options Jan 21, 5:00 pm
From: Brandon Stout <brandon.st...@gmail.com>
Date: Wed, 21 Jan 2009 15:00:42 -0700
Local: Wed, Jan 21 2009 5:00 pm
Subject: Re: [udbug] Re: SELECT DISTINCT vs GROUP BY

Joshua Tolley wrote:
> On Tue, Jan 20, 2009 at 10:56:33PM -0700, Brandon Stout wrote:
>> Does SELECT DISTINCT also sort, then, if it does the same as GROUP BY in
>> PostgreSQL?

> Neither sorts, unless you explicitly tell it to with an ORDER BY clause. The
> grouping algorithm used (at least most of the time) is hash-based, so there's
> no sorting required.

> - Josh

So, to further clarify, this table:

apple   red
apple   red
apple   red
apple   green
apple   green
banana  yellow
apple   red

when selecting distinct would return this, since it's not sorted:

apple   red
apple   green
banana  yellow
apple   red

right?

Brandon Stout
UDBUG Admin


    Reply to author    Forward  
You must Sign in before you can post messages.
To post a message you must first join this group.
Please update your nickname on the subscription settings page before posting.
You do not have the permission required to post.
Joshua Tolley  
View profile  
 More options Jan 21, 7:24 pm
From: Joshua Tolley <eggyk...@gmail.com>
Date: Wed, 21 Jan 2009 17:24:50 -0700
Local: Wed, Jan 21 2009 7:24 pm
Subject: Re: [udbug] Re: SELECT DISTINCT vs GROUP BY

Nope -- it still calculates the DISTINCT correctly, despite not sorting.
It finds distinct values by putting each datum into a hash table, and
then returning all the hash table's keys when it finishes.

5432 josh@josh# select * from fruit ;
 fruit_name | fruit_color
------------+-------------
 apple      | red
 apple      | red
 apple      | red
 apple      | green
 apple      | green
 banana     | yellow
 apple      | red
(7 rows)

5432 josh@josh*# select distinct * from fruit;
 fruit_name | fruit_color
------------+-------------
 banana     | yellow
 apple      | red
 apple      | green
(3 rows)

- Josh

  signature.asc
< 1K Download

    Reply to author    Forward  
You must Sign in before you can post messages.
To post a message you must first join this group.
Please update your nickname on the subscription settings page before posting.
You do not have the permission required to post.
Brandon Stout  
View profile  
 More options Jan 22, 12:55 pm
From: Brandon Stout <brandon.st...@gmail.com>
Date: Thu, 22 Jan 2009 10:55:36 -0700
Local: Thurs, Jan 22 2009 12:55 pm
Subject: Re: [udbug] Re: SELECT DISTINCT vs GROUP BY

Very good.  That's what I hoped since that's what happens with MySQL,
and that's what I would expect any true database to do.  This brings out
another weakness with spreadsheets - at least from the day I used them.
 You have to sort before you can do 'subtotals'.

Brandon Stout
UDBUG Admin


    Reply to author    Forward  
You must Sign in before you can post messages.
To post a message you must first join this group.
Please update your nickname on the subscription settings page before posting.
You do not have the permission required to post.
End of messages
« Back to Discussions « Newer topic     Older topic »

Create a group - Google Groups - Google Home - Terms of Service - Privacy Policy
©2009 Google