Skip to content

Commit 22fb11e

Browse files
committed
Updates
1 parent 8c1a15f commit 22fb11e

12 files changed

+250
-0
lines changed
Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,16 @@
1+
use employees;
2+
-- show department employee
3+
-- Department Name, First Name, Last Name, and Title
4+
-- Limit to active in department, active to title, order by department, last name
5+
SELECT dept_name, emp.first_name, emp.last_name, title
6+
FROM employees AS emp
7+
INNER JOIN dept_emp AS team
8+
ON emp.emp_no = team.emp_no
9+
INNER JOIN departments
10+
ON team.dept_no = departments.dept_no
11+
INNER JOIN titles
12+
ON emp.emp_no = titles.emp_no
13+
WHERE team.to_date >= '9999-01-01'
14+
AND titles.to_date >= '9999-01-01'
15+
ORDER BY dept_name, last_name
16+
;

working files/ch7.sql

Lines changed: 38 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,38 @@
1+
use employees;
2+
SELECT first_name, last_name FROM employees
3+
INNER JOIN
4+
dept_manager
5+
ON employees.emp_no = dept_manager.emp_no
6+
WHERE from_date > '1990-01-01' AND to_date > '1995-01-01';
7+
8+
-- Want to find the name of the department manager for department 3
9+
10+
-- Using aliases (emp, dm, dept)
11+
SELECT emp.emp_no, first_name, last_name, dept.dept_name, gender
12+
FROM employees as emp
13+
JOIN dept_emp as team
14+
ON emp.emp_no = team.emp_no
15+
JOIN departments as dept
16+
ON team.dept_no = dept.dept_no
17+
JOIN salaries as sal
18+
ON emp.emp_no = sal.emp_no
19+
WHERE dept_name = 'Quality Management' AND gender = 'F'
20+
GROUP BY emp.emp_no
21+
ORDER BY last_name ASC;
22+
23+
-- some extra stuff was throwing off group by issues
24+
25+
-- Get a list of current managers, creating a full name alias
26+
SELECT dept_name
27+
,CONCAT(first_name, " ", last_name) as fullName
28+
, gender
29+
FROM employees AS emp
30+
JOIN dept_manager AS dm
31+
ON emp.emp_no = dm.emp_no
32+
JOIN departments AS dept
33+
ON dm.dept_no = dept.dept_no
34+
WHERE dm.to_date >= '9999-01-01'
35+
-- AND emp.gender = 'F' would only show ladies
36+
ORDER BY dept_name ASC
37+
;
38+
Lines changed: 35 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,35 @@
1+
use employees;
2+
-- create employee record
3+
-- create title record
4+
-- create employe department record
5+
-- create employee salary record
6+
7+
INSERT INTO (employees, titles, dept_emp, salaries)
8+
(SELECT MAX(emp_no) +1
9+
,"1984-03-24"
10+
,"Elmer"
11+
,"Tucker"
12+
,"M"
13+
,"2006-09-12"),
14+
(SELECT MAX(emp_no) +1
15+
,"Grand Poobah"
16+
,"2006-01-01"
17+
,"9999-01-01"),
18+
(SELECT MAX(emp_no) +1
19+
,"d010"
20+
,"2006-01-01"
21+
,"9999-01-01"),
22+
(SELECT MAX(emp_no) +1
23+
,105000
24+
,"2006-01-01"
25+
,"9999-01-01"),
26+
FROM employees;
27+
28+
INSERT INTO employees SELECT MAX(emp_no) +1
29+
,"1984-03-24"
30+
,"Elmer"
31+
,"Tucker"
32+
,"M"
33+
,"2006-09-12" FROM employees;
34+
35+
INSERT INTO titles SELECT emp_no where

working files/chapter8-assignment.txt

Whitespace-only changes.

working files/chapter8.sql

Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,26 @@
1+
use employees;
2+
3+
-- INSERT INTO departments VALUES ('d011', 'Quiz Wizards');
4+
5+
SELECT * from employees;
6+
7+
-- insert INTO employees VALUES(SELECT max(emp_no), "1984-03-24", "Elmer", "Tucker", "M", "2006-09-12");
8+
INSERT INTO employees
9+
SELECT MAX(emp_no) +1
10+
,"1984-03-24"
11+
,"Elmer"
12+
,"Tucker"
13+
,"M"
14+
,"2006-09-12"
15+
FROM employees;
16+
17+
-- select * from employees WHERE first_name = "Elmer";
18+
19+
INSERT INTO employees
20+
SELECT MAX(emp_no) +1
21+
,"1984-03-24"
22+
,"Elmer"
23+
,"Tucker"
24+
,"M"
25+
,"2006-09-12"
26+
FROM employees;

working files/elvis.sql

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
use employees;
2+
-- Find Elvis' hired after 1990
3+
select emp_no from employees WHERE first_name = 'Elvis'
4+
AND hire_date > ('1990-01-01');
5+
Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
use employees;
2+
-- Using group by
3+
-- select first_name, count(*) from employees group by first_name;
4+
SELECT birth_date, count(*) from employees GROUP BY birth_date;
5+
SELECT birth_date, count(*) as birthday_friends from employees GROUP BY birth_date ORDER BY birthday_friends desc;
6+
SELECT salary, count(*) as sal_count from salaries group by salary order by sal_count desc;

working files/equijoins.sql

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
USE employees;
2+
3+
SELECT dept_name
4+
,emp.first_name
5+
,emp.last_name
6+
,title
7+
FROM employees AS emp, dept_emp AS team, departments, titles
8+
WHERE team.to_date >= '9999-01-01'
9+
AND titles.to_date >= '9999-01-01'
10+
AND emp.emp_no = team.emp_no
11+
AND titles.emp_no = emp.emp_no
12+
AND departments.dept_no = team.dept_no
13+
ORDER BY dept_name, last_name;
14+
15+
-- new cartesian

working files/outerjoins.sql

Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,26 @@
1+
USE employees;
2+
3+
SELECT * from employees where emp_no not in (SELECT emp_no from dept_manager);
4+
5+
SELECT * from employees as emp LEFT JOIN dept_manager as dm ON emp.emp_no = dm.emp_no ORDER BY to_date ASC;
6+
7+
SELECT emp.emp_no, dm.emp_no, first_name, last_name from employees as emp left join dept_manager as dm ON emp.emp_no = dm.emp_no WHERE dm.emp_no is not NULL;
8+
9+
-- Assignment
10+
-- show employees without current title
11+
-- title are in the titles table
12+
-- current titles have a to_date equal to '9999-01-01'
13+
14+
-- select employees, left join with titles, where title is null
15+
-- SELECT * FROM employees as emp LEFT JOIN titles ON emp.emp_no = titles.emp_no WHERE titles.title NOT IN ;
16+
SELECT * FROM employees as emp LEFT JOIN titles ON emp.emp_no = titles.emp_no WHERE titles.to_date != '9999-01-01';
17+
18+
19+
SELECT * from employees as emp where emp_no not in (select emp_no FROM titles where to_date = '9999-01-01');
20+
21+
SELECT * from
22+
employees AS emp
23+
JOIN
24+
titles as t
25+
on emp.emp_no = t.emp_no
26+
where emp.first_name = 'Elvis';

working files/salary-report.sql

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,19 @@
1+
-- Create report showing by year: total, max, min, and average
2+
-- exclude managers
3+
-- managers are dfined in the table dept_manager
4+
-- use NOT IN
5+
6+
use employees;
7+
8+
select DATE_FORMAT(from_date, '%Y') as year FROM salaries;
9+
10+
SELECT DISTINCT(emp_no) from dept_manager;
11+
12+
SELECT DATE_FORMAT(from_date,'%Y') AS year
13+
,SUM(salary) AS total
14+
,MAX(salary) AS max
15+
,MIN(salary) AS min
16+
,AVG(salary) AS average
17+
FROM salaries
18+
WHERE emp_no NOT IN ( SELECT DISTINCT(emp_no) FROM dept_manager)
19+
GROUP BY YEAR;

0 commit comments

Comments
 (0)