-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathAnalysis.sql
120 lines (95 loc) · 3.85 KB
/
Analysis.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
/* DATA ANALYSIS */
SET datestyle to MDY, SQL;
select now()::date;
/*
List the following details of each employee:
employee number, last name, first name, sex, and salary.
*/
DROP VIEW IF EXISTS employee_details_and_salary;
CREATE VIEW employee_details_and_salary AS
SELECT emp.emp_no AS "employee number",
emp.last_name "last name", emp.first_name AS "first name", emp.sex, sal.salary
FROM employees AS emp
LEFT JOIN salaries AS sal
ON emp.emp_no = sal.emp_no;
SELECT COUNT(*) FROM employee_details_and_salary;
SELECT * FROM employee_details_and_salary LIMIT 10;
/* List first name, last name, and hire date
for employees who were hired in 1986. */
DROP VIEW IF EXISTS employees_hired_in_1986;
CREATE VIEW employees_hired_in_1986 AS
SELECT first_name AS "first name", last_name AS "last name", hire_date AS "hire date"
FROM employees
WHERE EXTRACT(YEAR FROM hire_date) = 1986;
SELECT COUNT(*) FROM employees_hired_in_1986;
SELECT * FROM employees_hired_in_1986 LIMIT 10;
/* List the manager of each department with the following
information: department number, department name,
the manager's employee number, last name, first name.*/
DROP VIEW IF EXISTS dept_manager_details;
CREATE VIEW dept_manager_details AS
SELECT dep_mngr.dept_no AS "department number",
depts.dept_name AS "department name",
dep_mngr.emp_no AS "manager's employee number",
emp.last_name AS "manager's last name",
emp.first_name AS "manager's first name"
FROM dept_manager AS dep_mngr
LEFT JOIN departments AS depts ON dep_mngr.dept_no = depts.dept_no
LEFT JOIN employees AS emp ON dep_mngr.emp_no = emp.emp_no;
SELECT COUNT(*) FROM dept_manager_details;
SELECT * FROM dept_manager_details LIMIT 10;
/* List the department of each employee with the following information:
employee number, last name, first name, and department name.
*/
DROP VIEW IF EXISTS employee_dept_details;
CREATE VIEW employee_dept_details AS
SELECT emp.emp_no AS "employee number",
emp.last_name AS "last name",
emp.first_name AS "first name",
dept.dept_name AS "department name"
FROM employees as emp
LEFT JOIN dept_emp ON emp.emp_no = dept_emp.emp_no
LEFT JOIN departments AS dept ON dept_emp.dept_no = dept.dept_no;
SELECT COUNT(*) FROM employee_dept_details;
SELECT * FROM employee_dept_details LIMIT 10;
/*
List first name, last name, and sex for employees
whose first name is "Hercules" and last names begin with "B."
*/
SELECT first_name AS "first name", last_name AS "last name", sex
FROM employees
WHERE LOWER(first_name) = 'hercules'
AND LOWER(last_name) LIKE 'b%';
/*List all employees in the Sales department,
including their employee number, last name, first name, and department name.
*/
SELECT emp.emp_no AS "employee number",
emp.last_name AS "last name",
emp.first_name AS "first name",
dept.dept_name AS "department name"
FROM employees as emp
LEFT JOIN dept_emp ON emp.emp_no = dept_emp.emp_no
LEFT JOIN departments AS dept ON dept_emp.dept_no = dept.dept_no
WHERE dept.dept_name ILIKE 'sales';
SELECT * FROM employee_dept_details
WHERE "department name" ILIKE 'sales';
/*List all employees in the Sales and Development departments,
including their employee number, last name, first name, and department name.
*/
SELECT emp.emp_no AS "employee number",
emp.last_name AS "last name",
emp.first_name AS "first name",
dept.dept_name AS "department name"
FROM employees as emp
LEFT JOIN dept_emp ON emp.emp_no = dept_emp.emp_no
LEFT JOIN departments AS dept ON dept_emp.dept_no = dept.dept_no
WHERE dept.dept_name ILIKE 'sales' OR dept.dept_name ILIKE 'development';
SELECT * FROM employee_dept_details
WHERE "department name" ILIKE 'sales' OR "department name" ILIKE 'development';
/*In descending order, list the frequency count of
employee last names, i.e.,
how many employees share each last name.*/
SELECT last_name AS "last name", COUNT(*) AS "frequency count"
FROM employees
GROUP BY last_name
ORDER BY "frequency count" DESC;