转载的sql语句

1 查询每门课程成绩都大于80分学生的学号
数据库 表 student
name score course
A 85  语文
A 75  数学
A 82  英语
B   75  语文
B   89  数学
B   79  英语
天使美眉90 语文
天使美眉100 数学
天使美眉100 英语

请找出每门课程都超过80分的那个人名字的SQL语句

SQL1:

select name from test.stu
group by name
having count(score) =sum(case  when score>80 then 1 else 0 end )

SQL2:

select name from stu
group by name
having name not in (
select name from stu
where score <80)

SQL3:

select name from test.stu
group by name
having min(score)>=80



================================================

2. 查询课程001的成绩大于课程002成绩的学号

student表:sno(学号),sname(姓名),sex(性别),dept(系) 
course课程表:cno(课程号),课程名(cname) 
sc选课表:sno,cno,grade(成绩)

select cno from sc a inner join (select * from sc where cno=(select cno from course where cname='001')) as b on a.cno>o=(select cno from course where cname='002')






================================================

3、关于group by表内容:

2005-05-09 胜
2005-05-09 胜
2005-05-09 负
2005-05-09 负
2005-05-10 胜
2005-05-10 负
2005-05-10 负

如果要生成下列结果, 该如何写sql语句

            胜 负
2005-05-09  2  2
2005-05-10  1  2
--------------------------------------------------------
1) select rq,sum(case when shengfu='胜' then 1 else 0 end) as胜,sum(case when shengfu='负' then 1 else 0 end) as负from tab3 group by rq
2) select N.rq,N. 胜,M. 负 from
(select rq,count(*) 胜 from tab3 where shengfu='胜'group by rq)N inner join
(select rq,count(*) 负from tab3 where shengfu='负'group by rq)M on N.rq=M.rq
3) select a.rq,a. 胜  as胜,b.负  as  负from
(select rq,count(shengfu) 胜from tab3 where shengfu='胜' group by rq) a,
(select rq,count(shengfu) 负from tab3 where shengfu='负' group by rq) b
where a.rq=b.rq;
4)select time, sum(decode(status,'胜','')) 胜 ,sum(decode(status,'负','')) 负 from shengfu_table group by time;  

======================================================


4.表中有A B C三列,用SQL语句实现:当A列大于B列时选择A列否则选择B列,当B列大于C列时选择B列否则选择C列。
select (case when a>b then a else b end),(case when b>c then b else c end) from tab4


5.一个日期判断的sql语句请取出tab5表中日期(SendTime字段)为当天的所有记录 (SendTime字段为datetime型,包含日期与时间)
select * from tab5 t where to_char(t.SendTime,'yyyy-mm-dd')=to_char(sysdate,'yyyy-mm-dd')


6.有一张表,里面有3个字段:语文,数学,英语。其中有3条记录分别表示语文70分,数学80分,英语58分,请用一条sql语句查询出这三条记录并按以下条件显示出来(并写出您的思路): 
   大于或等于80表示优秀,大于或等于60表示及格,小于60分表示不及格。 
       显示格式: 
       语文              数学                英语 
       及格              优秀                不及格   
-------------------------------------------------------
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 tab5



==================================

7.请用一个sql语句得出结果
从table1,table2中取出如table3所列格式数据

table1

月份mon   部门dep   业绩yj
-------------------------------
一月份      01        10
一月份      02        10
一月份      03         5
二月份      02         8
二月份      04         9
三月份      03         8

table2

部门dep      部门名称depname
--------------------------------
      01      国内业务一部
      02      国内业务二部
      03      国内业务三部
      04      国际业务部

table3 (result)

部门dep     一月份      二月份   

 

查询英语、计算机成绩都>90分的学生的信息

table: stu

id       name    grade

-----------------------------

('s001','王一','一年级');
('s002','王二','二年级');
('s003','王三','三年级');
('s004','王四','一年级');

table:score

id   sid   subject   score

-----------------------------------------------

('f001','s001','计算机',93);
('f002','s001','英语',93);
('f003','s001','数学',93);
('f004','s002','计算机',93);
('f005','s002','数学',87);
('f006','s002','英语',34);
('f007','s003','计算机',93);
('f008','s003','英语',93);
('f009','s003','数学',93);
('f010','s004','英语',93);

select * from stu where id in (

select sid from 

(select * from score s1 where s1.subject='计算机' and s1.score>90) sc1

inner join

(select * from score s2 where s2.subject='英语' and s2.score>90) sc2

on sc1.sid=sc2.sid

);

 

 

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值