Tutorialsteacher

Follow Us

Articles
  • C#
  • C# OOP
  • ASP.NET Core
  • ASP.NET MVC
  • LINQ
  • Inversion of Control (IoC)
  • Web API
  • JavaScript
  • TypeScript
  • jQuery
  • Angular 11
  • Node.js
  • D3.js
  • Sass
  • Python
  • Go lang
  • HTTPS (SSL)
  • Regex
  • SQL
  • SQL Server
  • PostgreSQL
  • MongoDB
  • SQL Server - Get Started
  • Install SQL Server
  • SQL Server Management Studio
  • SQL Server - Windows Authentication
  • SQL Server - Authentication
  • SQL Server - Create New User
  • SQL Server - GRANT/REVOKE Permissions to User
  • SQL Server - Data Types
  • SQL Server - Naming Conventions
  • SQL Server - CREATE Database
  • SQL Server - CREATE Table
  • Add Columns
  • Identity Column
  • Rename Column, Table
  • Drop Columns
  • SQL Server - Schema
  • SQL Server - Tables Relations
  • SQL Server - Primary Keys
  • Modify/Delete Primary Keys
  • SQL Server - Foreign Keys
  • Modify/Delete Foreign Keys
  • SQL Server - Check Constraints
  • SQL Server - Unique Constraints
  • SQL Server - Views
  • Modify/Delete Views
  • SQL Server - Functions
  • SQL Server - Stored Procedures
  • Stored Procedure Parameters
  • SQL Server - Indexes
  • Non-clustered Indexes
  • Modify/Delete Indexes
  • SQL Server - Triggers
  • DDL Triggers
  • LOGON Triggers
  • Enable/Disable Triggers
  • Modify/Delete Triggers
  • SQL Server - Sequence
  • SQL Server - Synonyms
  • SQL Server - IF ELSE Statement
  • SQL Server - Loops
  • SQL Server - Insert Data
  • SQL Server - Update Data
  • SQL Server - Delete Data
  • SQL Server - Select Query
  • WHERE Clause
  • GROUP BY Clause
  • HAVING Clause
  • ORDER BY Clause
  • SQL Server - Inner Join
  • Left Join
  • Right Join
  • Full Join
  • Self Join
  • Dynamic SQL
  • Built-in Functions
Entity Framework Extensions - Boost EF Core 9
  Bulk Insert
  Bulk Delete
  Bulk Update
  Bulk Merge

SQL Server - ORDER BY Clause

In SQL Server, the ORDER BY clause is used in the SELECT query to sort the result in ascending or descending order of one or more columns.

Syntax:

SELECT column1, column2,...columnN FROM table_name [WHERE] [GROUP BY] [HAVING] [ORDER BY column(s) [ASC|DESC]]
ORDER BY Characteristics:
  • The ORDER BY clause is used to get the sorted records on one or more columns in ascending or descending order.
  • The ORDER BY clause must come after the WHERE, GROUP BY, and HAVING clause if present in the query.
  • Use ASC or DESC to specify the sorting order after the column name. Use ASC to sort the records in ascending order or use DESC for descending order. By default, the ORDER BY clause sort the records in ascending order if the order is not specified.

For the demo purpose, we will use the following Employee and Department in all examples.

orderby clause

The following query will fetch all the records from the Employee table and sorts the result in ascending order of the FirstName values.

SQL Server: Select Query with QRDER BY Clause
SELECT * FROM Employee
ORDER BY FirstName;

The above query will return the following result in SQL Server.

orderby clause

The following query will return rows in the descending order of the FirstName value.

SQL Server: Sorting in Descending Order
SELECT EmpId, FirstName, LastName FROM Employee
ORDER BY FirstName DESC;

The above query will return the following result in SQL Server.

orderby clause

Sort by Multiple Columns

The ORDER BY clause can include multiple columns in different sorting order (ascending or descending). When you include multiple columns with the ORDER BY clause, it will sort the records based on the first column, and if any two or more records have the same value in the first ORDER BY column, it will sort them by the second ORDER BY column.

To understand this, first, sort the result by DeptId column, as shown below.

SQL Server: QRDER BY Clause
SELECT * FROM Employee
ORDER BY DeptId;

The above query would display the following result.

orderby clause

Now, include the FirstName column in the ORDER BY clause.

SQL Server: Multiple Columns in QRDER BY Clause
SELECT * FROM Employee
ORDER BY DeptId, FirstName;

The above query will first sort the result by DeptId, and then the rows having the same DeptId will be sorted by the FirstName. Remember, we have not included ASC or DESC, So it will sort the result in ascending order by default, as shown below.

orderby clause

Sort Group of Records

The following query sorts the group of records.

SQL Server: GROUP BY
SELECT dept.Name as 'Department', count(emp.empid) as 'No of Employees' FROM Employee emp, Department dept WHERE emp.deptid = dept.DeptId GROUP by dept.DeptName ORDER BY dept.DeptName DESC
orderby clause
TUTORIALSTEACHER.COM

TutorialsTeacher.com is your authoritative source for comprehensive technologies tutorials, tailored to guide you through mastering various web and other technologies through a step-by-step approach.

Our content helps you to learn technologies easily and quickly for learners of all levels. By accessing this platform, you acknowledge that you have reviewed and consented to abide by our Terms of Use and Privacy Policy, designed to safeguard your experience and privacy rights.

[email protected]

ABOUT USTERMS OF USEPRIVACY POLICY
copywrite-symbol

2024 TutorialsTeacher.com. (v 1.2) All Rights Reserved.