題目1 
問題描述:
為管理崗位業務培訓信息,建立3個表:
s (s#,sn,sd,sa)   s#,sn,sd,sa 分別代表學號、學員姓名、所屬單位、學員年齡
c (c#,cn )        c#,cn       分別代表課程編號、課程名稱
sc ( s#,c#,g )    s#,c#,g     分別代表學號、所選修的課程編號、學習成績
要求實現如下5個處理:
  1. 使用標準sql嵌套語句查詢選修課程名稱為’稅收基礎’的學員學號和姓名
  2. 使用標準sql嵌套語句查詢選修課程編號為’c2’的學員姓名和所屬單位
  3. 使用標準sql嵌套語句查詢不選修課程編號為’c5’的學員姓名和所屬單位
  4. 使用標準sql嵌套語句查詢選修全部課程的學員姓名和所屬單位
  5. 查詢選修了課程的學員人數
  6. 查詢選修課程超過5門的學員學號和所屬單位
1. 使用標準sql嵌套語句查詢選修課程名稱為’稅收基礎’的學員學號和姓名 
--實現代碼:
select sn,sd from s
where [s#] in(
    select [s#] from c,sc
    where c.[c#]=sc.[c#]
        and cn=n'稅收基礎')
2. 使用標準sql嵌套語句查詢選修課程編號為’c2’的學員姓名和所屬單位
--實現代碼:
select s.sn,s.sd from s,sc
where s.[s#]=sc.[s#]
    and sc.[c#]='c2'
3. 使用標準sql嵌套語句查詢不選修課程編號為’c5’的學員姓名和所屬單位
--實現代碼:
select sn,sd from s
where [s#] not in(
    select [s#] from sc 
    where [c#]='c5')
4. 使用標準sql嵌套語句查詢選修全部課程的學員姓名和所屬單位
--實現代碼:
select sn,sd from s
where [s#] in(
    select [s#] from sc 
        right join c on sc.[c#]=c.[c#]
    group by [s#]
    having count(*)=count(distinct [s#]))
5. 查詢選修了課程的學員人數
--實現代碼:
select 學員人數=count(distinct [s#]) from sc
6. 查詢選修課程超過5門的學員學號和所屬單位
--實現代碼:
select sn,sd from s
where [s#] in(
    select [s#] from sc 
    group by [s#]
    having count(distinct [c#])>5)
trackback: http://tb.blog.csdn.net/trackback.aspx?postid=384967
[點擊此處收藏本文] 發表于 2005年05月31日 17:08:00
 yzluji 發表于2005-06-06 4:04 pm  ip: 61.186.252.*
sql練習一:第四題答案是不是有問題?是否應改為 
select sn,sd from s 
where [s#] in( 
select [s#] from sc 
group by [s#] 
having count(*)=(select count(*) from c)) 
 
 [email protected] 發表于2005-07-15 11:03 am  ip: 61.186.252.*
select * from s where s# in( 
select s# from sc 
group by s# 
having count( distinct c#)= (select count(*) from c)) 
多一個distinct是否更好呢。比如,可能一些沒有及格人或者其他情況考了2次,嘿嘿。
 shenjane 發表于2006-02-07 9:58 am  ip: 210.22.152.*
第四題好像有錯誤: 
select sn,sd from s 
where [s#] in (select [s#] from sc 
group by [s#] 
having count(*)= (select count(distinct c#) from c))
新聞熱點
疑難解答