SQL面试必问

1.用一条SQL 语句 查询出每门课都大于80 分的学生姓名

name   kecheng   fenshu
张三    语文       81
张三     数学       75
李四     语文       76
李四     数学       90
王五     语文       81
王五     数学       100
王五     英语       90

select name from table group by name having min(fenshu)>80
select distinct name from table where name not in(select distinct  name from table where fenshu<80)

2.查询出来10-20条之间的数据

select top 10 *from table Id >(select max(Id) from(select top 10 Id from table order by Id ASC) as Tmpdata) order by Id Asc

3. 学生表 如下:
自动编号   学号   姓名 课程编号 课程名称 分数
1        2005001 张三 0001     数学    69
2        2005002 李四 0001      数学    89
3        2005001 张三 0001      数学    69
删除除了自动编号不同, 其他都相同的学生冗余信息

delete from table where Id not in(select min(Id) from table group by SId,name,Curs,CurNAme.Score)

4.写SQL语句,求出2018年各个省份购物花费最多的人的姓名

现有:

商品表(Id,商品名,价格)

客户表(Id,姓名,手机号,省份)

订单表(Id,商品Id,客户Id,购买时间,购买渠道)

select *from a inner join b on a.Id=b.Id inner join c on c.Id=b.Id group by 省份 having max(money)

5.表结构和数据
date                       result
2011-02-01          胜
2011-02-01          负
2011-02-01          胜
2011-02-02          胜
2011-02-02          负

查询出来的结果:
date                  胜     负
2011-02-01    2       1
2011-02-02    1       1

思路:第一步先根据时间进行分组查询,查出来当前时间胜的次数再查询出来当前时间负的次数

select date,(select count(*) from table where  date=t.date and result='胜')  as '胜' ,(select count(*) from table where date=t.date and result='负') as '负' from table as t group by date

 

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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值