數據庫實驗-數據查詢語言

(1)查詢學生的基本信息;

 

 select * from S;

 

     

 

(2)查詢「CS」系學生的基本信息;       

select * from S where Sdept =’CS’;

(3)查詢「CS」系學生年齡不在19到21之間的學生的學號、姓名;  數據庫

select Sno, Sname from S 

where Sdept = ‘CS’ and Sage not Between 19 and 21;

 

             

(4)找出最大年齡;     

select max(Sage) from S;

(5)找出「CS」系年齡最大的學生,顯示其學號、姓名;spa

select * from S where

Sage = (select max(Sage) from S where Sdept = 'CS');   

  

(6)找出各系年齡最大的學生,顯示其學號、姓名;  

  select Sno, Sname from S 
  where Sage in
 (select max(Sage) from S group by Sdept);

           

(7)統計「CS」系學生的人數;  

 select count(*) from S;

(8)統計各系學生的人數,結果按升序排列;      3d

select Sdept, count(*) from S group by Sdept

Order by count(*) asc;

             

(9)按系統計各系學生的平均年齡,結果按降序排列;

select Sdept, avg(Sage) as AVG from 
group by Sdept 
order by AVG desc;

(10)查詢每門課程的課程名;code

select Cname from C;

              

(11)查詢無先修課的課程的課程名和學時數;

select Cname, Ccredit from C where Cpno is null;

            

(12)統計無先修課的課程的學時總數;

select sum(Ccredit) from C 
where Cpno is null;

              

(13)統計每位學生選修課程的門數、學分及其平均成績;

select Sno ,count(SC.Cno), avg(Grade),Sum(C.Credit)
from SC,C 
where SC.Cno = C.Cno 
group by Sno;

         

(14)統計選修每門課程的學生人數及各門課程的平均成績;

select Cno, count(Sno) as num, avg(Grade) as Avg 
from SC
group by Cno;

(15)找出平均成績在85分以上的學生,結果按系分組,並按平均成績的升序排列;blog

select S.Sdept ,avg(Grade) as Avg , S.Sname as 'name'  from S
left join  SC on S.Sno = SC.Sno 
group by Sdept,S.Sname 
having avg(Grade) > 85 order by avg(Grade);

(16)查詢選修了「1」或「2」號課程的學生學號和姓名;it

select S.Sno, S.Sname from S, SC
where (SC.Cno = 1 or SC.Cno = 2)
and S,Sno = SC.Sno group by S.Sno, S.Sname
order by S.Sno, S.Sname;

(18)查詢選修了課程名爲「數據庫系統」且成績在60分如下的學生的學號、姓名和成績;class

     

select S.Sno , S.Sname ,SC.Grade from S ,SC, C 
where C.Cname = '數據庫' 
and Grade < 60 
and SC.Cno = C.Cno 
and SC.Sno = S.Sno;

(19)查詢每位學生選修了課程的學生信息(顯示:學號,姓名,課程號,課程名,成績);bfc

select * from S,C,SC 
where S.Sno = SC.Sno 
and C.Cno = SC.Cno;

(20)查詢沒有選修課程的學生的基本信息;select

select * from S 
where Sno not in (select Sno from SC);

(21)查詢選修了3門以上課程的學生學號;im

select Sno 
from SC 
group by Sno 
having count(Cno)>=3;

(22)查詢選修課程成績至少有一門在80分以上的學生學號;

select S.Sno 
from S left join SC on SC.Sno = S.Sno 
where Grade > 80 and Grade is not null 
group by S.Sno 
order by S.Sno

(23)查詢選修課程成績均在80分以上的學生學號;

select Sno, Sname from S 
where Sno in
(select Sno from SC group by Sno having avg(Grade) >=80);

(24)查詢選修課程平均成績在80分以上的學生學號;

select Sno, Sname from S 
where Sno in
(select Sno from SC group by Sno having avg(Grade) >=80);

相關文章
相關標籤/搜索