zoukankan      html  css  js  c++  java
  • [置顶] 一道经典的sql面试题不同的写法

    用一条SQL语句   查询出每门课都大于80分的学生姓名,表( #test)如下:
      
    Name Course Mark
    张三 语文 81
    张三 数学 75
    李四 语文 76
    李四 数学 90
    王五 英语 100
    王五 语文 81

    create table #test
    (
    Name varchar(10),
    Course varchar(10),
    Mark float
    )

    insert into #test
    select '张三', '语文', 81 union
    select '张三', '数学', 75 union
    select '李四', '语文', 76 union
    select '李四', '数学', 90 union
    select '王五', '英语', 100 union
    select '王五', '语文', 81

    方法A select distinct Name
    from #test A
    where not exists(select Course
     from #test
     where Mark < 80 and Name = A.Name)

    方法B

    select * from #test a where mark > 80 and (select count(*) from #test where name=a.name)= (select count(*) from #test where name=a.name and mark > 80)

    方法C

    select distinct name from #test a where not exists(select * from #test where a.name=name and mark<80) (我认为这种较好)

    方法D

    select distinct name from  #test  where
    name not in (
    select name from #test where mark<=80
    )

    方法E

    select name,min(mark) from #test group by name having min(mark)>80
    -----66666
    DECLARE @test table (  Name varchar(10),  Course varchar(10),  Mark float) 
    insert into @test  select   '张三  ',   '语文  ', 81
     union select   '张三  ',   '数学  ', 75
    union select   '李四  ',   '语文  ', 76
     union select   '李四  ',   '数学  ', 90
     union  select   '王五  ',   '英语  ', 100
     union  select   '王五  ',   '语文  ', 81  
    SELECT NAME FROM @test GROUP BY name HAVING count(*)=count(case when mark>=80 then 1 else null end) 或者  
    SELECT NAME FROM @test GROUP BY name HAVING count(*)=sum(case when mark>=80 then 1 else 0 end)

    ---方法F

    select name from #test group  by name having min(mark)>80

  • 相关阅读:
    FreeRTOS——队列管理
    Replication Controller、Replica Set
    基本概念与组件
    YAML配置文件
    curl命令测试服务器是否支持断点续传
    浏览器http跳转至https问题
    常用Linux日志文件功能
    SSH连接时,长时间不操作就断开的解觉办法
    防HTTP慢速攻击的nginx安全配置
    Centos6.5升级安装openssh7.7p1
  • 原文地址:https://www.cnblogs.com/pangblog/p/3241199.html
Copyright © 2011-2022 走看看