Results 1 to 3 of 3
  1. #1
    Join Date
    Nov 2016
    Posts
    4

    Answered: Difference between GROUP BY and DISTINCT

    Hi everyone,
    Just want to know is there any difference between DISTINCT and GROUP BY in queries where I'm not using any aggregate functions.
    I mean the difference in performance.

    Thanks in advance.

  2. Best Answer
    Posted by DNance

    "Actually, GROUP BY can some times avoid a SORT(if there is an index that supports the grouping), whereas, a DISTINCT will always perform a SORT."


  3. #2
    Join Date
    Apr 2008
    Location
    Iasi, Romania
    Posts
    577
    Provided Answers: 3
    Getting an explain plan for the query will answer you to that.
    In theory, my lucky guess would be that the query plan will be the same. However, using GROUP BY in a query where you actually want to get the DISTINCT is a bad practice. Are you sure that 3 years after you write this query you will be able to understand what exactly you wanted to do there?
    Florin Aparaschivei
    DB2 9.7, 10.5 on Windows
    Iasi, Romania

  4. #3
    Join Date
    Oct 2007
    Posts
    151
    Provided Answers: 9
    Actually, GROUP BY can some times avoid a SORT(if there is an index that supports the grouping), whereas, a DISTINCT will always perform a SORT.

Tags for this Thread

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •