數據庫的有關知識==>>我們的血淚史之經典練習(1-2)


今天給大家說說數據庫的有關知識

抒情一下,想在好困,真的,雖然我在這溫暖的教室,身邊有知心的盆友, ,很高興還能是學生的一員,我們還年輕,我們也不會想的太多,高高興興上學,快快樂樂回家,每天吃的飽飽,聽着老濕講課,和童鞋打鬧....還是幸福的,不是嗎?好像有點精神了...

學習:

給大家說一下這些有關關鍵字的使用順序和內部運行順序(切記喲)

下面以如何查詢每門課程的平均分為例:

使用順序                                                                           內部運行順序

select SubjectId as 課程編號,AVG(studentresult) as 平均分    --04.投影結果
from Result                                                                     --01.定位到表
where SubjectId<3                                                          --02.分組前第一道過濾
group by SubjectId                                                          --03.分組
having COUNT(studentno)>3                                            --05.分組后第二道過濾
order by 課程編號 desc                                                     --06.最后排序

 

 

**************經典練習1*******************


--01.查詢每個年級的總學時數,並按照升序排列

select GradeId as 年級編號,SUM(ClassHour) as 總學時數
from Subject
group by GradeId
order by sum(ClassHour)

--02.查詢每個參加考試的學員平均分

select studentno as 學生編號,AVG(studentresult) as 平均分
from Result
group by StudentNo

--03.查詢每門課程的平均分,並按照降序排列

select subjectid as 學科編號,AVG(studentresult)as 平均分
from Result
group by SubjectId
order by 平均分 desc  --'平均分'也可以為'AVG(studentresult)'喲!

 --04.查詢每個學生參加所有考試的總分,並按照降序排列

select studentno as 學生編號,SUM(studentresult) as 總分
from Result
group by StudentNo
order by 總分 desc   --' 總分'也可以為'SUM(studentresult)'喲!

--05.每個年級 男女生總人數(gradeid,gender,人數)

select gradeid as 年級編號,gender as 性別,COUNT(1) as 人數
from student
group by GradeId,Gender
order by GradeId,Gender

--06.每個年級的總人數,滿足總人數必須大於等於3

select gradeid as 年級編號,COUNT(1) as 總人數
from student
group by GradeId
having COUNT(1)>=3

 *****************經典練習2****************

--01.查詢每年級學時數超過50 的課程數 S1

select gradeid as年級編號,COUNT(subjectid) as 課程數
from Subject
where  ClassHour>50
group by GradeId

--02.查詢每年級學生的平均年齡:

select gradeid as 年級編號,AVG(DATEDIFF(yy,birthday,GETDATE())) as 平均年齡
from student
group by gradeid

--03.查詢每個年級包含北京的學生總數 (like '%北京%')

select gradeid as 年級編號,COUNT(1) as 總人數
from student
where address like '%北京%'
group by Gradeid

 --04.查詢參加考試的學生中,平均分及格的學生記錄(學號,平均分),按照降序排列

select studentno as 學生編號,AVG(StudentResult) as 平均分
from Result
group by StudentNo
having AVG(StudentResult)>=60
order by 平均分 desc  --'平均分'也可改為'AVG(StudentResult)'喲!

--05.查詢考試時間為2015-12-20課程的及格平均分 (課程編號    所有課程平均分)

select subjectid as 學科編號,AVG(studentresult) as 平均分
from Result
where ExamDate>='2015-11-20' and  ExamDate<'2015-11-20'
group by SubjectId
having AVG(StudentResult)>=60

--06.統計至少有一次不及格的學生學號和次數。

select studentno as 學生編號,COUNT(1) as  次數
from Result
where StudentResult<60
group by StudentNo

 


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM