what is order by vs group by in sql server ?

  • Both order by and group by are used to organize data according to user needs.
  • ORDER BY is used  to sort the query result by specific columns.
  • GROUP BY is used  to group unique combinations of columns values.

ORDER BY

Table name: tbl_name

ID Name
1 c
2 b
3 a
4 c

select * from  tbl_name order by Name

Result

ID Name
3 a
2 b
1 c
4 c

GROUP BY

SELECT COUNT(ID) as Count,Name

FROM tbl_name

GROUP BY Name

Result

Count Name
1 a
1 b
2 c

 

 

Leave a Reply

Your email address will not be published. Required fields are marked *