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

IDName
1c
2b
3a
4c

select * from  tbl_name order by Name

Result

IDName
3a
2b
1c
4c

GROUP BY

SELECT COUNT(ID) as Count,Name

FROM tbl_name

GROUP BY Name

Result

CountName
1a
1b
2c

 

 

Leave a Reply

%d bloggers like this: