November 18 - ndgriffeth/Class-Notes-and-Lectures GitHub Wiki
What is the name of the department whose id is "d002"?
select dept_name from departments where dept_no="d002";
What employees were hired on June 22, 1988?
select * from employees where hire_date=str_to_date("June 22, 1988","%M %d, %Y");
or
select * from employees where hire_date='1988-06-22';
86 rows
What employees were hired before June 22, 1988?
select * from employees where hire_date<str_to_date("June 22, 1988","%M %d, %Y");
120106 rows
What employees work or have worked in the Finance department?
select first_name, last_name from employees, dept_emp, departments where employees.emp_no=dept_emp.emp_no and departments.dept_no=dept_emp.dept_no and dept_name="Finance";
or
select first_name, last_name from employees join dept_emp using (emp_no) join departments using (dept_no) where dept_name="Finance";
17346 rows
What employees currently work in the Finance department?
select first_name, last_name from employees, dept_emp, departments where employees.emp_no=dept_emp.emp_no and departments.dept_no=dept_emp.dept_no and dept_name="Finance" and to_date>curdate();
or
select first_name, last_name from employees join dept_emp using (emp_no) join departments using (dept_no) where dept_name="Finance" and to_date>curdate();
12437 rows
Who is the current manager of Mizuhito Kemmerer?
select e.first_name, e.last_name, de.to_date, dm.to_date, m.first_name, m.last_name from employees e, employees m, dept_emp de, dept_manager dm where e.emp_no=de.emp_no and m.emp_no=dm.emp_no and dm.dept_no=de.emp_no and e.first_name="Mizuhito" and e.last_name="Kemmerer" and de.to_date>curdate() and dm.to_date>curdate();
empty!!
It could be that he's not a current employee, or it could be that he doesn't have a manager.
List employees hired before Falck Mohua (should have been Mohua Falck).
select e2.* from employees e1, employees e2 where e1.first_name="Mohua" and e1.last_name="Falck" and e1.hire_date>e2.hire_date;
But there are two Mohua Falck's. You can pick one, or try this:
select * from employees where hire_date>ALL(select hire_date from employees where first_name="Mohua" and last_name="Falck");
What employees work in the same department as Odysseas Ressouche?
Currently or ever?
If we ignore time:
select e2.first_name, e2.last_name from employees e1, employees e2, dept_emp de1, dept_emp de2 where e1.emp_no=de1.emp_no and e2.emp_no=de2.emp_no and de1.dept_no=de2.dept_no and e1.first_name="Odysseas" and e1.last_name="Ressouche";
or
select e2.first_name, e2.last_name from employees e1 join dept_emp de1 using (emp_no) join (employees e2 join dept_emp de2 using (emp_no)) using (dept_no) where e1.first_name="Odysseas" and e1.last_name="Ressouche";
But if we require them to be working together currently:
select e2.first_name, e2.last_name from employees e1, employees e2, dept_emp de1, dept_emp de2 where e1.emp_no=de1.emp_no and e2.emp_no=de2.emp_no and de1.dept_no=de2.dept_no and e1.first_name="Odysseas" and e1.last_name="Ressouche" and de1.to_date>curdate() and de2.to_date>curdate();
The answer is no one because Odysseas doesn't work here any more!!!
What managers manage an employee making more than 100,000?
Question: Should this be "currently manage", or should it be "have ever managed"?
This is the "have ever managed version:
select distinct m.first_name, m.last_name from employees m, dept_manager dm, employees e, dept_emp de, salaries s where m.emp_no=dm.emp_no and e.emp_no=de.emp_no and e.emp_no=s.emp_no and de.dept_no=dm.dept_no and s.salary > 100000;
or
select distinct m.first_name, m.last_name from (employees m join dept_manager dm using (emp_no)) join ((employees e join dept_emp de using (emp_no)) join salaries s using (emp_no)) using (dept_no) where s.salary > 100000;
228,382 rows without the distinct 24 rows with it
This is the currently manage version:
select distinct m.first_name, m.last_name from employees m, dept_manager dm, employees e, dept_emp de, salaries s where m.emp_no=dm.emp_no and e.emp_no=de.emp_no and e.emp_no=s.emp_no and de.dept_no=dm.dept_no and dm.to_date>curdate() and de.to_date>curdate() and s.to_date>curdate() and s.salary > 100000;
or
select distinct m.first_name, m.last_name from (employees m join dept_manager dm using (emp_no)) join ((employees e join dept_emp de using (emp_no)) join salaries s using (emp_no)) using (dept_no) where dm.to_date>curdate() and de.to_date>curdate() and s.to_date>curdate() and s.salary > 100000;
9 rows
Who is currently managing a department with Engineers in it?
Answer:
select m.first_name, m.last_name from employees m, dept_manager dm, dept_emp de, employees e, titles where m.emp_no=dm.emp_no and dm.dept_no=de.dept_no and de.emp_no=e.emp_no and titles.to_date>curdate() and dm.to_date>curdate() and de.to_date>curdate() and titles.title like "%engineer%";
or
select distinct m.first_name, m.last_name from (employees m join dept_manager dm using (emp_no)) join (dept_emp de join employees e using (emp_no) join titles using (emp_no)) using (dept_no) where titles.to_date>curdate() and dm.to_date>curdate() and de.to_date>curdate() and titles.title like "%engineer%";