1. 程式人生 > >【Sql Server學習】Sql Server資料查詢

【Sql Server學習】Sql Server資料查詢

(1)查詢資訊工程學院(IS)的所有學生的姓名。

select sname,sdept
from student 
where sdept='is'

(2)查詢學生選課成績合格的成績資訊。
select grade from sc
where grade>=60

(3)查詢學分為3或4的課程名稱。

select cname from course where ccredit=3 or ccredit=4

(4)查詢所有課程名稱中含有“資料”的課程號。

select cno from course where cname like'%資料%'

(5)查詢所有姓王的學生資訊。
select * from student where sname like'%王%'

(7)查詢成績為88、90、92分的學生的成績資訊,查詢結果按成績升序排列。

select * from sc where grade in (88 ,90 ,92) order by grade

(8)查詢成績在80到90之間(包括80和90分),的學生的成績資訊,查詢結果按成績降序排列。

select * from sc where grade between 80 and 90 order by grade desc

(9)統計所有學生的平均年齡。

select avg (sage)from student

(10)統計學生的總人數。
select count(sname)from student

(11)查詢所有選課的學生的學號及其平均成績,按平均成績降序排列。

select distinct sno,avg(grade) from sc group by sno order by avg(grade) desc

(12)查詢所有學生的姓名和出生年份,並給該出生年份屬性列命名為出生年份。

select sname,2014-sage as'出生年月' from student

(13)查詢成績前3名的學生選課情況。

select top 3 * from sc order by grade desc