w3resource
SQL exercises

SQL Subquery Exercises: Display the department name and Id for all departments where they located, that Id is equal to the Id for the location where department number 30 is located

SQL SUBQUERY: Exercise-38 with Solution

Write a query in SQL to display the department name and Id for all departments where they located, that Id is equal to the Id for the location where department number 30 is located.

Sample table: departments


Sample Solution:

SELECT department_name, department_id
  FROM departments 
   WHERE location_id = 
        (
          SELECT location_id 
            FROM  departments  
             WHERE department_id = 30);

Sample Output:

department_name	department_id
Administration		10
Purchasing		30
Executive		90
Finance			100
Accounting		110
Treasury		120
Corporate Tax		130
Control And Credit	140
Shareholder Services	150
Benefits		160
Manufacturing		170
Construction		180
Contracting		190
Operations		200
IT Support		210
NOC			220
IT Helpdesk		230
Government Sales	240
Retail Sales		250
Recruiting		260
Payroll			270

Practice Online


Query Visualization:

Duration:

Query visualization of Display the department name and Id for all departments where they located, that Id is equal to the Id for the location where department number 30 is located - Duration

Rows:

Query visualization of Display the department name and Id for all departments where they located, that Id is equal to the Id for the location where department number 30 is located - Rows

Cost:

Query visualization of Display the department name and Id for all departments where they located, that Id is equal to the Id for the location where department number 30 is located - Cost

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

Previous: Write a query in SQL to display the first and last name, salary, and department ID for those employees who earn more than the maximum salary of a department which ID is 40.
Next: Write a query in SQL to display the first and last name, salary, and department ID for all those employees who work in that department where the employee works who hold the ID 201.

What is the difficulty level of this exercise?



New Content: Composer: Dependency manager for PHP, R Programming