w3resource

PostgreSQL Restricting and Sorting Data: Display the employees who work in the department 30 or 100 and arrange the result in ascending order on the department ID


2. Write a query to display the name, including first_name and last_name, and department ID who works in the department 30 or 100 and arrange the result in ascending order according to the department ID.

Sample Solution:

Code:

SELECT first_name, last_name, department_id 
FROM employees 
WHERE department_id IN (30, 100) 
ORDER BY  department_id  ASC;

Sample table: employees


Output:

pg_exercises=# SELECT first_name, last_name, department_id
pg_exercises-# FROM employees
pg_exercises-# WHERE department_id IN (30, 100)
pg_exercises-# ORDER BY  department_id  ASC;

 first_name  | last_name  | department_id
-------------+------------+---------------
 Karen       | Colmenares |            30
 Den         | Raphaely   |            30
 Alexander   | Khoo       |            30
 Shelli      | Baida      |            30
 Sigal       | Tobias     |            30
 Guy         | Himuro     |            30
 Nancy       | Greenberg  |           100
 Daniel      | Faviet     |           100
 John        | Chen       |           100
 Ismael      | Sciarra    |           100
 Jose Manuel | Urman      |           100
 Luis        | Popp       |           100
(12 rows)

Relational Algebra Expression:

Relational Algebra Expression: Display the employees who work in the department 30 or 100 and arrange the result in  ascending order on the department ID.

Relational Algebra Tree:

Relational Algebra Tree: Display the employees who work in the department 30 or 100 and arrange the result in  ascending order on the department ID.

Practice Online


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

Previous: Write a query to display the name, including first_name and last_name and salary for all employees whose salary is out of the range between $10,000 and $15,000.
Next: Write a query to display the name, including first_name and last_name, and salary who works in the department either 30 or 100 and salary is out of the range between $10,000 and $15,000.

What is the difficulty level of this exercise?



Become a Patron!

Follow us on Facebook and Twitter for latest update.

It will be nice if you may share this link in any developer community or anywhere else, from where other developers may find this content. Thanks.

https://www.w3resource.com/postgresql-exercises/restricting-and-sorting-data/restricting-and-sorting-data-exercise-2.php