-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathsubqueries_exercises.sql
64 lines (53 loc) · 1.84 KB
/
subqueries_exercises.sql
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
USE employees;
# Find all the employees with the same hire date
# as employee 101010 using a subquery.
# 69 Rows
SELECT CONCAT(first_name, ' ', last_name) AS 'Employee',
hire_date AS 'Hire Date'
FROM employees
WHERE emp_no =
(SELECT emp_no
WHERE hire_date = '1990-10-22');
# Find all the titles held by all employees with the first name Aamod.
# 314 total titles, 6 unique titles
SELECT title, COUNT(title)
FROM titles
WHERE emp_no IN (SELECT emp_no
FROM employees
WHERE first_name = 'Aamod')
GROUP BY title;
# SELECT CONCAT(first_name, ' ', last_name) AS 'Employees Same Name',
# title AS 'Titles'
# FROM employees, titles
# WHERE emp_no =
# (
# SELECT title
# WHERE first_name = 'Aamod'
# );
# Find all the current department managers that are female.
SELECT first_name, last_name AS 'Employee Name'
FROM employees
WHERE emp_no IN (SELECT emp_no
FROM dept_manager
WHERE YEAR(to_date) = '9999%'
AND gender = 'F');
# Find all the department names that currently have female managers.
SELECT dept_no
FROM dept_manager
WHERE dept_no IN (SELECT dept_no
FROM dept_manager
WHERE to_date
LIKE '9%'
AND emp_no IN (SELECT emp_no
FROM employees
WHERE gender = 'F'));
# Find the first and last name of the employee with the highest salary.
SELECT CONCAT(first_name) AS 'first_name',
CONCAT(last_name) AS 'last_name'
FROM employees
WHERE emp_no IN (SELECT emp_no
FROM salaries
WHERE to_date
LIKE '9%'
AND salary IN (SELECT MAX(salary)
FROM salaries));