How To Use Order By In Sql in SQL
How to Use ORDER BY in SQL
The ORDER BY clause is used to sort the result set of a query by one or more columns. You can sort data in ascending or descending order.
Syntax:
SELECT column1, column2, ...FROM table_nameORDER BY column1 [ASC|DESC], column2 [ASC|DESC], ...;ASCmeans ascending order (default).DESCmeans descending order.
Examples:
1. Sort employees by name in ascending order:
SELECT *FROM employeesORDER BY name ASC;2. Sort employees by salary in descending order:
SELECT *FROM employeesORDER BY salary DESC;3. Sort by multiple columns (e.g., by department ascending, then salary descending):
SELECT *FROM employeesORDER BY department ASC, salary DESC;Notes:
ORDER BYis usually the last clause in a SQL query.Sorting can be done on columns not included in
SELECT(if the DB supports it).Sorting large result sets may affect query performance.
If you want, I can show you how to use ORDER BY with LIMIT or pagination!