SQL Order By
SQL Order By
Sorting the result after extracting the data from the dataset is one of the most basic thing we do to
get the pattern or highest/lowest values in a particular field (column). In SQL sorting is done
using ORDER BY clause. So in this article we will explore how to use ORDER BY clause in
SQL.
SQL ORDER BY
SQL ORDER BY clause is used after the WHERE clause (i.e. after filtering the data) to sort the
result in either Ascending or Descending order.
ASC: Keyword is used to arrange the result in Ascending Order
DESC: Keyword is used to arrange the result in Descending Order.
Note:
Ascending is a default sort order.
ORDER BY clause comes after the WHERE, GROUP BY, and HAVING clause (if present
in query)
Syntax:
SELECT column_list
FROM table_name
WHERE conditions
ORDER BY column_names [ASC | DESC];
Now we will use the Employee dataset (that contains Employee ID, Name, Gender, Department,
Education, Month of Joining, and CTC) to do some examples to get a clear understanding of how
to use the ORDER BY clause in SQL.
Output
Employee ID Name Gender Department Education Month of Joining CTC(in Lacs)
1001 Ajay M Engineering Doctoral January 25
1002 Babloo M Engineering UG February 23
1005 Evina F Marketing UG March 16
1003 Chhavi F HR PG March 15
1004 Dheeraj M HR UG January 12
1006 Garima M Sales UG December 10
1007 Fredy F Sales PG March 10
1008 Hans M Admin PG November 8
1009 Ivanka F Admin Intermediate April 7
1010 Jai M Peon December 4
Now if you look closely, you will get that firstly the result is sorted in descending order
of CTC and since in the sales department both the employees are getting the same salary (10
lacs). So sorting further the result according to the Name will sort the rows with the same CTC
according to Name in Ascending Order.
Note
In the ORDER BY clause, instead of using the Column name, you can use column number
Using the ORDER BY clause, you can sort the results on a column not defined in the
SELECT statement.
Let’s consider a simple dataset with two tables: Customers and Orders.
The Customers table has 5 columns: customer_id, first_name, last_name, age, and country.
Orders Table has 4 columns: order_id, item, amount, customer_id
Customers Table
customer_id first_name last_name age country
1 John Doe 31 USA
2 Robert Luna 22 USA
3 David Robinson 22 UK
4 John Reinhardt 25 UK
5 Betty Doe 28 UAE
Orders Table
order_id item amount customer_id
1 Keyboard 400 4
2 Mouse 300 4
3 Monitor 12000 3
4 Keyboard 400 1
5 Mousepad 250 2
Problem Statement: Find the amount spent by each customer in the USA.
SELECT Customers.first_name, Customers.last_name, SUM(Orders.Amount) as Amount
FROM Customers
JOIN Orders ON Customers.customer_id = Orders.customer_id
WHERE Customers.country = 'USA'
GROUP BY Customers.first_name, Customers.last_name
ORDER BY Amount DESC;
Output
first_name last_name Amount
John Doe 400
Robert Luna 250
Explanation
FROM and JOIN: We start by identifying the ‘Customers‘ and ‘Orders‘ tables and joining
them on ‘customer_id‘.
WHERE: It will filter the record to include only those where ‘country‘ = ‘USA‘.
GROUP BY: Group the remaining entries (after filtering by WHERE clause) by ‘first_name‘
and ‘last_name‘.
SELECT: SELECT the ‘first_name‘, ‘last_name‘, and the sum of ‘Amount‘ for each group.
ORDER BY: Finally, the result is sorted by ‘Amount‘ in descending order.
Case-1: Let you want to filter the record based on the ‘Amount’ using the WHERE clause.
SELECT Customers.first_name, Customers.last_name, SUM(Orders.Amount) as Amount
FROM Customers
JOIN Orders ON Customers.customer_id = Orders.customer_id
WHERE Orders.Amount >300
GROUP BY Customers.first_name, Customers.last_name
ORDER BY Amount DESC;
Output
first_name last_name Amount
David Robinson 12000
John Doe 400
John Reinhardt 400
Case-2: Filter the record based on the ‘Amount’ using the HAVING clause.
SELECT Customers.first_name, Customers.last_name, SUM(Orders.Amount) as Amount
FROM Customers
JOIN Orders ON Customers.customer_id = Orders.customer_id
GROUP BY Customers.first_name, Customers.last_name
HAVING Amount > 300
ORDER BY Amount DESC;
Output
first_name last_name Amount
David Robinson 12000
John Reinhardt 700
John Doe 400