SQL Tutorial



ORDER BY CLAUSE


🔢 ORDER BY Clause in SQL

The ORDER BY clause in SQL is used to sort the result set in ascending (ASC) or descending (DESC) order based on one or more columns.

🧠 Syntax:
SELECT column1, column2, ...
FROM table_name
ORDER BY column1 [ASC|DESC], column2 [ASC|DESC], ...;

📊 Example: Sort Students by Marks (High to Low)

SELECT name, marks
FROM Students
ORDER BY marks DESC;
  

This query shows students ordered by their marks in descending order (highest marks first).


📦 Sample Table: Products

ProductID Name Category Price
101LaptopElectronics55000
102MobileElectronics30000
103ChairFurniture1500
104TableFurniture2500

🧩 Challenge:

Write a query to list all products ordered by price in ascending order.


🔁 Multiple Column Sorting

You can sort using more than one column. For example:

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

This query first sorts employees by department (A-Z), then by salary within each department (high to low).


🎯 Summary

  • ORDER BY lets you sort query results.
  • ASC (default) sorts from lowest to highest.
  • DESC sorts from highest to lowest.
  • You can sort by multiple columns for layered sorting.

Next ➡️: Learn to filter and sort together using WHERE + ORDER BY!


🌟 Enjoyed Learning with Us?

Help others discover Technorank Learning by sharing your honest experience.
Your support inspires us to keep building!

Leave a Google Review