We read:

The DISTINCT SQL statement is used to indicate that you should work only with unique column values.

The SQL GROUP BY clause is used to combine sample results across one or more columns.

As a result of working with these predicates, we get unique entries by any key. I can not understand what is their difference.

---- Added -----

In my understanding that GROUP BY is a grouping by any sign, and that is, for example, there is a field with values:

1 2 3 1 2 3 

after GROUP BY they should be:

 1 1 2 2 3 3 

but in fact we get:

 1 2 3 

What is the reason?

  • in the case of group by we get a tree at the output - Bald
  • @Bald, and how is the output from the tree if it is not a table structure? - perfect
  • but this question is already for you, for the resulting tree you can get some value by applying Sum(),Min(),Max(),... or you can build it yourself ... bypassing the grouping results - Bald
  • @Bald, now it has become clear, we can get a calculated record. I'm right? - perfect
  • if we talk about your example, with the help of group by you can get for example the number of duplicates, or for example the amount of the order consisting of a certain number of items by grouping by order - Bald

2 answers 2

DISTINCT - get unique strings (differing from each other by any displayed field). It works a little faster.

GROUP BY - grouped by any sign, while you can use the aggregate functions SUM, AVG, MAX, etc.

Added by

Given

 1 2 3 1 2 3 

Using ORDER BY, we get a sort

 1 1 2 2 3 3 

Using DISTINCT unique values

 1 2 3 

And with GROUP BY you can count the number of occurrences.

 SELECT value, COUNT(*) FROM table_test value COUNT(*) 1 2 2 2 3 2 
  • one
    let's say there is a field with a set of records {1,1,2,2,3,3}. you can apply DISTINCT to it and get {1,2,3} and you can apply GROUP BY and get {1,2,3}. and the first and second are the same. so what's the difference? I am quite capable of reading the definition, but what is the difference in their work is not quite clear to me .. - perfect
  • @perfect you give an not quite correct example ..... they perform a different function .... for example you need to select only unique names in the name column, then Distinct will help .......... And if you need for example calculate the amount of rubles for all purchased goods for each buyer - there will be a Group .. even if a million users with the same name will be there ........ no way will not help .. - Alexey Shimansky
  • @ Alexey Shimansky, how will all the goods purchased by this person fall into one single record and be calculated? Maybe I do not quite understand what comes to SELECT .. For me, SELECT is a kind of filter by columns (fields) - perfect
  • And at the expense of what distinct works a little faster? in fact, it performs the same sort, to suppress the same lines ... - Mike
  • one
    @Mark Can add something like this: for distinct "(removes duplicates)" , for group by "Merges several lines into one, with the ability to use aggregate functions" - Mike

The difference in the mechanics of work. Distinct - returns unique values, GroupBy - groups ALL values. In some cases, the result will be the same. The rule of use is simple - if you can use Distinct, use it, if not - then Gorup by.