查詢每門課程成績都大於80分學生的姓名


http://blog.sina.com.cn/s/blog_8ea826d10102vm1h.html

轉載的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     一月份      二月份      三月份
---------------------------------------------------
      01      10        null         null
      02      10         8           null
      03      5         null          8
      04      null       9           null
-------------------------------------------------------
1)
select t.depname,
(select yj from tab6 where mon='一月份' and dep=t.dep) 一月份,
(select yj from tab6 where mon='二月份' and dep=t.dep) 二月份,
(select yj from tab6 where mon='三月份' and dep=t.dep) 三月份
from tab7 t

---------------------
2)求總銷售額
select
sum(case when t1.mon='一月份' then t1.yj else 0 end) 一月份,
sum(case when t1.mon='二月份' then t1.yj else 0 end) 二月份,
sum(case when t1.mon='三月份' then t1.yj else 0 end) 三月份
from tab7 t,tab6 t1 where t.dep=t1.dep


8.一個表中的Id有多個記錄,把所有這個id的記錄查出來,並顯示共有多少條記錄數。
-------------------------------------------
select id,count(*) from tab8 group by id having count(*)>1

select * from (select tab8,count(id) as num from tab8 group by id) t where t.num>1




9.用一條SQL語句 查詢出每門課都大於80分的學生姓名 
8.一個叫department的表,里面只有一個字段name,一共有4條紀錄,分別是a,b,c,d,對應四個球對,現在四個球對進行比賽,用一條sql語句顯示所有可能的比賽組合.

select t.bh||'vs'||t1.bh from tab10 t,tab10 t1 where t.bh$amp;
select t.bh||'vs'||t1.bh from tab10 t,tab10 t1 where t.bh$amp;t1.bh這個是不分的



=================================
10.怎么把這樣一個表兒
year  month amount
1991   1     1.1
1991   2     1.2
1991   3     1.3
1991   4     1.4
1992   1     2.1
1992   2     2.2
1992   3     2.3
1992   4     2.4
查成這樣一個結果
year m1  m2  m3  m4
1991 1.1 1.2 1.3 1.4
1992 2.1 2.2 2.3 2.4

a):
select t.year,
(select a.amout from tab11 a where a.month=1 and a.year=t.year) m1,
(select b.amout from tab11 b where b.month=2 and b.year=t.year) m2,
(select c.amout from tab11 c where c.month=3 and c.year=t.year) m3,
(select d.amout from tab11 d where d.month=4 and d.year=t.year) m4
from tab11 t group by t.year


11.拷貝表(拷貝數據,源表名:a 目標表名:b)

SQL: insert into b(a, b, c) select d,e,f from b;

create table test as select * from dept; --從已知表復制數據和結構  

create table test as select * from dept where 1=2; --從已知表復制結構但不包括數據 



12.顯示文章、提交人和最后回復時間

  select a.title,a.username,b.adddate from table a,(select max(adddate) adddate from table where table.title=a.title) b


13.日程安排提前五分鍾提醒



14.兩張關聯表,刪除主表中已經在副表中沒有的信息
  delete from  fubiao a where a.fid not in(select id from zhubiao)


15.有兩個表tab12和tab13,均有key和value兩個字段,如果tab13的key在tab12中也有,就把tab13的value換為tab12中對應的value

update tab13 set value=(select value from tab12 where tab12.key=tab13.key)



16.原表:
courseid coursename score
-------------------------------------
   1       java      70
   2       oracle    90
   3       xml       40
   4       jsp       30
   5       servlet   80
-------------------------------------
為了便於閱讀,查詢此表后的結果顯式如下(及格分數為60):
courseid coursename score mark
---------------------------------------------------
  1        java     70  pass
  2        oracle   90  pass
  3        xml      40  fail
  4        jsp      30  fail
  5        servlet  80  pass
---------------------------------------------------
select t.courseid,t.coursename,t.score,(case when score>60 then 'pass' else 'fail' end) mark from tab14 t


17.表15
a1   a2
1    a
1    b
2    x
2    y
2    z
用select能選成以下結果嗎?
1 ab
2 xyz
SELECT a1, replace(max(sys_connect_by_path(a2, ' ')),' ','') NAME
  FROM (SELECT a1, a2, rn, LAG(rn) OVER(PARTITION BY a1 ORDER BY rn) rn1
          FROM (SELECT a1, a2, row_number() OVER(ORDER BY a1) rn FROM t) rn)
START WITH rn1 IS NULL
CONNECT BY rn1 = PRIOR rn
GROUP BY a1;


18.題為
有兩個表, t1, t2,
Table t1:

SELLER | NON_SELLER
-----    -----
  A       B
  A       C
  A       D
  B       A
  B       C
  B       D
  C       A
  C       B
  C       D
  D       A
  D       B
  D       C

Table t2:

SELLER |  BAL
------  --------
  A       100
  B       200
  C       300
  D       400
要求用SELECT 語句列出如下結果:------如A的SUM(BAL)為B,C,D的和,B的SUM(BAL)為A,C,D的和.......
且用的方法不要增加數據庫負擔,如用臨時表等
SELECT SELLER,a.total-t.BAL FROM t,(SELECT SUM(BAL) total FROM t)a;

 

 

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

SQL命令如何“列出所有課程都及格了的同學的名字”

select    姓名,count(1) as 及格課程數 from 學生 a join 選修 b on a.學號=b.學號 
where b.成績>60--及格分數
group by 姓名
having count(1)=(select count(課程號) from 課程)--課程數


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM