-
Notifications
You must be signed in to change notification settings - Fork 0
/
JoinsSetOperationsSubqueries.sql
117 lines (111 loc) · 2.24 KB
/
JoinsSetOperationsSubqueries.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
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
/*
Name: Kwame Acheampong
Date: 2021.02.01
Description: Joins, Set Operations, & Subqueries Excercies
*/
USE HR_DB;
GO
--1. Select the first name, last name, job title, and department name for every employee using a join
SELECT
first_name,
last_name,
job_title,
department_name
FROM
employees emp
LEFT JOIN
departments dep
ON
emp.department_id = dep.department_id
LEFT JOIN
jobs job
ON
emp.job_id = job.job_id;
--2. Select the first name, last name, and email of all the employees who have a dependant using a join and then the subquery
--JOIN
SELECT
emp.first_name,
emp.last_name,
email
FROM
employees emp
INNER JOIN
dependents depend
ON
emp.employee_id = depend.employee_id;
--SUBQUERY
SELECT
first_name,
last_name,
email
FROM
employees
WHERE
employee_id IN(
SELECT
employee_id
FROM
dependents
)
ORDER BY
employee_id;
/*
3. Create a query that selects the state/province and city from the locations table
and the number of employees in each of those locations ordered from highest to lowest
*/
SELECT DISTINCT
city,
state_province,
COUNT(employee_id) OVER(PARTITION BY state_province) AS 'employee count'
FROM
locations l
INNER JOIN
departments d
ON l.location_id = d.location_id
INNER JOIN
employees e
ON d.department_id = e.department_id
--4. Create a query to get the first and last names of all employees and dependents using UNION
SELECT first_name, last_name FROM employees
UNION
SELECT first_name, last_name FROM dependents;
/*
--5. Using EXCEPT select employees who make above the average salary for all employees and remove
any employees who are managers
*/
SELECT
first_name,
last_name,
salary
FROM employees emp1
WHERE
salary > (
SELECT
AVG(salary)
FROM
employees
)
EXCEPT
SELECT
first_name,
last_name,
salary
FROM employees
WHERE
employee_id IN (
SELECT DISTINCT
manager_id
FROM
employees
);
-- 6. Get the job title, the amount of people with that job title, and the average salary for that job title.
SELECT
job_title,
COUNT(employee_id) AS 'employee count',
AVG(salary) AS 'average salary'
FROM
jobs j
LEFT JOIN dbo.employees e
ON j.job_id = e.job_id
GROUP BY
job_title;