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

  • 相关阅读:
    异地协作,A地上传jar包到B地服务器上传速率慢
    linux一行命令查杀进程
    maven项目创建.m2文件夹
    模态框传递参数
    测试身份证信息
    jenkins:邮件配置良心之作
    python:不错的python编程核心思想
    jenkins:忘记密码怎么办
    docker:如何查看容器的挂载目录
    JavaScript + PHP 实现刷新继续保持倒计时的按钮
  • 原文地址:https://www.cnblogs.com/pangblog/p/3241199.html
Copyright © 2011-2022 走看看