題目一、學(xué)生表 如下:
自動(dòng)編號(hào)???學(xué)號(hào)???姓名 課程編號(hào) 課程名稱 分?jǐn)?shù)
1??????? 2005001? ?張三? 0001? ? ? ? ?數(shù)學(xué)? ? ? ?69
2??????? 2005002? ?李四? 0001? ? ? ? ? 數(shù)學(xué)? ? ? 89
3??????? 2005001? ? 張三? 0001? ? ? ? ?數(shù)學(xué)? ? ? 69
刪除除了自動(dòng)編號(hào)不同,?其他都相同的學(xué)生冗余信息
解答:delete from tablename where 自動(dòng)編號(hào) not in (select min(自動(dòng)編號(hào)) from tablename group by 學(xué)號(hào),姓名,課程編號(hào),課程名稱,分?jǐn)?shù))
題目二:請(qǐng)用SQL?語(yǔ)句實(shí)現(xiàn):從TestDB?數(shù)據(jù)表中查詢出所有月份的發(fā)生額都比101?科目相應(yīng)月份的發(fā)生額高的科目。請(qǐng)注意:TestDB?中有很多科目,都有1?-12?月份的發(fā)生額。
AccID:科目代碼,Occmonth?:發(fā)生額月份,DebitOccur?:發(fā)生額。數(shù)據(jù)庫(kù)名:JcyAudit?,數(shù)據(jù)集:Select * from TestDB
select a.*?
from TestDB a, (select Occmonth, max(DebitOccur) as maxdebitoccur from TestDB? where AccID='101' group by Occmonth) b
where a.Occmonth=b.Occmonth and a.DebitOccur>b.maxdebitoccur
題目三:復(fù)制表(?只復(fù)制結(jié)構(gòu),?源表名:a新表名:b)
select into b from a where 1<>1(只復(fù)制結(jié)構(gòu),不復(fù)制內(nèi)容)
select into b from a where 1=1(復(fù)制結(jié)構(gòu)和內(nèi)容)
題目四:日程安排提前五分鐘提醒
select * from 日程安排 where datediff('minute', 開始時(shí)間,getdate())>5
題目五:有兩個(gè)表A?和B?,均有key?和value?兩個(gè)字段,如果B?的key?在A?中也有,就把B?的value?換為A?中對(duì)應(yīng)的value
select B set B.value=(select A.value from A where A.key=B.key) where B.id=(select B.id from A,B where A.key=B.key)
題目六:
15.sql面試題(2)有表A,結(jié)構(gòu)如下:
p_ID p_Num s_id
1 10 01
1 12 02
2 8 01
3 11 01
3 8 03
其中:p_ID為產(chǎn)品ID,p_Num為產(chǎn)品庫(kù)存量,s_id為倉(cāng)庫(kù)ID。請(qǐng)用SQL語(yǔ)句實(shí)現(xiàn)將上表中的數(shù)據(jù)合并,合并后的數(shù)據(jù)為:
p_ID s1_id s2_id s3_id
1 10 12 0
2 8 0 0
3 11 0 8
其中:s1_id為倉(cāng)庫(kù)1的庫(kù)存量,s2_id為倉(cāng)庫(kù)2的庫(kù)存量,s3_id為倉(cāng)庫(kù)3的庫(kù)存量。如果該產(chǎn)品在某倉(cāng)庫(kù)中無(wú)庫(kù)存量,那么就是0代替。
解答:
select p_ID, sum(case when s_id='01' then p_Num else 0) as s1_id,?sum(case when s_id='02' then p_Num else 0) as s2_id,?sum(case when s_id='03' then p_Num else 0) as s3_id
from table?
group by p_ID
題目七:查詢學(xué)過(guò)“葉平”老師所教的所有課的同學(xué)的學(xué)號(hào)、姓名;
select ssid, sname from student where ssid in (select scsid from sc, s, t, c where t.name=葉平 and t.tid=c.tid and c.cid=sc.cid group by scsid having count(cid)=(select count(cid) as counts from t, c where t.tid=c.cid and t.name=葉平)
題目八:按平均成績(jī)從高到低顯示所有學(xué)生的“數(shù)據(jù)庫(kù)”、“企業(yè)管理”、“英語(yǔ)”三門的課程成績(jī),按如下形式顯示:?學(xué)生ID,,數(shù)據(jù)庫(kù),企業(yè)管理,英語(yǔ),有效課程數(shù),有效平均分’
?SELECT?stuId?as?學(xué)生ID,(SELECT?score?FROM?Scores?WHERE?Scores.stuId=t.stuId?AND?courseId='004')?AS?數(shù)據(jù)庫(kù),(SELECT?score?FROM?Scores?WHERE?Scores.stuId=t.stuId?AND?courseId='001')?AS?企業(yè)管理,(SELECT?score?FROM?Scores?WHERE?Scores.stuId=t.stuId?AND?courseId='006')?AS?英語(yǔ), COUNT(*)?AS?有效課程數(shù),?AVG(t.score)?AS?平均成績(jī)
FROMScores?AS?t
GROUP?BY?stuId
ORDER?BY?avg(t.score) DESC
題目九:按各科平均成績(jī)從低到高和及格率的百分?jǐn)?shù)從高到低順序
SELECT?t.courseId?AS?課程號(hào),max(course.courseName)AS?課程名,isnull(AVG(score),0)?AS?平均成績(jī),100?*?SUM(CASE?WHEN??isnull(score,0)>=60?THEN?1?ELSE?0?END)/COUNT(*)?AS?及格百分率
FROM?Scores?T,Course
where?t.courseId=course.courseId
GROUP?BY?t.courseId
ORDER?BY?100?*?SUM(CASE?WHEN??isnull(score,0)>=60?THEN?1?ELSE?0?END)/COUNT(*)?DEScores
題目十:1981年出生的學(xué)生名單(注:Student表中stuAge列的類型是datetime)
select stuName, datediff(year,stuAge, getdate()) as age
from student
where convert(nvarchar(11), datepart(year, stuAge))='1981'
題目十一:查詢兩門以上不及格課程的同學(xué)的學(xué)號(hào)及其平均成績(jī)
select stuID, avg(isnull(score,0) from Scores where stuID in (select stuID from Scores where score<60 group by stuID having count(*)>2) group by stuID