【正文】
atetime, 專業(yè) char(10),年級 int) 課程 (課程號 char(4),課程名 char(10),課程學分 int 選課 (學生號 char(7),課程號 char(4),成績 int) 1. select distinct x.* from 學生 x, 選課 y where = 從教學庫中查詢出選修了課程的所有學生信息 2. select * from 學生 where exists (select * from 選課 where 學生 .學生號 =選課 .學生號 group by 選課 .學生號 having count(*)=3 從教學庫中查詢出同時選修了 3門課程的全部學生信息 3. select x.*,課程名 ,課程學分 ,成績 from 學生 x,課程 y,選課 z where = and =號 order by , 從教學庫中查詢出每個學生選課的全部情況,并依次按學生號和成績排序 4. select * from 學生 where 學生號 in (select 學生號 from 選課 group by 學生號 having count(*)=2 從教學庫中查詢出選修 2門課程的全部學生 5. select 課程 .課程號 ,課程名 ,count(課程 .課程號 ) as 人數(shù) from 課程 ,選課 where 課程 .課程號 =選課 .課程號 group by 課程 .課程號 ,課程名 order by 人數(shù) 從教學庫中查詢出每門課程被選修的學生人數(shù),并按所選人數(shù)的升序排列出課程號、課程名和選課人數(shù)。 6. select , , from 選課 x,選課 y where =@s1 and =@s2 and = 從教學庫中查詢出學生號為 @s1 的學生和學生號為 @s2 的學生所選修的共同課程的課程號 7. select * from 課程 where 課程號 in (select 課程號 from 選課 group by 課程號 having count(*)5 從教學庫中查詢出被 5個以上學生選修的全部課程 8. select 專業(yè) ,count(*) as 專業(yè)人數(shù) from 學生 group by 專業(yè) order by 專業(yè)人數(shù) desc 從教學庫中查詢出每個專業(yè)的學生人數(shù),并按人數(shù)多少降序排列。 9. select 課程號 , count(課程號) as 學生人數(shù) From 選課 Group by 課程號 從教學庫中查詢出每門課程被選修的學生人數(shù) 10. Select * Form 課程 Where not exists (select * Form 選課 Where 課程 .課程號 =選課 .課程號 ) 從教學庫中查詢出所有未被學生選修的課程信息。 11. Select 專業(yè),性別, count(*) as 人數(shù) From 學生 Group by 專業(yè),性別 Order by 專業(yè) 從教學庫中查詢出每個專業(yè)每種性別的學生人數(shù),并按專業(yè)升序排列。 五、根據(jù)下面所給的 AAA數(shù)據(jù)庫,寫出每小題所能實現(xiàn)的功能。(每小題 5分,共 10分) 假設(shè)使用名稱為 AAA的數(shù)據(jù)庫,它包括 : Students( 學號 char(8),姓名 varchar(8),年齡 int,專業(yè) varchar(20),入學日期 DateTime)和 Score( 學號 char(8), 課程名 varchar(10),成績 numeric(5,2))兩張表。 1. declare @a numeric(5,2),@b numeric(5,2) set @a=(select max(成績 ) from score) set @b=(select min(成績 ) from score) print @a@b 求出 score表中最高成績與最低成績的分數(shù)之差 2. create procedure xxk2 as begin select , , ,count(*) as 門數(shù) from students x,score y where = group by , , end 顯示出 AAA庫中每個學生的學號、姓名、專業(yè)等信息及選課門數(shù) procedure xxk3 as begin select 學號 ,avg(成績 ) as 平均成績 from score group by 學號 end 顯示出 AAA庫中每個學生的平均成績 1. declare @a char(8) set @a=39。計算機 39。 select count(*) as 計算機專業(yè)人數(shù) from students where left(專業(yè) ,3)=@a 從 students表中統(tǒng)計出專業(yè)名開頭為 @a的值(即“計算機”)的所有學生人數(shù) procedure xxk4 ( @a char(8),@b varchar(10),@c numeric(5,2) ) as begin update score set 成績 =@c where 學號 =@a and 課程名 =@b end 修改 score表中學號為 @a的 值、課程名為 @b 的值的學生的成績?yōu)?@c的值 1. select year(入學日期 ) as 入學年份 ,count(*) as 人數(shù) from students group by year(入學日期 ) 從 Students 表中分組統(tǒng)計出每個年份入學的學生人數(shù)。 2. declare @a numeric(5,2) set @a=(select avg(成績 ) from score) select * from score where 成績 =@a 從 Score表中查詢出大于等于平均成績的所有記錄。 ‘學生號 ’,’課程號 ’,isnull(cast(null as char(6)),’無成績 ’) 學生號 課程號 無成績 0. create procedure xxk4 ( @a,char(8),@b,varchar(10),@c,numeric(5,2) ) As Begin Update score Set 成績 =@c Where 學號 =@a and 課程名 =@b End 修改 score表中學號為 @a 的值,課程為 @b 的值的學生的成績?yōu)?@c 的值。 students 表中所有學生記錄的學號列的前四個字符 Selext left (學號, 4) From students 2. create procedure xxkl as begin select * from students x, score y where = end 顯示出 AAA 庫中所有學生的記錄信息及選課成績。