Sie sind auf Seite 1von 3

Difference between Group By and Order By in SQL Server S.No 1 Group By Meaning: 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. Used for: Controlling the presentation of the rows for the results of the SELECT statement. Change in order of columns: Order of columns make no difference in the GROUP BY Clause. For Example, GROUP BY A, B, C and GROUP BY C,A,B Both returns the same results. 4 Allowed in Create View Statement or not: GROUP BY clause can be used in the CREATE VIEW statement to sort data. Execution Sequence: The GROUP BY clause is always placed before the ORDER BY clause in the SELECT statement. Impact 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. ORDER BY A, B, C and ORDER BY C,A,B Both returns the same number of Records. But Row order will be different. Allowed in Create View Statement or not: ORDER BY clause is not allowed in the CREATE VIEW statement. Execution Sequence: The ORDER BY clause is always placed after the GROUP BY clause in the SELECT statement. 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. Order By 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. Used for: Controlling the presentation of the columns for the results of the SELECT statement. Change in order of columns: Order of Columns makes difference in ORDER BY Clause. For Example,

Example-1 for Group By:

select * from employee group by emp_name. this gives the output data in the group of the emp_name. Example-2 for Group By: SELECT department, sum(salary) FROM tblEmployee GROUP BY department; will give us salary totals by department, whereas the sum statement by itself would just give us the grand total of all salaries in tblEmployee. Example-3 for Group By: Group By helps us to display any aggregate of any column based on a field what has repeated names. use AdventureWorksDW2008R2 go select Title,SUM(BaseRate) as Rate from dbo.DimEmployee group by Title

Title 1 2 3 4 5 6 7 8 9 10 11 12 The Accountant Accounts Manager Accounts Payable Specialist Accounts Receivable Specialist Application Specialist Benefits Specialist Buyer Chief Executive Officer Chief Financial Officer Control Specialist Database Administrator

Rate 52.88 34.74 38 57 109.62 16.59 164.42 125.5 120.19 33.65 76.92

Assistant to the Chief Financial Officer 13.46

Example-1 for Order By: select * from employee order by emp_name desc,emp_no asc; this query gives the output in the ascending order of the emp_no and descending order of the emp_name. Example-2 for Order By:

SELECT * FROM tblEmployee ORDER BY lastname; will give us all tblEmployee data, in order of last name. If we want the results in descending (reversed) order, simply add DESC to the end of the clause: ORDER BY lastname DESC; Example-3 for Order By: Order By clause helps us to display any table based on the values present on that particular column. use AdventureWorksDW2008R2 go select FirstName,Title from dbo.DimEmployee order by FirstName First Name 1 2 3 4 5 6 7 8 9 10 11 12 Summary: 1. All non-aggregate columns selected must be listed in the GROUP BY clause. 2. Integers cannot be used in the GROUP BY to represent columns after the SELECT keyword, similar to using the ORDER BY clause. 3. The GROUP BY clause is generally not necessary unless using aggregate functions. 4. 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 5. 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. And, further updates on difference between questions and answers, please visit my blog @ http://onlydifferencefaqs.blogspot.in/ A. Scott Alan Alejandro Alex Alice Amy Andreas Andrew Andrew Andy Angela Anibal Title The Master Scheduler Scheduling Assistant Production Technician - WC40 Production Technician - WC45 Production Technician - WC50 European Sales Manager Quality Assurance Technician Production Supervisor -WC10 Production Technician - WC45 Production Technician - WC30 Production Technician - WC50 Production Technician - WC20

Das könnte Ihnen auch gefallen