本題中所用的數據庫是上次實驗中所建立的Study數據庫。請寫出相應的查詢語句。並將查詢結果貼在下方。
- 查詢所有同學的基本信息,包括:學號s_no、班級號class_no、姓名s_name、性別s_sex、出生日期s_birthday。
use Study;
select s_no,class_no, s_name, s_sex, s_birthday from Student
go
- 查詢所有同學,要求顯示其學號s_no、姓名s_name。
use Study;
select s_no,s_name from Student
go
- 查詢所有男同學,要求顯示其學號s_no、姓名s_name、出生日期s_birthday。
use Study;
select s_no,s_name,s_birthday from Student where s_sex='男'
go
- 查詢所有出生日期在“1980-01-01”前的女同學,要求顯示其學號s_no、姓名s_name、性別s_sex、出生日期s_birthday。
use Study;
select s_no,s_name,s_sex,s_birthday from Student where s_sex='女' and s_birthday < '1980-01-01'
go
- 查詢所有姓“李”的男同學,要求顯示其學號s_no、姓名s_name、性別s_sex、出生日期s_birthday。
use Study;
select s_no,s_name,s_sex,s_birthday from Student where s_sex='男' and s_name like '李%'
go
- 查詢所有姓名中含有“一”字的同學,要求顯示其學號s_no、姓名s_name。
use Study;
select s_no,s_name from Student where s_name like '%一%' go
- 查詢所有職稱不是“講師”的教師,要求顯示其教師號t_no、姓名t_name、職稱t_title。
use Study;
select t_no,t_name,t_title from Teacher where t_title!='講師'
go
- 查詢雖選修了課程,但未參加考試的所有同學,要求顯示出這些同學的學號s_no。
use Study;
select s_no from Choice where score is null
go
- 查詢所有考試不及格的同學,要求顯示出這些同學的學號s_no、成績score,並按成績降序排列。
use Study;
select s_no,score from Choice where score<60 order by score desc
go
- 查詢出課程號爲01001、02001、02003的所有課程,要求顯示出課程號course_no、課程名稱course_name。(要求用in運算符)。
use Study;
select course_no, course_score from Course where course_no in('01001','02001','02003')
go
- 查詢所有在1970年出生的教師,要求顯示其教師號t_no、姓名t_name、出生日期t_birthday。
use Study;
select t_no, t_name,t_birthday
from Teacher
where t_birthday between '1970-01-01'and'1970-12-31'
go
- 查詢出選了課的學生的學號
use Study;
select distinct(s_no) from Choice
go
- 查詢出各個課程號course_no及相應的選課人數。
use Study;
select course_no,count(course_no) from Choice
group by(course_no)
go
- 查詢出教授兩門以上課程的教師號t_no。
use Study;
select t_no from Teaching
group by(t_no) having count(course_no)>=2
go
- 查詢出選修了01001課程的學生平均分數、最低分數及最高分數。
use Study;
select avg(score) as 平均分,min(score) as 最低分,max(score) as 最高分
from Choice
where course_no='01001'
go
- 查詢1960年以後出生的,職稱爲講師的教師的姓名t_name、出生日期t_birthday,並按出生日期升序排列。
use Study;
select t_name,t_birthday from Teacher where t_title='講師' and t_birthday>='1961-01-01'
order by(t_birthday)
go
補充題目:
本題中所用的數據庫是上次實驗中所建立的Study數據庫。請寫出相應的查詢語句。並將查詢結果貼在下方。
(1)查詢所有同學的選課及成績情況,要求顯示學生的學號s_no、姓名s_name、課程號course_no和課程的成績score。
use Study
select Student.s_no,s_name,course_no,score
from Choice,Student
where Student.s_no = Choice.s_no
go
(2)查詢所有同學的選課及成績情況,要求顯示學生的姓名s_name、課程名稱course_ name、課程的成績score,並將查詢結果存放到一個新的數據表new_table中。
use Study
select s_name,course_name,score
into new_table
from Choice,Student,Course
where Student.s_no = Choice.s_no and Choice.course_no=Course.course_no
go
(3)查詢“計算機99-1”班的同學的選課及成績情況,要求顯示學生的學號s_no、姓名s_name、課程號course_no、課程名稱course_name、課程的成績score。
use Study
select Student.s_no,Student.s_name,Course.course_name,Course.course_no,score
from Choice,Course,Student,Class
where
Choice.course_no=Course.course_no
and Student.s_no=Choice.s_no
and Student.class_no= Class.class_no
and class_name='計算機99-1'
go
(4)查詢所有同學的學分情況(假設課程成績≥60分時可獲得該門課程的學分),要求顯示學生的學號s_no、姓名s_name、總學分(將該列定名爲:total_score)。(用JOIN)
use Study
select Choice.s_no,s_name,sum(course_score) as total_score
from Choice join Student on Choice.s_no = Student.s_no join Course on Choice.course_no = Course.course_no
where score>=60
group by s_name,Choice.s_no
go
(5)查詢所有同學的平均成績及選課門數,要求顯示學生的學號s_no、姓名s_name、平均成績(將該列定名爲average_score)、選課的門數(將該列定名爲:choice_num)。
use Study
select Student.s_no,s_name,avg(score) as average_score,count(course_no) as choice_num
from Student join Choice on Student.s_no = Choice.s_no
group by s_name,Student.s_no
go
(6)查詢所有選修了課程但未參加考試的所有同學及相應的課程,要求顯示學生的學號s_no、姓名s_name、課程號course_no、課程名稱course_name。
use Study
select Student.s_no,s_name,Course.course_no,Course.course_name
from Student, Choice , Course
where Student.s_no =Choice.s_no and Choice.course_no =Course.course_no and score is null
go
(7)查詢所有選修了課程但考試不及格(假設<60分爲不及格)的所有同學及相應的課程,要求顯示學生的學號s_no、姓名s_name、課程號course_no、課程名稱course_name、學分course_score。
use Study
select Student.s_no,s_name,Course.course_no,course_name,course_score
from Choice,Student,Course
where Student.s_no = Choice.s_no and Choice.course_no=Course.course_no and score<60
go
(8)查詢選修了課程名爲“程序設計語言”的所有同學及成績情況,要求顯示學生的姓名s_name、課程的成績score。(使用ANY)
use Study
select s_name, score
from Choice,Student,Course
where Student.s_no = Choice.s_no and Choice.course_no=any(select course_no from Course where course_name='程序設計語言')
go
(9)查詢“計算機系”的所有同學及成績情況,要求顯示學生的學號s_no、姓名s_name、班級名稱class_name、課程號course_no、課程名稱course_name、課程的成績score。
use Study
select Choice.s_no,s_name,Class.class_name,Choice.course_no,course_name,score from Choice,Student,Course,Class
where Choice.course_no = Course.course_no and Student.s_no = Choice.s_no and Class.class_no = Student.class_no and class_special = '計算機'
go
(10)查詢所有教師的任課情況,要求顯示教師姓名t_name、擔任課程的名稱course_name。
use Study
select t_name,course_name
from Course,Teaching,Teacher
where Teaching.course_no = Course.course_no and Teaching.t_no = Teacher.t_no
go
(11)查詢所有教師的任課門數,要求顯示教師姓名t_name、擔任課程的門數(將該列定名爲course_number)。
use Study
select Teacher.t_no,t_name,count(Course.course_no) as course_number
from Course,Teaching,Teacher
where Teaching.course_no = Course.course_no and Teaching.t_no = Teacher.t_no
group by Teacher.t_no, t_name
go
(12)查詢和“李建國”是同一班級的同學的姓名。(使用子查詢)
use Study
select s_name
from Student
where Student.class_no = (select class_no from Student where s_name='李建國') and s_name!= '李建國'
go
(13)查詢沒有選修“計算機基礎”課程的學生姓名。(用NOT EXISTS)
use Study
select s_name
from Student
where not exists (select *
from Course,Choice
where Choice.course_no = Course.course_no and Student.s_no =Choice.s_no and course_name='計算機基礎' )
go
(14)查詢主講“數據庫原理與應用”和主講“數據結構”的教師姓名。(用UNION)
use Study
select t_name
from Teacher,Teaching,Course
where Teacher.t_no= Teaching. t_no and Teaching.course_no = Course.course_no and Course.course_name = '數據庫原理與應用'
union
select t_name
from Teacher,Teaching,Course
where Teacher.t_no= Teaching. t_no and Teaching.course_no = Course.course_no and Course.course_name = '數據結構'
go
(15)查詢講授了所有課程的教師的姓名。
use Study
select t_name
from Teacher
where not exists (select *
from Course
where not exists (select *
from Teaching
where Course.course_no = Teaching.course_no and Teacher.t_no = Teaching.t_no)
)
go