【笔试/面试】SQL 经典面试题
来源:互联网 发布:淘宝白菜价网站 编辑:程序博客网 时间:2024/06/05 10:04
基本概念
(1)any/all,构成 where 子句的条件判断,any:表示或(or)的概念,all:则表示与(and)的概念,这两个关键字的出现是为了语句的简化;
(2)先分组再做聚合,逻辑上也应当如此,聚合(取最值)之后便无分组的必要;
select region, sum(population), sum(area) from bbc group by region;
(3)group by having,having 对分组后的数据进行筛选,这是 where 所做不到的;
1. 不使用 min,找出表 ppp 中 num(列)最小的数
select num from ppp where num <= all(select num from ppp);
不可以使用 min 函数,但可以实用 order by 和 limit 相组合呀;
select * from ppp order by num desc limit 1;
举一反三
自然,不使用 max,找出表 ppp 中 num 最大的数:
select num from ppp where num >= all(select num from ppp);select num from ppp order by num limit 1;
2. 选择表 ppp 中的重复记录
select * from ppp where num in (select num from ppp group by num having count(num) > 1);
注意,如下的语句只返回单独的一条记录
select * from ppp group by num having count(num) > 1;
举一反三
查询表中出现 四 次的记录,group by having
select * from ppp where num in (select num from ppp group by num having count(num) = 4);
3. 用一条SQL语句查询出每门课都大于80分的学生姓名
name kecheng fenshu
张三 语文 81
张三 数学 75
李四 语文 76
李四 数学 90
王五 语文 81
王五 数学 100
王五 英语 90
select distinct name from stu where name not in (select distinct name from stu where fenshu <= 80);
4. 影分身,一表当做两表用
表形式如下:
Year Salary
2000 1000
2001 2000
2002 3000
2003 4000
想得到如下形式的查询结果
Year Salary
2000 1000
2001 3000
2002 6000
2003 10000
sql语句怎么写?
select b.year, sum(a.salary) from hell0 a, hello b where a.year <= b.year group by b.year;
References
[1] SQL经典面试题及答案
0 1
- 【笔试/面试】SQL 经典面试题
- 经典笔试面试题(二)
- 经典数据库笔试面试题
- 多线程经典笔试面试题
- 经典SQL面试题
- 经典SQL面试题
- SQL经典面试题
- SQL经典面试题
- SQL经典面试题
- 经典SQL面试题
- 经典sql面试题
- 经典sql面试题
- SSH面试(经典面试题)
- 一题四解:SQL经典面试题
- 经典Sql面试题收集!!
- SQL面试题[经典收录]
- SQL面试题[经典收录]
- 经典Sql面试题收集!!
- [BZOJ2456] mode - 贪心
- IOS学习 GCD 单例模式 一次性执行 涉及记录时间,计算时差
- style、selector、attrs、shape
- LeetCode上做题之体会(一)
- 数据库全面知识
- 【笔试/面试】SQL 经典面试题
- C++实验3-个人所得税计算器
- shell脚本攻略第二章总结---命令之乐
- #3变量实战讲解#
- Vulkan Cmd buffer
- 4月5日学习记录
- java.lang.Throwable: Explicit termination method 'close' not called
- 悼念512汶川大地震遇难同胞
- Java基础学习第二十七天——类加载器和反射