Sorting data using Order by

Sorting data
  • Using order by clause you can sort retrieve rows in acceding  or descending order.
  • If you are not define any order then default is acceding order.
  • ASC      – Acceding order (default)
  • DESC    – Descending order
  • Order by Comes last in select statement.

Display employee name in acceding order

SELECT last_name, job_id
FROM employees
ORDER BY last_name;sql-web

Display employee name in descending order

SELECT last_name, job_id
FROM employees
ORDER BY last_name DESC;order-by-desc

You can sort Retrieve rows using

  • Column name
 SELECT last_name, job_id
 FROM employees ORDER BY last_name;
  • Column Alias name
 SELECT employee_id, last_name, salary*12 "Annual Salary"
 FROM employees
 ORDER BY "Annual Salary" ;
  • Column number
SELECT employee_id, last_name, salary*12 "Annual Salary"
FROM employees
ORDER BY 3

Leave a Reply

Your email address will not be published. Required fields are marked *