1、 用一條SQL 語句 查詢出每門課都大於80 分的學生姓名
name kecheng fenshu
張三 語文 81
張三 數學 75
李四 語文 76
李四 數學 90
王五 語文 81
王五 數學 100
王五 英語 90
A: select distinct name from table where name not in (select distinct name from table where fenshu<=80) select name from table group by name having min(fenshu)>80
2、學生表 如下:
自動編號 學號 姓名 課程編號 課程名稱 分數
1 2005001 張三 0001 數學 69
2 2005002 李四 0001 數學 89
3 2005001 張三 0001 數學 69
刪除除了自動編號不同, 其他都相同的學生冗余信息
A: delete table where 自動編號 not in(select min( 自動編號) from table group by 學號, 姓名, 課程編號, 課程名稱, 分數)
3、 面試題:怎么把這樣一個表兒
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
答案一、
select year, (select amount from aaa m where month=1 and m.year=aaa.year) as m1, (select amount from aaa m where month=2 and m.year=aaa.year) as m2, (select amount from aaa m where month=3 and m.year=aaa.year) as m3, (select amount from aaa m where month=4 and m.year=aaa.year) as m4 from aaa group by year
5、有一張表,里面有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 TABLE;
6、一道SQL語句面試題,關於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 year, (select count(losu) from taba a where a.losu='勝' and a.year=taba.year ) as 勝, (select count(losu) from taba a where a.losu='負' and a.year=taba.year ) as 負 from taba group by year;
答案2
select year, sum(case when losu='勝' then 1 else 0 end ) as 勝, sum(case when losu='負' then 1 else 0 end ) as 負 from taba group by year ;
7、表形式如下:
Year Salary
2000 1000
2001 2000
2002 3000
2003 4000
想得到如下形式的查詢結果
Year Salary
2000 1000
2001 3000
2002 6000
2003 10000
sql語句怎么寫?
select b.year, sum(a.salary) from hello a, hello b where a.year <= b.year group by b.year;
8、一個叫teamt的表,里面只有一個字段name,一共有4條紀錄,分別是a,b,c,d,對應四個球對,現在四個球對進行比賽,用一條sql語句顯示所有可能的比賽組合.
select a.name, b.name from team a, team b where a.name < b.name
9、請用SQL語句實現:從TestDB數據表中查詢出所有月份的發生額都比101科目相應月份的發生額高的科目。請注意:TestDB中有很多科目,都有1-12月份的發生額。
AccID:科目代碼,Occmonth:發生額月份,DebitOccur:發生額。
數據庫名:JcyAudit,數據集:Select * from TestDB
答:select a.*
from TestDB a
,(select Occmonth,max(DebitOccur) Debit101ccur from TestDB where AccID='101' group by Occmonth) b
where a.Occmonth=b.Occmonth and a.DebitOccur>b.Debit101ccur
10、原表:
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 courseid,coursename,score,(case when score >=60 then 'pass' else 'fail' end) mark from sc
select courseid,coursename,score,(decode(sign(score-60),-1,'fail','pass') mark from sc
11.原表:
id proid proname
1 1 M
1 2 F
2 1 N
2 2 G
3 1 B
3 2 A
查詢后的表:
id pro1 pro2
1 M F
2 N G
3 B A
select a.id, (select b.proname from table3 b where proid=1 and b.id=a.id) pro1, (select b.proname from table3 b where proid=2 and b.id=a.id) pro2 from table3 a group by id;
12、sql求解
表a
列 a1 a2
記錄 1 a
1 b
2 x
2 y
2 z
用select能選成以下結果嗎?
1 ab
2 xyz