S.No Group By Order By 1Meaning:Group By performs grouping operation i.e., it provides a way to sub-total SQL Query results,or perform some other aggregate functions (GROUPING SETS, CUBE, ROLLUP, WITH CUBE, or WITH ROLLUP) on them.Meaning:Order By performs sorting operation i.e., it provides a way to sort SQL Query results like ascending or descending . It does not affect what shows up in the result set,only what order it is displayed. 2Used for:Controlling the presentation of the rows for the results of the SELECT statement.Used for:Controlling the presentation of the columns for the results of the SELECT statement. 3Change in order of columns:Order of columns make no difference in the GROUP BY Clause.For Example,GROUP BY A, B, CandGROUP BY C,A,BBoth returns the same results.Change in order of columns:Order of Columns makes difference in ORDER BY Clause.For Example,ORDER BY A, B, CandORDER BY C,A,B 4Allowed in Create View Statement or not:GROUP BY clause can be used in the CREATE VIEW statement to sort data.Allowed in Create View Statement or not:ORDER BY clause is not allowed in the CREATE VIEW statement. 5Execution Sequence:The GROUP BY clause is always placed before the ORDER BY clause in the SELECT statement.Execution Sequence:The ORDER BY clause is always placed after the GROUP BY clause in the SELECT statement. 6Impact on performance:As we know that Group By clubs all the similar rows and display only the distinct data. So here clubbing and displaying distinct data is an overhead.Impact on performance:As we now that Order By sorts the data either in ascending order or in descending order as specified in the query. So here Sorting the data is an overhead.
Title Rate 1 The Accountant 52.88 2 Accounts Manager 34.74 3 Accounts Payable Specialist 38 4 Accounts Receivable Specialist 57 5 Application Specialist 109.62 6 Assistant to the Chief Financial Officer 13.46 7 Benefits Specialist 16.59 8 Buyer 164.42 9 Chief Executive Officer 125.5 10 Chief Financial Officer 120.19 11 Control Specialist 33.65 12 Database Administrator 76.92
First Name Title 1 A. Scott The Master Scheduler 2 Alan Scheduling Assistant 3 Alejandro Production Technician - WC40 4 Alex Production Technician - WC45 5 Alice Production Technician - WC50 6 Amy European Sales Manager 7 Andreas Quality Assurance Technician 8 Andrew Production Supervisor -WC10 9 Andrew Production Technician - WC45 10 Andy Production Technician - WC30 11 Angela Production Technician - WC50 12 Anibal Production Technician - WC20
- All non-aggregate columns selected must be listed in the GROUP BY clause.
- Integers cannot be used in the GROUP BY to represent columns after the SELECT keyword, similar to using the ORDER BY clause.
- The GROUP BY clause is generally not necessary unless using aggregate functions.
- If we GROUP, the results are not necessarily sorted; although in many cases they may come out in an intuitive order, that's not guaranteed by the GROUP clause. If we want our groups sorted, always use an explicity ORDER BY after the GROUP BY. – Dave Costa
- Processing of "group by" or "order by" clause often requires creation of Temporary tables to process the results of the query. Which depending of the result set can be very expensive.