w3resource

PostgreSQL Basic SELECT Statement: Get the details of all employees in descending order by their first name


3. Write a query to get the details of all employees from the employee table in descending order by their first name.

Sample Solution:

Code:

-- This SQL query selects all columns from the 'employees' table and sorts the result set in descending order based on the 'first_name' column.

SELECT * 
FROM employees 
ORDER BY first_name DESC;

Explanation:

  • The SELECT * statement is used to retrieve all columns from a table.
  • employees is the name of the table from which data is being retrieved.
  • ORDER BY first_name DESC specifies that the result set should be sorted based on the values in the 'first_name' column in descending order.
  • This query retrieves all records from the 'employees' table and arranges them in descending order based on the values in the 'first_name' column.

Sample table: employees


Output:

pg_exercises=# SELECT * 
pg_exercises-# FROM employees
pg_exercises-# ORDER BY first_name DESC;
 employee_id | first_name  |  last_name  |  email   |    phone_number    | hire_date  |   job_id   |  salary  | commission_pct | manager_id | department_id
-------------+-------------+-------------+----------+--------------------+------------+------------+----------+----------------+------------+---------------
         180 | Winston     | Taylor      | WTAYLOR  | 650.507.9876       | 1987-09-05 | SH_CLERK   |  3200.00 |           0.00 |        120 |            50
         171 | William     | Smith       | WSMITH   | 011.44.1343.629268 | 1987-08-27 | SA_REP     |  7400.00 |           0.15 |        148 |            80
         206 | William     | Gietz       | WGIETZ   | 515.123.8181       | 1987-10-01 | AC_ACCOUNT |  8300.00 |           0.00 |        205 |           110
         195 | Vance       | Jones       | VJONES   | 650.501.4876       | 1987-09-20 | SH_CLERK   |  2800.00 |           0.00 |        123 |            50
         106 | Valli       | Pataballa   | VPATABAL | 590.423.4560       | 1987-06-23 | IT_PROG    |  4800.00 |           0.00 |        103 |            60
         141 | Trenna      | Rajs        | TRAJS    | 650.121.8009       | 1987-07-28 | ST_CLERK   |  3500.00 |           0.00 |        124 |            50
         132 | TJ          | Olson       | TJOLSON  | 650.124.8234       | 1987-07-19 | ST_CLERK   |  2100.00 |           0.00 |        121 |            50
         190 | Timothy     | Gates       | TGATES   | 650.505.3876       | 1987-09-15 | SH_CLERK   |  2900.00 |           0.00 |        122 |            50
         170 | Tayler      | Fox         | TFOX     | 011.44.1343.729268 | 1987-08-26 | SA_REP     |  9600.00 |           0.20 |        148 |            80
         203 | Susan       | Mavris      | SMAVRIS  | 515.123.7777       | 1987-09-28 | HR_REP     |  6500.00 |           0.00 |        101 |            40
		 ....

Relational Algebra Expression:

Relational Algebra Expression: Get the details of all employees in descending order by their first name.

Relational Algebra Tree:

Relational Algebra Tree: Get the details of all employees in descending order by their first name.

Practice Online



Have another way to solve this solution? Contribute your code (and comments) through Disqus.

Previous: Write a query to get a unique department ID from employee table.
Next: Write a query to get the names (first_name, last_name), salary and 15% of salary as PF for all the employees.

What is the difficulty level of this exercise?



Follow us on Facebook and Twitter for latest update.