# question 1CREATE TABLE `employees` (`emp_no`int(11) NOT NULL,`birth_date`date NOT NULL,`first_name`varchar(14) NOT NULL,`last_name`varchar(16) NOT NULL,`gender`char(1) NOT NULL,`hire_date`date NOT NULL,PRIMARY KEY (`emp_no`));CREATE TABLE `dept_emp` (`emp_no`int(11) NOT NULL,`dept_no`char(4) NOT NULL,`from_date`date NOT NULL,`to_date`date NOT NULL,PRIMARY KEY (`emp_no`,`dept_no`));# Question 2CREATE TABLE `employees` (`emp_no`int(11) NOT NULL,`birth_date`date NOT NULL,`first_name`varchar(14) NOT NULL,`last_name`varchar(16) NOT NULL,`gender`char(1) NOT NULL,`hire_date`date NOT NULL,PRIMARY KEY (`emp_no`));CREATE TABLE `dept_emp` (`emp_no`int(11) NOT NULL,`dept_no`char(4) NOT NULL,`from_date`date NOT NULL,`to_date`date NOT NULL,PRIMARY KEY (`emp_no`,`dept_no`));createtableemp_bonus(emp_no intnot null,received datetimenot null,btype smallintnot null);CREATE TABLE `salaries` (`emp_no`int(11) NOT NULL,`salary`int(11) NOT NULL,`from_date`date NOT NULL,`to_date`date NOT NULL, PRIMARY KEY (`emp_no`,`from_date`));# Question 3CREATE TABLE `salaries` ( `emp_no`int(11) NOT NULL,`salary`int(11) NOT NULL,`from_date`date NOT NULL,`to_date`date NOT NULL,PRIMARY KEY (`emp_no`,`from_date`));#Question 4CREATE TABLE `employees` (`emp_no`int(11) NOT NULL,`birth_date`date NOT NULL,`first_name`varchar(14) NOT NULL,`last_name`varchar(16) NOT NULL,`gender`char(1) NOT NULL,`hire_date`date NOT NULL,PRIMARY KEY (`emp_no`));如,输入为:INSERT INTO employees VALUES(10001,'1953-09-02','Georgi','Facello','M','1986-06-26');INSERT INTO employees VALUES(10002,'1964-06-02','Bezalel','Simmel','F','1985-11-21');INSERT INTO employees VALUES(10005,'1955-01-21','Kyoichi','Maliniak','M','1989-09-12');INSERT INTO employees VALUES(10006,'1953-04-20','Anneke','Preusig','F','1989-06-02');
Questions
使用含有关键字exists查找未分配具体部门的员工的所有信息。
select*from employees as ewherenotexists (select d.emp_nofrom dept_emp as d where d.emp_no = e.emp_no);# withoutusingexists keywordselect*from employees as ewhere e.emp_no notin (select d.emp_nofrom dept_emp as d);
select e.emp_no, e.first_name, e.last_name, b.btype, s.salary, casewhen b.btype=1then s.salary*0.1when b.btype=2then s.salary*0.2else s.salary *0.3end bonusfrom emp_bonus as b left join employees as e on b.emp_no = e.emp_noleft join salaries as s on b.emp_no = s.emp_nowhere s.to_date ="9999-01-01";
select a.first_namefrom employees e left join(select e.first_name, ROW_NUMBER() over (order by e.first_name ) as r_numberfrom employees as e) as aon e.first_name = a.first_namewhere a.r_number % 2=1;
Conclusion
这里回顾了Window Function用法
在window function里面 func(..) over (..) 如果over()里面没有指定partition by进行分组, 那么window function 会对当前的row之前的所有row(包括当前的row) 进行window function的操作。 比如下面 sum salary 里面没有用partition by, 那么它就会第 i 行的running——total的值等于前面 1~ i行的salary的sum
select s.emp_no, s.salary, sum(s.salary) over (order by s.emp_no) as running_totalfrom salaries as swhere s.to_date ="9999-01-01";
Case when的用法回顾
selectcasewhen condition_a then do_acasewhen condition_b then do_b...else default_value end column_alias_namefrom ...;