目錄
🎇簡單查詢
🎇連接查詢
🎇嵌套查詢
分析&思考?
🎇簡單查詢
--練習簡單查詢
--select * from classes
--select * from student
--select * from scores
--1.按Schedule表的結構要求用SQL語言創建Schedule表
--字段名 字段描述 數據類型 主鍵 外鍵 非空 唯一 自增
--ID 編號 int 是√ 否 是 是√ 是√
--TeacherID 教師號 char(8) 否 是√ 是√ 否 否
--CourseID 課程號 char(8) 否 是 是 否 否
--ClassID 班級號 char(8) 否 是 是 否 否
--Semester 開課學期 int 否 否 是 否 否
--SchoolYear 學年 int 否 否 否 否 否--?
--use student
--go
--create table schedule
--(
--ID int identity primary key,
--TeacherID char(8) not null references Teacher(TeacherID),
--CourseID char(8) not null references Course(CourseID),
--ClassID char(8) not null references Class(ClassID),
--Semester int not null,
--SchoolYear int
--)
--go--//
--(2)查詢teacher表中所有教師的姓名和年齡。
--select teachername 姓名,year(getdate())-year(birth) 年齡
--from teacher
--//--(2)查詢student表中所有學生的姓名和性別。
--select sname 姓名,ssex 性別 from student--(3)查詢所有系的信息。
--select * from classes--(4).查詢成績值大于等于60的學生的學號。
--select sno from scores
--where grade>60--(5)查詢軟件06101班的女生信息。
--select * from student
--where ssex='女'
--and classno='軟件06101'--(6)查詢學生姓名中第2個字為“麗”的學生信息。
--select * from student
--where sname like '_麗%'--(7)查詢student表姓名和性別。
--select sname 姓名,ssex 性別 from student--(8)查詢student表中前5行數據。
--select top 5 * from student--(9)查詢選修了體育課程的學生的學號和成績,將查詢結果按成績降序排序,成績相同按學號升序排序。
--select sno 學號,grade 成績 from scores
--where course='大學英語'
--order by grade desc,sno asc--成績降序,學號升序----//(10)查詢course表中的最大學分的課程名。(用order by子句)。
--select top 1 with ties coursename
--from course
--order by credit desc//降序
----//--(11)查詢不同系部的班級數。
--select count(classno)
--from classes
--group by dept----(12)統計各個班的學生人數。
--select classno,count(sno)
--from student
--group by classno--(13)查詢課程Dp030001的最高分、最低分和平均分。
--select max(grade) 最高分,min(grade) 最低分,avg(grade) 平均分
--from scores
--where course='體育'--(14)查詢grade表中選修了3門以上課程的學生學號。
--select studentid
--from grade
--group by studentid
--having count(courseid)>3----(15)查詢不同班級不同性別學生人數。
--select classno,ssex,count(sno) from student
--group by classno,ssex
🎇連接查詢
--(1)查詢課程名及該課程的得分情況。
--select course 課程名,grade 得分 from scores --兩張表
--(2)查詢學生姓名及其所學生的課程名。
--select sname 姓名,course 課程名 from student join scores on student.sno=scores.sno------(3)查詢和‘程玲’同學同班的學生信息。
--select * from student
--where classno=(select classno from student where sname='程玲')--一張表
--(4)查詢比6320210615 同學成績高的信息。
--select s1.grade from scores s1
--join scores s2 on s1.grade>s2.grade
--and s2.sno='6320210615 '--(5)
--select dept,student.classno,sno,sname,ssex from student join classes on student.classno=classes.classno--(6)查詢既學了了又選修了課程的學生的學號,姓名。
--select student.sno 學號,sname 姓名 from student join scores on student.sno=scores.sno
--where course='大學英語' and course='體育'--(7) “大學英語(一)”成績不及格的學生人數是多少?
--select count(student.sno) from student join scores on student.sno=scores.sno
--where course='大學英語' and grade<60--(8)計算機系的平均成績為多少?
--select avg(grade) from student
--join classes on student.classno=classes.classno
--join scores on student.sno=scores.sno
--where dept='計算機系'--(9)查詢全部教師、全部課程的課程安排。--(10)查詢“大學英語”課程前三名的學生學號、姓名和成績。
--select top 3 with ties
--student.sno,sname,grade from student join scores on student.sno=scores.sno
--where grade='大學英語'
--order by grade desc--降序
🎇嵌套查詢
----(1)嵌套查詢 “計算機系”的學生信息。
--select * from student
--where classno in(select classno from classes where dept='計算機系')--(2)嵌套查詢“計算機系”的全部學生信息。
--select * from student
--where classno in(select classno from classes where dept='計算機系')--(3)嵌套查詢xx課程中成績未達到該門課程平均分的同學信息。
--SELECT *
--FROM grade
--WHERE CourseID = 'Dp010001'
--AND grade <
-- (SELECT AVG(grade)
-- FROM grade
-- WHERE CourseID='Dp010001')
--或者
--select * from student
--where sno in(select sno from scores where course='大學英語' and
--grade <(select avg(grade) from scores where course='大學英語'
--))--(4)嵌套查詢大學英語課程中最低分的學生信息。
--select * from student where sno in
--(select sno from scores where course='大學英語'
--and grade=(select min(grade) from scores where course='大學英語'))--(4)嵌套查詢Dp010001課程中最低分的學生信息。
--SELECT *
--FROM student
--WHERE StudentID IN
-- (SELECT StudentID
-- FROM grade
-- WHERE CourseID = 'Dp010001'
-- AND grade =
-- (SELECT MIN(grade)
-- FROM grade
-- WHERE CourseID = 'Dp010001'))--(5)嵌套查詢Cs010901班比Cs010902班年齡都大的學生信息。
--SELECT *
--FROM student
--WHERE ClassID='Cs010901'
--AND Birth < ALL
-- (SELECT Birth
-- FROM student
-- WHERE ClassID='Cs010902')--(6)用帶EXISTS子查詢選修了Dp010001的學生學號和姓名。
--SELECT StudentID,StudentName
--FROM student
--WHERE EXISTS
-- (SELECT *
-- FROM grade
-- WHERE CourseID='Dp010001'
-- AND student.StudentID=grade.StudentID)--(7)查詢選修了Dp010001課程而沒有選修Dp010002號課程的學生學號。
--SELECT StudentID
--FROM grade
--WHERE CourseID='Dp010001'
--AND StudentID NOT IN
-- (SELECT StudentID
-- FROM grade
-- WHERE CourseID='Dp010002')
----(8)合并顯示教師中的男性教師和有教授職稱的教師。
--SELECT *
--FROM Teacher
--WHERE Sex='男'
--UNION
--SELECT *
--FROM Teacher
--WHERE Profession='教授'--(9)查詢student表中女同學的學號,姓名及根據學號前兩位判斷所處年級(St01為‘大一’02為‘大二’,其他的不清楚)。
--SELECT StudentID,StudentName,
-- CASE
-- WHEN StudentID LIKE 'St01%' THEN '大一'
-- WHEN StudentID LIKE 'St02%' THEN '大二'
-- ELSE '不清楚'
-- END as 年級
--FROM Student
--WHERE Sex='女'
分析&思考?
?