TGViewer
Data Science & Machine Learning Data Science & Machine Learning @datasciencefun ยท 77.7K subscribers
Post #4646 1.67K
๐Ÿš€ Data Science Roadmap 2026

๐Ÿ“ Phase 3: SQL for Data Science

๐Ÿ“– Topic 3 โ€” ORDER BY

ORDER BY is used to sort the rows returned by a SQL query. It helps you arrange data in a meaningful order, such as:

โ€ข Highest salary to lowest salary

โ€ข Lowest price to highest price

โ€ข Newest date to oldest date

โ€ข Aโ€“Z or Zโ€“A

โ€ข Highest sales to lowest sales

๐Ÿ”น Basic Syntax

SELECT column1, column2
FROM table_name
ORDER BY column_name;


By default, SQL sorts in ascending order.

SELECT *
FROM employees
ORDER BY salary;


This generally returns employees from the lowest salary to the highest salary.

๐Ÿ”น ASC โ€” Ascending Order

ASC means ascending.

SELECT *
FROM employees
ORDER BY salary ASC;


For numbers: 10000, 25000, 40000, 75000, 100000

For text: Amazon, Apple, Google, Microsoft

For dates: 2024-01-01, 2024-05-15, 2024-12-20, 2025-03-10

ASC is usually the default, so ORDER BY salary; is equivalent to ORDER BY salary ASC;

๐Ÿ”น DESC โ€” Descending Order

DESC means descending.

SELECT *
FROM employees
ORDER BY salary DESC;


Example: 100000, 75000, 40000, 25000, 10000

This is especially useful when you want to find:

โ€ข Highest-paid employees

โ€ข Top-selling products

โ€ข Most expensive products

โ€ข Most recent transactions

โ€ข Highest-performing regions

๐Ÿ”น Sorting Text

You can sort text columns alphabetically.

SELECT employee_name, department
FROM employees
ORDER BY employee_name ASC;


SELECT employee_name, department
FROM employees
ORDER BY employee_name DESC;


๐Ÿ”น Sorting Dates

To find the most recent transactions:

SELECT transaction_id, transaction_date, amount
FROM transactions
ORDER BY transaction_date DESC;


To see transactions from oldest to newest:

SELECT transaction_id, transaction_date, amount
FROM transactions
ORDER BY transaction_date ASC;


๐Ÿ”น Sorting by Multiple Columns

This is very important. Suppose you want to sort employees:

1. By department

2. Within each department, by salary from highest to lowest

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


SQL first sorts by department. When multiple rows have the same department, it then uses salary to determine their order.

Example:

Finance 90000

Finance 70000

Finance 50000

HR 85000

HR 60000

IT 120000

IT 95000

๐Ÿ”น ORDER BY with Calculations

You can also sort using an expression.

SELECT product_name, quantity, price,
quantity * price AS total_value
FROM products
ORDER BY quantity * price DESC;


You can also use the alias in many SQL databases:

SELECT product_name,
quantity * price AS total_value
FROM products
ORDER BY total_value DESC;


๐Ÿ”น ORDER BY with DISTINCT

SELECT DISTINCT department
FROM employees
ORDER BY department ASC;


This returns each department once and sorts them alphabetically.

๐Ÿ”น ORDER BY and NULL Values

NULL represents a missing or unknown value. The position of NULL values when using ORDER BY can vary between SQL databases.

Some databases also support explicit control such as:

ORDER BY salary ASC NULLS LAST;


or

ORDER BY salary DESC NULLS FIRST;


Always check the syntax supported by your SQL database.

๐Ÿ”น ORDER BY with WHERE

WHERE filters the rows first, and ORDER BY sorts the resulting rows.

SELECT employee_name, salary
FROM employees
WHERE department = 'IT'
ORDER BY salary DESC;
  • โค 4
More from @datasciencefun
  1. Oct 2, 2026Data Visualisation tips for beginners
  2. Sep 29, 2026๐—™๐—ฅ๐—˜๐—˜ ๐—ฅ๐—ฒ๐˜€๐—ผ๐˜‚๐—ฟ๐—ฐ๐—ฒ๐˜€ ๐—ง๐—ผ ๐—Ÿ๐—ฒ๐—ฎ๐—ฟ๐—ป ๐—”๐—œ ๐—ถ๐—ป ๐Ÿฎ๐Ÿฌ๐Ÿฎ๐Ÿฒ๐Ÿš€ โ€‹ Explore 6 free resourceโ€ฆ
  3. Sep 28, 2026๐Ÿ”น Real-World Data Science Example Suppose you are analyzing sales data. You want to identโ€ฆ
  4. Sep 28, 2026๐ŸŽ“ ๐—›๐—”๐—ฅ๐—ฉ๐—”๐—ฅ๐—— ๐—จ๐—ก๐—œ๐—ฉ๐—˜๐—ฅ๐—ฆ๐—œ๐—ง๐—ฌ ๐—™๐—ฅ๐—˜๐—˜ ๐—ข๐—ก๐—Ÿ๐—œ๐—ก๐—˜ ๐—–๐—ข๐—จ๐—ฅ๐—ฆ๐—˜๐—ฆ ๐Ÿ˜ Dreaming ofโ€ฆ
  5. Sep 27, 2026Complete Data Analytics Mastery: From Basics to Advanced ๐Ÿš€ Begin your Data Analytics jourโ€ฆ
  6. Sep 27, 2026๐—Ÿ๐—ฒ๐˜ƒ๐—ฒ๐—น ๐—จ๐—ฝ ๐—ฌ๐—ผ๐˜‚๐—ฟ ๐—ฆ๐—ธ๐—ถ๐—น๐—น๐˜€ ๐˜„๐—ถ๐˜๐—ต ๐—ง๐—ต๐—ฒ๐˜€๐—ฒ ๐—š๐—ฎ๐—บ๐—ฒ-๐—–๐—ต๐—ฎ๐—ป๐—ด๐—ถ๐—ป๐—ด ๐—–๐—ผ๐˜‚โ€ฆ
Threads Profile ViewerView any public Threads profile without an account.Open ThreadLook โ†’Writing with AI? Make it sound human.Metric37 rewrites AI drafts so they read naturally. Free AI detector, 1,500 words free.Try Metric37 โ†’