DataBolt

All Lessons

Introduction to SQL
SQL Lesson 1: Your Sample Database
SQL Lesson 2: SELECT — Reading data
SQL Lesson 3: WHERE — Filtering rows
SQL Lesson 4: AND, OR, NOT
SQL Lesson 5: BETWEEN, IN, LIKE
SQL Lesson 6: NULL — The Mystery Value
SQL Lesson 7: ORDER BY
SQL Lesson 8: LIMIT & OFFSET
SQL Lesson 9: Aggregate Functions
SQL Lesson 10: GROUP BY
SQL Lesson 11: HAVING
SQL Lesson 12: INNER JOINs
SQL Lesson 13: LEFT JOINs
SQL Lesson 14: RIGHT JOINs
SQL Lesson 15: SELF JOINs
SQL Lesson 16: UNION JOINs
SQL Lesson 17: Joining Multiple Tables
SQL Lesson 18: Subqueries
SQL Lesson 19: CTEs (WITH)
SQL Lesson 20: CASE Statements
SQL Lesson 21: Window Functions
SQL Lesson 22: String Functions
SQL Lesson 23: Date & Time Functions
SQL Lesson 24: INSERT, UPDATE, DELETE
SQL Lesson 25: CREATE TABLE & DDL
SQL Lesson 26: Indexes & Performance
SQL Lesson 27: Transactions & ACID
SQL Lesson 28: SQL Execution Order
SQL Lesson 29: 50 Practice Problems

CHAPTER 8

ORDER, BY

Controlling the sequence of your results.

IN THIS CHAPTER

  • Ascending (ASC) and descending (DESC) sort
  • Sorting by multiple columns — primary and secondary sort
  • Sorting by expressions and aliases

8.1 - Ascending and Descending

Emloyees sorted by salary - lowest first

SELECT name, salary 
FROM employees
ORDER BY salary ASC;

8.2 - Multi-Column Sort

Group by department, then rank by salary within each group

SELECT name, department, salary
FROM employees
ORDER BY department ASC, salary DESC;

🌟 Note: SQL reads ORDER BY columns left to right: first sort by department (A→Z), then within each department sort by salary (high→low).

Exercise 👇

Exercise:

Tasks

1.👉Sort employees by salary in ascending order.
2.Sort employees by salary in descending order.
3.Sort employees by department in alphabetical order.
4.Sort employees by department (A-Z) and within each department by salary (highest first).
Stuck? Read this task's