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.



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).


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.


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.


Here is the syntax of Group By clause in SQL.

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


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.

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

vlookup example downloadhow to insert command button in excelmicrosoft excel 2007 exam questions and answersddl and dml in sql serversaving macros in excelunlock excel spreadsheet with passwordvba save workbookexcel hyperlink to filedelete empty row excelvba open excel fileintroduction to vba for excelhow to delete duplicate rowsvba application calculationmacro basics excelunlock excel documentmsgbox function in vbscripthow to unhide rows in excel 2013create userform in excel 2007how to unhide all in exceldml statements in sqlcolumn index number excelvlookup dynamic table arrayexcel lookup formulasinput message box vbaproject dashboard templatesas guide for beginnersexcel userform examplesvba code to connect to sql serverscreenupdating vbaado connection string sql server 2008what is ddl dml and dcl in sqlexcel scripting tutorialwhat is the purpose of vlookup function in excelexcel vba loopdelete row in excel vbausing vbavbs output to filehow to unprotect workbook in excelms access vba functionsvbscript file existvba scripting.filesystemobjectmacro in vbahow to remove empty columns in excelexcel vba lessonsexcel vba if statementsmysql adodb connection stringrange variable vbavba close userformexcel vba pivot table filterfunctions in vba excelvba questionsexcel vba iserrorfiledialog filterrun access macro from excelexcel macro collectionhlookup function excelincrease column width in exceltutorial vlookup excel 2010autofit excel 2010sas programming video tutorialsactivesheet vbaquestions on excel formulasvba sampleslistbox clearmultiple if statements excelifs excelsorting vbainserting a macro in excelvba if statement multiple conditionscheckbox excel vbadelete blank rows excel 2010how to unhide a row in excelmsgbox function in vbexcel vba windowunlock excel file without passwordsort vbadelete blanksvba msgbox new linehow to remove blank rows in excel 2007