1.
select * from employees order by hire_date desc limit 1;
2.
select * from employees order by hire_date desc limit 2,1;
3.
select a.*,b.dept_no from salaries as a inner join dept_manager as b on a.emp_no=b.emp_no where a.to_date='9999-01-01' and
b.to_date='9999-01-01' order by emp_no;
//此题牛客代码编辑器一直报错,不明原因
4.
select b.last_name,b.first_name,a.dept_no from dept_emp as a inner join employees as b on a.emp_no=b.emp_no;
5.
select b.last_name,b.first_name,a.dept_no from employees as b left join dept_emp as a on a.emp_no=b.emp_no;
6.
select a.emp_no,b.salary from employees as a inner join salaries as b
on a.emp_no=b.emp_no and a.hire_date=b.from_date
order by a.emp_no desc;
// sql 语句其实有很多写法 ,可以只针对 b 分组查询。我倾向使用join.
SELECT
emp_no,
salary
FROM
salaries
GROUP BY
emp_no
HAVING
min( from_date )
ORDER BY
emp_no DESC
7.
select emp_no,count(*) as t from salaries group by emp_no having t>15;
8.
select distinct salary from salaries where to_date='9999-01-01' order by salary desc
9.
select b.dept_no,b.emp_no,a.salary from salaries as a left join dept_manager as b on a.emp_no=b.emp_no
where a.to_date='9999-01-01' and b.to_date='9999-01-01';
10.
select a.emp_no from employees as a left join dept_manager as b on a.emp_no=b.emp_no where b.emp_no is null;
11.
select a.emp_no,b.emp_no as manager_no from dept_emp as a left join dept_manager as b
on b.dept_no=a.dept_no where a.emp_no!=b.emp_no and b.to_date='9999-01-01';
// 题目限制条件太多, 容易看漏
12.
不会!!
MySQL 报错的答案能通过所有测试用例 , 服气!
13.
select title,count(*) as t from titles group by title having t >= 2;
14.
select title,count(*) as t from
(select distinct emp_no,title from titles)
group by title having t >= 2;
15.
select * from employees where (emp_no%2)==1 and last_name!='Mary' order by hire_date desc;
16.
select a.title, avg(b.salary) as avg
from salaries as b inner join titles as a
on a.emp_no=b.emp_no
where b.to_date='9999-01-01' and a.to_date='9999-01-01'
group by a.title;
17.
select emp_no,salary from salaries where to_date='9999-01-01' order by salary desc limit 1,1
18.
select a.emp_no, b.salary, a.last_name, a.first_name
from employees as a inner join salaries as b on a.emp_no=b.emp_no
where b.to_date='9999-01-01'
and b.salary = (select max(s.salary) from salaries as s where s.salary < (select max(salary) from salaries) )
19.
select a.last_name,a.first_name,b.dept_name
from employees as a left join
(select c.emp_no,c.dept_no,d.dept_name from dept_emp as c left join departments as d
on c.dept_no=d.dept_no) as b
on a.emp_no=b.emp_no;
20.
select Max(salary)-Min(salary) as growth from salaries where emp_no=10001 group by emp_no
SQL 实战 牛客网 20/61题
©著作权归作者所有,转载或内容合作请联系作者
平台声明:文章内容(如有图片或视频亦包括在内)由作者上传并发布,文章内容仅代表作者本人观点,简书系信息发布平台,仅提供信息存储服务。
平台声明:文章内容(如有图片或视频亦包括在内)由作者上传并发布,文章内容仅代表作者本人观点,简书系信息发布平台,仅提供信息存储服务。
推荐阅读更多精彩内容
- 57.使用含有关键字exists查找未分配具体部门的员工的所有信息 58.获取employees中的行数据,且这些...
- 25.获取员工其当前的薪水比其manager当前薪水还高的相关信息 题目描述:获取员工其当前的薪水比其manage...
- 33.创建一个actor表,包含如下信息 34.批量插入数据 35.批量插入数据,不使用replace操作 36....
- 41.构造一个触发器audit_log 42.删除emp_no重复的记录,只保留最小的id对应的记录 43.将所有...