DEV Community

vidhya murali
vidhya murali

Posted on

PostgreSQL ORDER BY

Sort Data

The ORDER BY keyword is used to sort the result in ascending or descending order.

The ORDER BY keyword sorts the records in ascending order by default. To sort the records in descending order, use the DESC keyword.

  1. . Display all employees ordered by salary from lowest to highest.

  2. Display all employees ordered by salary from highest to lowest.

  3. Display unique cities in alphabetical order.

  4. Display unique departments in alphabetical order.

  5. Display Chennai employees ordered by salary descending.

  6. Display IT employees ordered by salary ascending.

  7. Display all employees ordered by city ascending and salary descending.

select * from employees_india
order by salary asc

select * from employees_india
order by salary desc

select distinct city
from employees_india 
order by city asc

select distinct department
from employees_india 
order by department asc

select * from employees_india
where city='Chennai'
order by salary desc

select * from employees_india
where department='IT'
order by salary asc

select * from employees_india
order by city asc ,salary desc
Enter fullscreen mode Exit fullscreen mode

Top comments (0)