w3resource
SQL exercises

SQL Subquery Exercises: Write a query to get the details of employees who manage a department

SQL SUBQUERY: Exercise-33 with Solution

Write a query to get the details of employees who manage a department.

Sample table: employees


Sample table: departments


Sample Solution:

SELECT * 
FROM employees 
WHERE employee_id=ANY 
( SELECT manager_id FROM departments );

Sample Output:

employee_id	first_name	last_name	email	phone_number	hire_date	job_id	salary	commission_pct	manager_id	department_id
100		Steven		King		SKING	515.123.4567	2003-06-17	AD_PRES	24000.00	0.00	0		90
103		Alexander	Hunold		AHUNOLD	590.423.4567	2006-01-03	IT_PROG	9000.00		0.00	102		60
108		Nancy		Greenberg	NGREENBE515.124.4569	2002-08-17	FI_MGR	12000.00	0.00	101		100
114		Den		Raphaely	DRAPHEAL515.127.4561	2002-12-07	PU_MAN	11000.00	0.00	100		30
121		Adam		Fripp		AFRIPP	650.123.2234	2005-04-10	ST_MAN	8200.00		0.00	100		50
145		John		Russell		JRUSSEL	011.44.1344.4292682004-10-01    SA_MAN	14000.00	0.40	100		80
200		Jennifer	Whalen		JWHALEN	515.123.4444	2003-09-17	AD_ASST	4400.00		0.00	101		10
201		Michael		Hartstein	MHARTSTE515.123.5555	2004-02-17	MK_MAN	13000.00	0.00	100		20
203		Susan		Mavris		SMAVRIS	515.123.7777	2002-06-07	HR_REP	6500.00		0.00	101		40
204		Hermann		Baer		HBAER	515.123.8888	2002-06-07	PR_REP	10000.00	0.00	101		70
205		Shelley		Higgins		SHIGGINS515.123.8080	2002-06-07	AC_MGR	12000.00	0.00	101		110

Practice Online


Query Visualization:

Duration:

Query visualization of Write a query to get the details of employees who manage a department - Duration

Rows:

Query visualization of Write a query to get the details of employees who manage a department - Rows

Cost:

Query visualization of Write a query to get the details of employees who manage a department - Cost

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

Previous: Write a query to get the details of employees who are managers.
Next: Write a query to display the employee id, name ( first name and last name ), salary, department name and city for all the employees who gets the salary as the salary earn by the employee which is maximum within the joining person January 1st, 2002 and December 31st, 2003.

What is the difficulty level of this exercise?



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