-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathanalysis_query.sql
68 lines (52 loc) · 1.76 KB
/
analysis_query.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
-- Analysis Query
-- 1. Emp_no, last_name, first_name, sex, salary each employee
SELECT emp_no, last_name, first_name, sex,
(SELECT salaries.salary
FROM salaries
WHERE employees.emp_no = salaries.emp_no) AS "Salary"
FROM employees;
-- 2. First_name, Last_name, hire_date = 1986
SELECT first_name, last_name, hire_date
FROM employees
WHERE date_part('year',hire_date) = 1986
-- 3. Manager for each depart - department nmbr, depart name, emp_no, last_name, first_name
SELECT dm.dept_no, d.dept_name, dm.emp_no, e.last_name, e.first_name
FROM dept_manager dm
INNER JOIN departments d
ON dm.dept_no = d.dept_no
INNER JOIN employees e
ON dm.emp_no = e.emp_no;
-- 4. Each employee - emp_no, last_name, first_name, dept_no, dept_name
SELECT e.emp_no, e.last_name, e.first_name, de.dept_no,
(SELECT d.dept_name
FROM departments d
WHERE de.dept_no = d.dept_no)
FROM employees e
INNER JOIN dept_emp de
ON e.emp_no = de.emp_no;
-- 5. first_name, last_name, sex of FN = Hercules and LN begins 'B'
SELECT first_name, last_name, sex
FROM employees
WHERE first_name = 'Hercules'
AND last_name LIKE 'B%';
-- 6. Sales department - emp_no, last_name, first_name
SELECT de.emp_no, e.last_name, e.first_name
FROM dept_emp de
INNER JOIN employees e
ON de.emp_no = e.emp_no
WHERE de.dept_no = 'd007';
-- 7. Sales and Dev departments - emp_no, last_name, first_name, dept_name
SELECT de.emp_no, e.last_name, e.first_name,
(SELECT d.dept_name
FROM departments d
WHERE de.dept_no = d.dept_no)
FROM dept_emp de
INNER JOIN employees e
ON de.emp_no = e.emp_no
WHERE de.dept_no = 'd007' -- Sales department
OR de.dept_no = 'd005'; -- Dev department
-- 8. Count of surname DESC
SELECT last_name, COUNT(last_name) AS "Count of Last Name"
FROM employees
GROUP BY last_name
ORDER BY "Count of Last Name" DESC;