牛客网SQL刷题记录

it2022-05-05  125

先根据考察的知识点对题目做个整理:

聚集函数: 1.查找最晚入职员工的所有信息

order by(limit): 2.查找入职员工时间排名倒数第三的员工所有信息 6.查找所有员工入职时候的薪水情况 17.获取当前薪水第二多的员工的emp_no以及其对应的薪水salary

内连接(inner join) 3.查找当前薪水详情以及部门编号dept_no 4.查找所有已经分配部门的员工的last_name和first_name 9.获取所有部门当前manager的当前薪水情况,给出dept_no, emp_no以及salary,当前表示 24.获取所有非manager员工当前的薪水情况

左连接(left join) 5.查找所有员工的last_name和first_name以及对应部门编号dept_no 11.获取所有员工当前的manager 19.查找所有员工的last_name和first_name以及对应的dept_name

自连接 21.查找所有员工自入职以来的薪水涨幅情况dept_name

group by 7.查找薪水涨幅超过15次的员工号emp_no以及其对应的涨幅次数t 12.获取所有部门中当前员工薪水最高的相关信息 13.从titles表获取按照title进行分组 14.从titles表获取按照title进行分组,注意对于重复的emp_no进行忽略 15.查找employees表 16.统计出当前各个title类型对应的员工当前薪水对应的平均工资 22.统计各个部门对应员工涨幅的次数总和 26.汇总各个部门当前员工的title类型的分配数目

distinct 8.找出所有员工当前薪水salary情况

子查询 10.获取所有非manager的员工emp_no 18.获取当前薪水第二多的员工的emp_no以及其对应的薪水salary,不准使用order by 20.查找员工编号emp_no为10001其自入职以来的薪水salary涨幅值growth 25.获取员工其当前的薪水比其manager当前薪水还高的相关信息

多个考点(或者说我不会的点。。。) 20.查找员工编号emp_no为10001其自入职以来的薪水salary涨幅值growth 21.查找所有员工自入职以来的薪水涨幅情况dept_name 23.对所有员工的薪水按照salary进行按照1-N的排名 25.获取员工其当前的薪水比其manager当前薪水还高的相关信息 (太难了,我太难了,完全没有思路) 27.给出每个员工每年薪水涨幅超过5000的员工编号emp_no

1.查找最晚入职员工的所有信息

题目描述: CREATE 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)); 代码:

SELECT * FROM employees WHERE hire_date=(SELECT max(hire_date) FROM employees) 2.查找入职员工时间排名倒数第三的员工所有信息

题目描述: CREATE 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)); 代码:

select * from employees order by hire_date desc limit 2,1 /*select * from tableName limit i,n # tableName:表名 # i:为查询结果的索引值(默认从0开始),当i=0时可省略i # n:为查询结果返回的数量 # i与n之间使用英文逗号","隔开 */ /*本题直接按入职时间降序排列,然后取第三个*/ 3.查找当前薪水详情以及部门编号dept_no

题目描述:查找各个部门当前(to_date=‘9999-01-01’)领导当前薪水详情以及其对应部门编号dept_no

CREATE TABLE dept_manager ( dept_no char(4) NOT NULL, emp_no int(11) NOT NULL, from_date date NOT NULL, to_date date NOT NULL, PRIMARY KEY (emp_no,dept_no)); 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));

Remark:我这道题都没看清题目,写起来落这落那的,补了半天条件才通过,被自己菜哭。当初学的数据库和SQL都还老师了o(╥﹏╥)o 代码:

select salaries.emp_no,salaries.salary,salaries.from_date,salaries.to_date,dept_manager.dept_no from salaries inner join dept_manager on salaries.emp_no=dept_manager.emp_no and salaries.to_date='9999-01-01' and dept_manager.to_date='9999-01-01' 4.查找所有已经分配部门的员工的last_name和first_name

题目描述:查找所有已经分配部门的员工的last_name和first_name

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)); CREATE 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));

代码:

select employees.last_name, employees.first_name, dept_emp.dept_no from employees inner join dept_emp on employees.emp_no=dept_emp.emp_no 5.查找所有员工的last_name和first_name以及对应部门编号dept_no

题目描述:查找所有员工的last_name和first_name以及对应部门编号dept_no 查找所有员工的last_name和first_name以及对应部门编号dept_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)); CREATE 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)); 代码:

select a.last_name,a.first_name,b.dept_no from employees a left join dept_emp b #此处不论有没有分配部门的员工都要选出来,所以用左连接 on a.emp_no=b.emp_no

LEFT JOIN 关键字会从左表 (table_name1) 那里返回所有的行,即使在右表 (table_name2) 中没有匹配的行。 具体语法看这里

6.查找所有员工入职时候的薪水情况

题目描述:查找所有员工入职时候的薪水情况,给出emp_no以及salary, 并按照emp_no进行逆序 CREATE 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 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)); Remak:还是没看清题目,写起来丢三落四,被自己菜哭 代码:

select e.emp_no,s.salary from employees e left join salaries s on e.emp_no=s.emp_no and s.from_date=e.hire_date order by e.emp_no desc 7.查找薪水涨幅超过15次的员工号emp_no以及其对应的涨幅次数t

题目描述:查找薪水涨幅超过15次的员工号emp_no以及其对应的涨幅次数t 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));

代码:

select * from( select emp_no,count(*) t from salaries group by emp_no ) where t>15 select emp_no,count(salary) as t from salaries group by emp_no having count(salary)>15

group by语法 GROUP BY 语句用于结合合计函数,根据一个或多个列对结果集进行分组。 这里根据emp_no进行分组并计算列数,也就是工资涨幅的次数,然后把选出的emp_no和t作为新的表被外层的select查询

8.找出所有员工当前薪水salary情况

题目描述:找出所有员工当前(to_date=‘9999-01-01’)具体的薪水salary情况,对于相同的薪水只显示一次,并按照逆序显示 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)); 代码:

select distinct salary from salaries where to_date='9999-01-01' order by salary desc 9.获取所有部门当前manager的当前薪水情况,给出dept_no, emp_no以及salary,当前表示to_date='9999-01-01'

题目描述: CREATE TABLE dept_manager ( dept_no char(4) NOT NULL, emp_no int(11) NOT NULL, from_date date NOT NULL, to_date date NOT NULL, PRIMARY KEY (emp_no,dept_no)); 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));

代码:

select d.dept_no,d.emp_no,s.salary from dept_manager as d inner join salaries as s on d.emp_no=s.emp_no and d.to_date='9999-01-01' and s.to_date='9999-01-01' 10.获取所有非manager的员工emp_no

题目描述:获取所有非manager的员工emp_no CREATE TABLE dept_manager ( dept_no char(4) NOT NULL, emp_no int(11) NOT NULL, from_date date NOT NULL, to_date date NOT NULL, PRIMARY KEY (emp_no,dept_no)); CREATE 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));

代码:

select e.emp_no from employees e left join dept_manager d on e.emp_no=d.emp_no where d.emp_no is null select emp_no from employees where emp_no not in (select emp_no from dept_manager)

Remark:最后的条件要用‘where’不能用‘and’。。。

11.获取所有员工当前的manager

题目描述:获取所有员工当前的manager,如果当前的manager是自己的话结果不显示,当前表示to_date=‘9999-01-01’。 结果第一列给出当前员工的emp_no,第二列给出其manager对应的manager_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)); CREATE TABLE dept_manager ( dept_no char(4) NOT NULL, emp_no int(11) NOT NULL, from_date date NOT NULL, to_date date NOT NULL, PRIMARY KEY (emp_no,dept_no)); 代码:

select a.emp_no,b.emp_no manager_no from dept_emp a inner join dept_manager b on a.dept_no=b.dept_no where a.to_date='9999-01-01' and b.to_date='9999-01-01' and a.emp_no<>b.emp_no 12.获取所有部门中当前员工薪水最高的相关信息

题目描述:获取所有部门中当前员工薪水最高的相关信息,给出dept_no, emp_no以及其对应的salary 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)); 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)); 代码:

select d.dept_no,d.emp_no,max(s.salary) salary from dept_emp d inner join salaries s on d.emp_no=s.emp_no where d.to_date='9999-01-01' and s.to_date='9999-01-01' group by d.dept_no 13.从titles表获取按照title进行分组

题目描述:从titles表获取按照title进行分组,每组个数大于等于2,给出title以及对应的数目t。 CREATE TABLE IF NOT EXISTS “titles” ( emp_no int(11) NOT NULL, title varchar(50) NOT NULL, from_date date NOT NULL, to_date date DEFAULT NULL);

代码:

select title,count(*) t from titles group by title 14.从titles表获取按照title进行分组,注意对于重复的emp_no进行忽略

题目描述:从titles表获取按照title进行分组,每组个数大于等于2,给出title以及对应的数目t。 注意对于重复的emp_no进行忽略。 CREATE TABLE IF NOT EXISTS titles ( emp_no int(11) NOT NULL, title varchar(50) NOT NULL, from_date date NOT NULL, to_date date DEFAULT NULL);

代码:

select title,count(distinct emp_no) t from titles group by title having t>=2; 15.查找employees表

题目描述:查找employees表所有emp_no为奇数,且last_name不为Mary的员工信息,并按照hire_date逆序排列 CREATE 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)); 代码:

select * from employees where emp_no%2<>0 and last_name <> 'Mary' order by hire_date desc 16.统计出当前各个title类型对应的员工当前薪水对应的平均工资

题目描述:统计出当前各个title类型对应的员工当前(to_date=‘9999-01-01’)薪水对应的平均工资。结果给出title以及平均工资avg。 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)); CREATE TABLE IF NOT EXISTS “titles” ( emp_no int(11) NOT NULL, title varchar(50) NOT NULL, from_date date NOT NULL, to_date date DEFAULT NULL);

代码:

select t.title,avg(s.salary) as avg from titles t inner join salaries s on t.emp_no=s.emp_no where t.to_date='9999-01-01' and s.to_date='9999-01-01' group by title 17.获取当前薪水第二多的员工的emp_no以及其对应的薪水salary

题目描述:获取当前(to_date=‘9999-01-01’)薪水第二多的员工的emp_no以及其对应的薪水salary 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)); 代码:

select emp_no,salary from salaries where to_date='9999-01-01' order by salary desc limit 1,1 select emp_no,max(salary) as salary from salaries where to_date='9999-01-01' and salary not in (select max(salary) from salaries); 18.获取当前薪水第二多的员工的emp_no以及其对应的薪水salary,不准使用order by

题目描述:查找当前薪水(to_date=‘9999-01-01’)排名第二多的员工编号emp_no、薪水salary、last_name以及first_name,不准使用order by CREATE 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 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)); 代码:

select e.emp_no,max(s.salary) salary,e.last_name,e.first_name from employees e inner join salaries s on e.emp_no=s.emp_no where s.to_date='9999-01-01' and s.salary not in (select max(salary) from salaries where to_date='9999-01-01') 19.查找所有员工的last_name和first_name以及对应的dept_name

题目描述:查找所有员工的last_name和first_name以及对应的dept_name,也包括暂时没有分配部门的员工 CREATE TABLE departments ( dept_no char(4) NOT NULL, dept_name varchar(40) NOT NULL, PRIMARY KEY (dept_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)); CREATE 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));

代码:

select e.last_name,e.first_name,dp.dept_name from employees e left join dept_emp d on e.emp_no=d.emp_no left join departments dp on d.dept_no=dp.dept_no 20.查找员工编号emp_no为10001其自入职以来的薪水salary涨幅值growth

题目描述:查找员工编号emp_no为10001其自入职以来的薪水salary涨幅值growth 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)); 思路: 1、先分别找到emp_no=10001的员工的第一次工资记录与最后一次工资记录 2、再将最后一次工资记录减去第一次工资记录得到入职以来salary的涨幅,最后用别名growth代替 代码:

select ( (select salary from salaries where emp_no=10001 order by to_date desc limit 1)- (select salary from salaries where emp_no=10001 order by to_date limit 1) ) as growth 21.查找所有员工自入职以来的薪水涨幅情况

题目描述:查找所有员工自入职以来的薪水涨幅情况,给出员工编号emp_no以及其对应的薪水涨幅growth,并按照growth进行升序 CREATE 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 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)); 代码:

select e.emp_no,(a.salary - b.salary) growth from employees e inner join salaries a on e.emp_no=a.emp_no and a.to_date='9999-01-01' inner join salaries b on e.emp_no=b.emp_no and b.from_date=e.hire_date order by growth asc 22.统计各个部门对应员工涨幅的次数总和

题目描述:统计各个部门对应员工涨幅的次数总和,给出部门编码dept_no、部门名称dept_name以及次数sum CREATE TABLE departments ( dept_no char(4) NOT NULL, dept_name varchar(40) NOT NULL, PRIMARY KEY (dept_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)); 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)); 代码:

select d.dept_no,dp.dept_name,count(*) sum from dept_emp d inner join salaries s on d.emp_no=s.emp_no inner join departments dp on dp.dept_no=d.dept_no group by d.dept_no 23.对所有员工的薪水按照salary进行按照1-N的排名

题目描述:对所有员工的当前(to_date=‘9999-01-01’)薪水按照salary进行按照1-N的排名,相同salary并列且按照emp_no升序排列 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)); 代码:

select s1.emp_no,s1.salary,count(distinct s2.salary) rank from salaries s1,salaries s2 where s1.to_date='9999-01-01' and s2.to_date='9999-01-01' and s1.salary<=s2.salary group by s1.emp_no order by rank asc 24.获取所有非manager员工当前的薪水情况

题目描述:获取所有非manager员工当前的薪水情况,给出dept_no、emp_no以及salary ,当前表示to_date=‘9999-01-01’ 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)); CREATE TABLE dept_manager ( dept_no char(4) NOT NULL, emp_no int(11) NOT NULL, from_date date NOT NULL, to_date date NOT NULL, PRIMARY KEY (emp_no,dept_no)); CREATE 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 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)); 代码:

select d.dept_no,d.emp_no,s.salary from dept_emp d,salaries s where d.emp_no=s.emp_no and s.to_date='9999-01-01' and d.to_date='9999-01-01' and d.emp_no not in (select emp_no from dept_manager) 25.获取员工其当前的薪水比其manager当前薪水还高的相关信息

题目描述:题目描述 获取员工其当前的薪水比其manager当前薪水还高的相关信息,当前表示to_date=‘9999-01-01’, 结果第一列给出员工的emp_no, 第二列给出其manager的manager_no, 第三列给出该员工当前的薪水emp_salary, 第四列给该员工对应的manager当前的薪水manager_salary 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)); CREATE TABLE dept_manager ( dept_no char(4) NOT NULL, emp_no int(11) NOT NULL, from_date date NOT NULL, to_date date NOT NULL, PRIMARY KEY (emp_no,dept_no)); 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));

代码:

select se.emp_no emp_no,sm.emp_no as manager_no, se.salary as emp_salary,sm.salary as manager_salary from ( select d.emp_no,d.dept_no,s.salary from dept_emp d inner join salaries s on d.emp_no=s.emp_no where s.to_date='9999-01-01' )as se inner join ( select dp.emp_no,dp.dept_no,s.salary from dept_manager dp inner join salaries s on dp.emp_no=s.emp_no where s.to_date='9999-01-01' ) as sm on se.dept_no=sm.dept_no where se.salary>sm.salary 26.汇总各个部门当前员工的title类型的分配数目

题目描述:汇总各个部门当前员工的title类型的分配数目,结果给出部门编号dept_no、dept_name、其当前员工所有的title以及该类型title对应的数目count CREATE TABLE departments ( dept_no char(4) NOT NULL, dept_name varchar(40) NOT NULL, PRIMARY KEY (dept_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)); CREATE TABLE IF NOT EXISTS titles ( emp_no int(11) NOT NULL, title varchar(50) NOT NULL, from_date date NOT NULL, to_date date DEFAULT NULL);

代码:

select d.dept_no,dp.dept_name,t.title,count(t.emp_no) as count from dept_emp d inner join departments dp on d.dept_no=dp.dept_no inner join titles t on d.emp_no=t.emp_no and d.to_date='9999-01-01' and t.to_date='9999-01-01' group by d.dept_no,t.title 27.给出每个员工每年薪水涨幅超过5000的员工编号emp_no

题目描述:给出每个员工每年薪水涨幅超过5000的员工编号emp_no、薪水变更开始日期from_date以及薪水涨幅值salary_growth,并按照salary_growth逆序排列。 提示:在sqlite中获取datetime时间对应的年份函数为strftime(’%Y’, to_date) 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));

代码:

select b.emp_no,b.from_date,b.salary-a.salary as salary_growth from salaries a,salaries b where a.emp_no=b.emp_no and b.salary-a.salary>5000 and strftime('%Y',b.to_date)-strftime('%Y',a.to_date)=1 order by salary_growth desc;

28.查找描述信息中包括robot的电影对应的分类名称以及电影数目,而且还需要该分类对应电影 题目描述: CREATE TABLE IF NOT EXISTS film ( film_id smallint(5) NOT NULL DEFAULT ‘0’, title varchar(255) NOT NULL, description text, PRIMARY KEY (film_id)); CREATE TABLE category ( category_id tinyint(3) NOT NULL , name varchar(25) NOT NULL, last_update timestamp, PRIMARY KEY ( category_id )); CREATE TABLE film_category ( film_id smallint(5) NOT NULL, category_id tinyint(3) NOT NULL, last_update timestamp);

查找描述信息中包括robot的电影对应的分类名称以及电影数目,而且还需要该分类对应电影数量>=5部

代码:

select c.name,count(f.film_id) from ( select category_id,count(film_id) from film_category group by category_id having count(film_id)>=5 ) as cc, film f,film_category fc,category c where f.description like '%robot%' and f.film_id=fc.film_id and fc.category_id=c.category_id and c.category_id=cc.category_id

29.使用join查询方式找出没有分类的电影id以及名称 题目描述: 使用join查询方式找出没有分类的电影id以及名称 代码:

select f.film_id,f.title from film f left join film_category fc on f.film_id=fc.film_id where fc.category_id is null

30.使用子查询的方式找出属于Action分类的所有电影对应的title,description 题目描述: 使用子查询的方式找出属于Action分类的所有电影对应的title,description 代码:

select f.title,f.description from film f where f.film_id in ( select fc.film_id from film_category fc where fc.category_id in ( select c.category_id from category c where c.name='Action' ) );

31.获取select * from employees对应的执行计划 题目描述:获取select * from employees对应的执行计划 代码:

explain select * from employees;

explain的用法

32.将employees表的所有员工的last_name和first_name拼接起来作为Name 题目描述:将employees表的所有员工的last_name和first_name拼接起来作为Name,中间以一个空格区分 CREATE 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)); 代码:

select last_name ||' '|| first_name as Name from employees

Remark:Mysql 数据库可以使用CONCAT或者CONCAT_WS两种函数进行拼接,但是SQLLite得使用||进行拼接

33.创建一个actor表,包含如下列信息 题目描述: 代码:

create table actor ( actor_id smallint(5) not null primary key, first_name varchar(45) not null, last_name varchar(45) not null, last_update timestamp not null default (datetime('now','localtime')) );

34.批量插入数据 题目描述: 代码:

insert into actor values ( 1,'PENELOPE','GUINESS','2006-02-15 12:34:33' ), ( 2,'NICK','WAHLBERG','2006-02-15 12:34:33' ) ;

35.批量插入数据,不使用replace操作 题目描述: 代码:

insert or ignore into actor values ( '3','ED','CHASE','2006-02-15 12:34:33' );

Remark:

36.创建一个actor_name表 题目描述: 代码:

create table actor_name ( first_name varchar(45) not null, last_name varchar(45) not null ); insert into actor_name select first_name,last_name from actor;

37.对first_name创建唯一索引uniq_idx_firstname 题目描述: 代码:

create unique index uniq_idx_firstname on actor(first_name); create index idx_lastname on actor(last_name);

38.针对actor表创建视图actor_name_view 题目描述: 代码:

create view actor_name_view as select first_name as first_name_v,last_name as last_name_v from actor

39.针对上面的salaries表emp_no字段创建索引idx_emp_no 题目描述: 代码:

select * from salaries indexed by idx_emp_no where emp_no='10005'

40.在last_update后面新增加一列名字为create_date 题目描述: 存在actor表,包含如下列信息: CREATE TABLE IF NOT EXISTS actor ( actor_id smallint(5) NOT NULL PRIMARY KEY, first_name varchar(45) NOT NULL, last_name varchar(45) NOT NULL, last_update timestamp NOT NULL DEFAULT (datetime(‘now’,‘localtime’))); 现在在last_update后面新增加一列名字为create_date, 类型为datetime, NOT NULL,默认值为’0000 00:00:00’ 代码:

alter table actor add column create_date datetime NOT NULL default '0000-00-00 00:00:00'

41.构造一个触发器audit_log 题目描述: 构造一个触发器audit_log,在向employees_test表中插入一条数据的时候,触发插入相关的数据到audit中。 CREATE TABLE employees_test( ID INT PRIMARY KEY NOT NULL, NAME TEXT NOT NULL, AGE INT NOT NULL, ADDRESS CHAR(50), SALARY REAL ); CREATE TABLE audit( EMP_no INT NOT NULL, NAME TEXT NOT NULL ); 代码:

在这里插入代码片

42.删除emp_no重复的记录,只保留最小的id对应的记录。 题目描述: 删除emp_no重复的记录,只保留最小的id对应的记录。 CREATE TABLE IF NOT EXISTS titles_test ( id int(11) not null primary key, emp_no int(11) NOT NULL, title varchar(50) NOT NULL, from_date date NOT NULL, to_date date DEFAULT NULL);

insert into titles_test values (‘1’, ‘10001’, ‘Senior Engineer’, ‘1986-06-26’, ‘9999-01-01’), (‘2’, ‘10002’, ‘Staff’, ‘1996-08-03’, ‘9999-01-01’), (‘3’, ‘10003’, ‘Senior Engineer’, ‘1995-12-03’, ‘9999-01-01’), (‘4’, ‘10004’, ‘Senior Engineer’, ‘1995-12-03’, ‘9999-01-01’), (‘5’, ‘10001’, ‘Senior Engineer’, ‘1986-06-26’, ‘9999-01-01’), (‘6’, ‘10002’, ‘Staff’, ‘1996-08-03’, ‘9999-01-01’), (‘7’, ‘10003’, ‘Senior Engineer’, ‘1995-12-03’, ‘9999-01-01’);

代码:

delete from titles_test where id not in (select min(id) from titles_test group by emp_no);

43.将所有to_date为9999-01-01的全部更新为NULL 题目描述: 将所有to_date为9999-01-01的全部更新为NULL,且 from_date更新为2001-01-01。 CREATE TABLE IF NOT EXISTS titles_test ( id int(11) not null primary key, emp_no int(11) NOT NULL, title varchar(50) NOT NULL, from_date date NOT NULL, to_date date DEFAULT NULL);

insert into titles_test values (‘1’, ‘10001’, ‘Senior Engineer’, ‘1986-06-26’, ‘9999-01-01’), (‘2’, ‘10002’, ‘Staff’, ‘1996-08-03’, ‘9999-01-01’), (‘3’, ‘10003’, ‘Senior Engineer’, ‘1995-12-03’, ‘9999-01-01’), (‘4’, ‘10004’, ‘Senior Engineer’, ‘1995-12-03’, ‘9999-01-01’), (‘5’, ‘10001’, ‘Senior Engineer’, ‘1986-06-26’, ‘9999-01-01’), (‘6’, ‘10002’, ‘Staff’, ‘1996-08-03’, ‘9999-01-01’), (‘7’, ‘10003’, ‘Senior Engineer’, ‘1995-12-03’, ‘9999-01-01’); 代码:

update titles_test set to_date=null,from_date='2001-01-01' where to_date='9999-01-01';

44.将id=5以及emp_no=10001的行数据替换成id=5以及emp_no=10005 题目描述: 将id=5以及emp_no=10001的行数据替换成id=5以及emp_no=10005,其他数据保持不变,使用replace实现。 CREATE TABLE IF NOT EXISTS titles_test ( id int(11) not null primary key, emp_no int(11) NOT NULL, title varchar(50) NOT NULL, from_date date NOT NULL, to_date date DEFAULT NULL);

insert into titles_test values (‘1’, ‘10001’, ‘Senior Engineer’, ‘1986-06-26’, ‘9999-01-01’), (‘2’, ‘10002’, ‘Staff’, ‘1996-08-03’, ‘9999-01-01’), (‘3’, ‘10003’, ‘Senior Engineer’, ‘1995-12-03’, ‘9999-01-01’), (‘4’, ‘10004’, ‘Senior Engineer’, ‘1995-12-03’, ‘9999-01-01’), (‘5’, ‘10001’, ‘Senior Engineer’, ‘1986-06-26’, ‘9999-01-01’), (‘6’, ‘10002’, ‘Staff’, ‘1996-08-03’, ‘9999-01-01’), (‘7’, ‘10003’, ‘Senior Engineer’, ‘1995-12-03’, ‘9999-01-01’); 代码:

replace into titles_test values(5,10005,'Senior Engineer', '1986-06-26', '9999-01-01');

45.将titles_test表名修改为titles_2017 题目描述: 将titles_test表名修改为titles_2017。 CREATE TABLE IF NOT EXISTS titles_test ( id int(11) not null primary key, emp_no int(11) NOT NULL, title varchar(50) NOT NULL, from_date date NOT NULL, to_date date DEFAULT NULL);

insert into titles_test values (‘1’, ‘10001’, ‘Senior Engineer’, ‘1986-06-26’, ‘9999-01-01’), (‘2’, ‘10002’, ‘Staff’, ‘1996-08-03’, ‘9999-01-01’), (‘3’, ‘10003’, ‘Senior Engineer’, ‘1995-12-03’, ‘9999-01-01’), (‘4’, ‘10004’, ‘Senior Engineer’, ‘1995-12-03’, ‘9999-01-01’), (‘5’, ‘10001’, ‘Senior Engineer’, ‘1986-06-26’, ‘9999-01-01’), (‘6’, ‘10002’, ‘Staff’, ‘1996-08-03’, ‘9999-01-01’), (‘7’, ‘10003’, ‘Senior Engineer’, ‘1995-12-03’, ‘9999-01-01’); 代码:

alter table titles_test rename to titles_2017;

46.在audit表上创建外键约束,其emp_no对应employees_test表的主键id 题目描述: 在audit表上创建外键约束,其emp_no对应employees_test表的主键id。 CREATE TABLE employees_test( ID INT PRIMARY KEY NOT NULL, NAME TEXT NOT NULL, AGE INT NOT NULL, ADDRESS CHAR(50), SALARY REAL );

CREATE TABLE audit( EMP_no INT NOT NULL, create_date datetime NOT NULL ); 代码:

drop table audit; create table audit( emp_no int not null, create_date datetime not null, foreign key(emp_no) references employees_test(ID));

47.如何获取emp_v和employees有相同的数据no 题目描述: 存在如下的视图: create view emp_v as select * from employees where emp_no >10005; 如何获取emp_v和employees有相同的数据? CREATE 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)); 输出格式: 代码:

select * from emp_v

48.将所有获取奖金的员工当前的薪水增加10% 题目描述:将所有获取奖金的员工当前的薪水增加10%。 create table emp_bonus( emp_no int not null, recevied datetime not null, btype smallint not 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)); 代码:

update salaries set salary=salary*1.1 where emp_no in (select emp_no from emp_bonus);

49.针对库中的所有表生成select count(*)对应的SQL语句 题目描述: 题目描述 针对库中的所有表生成select count(*)对应的SQL语句 CREATE 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 emp_bonus( emp_no int not null, recevied datetime not null, btype smallint not null); 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)); CREATE TABLE dept_manager ( dept_no char(4) NOT NULL, emp_no int(11) NOT NULL, from_date date NOT NULL, to_date date NOT NULL, PRIMARY KEY (emp_no,dept_no)); 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)); 输出格式: 代码:

select "select count(*) from "|| name ||";" as cnts from sqlite_master where type='table'

50.将employees表中的所有员工的last_name和first_name通过(’)连接起来。 题目描述: 将employees表中的所有员工的last_name和first_name通过(’)连接起来。 CREATE 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)); 输出格式: 代码:

select last_name ||"'"|| first_name as name from employees;

51.查找字符串’10,A,B’ 中逗号’,'出现的次数cnt 题目描述:查找字符串’10,A,B’ 中逗号’,'出现的次数cnt。 代码:

select length('10,A,B')-length(replace('10,A,B',',','')) as cnt;

52.获取Employees中的first_name 题目描述: 获取Employees中的first_name,查询按照first_name最后两个字母,按照升序进行排列 CREATE 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)); 输出格式: 代码:

select first_name from employees order by substr(first_name,length(first_name)-1);

substr的用法

53.按照dept_no进行汇总 题目描述:按照dept_no进行汇总,属于同一个部门的emp_no按照逗号进行连接,结果给出dept_no以及连接出的结果employees 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)); 输出格式: 代码:

select dept_no,group_concat(emp_no) as employees from dept_emp group by dept_no;

54.查找排除当前最大、最小salary之后的员工的平均工资avg_salary 题目描述:查找排除当前最大、最小salary之后的员工的平均工资avg_salary。 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)); 输出格式: 代码:

select avg(salary) as avg_salary from salaries where salary <> (select max(salary) from salaries) and salary<>(select min(salary) from salaries) and to_date='9999-01-01';

55.分页查询employees表,每5行一页,返回第2页的数据 题目描述:分页查询employees表,每5行一页,返回第2页的数据 CREATE 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)); 代码:

select * from employees limit 5,5;

56.获取所有员工的emp_no 题目描述: 获取所有员工的emp_no、部门编号dept_no以及对应的bonus类型btype和received ,没有分配具体的员工不显示 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)); CREATE TABLE dept_manager ( dept_no char(4) NOT NULL, emp_no int(11) NOT NULL, from_date date NOT NULL, to_date date NOT NULL, PRIMARY KEY (emp_no,dept_no)); CREATE 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 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)); create table emp_bonus( emp_no int not null, recevied datetime not null, btype smallint not null); 输出格式: 代码:

select e.emp_no,d.dept_no,eb.btype,eb.recevied from employees as e inner join dept_emp as d on e.emp_no=d.emp_no left join emp_bonus as eb on d.emp_no=eb.emp_no;

57.使用含有关键字exists查找未分配具体部门的员工的所有信息。 题目描述:使用含有关键字exists查找未分配具体部门的员工的所有信息。 CREATE 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)); 输出格式: 代码:

select * from employees where not exists (select emp_no from dept_emp where emp_no=employees.emp_no);

58.获取employees中的行数据,且这些行也存在于emp_v中 题目描述:存在如下的视图: create view emp_v as select * from employees where emp_no >10005; CREATE 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)); 获取employees中的行数据,且这些行也存在于emp_v中。注意不能使用intersect关键字。 输出格式: 代码:

select * from employees where emp_no >10005;

59.获取有奖金的员工相关信息。 题目描述: 获取有奖金的员工相关信息。 CREATE 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)); create table emp_bonus( emp_no int not null, recevied datetime not null, btype smallint not 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)); 给出emp_no、first_name、last_name、奖金类型btype、对应的当前薪水情况salary以及奖金金额bonus。 bonus类型btype为1其奖金为薪水salary的10%,btype为2其奖金为薪水的20%,其他类型均为薪水的30%。 当前薪水表示to_date=‘9999-01-01’ 输出格式: 代码:

select e.emp_no,e.first_name,e.last_name,eb.btype,s.salary, (case eb.btype when 1 then s.salary*0.1 when 2 then s.salary*0.2 else s.salary*0.3 end) as bonus from employees e inner join emp_bonus eb on e.emp_no=eb.emp_no inner join salaries s on s.emp_no=e.emp_no and s.to_date='9999-01-01';

60.统计salary的累计和running_total 题目描述:题目描述 按照salary的累计和running_total,其中running_total为前两个员工的salary累计和,其他以此类推。 具体结果如下Demo展示。。 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)); 输出格式: 代码:

select s1.emp_no,s1.salary, (select sum(s2.salary) from salaries s2 where s2.emp_no<=s1.emp_no and s2.to_date='9999-01-01') as running_total from salaries s1 where s1.to_date='9999-01-01' order by s1.emp_no;

61.对于employees表中,给出奇数行的first_name 题目描述:题目描述 对于employees表中,给出奇数行的first_name CREATE 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)); 输出格式: 代码:

select e1.first_name from employees e1 where (select count(*) from employees e2 where e2.first_name<=e1.first_name)%2=1

最新回复(0)