Menu

How to sort multiple columns in SQL and in different directions?

How to sort multiple columns in SQL and in different directions?

Written by Jagdeesh | 2 min read

Problem

How to sort multiple columns in SQL and in different directions?

Input

Let’s create a table named Employees with columns: id, first_name, last_name, and salary.

sql
CREATE TABLE Employees (
    id INT PRIMARY KEY AUTO_INCREMENT,
    first_name VARCHAR(100),
    last_name VARCHAR(100),
    salary DECIMAL(10, 2)
);

-- Insert data into the table
INSERT INTO Employees (first_name, last_name, salary) VALUES
('John', 'Doe', 50000),
('Jane', 'Smith', 75000),
('Bill', 'Johnson', 60000),
('Alice', 'Johnson', 72000),
('Eve', 'Doe', 65000);

Input Table (Employees):

idfirst_namelast_namesalary
1JohnDoe50,000
2JaneSmith75,000
3BillJohnson60,000
4AliceJohnson72,000
5EveDoe65,000

Let’s sort the results based on last_name in ascending order and then by salary in descending order.

Desired Solution

idfirst_namelast_namesalary
5EveDoe65,000
1JohnDoe50,000
4AliceJohnson72,000
3BillJohnson60,000
2JaneSmith75,000

Solution:

Sort multiple columns in SQL and in different directions

sql
SELECT * FROM Employees
ORDER BY last_name ASC, salary DESC;

Output:

idfirst_namelast_namesalary
5EveDoe65,000
1JohnDoe50,000
4AliceJohnson72,000
3BillJohnson60,000
2JaneSmith75,000

Explanation:

To sort multiple columns in SQL, you can specify the columns you want to sort by in the ORDER BY clause, separated by commas. You can also specify the sorting direction for each column using the ASC (for ascending) or DESC (for descending) keyword.

When you run the above query on the provided input table, the results will be sorted first by the last_name in ascending order and then by salary in descending order for records with the same last name.

  1. SQL for Data Science – Level 1
  2. SQL for Data Science – Level 2
  3. SQL for Data Science – Level 3
  1. Introduction to SQL
  2. SQL Window Functons – Made Simple and Easy
  3. SQL Subquery
  4. SQL Order by

More SQL Questions

  1. How to access previous row value in SQL?
  2. How to exclude specific columns using select except?
  3. How to transpose columns to rows in SQL?
  4. How to select first row in each GROUP BY group?
Free Course
Master Core Python — Your First Step into AI/ML

Build a strong Python foundation with hands-on exercises designed for aspiring Data Scientists and AI/ML Engineers.

Start Free Course
Trusted by 50,000+ learners
Jagdeesh
Written by
Related Course
Master SQL — Hands-On
Join 5,000+ students at edu.machinelearningplus.com
Explore Course
Machine Learning Plus
MachineLearningPlus Expert AI/ML Training

Get a Free 30-Min
Guidance Call

Let our ML expert call you back and guide you for free

You're all set!
Tired of tutorials that go nowhere? This video shows you why and what to do instead.
Watch the Free Training
Redirecting in 5 seconds...
No thanks, I'll figure it out myself

Get your Start in AI/ML with the Foundations for Data Science (AI/ML) Course

Enroll for Free
Scroll to Top
Scroll to Top
Course Preview

Machine Learning A-Z™: Hands-On Python & R In Data Science

Free Sample Videos:

Machine Learning A-Z™: Hands-On Python & R In Data Science

Machine Learning A-Z™: Hands-On Python & R In Data Science

Machine Learning A-Z™: Hands-On Python & R In Data Science

Machine Learning A-Z™: Hands-On Python & R In Data Science

Machine Learning A-Z™: Hands-On Python & R In Data Science