top button
Flag Notify
    Connect to us
      Site Registration

Site Registration

What is the difference between GROUP BY and ORDER BY in Sql/MySql?

+3 votes
858 views
What is the difference between GROUP BY and ORDER BY in Sql/MySql?
posted May 7, 2014 by Sachin Dahda

Share this question
Facebook Share Button Twitter Share Button LinkedIn Share Button

2 Answers

+1 vote
 
Best answer

The ORDER BY keyword is used to sort the result-set by one or more columns.

The ORDER BY keyword sorts the records in ascending order by default. To sort the records in a descending order, we can use the DESC keyword.
Here is one example :
SELECT column_name,column_name
FROM table_name
ORDER BY column_name,column_name ASC|DESC;

The GROUP BY statement is used in conjunction with the aggregate functions to group the result-set by one or more columns.
Here is one example :
SELECT column_name, aggregate_function(column_name)
FROM table_name
WHERE column_name operator value
GROUP BY column_name;

We can also use the GROUP BY statement on more than one column.

answer Mar 11, 2015 by Tanmoy Debnath
+1 vote

ORDER BY alters the order in which items are returned i.e. ascending or descending

GROUP BY will aggregate records by the specified columns which allows you to perform aggregation functions on non-grouped columns (such as SUM, COUNT etc etc).

answer May 7, 2014 by Salil Agrawal
...