天天看點

SQL Server 2008:SELECT語句操作(包括:表的連接配接,複合查詢,子查詢, 條件、排序、彙總)

--五、 SELECT語句操作(包括:表的連接配接,複合查詢,子查詢, 條件、排序、彙總)

--1.查詢姓名第二個字為“寶”的學生資訊

select * from Student where StuName like '_寶%'

--2.從學生表(student)中查詢所有的資訊,要求隻顯示查詢結果的前行資料。

select top 6 * from Student

--3.查詢課程表的教師名,課程編号,課程名,要求查詢結果首先按照教師名降序排序,教師名相同時,則按照課程編号升序排序。

select Teacher,CouNo,CouName from Course order by Teacher desc,CouNo

--4.查詢課程表course的課程資訊、報名人數與限選人數之比。

select CouNo,CouName,Kind,Credit, WillNum/LimitNum as 報名人數與限選人數之比 from Course

--5.查詢報名人數大于平均報名人數的課程資訊。

select * from Course where WillNum>(select AVG(willnum) from course)

--6. 查詢學生的資訊,顯示資訊包括學生基本資訊和班級名稱。(連接配接查詢)

select student.*,ClassName from Student join Class on Student.ClassNo=Class.ClassNo

--7.按類别顯示課程資訊,并計算各類課程的平均報名人數。

select kind as 課程類别,AVG(WillNum) as 平均報名人數 from Course group by kind

--8.查詢所有學生報名選修課程的詳細情況,要求包括已報名選修課程的學生,

--也包括未報名選修課程的學生情況,顯示内容有學号stuno,課程編号couno,課程名稱couname.

select Student.stuno,Course.couno,couname from StuCou  right join Student on Student.StuNo=StuCou.StuNo

left join Course on StuCou.CouNo=Course.CouNo

--9.查詢報名人數多于或者少于人的課程資訊,要求查詢結果按報名人數降序排序。

select * from Course where WillNum>25 or WillNum<15 order by WillNum desc

--10.從課程表(course)中查詢課程類别,要求消除選擇值相同的那些行。

select distinct(kind) from Course