Skip to content

Commit f05d5c2

Browse files
committed
Q8
1 parent 0377e0f commit f05d5c2

File tree

3 files changed

+105
-2
lines changed

3 files changed

+105
-2
lines changed

Images/Q8_sample_output.png

29.5 KB
Loading

Output/Analysis.sql

+104
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,104 @@
1+
/* DATA ANALYSIS */
2+
3+
SET datestyle to MDY, SQL;
4+
select now()::date;
5+
6+
/*
7+
List the following details of each employee:
8+
employee number, last name, first name, sex, and salary.
9+
*/
10+
11+
DROP VIEW IF EXISTS employee_details_and_salary;
12+
CREATE VIEW employee_details_and_salary AS
13+
SELECT emp.emp_no, emp.last_name, emp.first_name, emp.sex, sal.salary
14+
FROM employees AS emp
15+
LEFT JOIN salaries AS sal
16+
ON emp.emp_no = sal.emp_no;
17+
18+
SELECT COUNT(*) FROM employee_details_and_salary;
19+
SELECT * FROM employee_details_and_salary LIMIT 10;
20+
21+
/* List first name, last name, and hire date
22+
for employees who were hired in 1986. */
23+
24+
CREATE VIEW employees_hired_in_1986 AS
25+
SELECT emp.first_name, emp.last_name, emp.hire_date
26+
FROM employees as emp
27+
WHERE EXTRACT(YEAR FROM emp.hire_date) = 1986;
28+
29+
SELECT COUNT(*) FROM employees_hired_in_1986;
30+
SELECT * FROM employees_hired_in_1986 LIMIT 10;
31+
32+
/* List the manager of each department with the following
33+
information: department number, department name,
34+
the manager's employee number, last name, first name.*/
35+
36+
CREATE VIEW dept_manager_details AS
37+
SELECT dep_mngr.dept_no, depts.dept_name, dep_mngr.emp_no, emp.last_name, emp.first_name
38+
FROM dept_manager AS dep_mngr
39+
LEFT JOIN departments AS depts ON dep_mngr.dept_no = depts.dept_no
40+
LEFT JOIN employees AS emp ON dep_mngr.emp_no = emp.emp_no;
41+
42+
SELECT COUNT(*) FROM dept_manager_details;
43+
SELECT * FROM dept_manager_details LIMIT 10;
44+
45+
/* List the department of each employee with the following information:
46+
employee number, last name, first name, and department name.
47+
*/
48+
CREATE VIEW employee_dept_details AS
49+
SELECT emp.emp_no, emp.last_name, emp.first_name, dept.dept_name
50+
FROM employees as emp
51+
LEFT JOIN dept_emp ON emp.emp_no = dept_emp.emp_no
52+
LEFT JOIN departments AS dept ON dept_emp.dept_no = dept.dept_no;
53+
54+
SELECT COUNT(*) FROM employee_dept_details;
55+
SELECT * FROM employee_dept_details LIMIT 10;
56+
57+
58+
/*
59+
List first name, last name, and sex for employees
60+
whose first name is "Hercules" and last names begin with "B."
61+
*/
62+
63+
SELECT emp.first_name, emp.last_name, emp.sex
64+
FROM employees as emp
65+
WHERE LOWER(emp.first_name) = 'hercules'
66+
AND emp.last_name LIKE 'B%';
67+
68+
/*List all employees in the Sales department,
69+
including their employee number, last name, first name, and department name.
70+
*/
71+
72+
SELECT emp.emp_no, emp.last_name, emp.first_name, dept.dept_name
73+
FROM employees as emp
74+
LEFT JOIN dept_emp ON emp.emp_no = dept_emp.emp_no
75+
LEFT JOIN departments AS dept ON dept_emp.dept_no = dept.dept_no
76+
WHERE dept.dept_name = 'Sales';
77+
78+
SELECT * FROM employee_dept_details
79+
WHERE dept_name = 'Sales';
80+
81+
/*List all employees in the Sales and Development departments,
82+
including their employee number, last name, first name, and department name.
83+
*/
84+
85+
SELECT emp.emp_no, emp.last_name, emp.first_name, dept.dept_name
86+
FROM employees as emp
87+
LEFT JOIN dept_emp ON emp.emp_no = dept_emp.emp_no
88+
LEFT JOIN departments AS dept ON dept_emp.dept_no = dept.dept_no
89+
WHERE dept.dept_name = 'Sales' OR dept.dept_name = 'Development';
90+
91+
SELECT * FROM employee_dept_details
92+
WHERE dept_name = 'Sales' OR dept_name = 'Development';
93+
94+
/*In descending order, list the frequency count of
95+
employee last names, i.e.,
96+
how many employees share each last name.*/
97+
98+
SELECT last_name, COUNT(*) AS freqency_count
99+
FROM employees
100+
GROUP BY last_name
101+
ORDER BY freqency_count DESC;
102+
103+
104+

Output/postgres-employee-data-insert-data.sql

+1-2
Original file line numberDiff line numberDiff line change
@@ -18,5 +18,4 @@ select pg_sleep(1);
1818
COPY dept_emp FROM '/tmp/dept_emp.csv' DELIMITER ',' CSV HEADER;
1919
select pg_sleep(1);
2020
COPY dept_manager FROM '/tmp/dept_manager.csv' DELIMITER ',' CSV HEADER;
21-
select pg_sleep(1);
22-
21+
select pg_sleep(1);

0 commit comments

Comments
 (0)