Take your SQL skills beyond basic sorting!
In this tutorial we’ll master the ORDER BY clause — from simple ascending/descending sorts to powerful custom sorting with CASE statements.
You’ll learn:
✅ How ORDER BY works in SQL
✅ Sorting in ascending (ASC) and descending (DESC) order
✅ Sorting by multiple columns
✅ Using CASE inside ORDER BY to apply custom sort logic
By the end, you’ll be able to control exactly how your query results appear — a must-have for analytics, reporting, and SQL interview challenges.
Works with MySQL, PostgreSQL, SQL Server, Oracle, SQLite and more.
#SQL #ORDERBY #CASE #SQLTutorial
-- Create the employees table
CREATE TABLE employees (
employee_id INT PRIMARY KEY,
name VARCHAR(255) NOT NULL,
department_id INT,
department VARCHAR(255),
salary INT,
bonus INT,
hire_date DATE
);
-- Insert the data into the employees table
INSERT INTO employees (employee_id, name, department_id, department, salary, bonus, hire_date) VALUES
(101, 'Alice Johnson', 1, 'Sales', 60000, 5000, '2018-03-15'),
(102, 'Bob Williams', 2, 'Engineering', 75000, 7500, '2019-06-22'),
(103, 'Charlie Brown', 1, 'Sales', 60000, 4000, '2018-04-10'),
(104, 'David Garcia', 3, 'Marketing', 55000, 3000, '2020-01-05'),
(105, 'Eve Davis', 2, 'Engineering', 80000, 8000, '2017-11-20'),
(106, 'Frank Miller', 1, 'Sales', 70000, 6000, '2021-02-28'),
(107, 'Grace Taylor', 3, 'Marketing', 55000, 3500, '2020-03-18'),
(108, 'Henry Jones', 2, 'Engineering', 80000, 8500, '2017-12-01'),
(109, 'Ivy Clark', 4, 'Human Resources', 50000, 2000, '2022-09-01'),
(110, 'Jack White', 4, 'Human Resources', 52000, 2500, '2022-10-15');