Sorting and Grouping the Data in SQL – ORDER BY GROUP BY

Home/SQL/Sorting and Grouping the Data in SQL – ORDER BY GROUP BY

When we are dealing with huge data, we can not analyse the data by looking into each record. We sort and group the data based on certain variables and understand the data. We use Order By, Group by,Where and Having Clauses to Sort and group the data in SQL.

PREMIUM TEMPLATES LIMITED TIME OFFER

ON SALE80% OFF

50+ Project Management Templates Pack
Excel PowerPoint Word

Advanced Project Plan & Portfolio Template
Excel Template

Business Presentations Templates Pack
PowerPoint Slides

20+ Excel Project Management Pack
Excel Templates

20+ PowerPoint Project Management Pack
PowerPoint Templates

10+ MS Word Project Management Pack
Word Templates


Following are detailed examples to explain ORDER BY GROUP BY clauses in SQL.

Sorting in SQL:

Order By clause sort the result set from the query (with a where clause) in a specific order using the ORDER BY clause based on specified column(s).

Syntax:

Here is the syntax of Order By clause in SQL.

[code lanuage=”sql”] Select columns
From tables
Where <condition>
Order By column ASC/DSC;

ASC option sort the data in Ascending order and DSC option sort the selected data in Descending Order.

Example:

Here is the example to sort the data based on the Department variable. It fetches the data from Employee table and sort the data EID,NAME,Salary,Department based on Department.,[code lanuage=”sql”] SELECT EID,Name,Salary, Department
FROM Employee
ORDER BY Department ASC;

Grouping in SQL:

SQL Group By clause is used to retrieve data grouped according to one or more columns.

Syntax:

Here is the syntax of Group By clause in SQL.

[code lanuage=”sql”] Select columns
From tables
GROUP BY column;

Example:

The following example group the data based on Department. It fetches the data from Employee table and sort the data based on Department Variable.[code lanuage=”sql”]

SELECT Department, Sum(Salary)
FROM Employee
GROUP BY Department;

If you want to use Group By with a aggregated function do not use Where Clause, use Having Clause.
The reasons are:
WHERE applies to each and single row and HAVING applies to summarized rows. HAVING clause fetch the data first and then separated according to condition.HAVING is used only with the SELECT statement and WHERE Clause can be used other than Select statement also.WHERE is used before GROUP BY clause, HAVING is used after GROUP BY clause.

LIMITED TIME OFFER
By |July 1st, 2013|SQL|0 Comments

About the Author:

PNRao is a passionate business analyst and having close to 10 years of experience in Data Mining, Data Analysis and Application Development. This blog is his passion to learn new skills and share his knowledge to make you expertise in Data Analysis (Excel, VBA, SQL, SAS, Statistical Methods, Market Research Methodologies and Data Analysis Techniques).

Leave A Comment


Related pages


calling functions in vbaado excelms excel lookupdefine macro in excelexcel delete duplicate rowsexcel vba current sheetvba array lengthmultiple criteria vlookupvba code to save workbookstacked bar chart excel 2010dynamic excel chart titleexcel userformslearn vba excelhow to connect excel to sqlpie chart examples in excelvba sort listboxunlock excel fileproforma of minutes of meetingfrequently asked interview questions in sqlcan you delete duplicates in excelsheet tab excelsample excel dashboardhow do i autofit in excelcheck if workbook is open vbaexcel show hidden columnshow to create bar and line chart in excel 2010find duplicates in excel 2003excel macro wait timeexcel macro close excelhow to find duplicate value in excelcountif excel examplemicrosoft word macros examplesunprotect excel sheet with passwordhow to delete a sheet in excelword applicationsformula sumifms excel checkboxdcl sqlhow to unhide in excelexcel formatting cellsvba select sheetexcel vba stepexcel macros advancedexcel vba interior colormacros vbaactiveworkbook.saveasproject management gantt chart excelremove sheet protection excelexcel with macrosvba sampleshyperlink formula in excelexcel templates dashboardprotect an excel spreadsheetvba vbcrexcel sheet hiddensql quiz for interviewwhat does vba stand forhow to unhide the hide columns in excelexcel identify duplicate valuescode for comboboxexample of hlookupvba excel range cellsexcel macro save as pdfentirerow deleteexcel 2007 all formulas with examplesms excel password protectremove password from excel workbookhide and unhide columnstable of contents excel templatehow to adjust cell width in excelexcel 2007 countifsdim conn as new adodb connectionmicrosoft excel macro programmingmicrosoft excel gantt chart templateuserforms in excel 2010