-
Notifications
You must be signed in to change notification settings - Fork 0
/
codesolution.sql
149 lines (121 loc) · 5.09 KB
/
codesolution.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
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
--CREATE OUR 6 TABLES USING EXPORT METHOD FROM QUICKDBD.COM
CREATE TABLE departments (
"dept_no" VARCHAR NOT NULL,
"dept_name" VARCHAR NOT NULL,
CONSTRAINT "pk_departments" PRIMARY KEY (
"dept_no"
)
);
--verify table departments was created successfully
SELECT * FROM departments
CREATE TABLE dept_emp (
"emp_no" INT NOT NULL,
"dept_no" VARCHAR NOT NULL
);
--verify table dept_emp was created successfully
SELECT * FROM dept_emp
CREATE TABLE dept_manager (
"dept_no" VARCHAR NOT NULL,
"emp_no" INT NOT NULL
);
--verify table dept_manager was created successfully
SELECT * FROM dept_manager
CREATE TABLE employees (
"emp_no" INT NOT NULL,
"emp_title" VARCHAR NOT NULL,
"birth_date" DATE NOT NULL,
"first_name" VARCHAR NOT NULL,
"last_name" VARCHAR NOT NULL,
"sex" VARCHAR NOT NULL,
"hire_date" DATE NOT NULL,
CONSTRAINT "pk_employees" PRIMARY KEY (
"emp_no"
)
);
--verify table employees was created successfully
SELECT * FROM employees
CREATE TABLE salaries (
"emp_no" INT NOT NULL,
"salary" INT NOT NULL
);
--verify table salaries was created successfully
SELECT * FROM salaries
WHERE emp_no= 10005
CREATE TABLE titles (
"title_id" VARCHAR NOT NULL,
"title" VARCHAR NOT NULL,
CONSTRAINT "pk_titles" PRIMARY KEY (
"title_id"
)
);
--verify table titles was created successfully
SELECT * FROM titles
---RUN THESE STATEMENTS AFTER IMPORTING CSV FOR EACH TABLE
ALTER TABLE "dept_emp" ADD CONSTRAINT "fk_dept_emp_emp_no" FOREIGN KEY("emp_no")
REFERENCES "employees" ("emp_no");
ALTER TABLE "dept_emp" ADD CONSTRAINT "fk_dept_emp_dept_no" FOREIGN KEY("dept_no")
REFERENCES "departments" ("dept_no");
ALTER TABLE "dept_manager" ADD CONSTRAINT "fk_dept_manager_dept_no" FOREIGN KEY("dept_no")
REFERENCES "departments" ("dept_no");
ALTER TABLE "dept_manager" ADD CONSTRAINT "fk_dept_manager_emp_no" FOREIGN KEY("emp_no")
REFERENCES "employees" ("emp_no");
ALTER TABLE "employees" ADD CONSTRAINT "fk_employees_emp_title" FOREIGN KEY("emp_title")
REFERENCES "titles" ("title_id");
ALTER TABLE "salaries" ADD CONSTRAINT "fk_salaries_emp_no" FOREIGN KEY("emp_no")
REFERENCES "employees" ("emp_no");
-- 1. List the following details of each employee: employee number, last name, first name, sex, and salary.
--JOIN SALARIES AND EMPLOYEES TABLES
SELECT employees.emp_no, employees.last_name, employees.first_name, employees.sex, salaries.salary
FROM employees
INNER JOIN salaries ON
salaries.emp_no = employees.emp_no
-- 2. List first name, last name, and hire date for employees who were hired in 1986.
SELECT first_name, last_name, hire_date
FROM employees
WHERE hire_date between '1/1/1986' and '12/31/1986';
-- 3. List the manager of each department with the following information: department number, department name, the manager's employee number, last name, first name.
--JOIN dept_manager, departments, & employees
SELECT departments.dept_no, departments.dept_name, dept_manager.emp_no, employees.first_name, employees.last_name
FROM departments
INNER JOIN dept_manager ON
departments.dept_no = dept_manager.dept_no
INNER JOIN employees ON
dept_manager.emp_no = employees.emp_no
-- 4. List the department of each employee with the following information: employee number, last name, first name, and department name.
--JOIN departments, dept_emp, & employees
SELECT departments.dept_name, dept_emp.emp_no, employees.first_name, employees.last_name
FROM departments
INNER join dept_emp ON
dept_emp.dept_no = departments.dept_no
INNER JOIN employees ON
employees.emp_no = dept_emp.emp_no
-- 5. List first name, last name, and sex for employees whose first name is "Hercules" and last names begin with "B."
SELECT first_name, last_name, sex
FROM employees
WHERE first_name = 'Hercules'
AND last_name LIKE 'B%';
-- 6. List all employees in the Sales department, including their employee number, last name, first name, and department name.
SELECT departments.dept_name, dept_emp.emp_no, employees.first_name, employees.last_name
FROM departments
INNER join dept_emp ON
dept_emp.dept_no = departments.dept_no
INNER JOIN employees ON
employees.emp_no = dept_emp.emp_no
WHERE departments.dept_name= 'Sales';
-- 7. List all employees in the Sales and Development departments, including their employee number, last name, first name, and department name.
--Somehow use the below code from question 4:
--USE ORDERBY TO LIST ALL EMPLOYEES IN DEPARTMENT "DEVELOPMENT" FIRST THEN LIST ALL EMPLOYEES IN DEPARTMENT "SALES"
--TRANSITION FROM DEVELOPMENT TO SALES OCCURS AT ROW 85708
SELECT departments.dept_name, dept_emp.emp_no, employees.first_name, employees.last_name
FROM departments
INNER join dept_emp ON
dept_emp.dept_no = departments.dept_no
INNER JOIN employees ON
employees.emp_no = dept_emp.emp_no
WHERE departments.dept_name= 'Sales' OR departments.dept_name= 'Development'
ORDER BY departments.dept_name='Development' DESC;
-- 8. List the frequency count of employee last names (i.e., how many employees share each last name) in descending order.
SELECT last_name, COUNT(last_name)
FROM employees
GROUP BY last_name
ORDER BY COUNT(last_name) DESC;