一些比较难的SQL语句查询
2017-10-15 16:52
253 查看
1查询各科成绩都高于90分的人。
有一张表:
stu_course:
mysql>select t1.stu_id,t1.no from (select stu_id,count(*) no from stu_course group by stu_id)t1 inner join (select stu_id,count(*) no from stu_course where score >=90 group by stu_id)t2
on t1.stu_id = t2.stu_id and t1.no = t2.no;
mysql> select distinct tb_1.stu_id,
tb_1.name from stu_course tb_1where (select min(tb_2.score) from stu_course tb_2 where tb_2.stu_id =tb_1.stu_id)>=90;
2查询每个部门薪水最高的人
有两张表:
employees
departments
mysql> select *from employees e where(e.department_id,e.salary)
in
(select e1.department_id,max(e1.salary) from employees e1 group by department_id) ;
mysql> select *from employees e where
e.salary =(select max(e1.salary) from employees e1 where e1.department_id = e.department_id) ;
3查询每个部门薪水最高的前二人。
使用的表就是题2的表
mysql> select * from employees e1 where (select count(*) from employees e2 where e2.department_id=e1.department_id and e2.salary > e1.salary)<2 order by department_id;
有一张表:
stu_course:
mysql>select t1.stu_id,t1.no from (select stu_id,count(*) no from stu_course group by stu_id)t1 inner join (select stu_id,count(*) no from stu_course where score >=90 group by stu_id)t2
on t1.stu_id = t2.stu_id and t1.no = t2.no;
mysql> select distinct tb_1.stu_id,
tb_1.name from stu_course tb_1where (select min(tb_2.score) from stu_course tb_2 where tb_2.stu_id =tb_1.stu_id)>=90;
2查询每个部门薪水最高的人
有两张表:
employees
departments
mysql> select *from employees e where(e.department_id,e.salary)
in
(select e1.department_id,max(e1.salary) from employees e1 group by department_id) ;
mysql> select *from employees e where
e.salary =(select max(e1.salary) from employees e1 where e1.department_id = e.department_id) ;
3查询每个部门薪水最高的前二人。
使用的表就是题2的表
mysql> select * from employees e1 where (select count(*) from employees e2 where e2.department_id=e1.department_id and e2.salary > e1.salary)<2 order by department_id;
相关文章推荐
- 关于“怎么比较两个SQL查询语句产生的结果集是否相同”搜集到的一些资料
- JDBC获取SQL查询语句返回记录数的三种方式效率比较
- 一些比较有用的oracle查询语句
- 一些记录查询的SQL语句
- Mysql一些复杂的sql语句(查询与删除重复的行)
- 比较常用的sql 查询语句
- 安卓开发SQlite使用执行SQL语句一些简单的处理——2.查询数据库的数据
- 比较常用的sql 查询语句
- 【Sql语句】自己摸索出得比较复杂的一个查询语句
- 10万数据量的数据库查询效率比较 纯sql语句 和 view 视图
- 本人搜集的一些常用的SQL查询语句(不定期更新)
- C#中??和?分别是什么意思? 在ASP.NET开发中一些单词的标准缩写 C#SESSION丢失问题的解决办法 在C#中INTERFACE与ABSTRACT CLASS的区别 SQL命令语句小技巧 JQUERY判断CHECKBOX是否选中三种方法 JS中!=、==、!==、===的用法和区别 在对象比较中,对象相等和对象一致分别指的是什么?
- 分享一些常见的SQL语句,包含单表查询、高级查询(连接查询、复合条件查询、嵌套查询等)。
- SQL比较时间查询语句
- SQL 单表查询的一些常用语句和函数及单表的插入修改删除操作
- 个人收集的一些常用的sql查询语句与函数
- sql语句的优化分析之一查询语句中左连接和函数效率分析比较
- 一些精妙的Sql查询语句汇总【1】
- 一些比较容易出问题的sql语句
- 你必须掌握的一些常见的SQL语句,包含单表查询、高级查询(连接查询、复合条件查询、嵌套查询)