In this post, we will understand the difference between group by and order by in SQL.
GROUP BY
It is used to group the rows which have the same value.
It can be used in CREATE VIEW statement.
In select statement, it has to be used before the ‘ORDER BY’ keyword.
Attribute can’t be in a ‘GROUP BY’ statement when it is under aggregate function.
The tuples are grouped based on how similar the attribute values of the tuples are.
It controls the presentation of the tuples/rows.
ORDER BY
It doesn’t use the ‘CREATE VIEW’ statement.
The result-set is sorted in ascending or descending order.
It is used after the ‘group by’ keyword.
The attribute can be under an aggregate function.
It controls the presentation of columns.
Following is the syntax of Group By and Order By in a single query −
SELECT column1, column2 FROM table_name WHERE [ conditions ] GROUP BY column1, column2 ORDER BY column1, column2