PostgreSQL Basic SELECT Statement: Get unique department ID from employee table
2. Write a query to get a unique department ID from employee table.
Sample Solution:
Code:
-- This SQL query selects unique values from the 'department_id' column in the 'employees' table.
SELECT DISTINCT department_id
FROM employees;
Explanation:
- The SELECT DISTINCT statement is used to retrieve unique values from a specified column or columns in a table.
- department_id is the name of the column being selected from the employees table.
- This query retrieves unique values of department IDs from the 'employees' table. It eliminates duplicate values and returns only distinct values of the 'department_id' column.
Sample table: employees
Output:
pg_exercises=# SELECT DISTINCT department_id pg_exercises-# FROM employees; department_id --------------- 90 20 100 40 110 80 70 50 60 30 10 0 (12 rows)
Relational Algebra Expression:
Relational Algebra Tree:
Practice Online
Have another way to solve this solution? Contribute your code (and comments) through Disqus.
Previous: Write a query to display the names (first_name, last_name) using an alias name "First Name", "Last Name".
Next: Write a query to get the details of all employees from the employee table in descending order by their first name.
What is the difficulty level of this exercise?
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/basic/basic-select-statement-exercise-2.php
- Weekly Trends and Language Statistics
- Weekly Trends and Language Statistics