sql经典面试题汇总(可以本地测试练习)

1.表名:number
ID(number型) Name(varchar2型)
1 a
2 b
3 b
4 a
5 c
6 c
要求:执行一个删除语句,当Name列上有相同时,只保留ID这列上值小的
例如:删除后的结果应如下:
ID(number型) Name(varchar2型)
1 a
2 b
5 c
请写出SQL查询语句。
答案:
delete from number where id ont in (select min(id) from number group by name);
这个时候会报一个错误:
ERROR 1093 (HY000): You can't specify target table 'number' for update in FROM clause;
这个是mysql的一个bug,意思是不能先select出同一表中的某些值,再update这个表(在同一语句中)。
解决之后的正确sql语句:
  delete from number where id not in (select a.id from ((select min(id) as id from number group by name) a));
也就是说将select出的结果再通过中间表select一遍,这样就规避了错误。注意,这个问题只出现于mysql,postgresql和oracle不会出现此问题。

2.表名:student
name course score
张青 语文 72
王华 数学 72
张华 英语 81
张青 物理 67
李立 化学 98
张燕 物理 70
张青 化学 76
用sql查询出“张”姓学生中平均成绩大于75分的学生信息;
sql语句: select * from student where name in (select name from student
where name like '张%' group by name having avg(score) > 75);

3.一张比赛结果表pmatch,
+------------+--------+
| date       | result |
+------------+--------+
| 2005-05-09 | 胜     |
| 2005-05-09 | 胜     |
| 2005-05-09 | 负     |
| 2005-05-09 | 负     |
| 2005-05-10 | 胜     |
| 2005-05-10 | 负     |
| 2005-05-10 | 负     |
+------------+--------+
如果要生成下列结果,如何写sql,
+------------+------+------+
| date       | 胜   | 负   |
+------------+------+------+
| 2005-05-09 |    2 |    2 |
| 2005-05-10 |    1 |    2 |
+------------+------+------+
sql语句: select date,sum(case when result="胜" then 1 else 0 end) "胜",sum(case when result="负" then 1 else 0 end) "负" from pmatch group by date;
备注:此处要注意区别sum(0),sum(1)与count(0),count(1),后两个结果一样,前两个结果是不一样的。

4.表中有A B C三列,
用SQL语句实现:当A列大于B列时选择A列否则选择B列,当B列大于C列时选择B列否则选择C列。
 select * from number_sort;
+------+------+------+
| A    | B    | C    |
+------+------+------+
|    2 |    3 |    1 |
|    9 |    9 |    3 |
|    4 |    5 |    6 |
|    1 |    0 |    9 |
|    8 |    7 |    6 |
+------+------+------+
sql查询结果如下显示:
+------+------+
| max1 | max2 |
+------+------+
|    3 |    3 |
|    9 |    9 |
|    5 |    6 |
|    1 |    9 |
|    8 |    7 |
+------+------+
sql语句: select (case when A>B then A else B end) max1,(case when B>C then B else C end) max2 from number_sort;

5.有一张国家人口统计表country:
+--------+------+------------+
| name   | sex  | population |
+--------+------+------------+
| 中国   |    1 |        340 |
| 中国   |    2 |        260 |
| 美国   |    1 |         45 |
| 美国   |    2 |         55 |
| 英国   |    1 |         51 |
| 英国   |    2 |         49 |
| 日本   |    1 |          7 |
| 日本   |    2 |          1 |
+--------+------+------------+
sql查询显示如下结果:
+--------+------+------+
| name   | 男   | 女   |
+--------+------+------+
| 中国   |  340 |  260 |
| 日本   |    7 |    1 |
| 美国   |   45 |   55 |
| 英国   |   51 |   49 |
+--------+------+------+
sql语句:
select name,sum(case when sex=1 then population else 0 end) "男",sum(case when sex=2 then population else 0 end) "女" from country group by name;

6.有一张表score,里面有3个字段:语文,数学,英语。
其中有3条记录分别表示语文70分,数学80分,英语58分,
+--------+--------+--------+--------+
| 姓名   | 语文   | 数学   | 英语   |
+--------+--------+--------+--------+
| 张三   |     70 |     80 |     58 |
| 李四   |     90 |     88 |     75 |
| 王五   |     60 |     59 |     61 |
+--------+--------+--------+--------+
请用一条sql语句查询出这三条记录并按以下条件显示出来(并写出您的思路):  
   大于或等于80表示优秀,大于或等于60表示及格,小于60分表示不及格。  
       显示格式:  
       姓名 语文              数学                英语  
       XX 及格              优秀                不及格   
sql语句: select 姓名,(case when 语文>=80 then '优秀' when 语文>=60 then '及格' else '不及格' end) as 语文,
  (case when 数学>=80 then '优秀' when 数学>=60 then '及格' else '不及格' end) as 数学,
    (case when 英语>=80 then '优秀' when 英语>=60 then '及格' else '不及格' end) as 英语
   from score;

7.
一个日期判断的sql语句?
请取出tb表中日期(sendTime字段)为当天的所有记录?(sendTime字段为datetime型,包含日期与时间);
sql语句:
select * from tb where datediff(sendTime,getdate())=0;
DATEDIFF(date1,date2) 函数返回两个日期之间的天数,date1 和 date2 参数是合法的日期或日期/时间表达式。



  • 2
    点赞
  • 39
    收藏
    觉得还不错? 一键收藏
  • 2
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 2
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值