SQL Server经典查询语句练习题及答案
注意:在插入數(shù)據(jù)的時(shí)候,需要將zahowei改成中文,原數(shù)據(jù)是中文的,因?yàn)樽罱@個(gè)詞不能過審,只能用拼音代替了,可能是那個(gè)人出了啥事吧,審核不通過就挺莫名其妙的
現(xiàn)在有一教學(xué)管理系統(tǒng),具體的關(guān)系模式如下:
? ? ? ?Student (no, name, sex, birthday, class)
? ? ? ?Teacher (no, name, sex, birthday, prof, depart)
? ? ? ?Course (cno, cname, tno)
? ? ? ?Score (no, cno, degree)
其中表中包含如下數(shù)據(jù):
Course表:
Score表:
Student表:
Teacher表:
首先創(chuàng)建教學(xué)管理系統(tǒng)數(shù)據(jù)庫:
use mastercreate database Teach_manage on primary ( name='Teach_mange', filename='D:\sqldata\Teach_mange.mdf', size=4, maxsize=30, filegrowth=10% ) log on ( name='mange_log', filename='D:\sqldata\mange_log.ldf', size=3, maxsize=20, filegrowth=10% )DDL
1.寫出上述表的建表語句
命令:
create table Student(no int,name varchar(14),sex varchar(2),birthday date,class int); create table Teacher(no int,name varchar(14),sex varchar(2),birthday date,prof varchar(10),depart varchar(10)); create table Course(cno varchar(8),cname varchar(14),tno int); create table Score(no int,cno varchar(8),degree int);DML
2.給出相應(yīng)的INSERT語句來完成題中給出數(shù)據(jù)的插入
命令:
insert into Student values(5001,'李勇','男','1987-7-22',95001); insert into Student values(5002,'劉晨','女','1987-11-15',95002); insert into Student values(5003,'王敏','女','1987-10-5',95001); insert into Student values(5004,'李好尚','男','1987-9-25',95003); insert into Student values(5005,'李軍','男','1987-7-17',95004); insert into Student values(5006,'范新位','女','1987-6-18',95005); insert into Student values(5007,'張霞東','女','1987-8-29',95006); insert into Student values(5008,'zhaowei','男','1987-6-15',95007); insert into Student values(5009,'錢民將','女','1987-6-23',95008); insert into Student values(5010,'孫儷','女','1987-9-24',95002); insert into Student values(108,'趙里','男','1987-6-15',95007); insert into Student values(109,'丘處機(jī)','男','1987-6-23',95008); insert into Student values(107,'楊康','男','1987-9-24',95001);insert into Teacher values(1,'李衛(wèi)','男','1957-11-5','教授','電子工程系'); insert into Teacher values(2,'劉備','男','1967-10-9','副教授','math'); insert into Teacher values(3,'關(guān)羽','男','1977-9-20','講師','sc'); insert into Teacher values(4,'李修','男','1957-6-25','教授','elec'); insert into Teacher values(5,'諸葛亮','男','1977-6-15','教授','計(jì)算機(jī)系'); insert into Teacher values(6,'殷素素','女','1967-1-5','副教授','sc'); insert into Teacher values(7,'周芷若','女','1947-2-23','教授','sc'); insert into Teacher values(8,'趙云','男','1980-6-13','副教授','計(jì)算機(jī)系'); insert into Teacher values(9,'張敏','女','1985-5-5','助教','sc'); insert into Teacher values(10,'黃蓉','女','1967-3-22','副教授','sc'); insert into Teacher values(11,'張三','男','1967-3-22','副教授','sc');insert into Course values('3-101','數(shù)據(jù)庫',1); insert into Course values('5-102','數(shù)學(xué)',3); insert into Course values('3-103','信息系統(tǒng)',4); insert into Course values('3-104','操作系統(tǒng)',6); insert into Course values('3-105','數(shù)據(jù)結(jié)構(gòu)',4); insert into Course values('3-106','數(shù)據(jù)處理',5); insert into Course values('4-107','pascal語言',5); insert into Course values('4-108','C++',7); insert into Course values('4-109','java',8); insert into Course values('3-245','數(shù)據(jù)挖掘',10); insert into Course values('3-111','軟件工程',11);insert into Score values(5001,'3-105',69); insert into Score values(5001,'5-102',55); insert into Score values(5003,'4-108',85); insert into Score values(5004,'3-105',77); insert into Score values(5005,'3-245',100); insert into Score values(5006,'3-105',53); insert into Score values(5003,'4-109',45); insert into Score values(5008,'3-105',98); insert into Score values(5004,'4-109',68); insert into Score values(5010,'3-105',88); insert into Score values(5003,'3-105',98); insert into Score values(5005,'4-109',68); insert into Score values(5002,'3-105',88); insert into Score values(107,'3-105',98); insert into Score values(108,'4-109',68); insert into Score values(109,'3-105',88); insert into Score values(109,'4-109',80); insert into Score values(107,'3-111',88); insert into Score values(5003,'3-111',80);單表查詢
3.以class降序輸出student的所有記錄(student表全部屬性)
select *from Student order by class DESC4.列出教師所在的單位depart(不重復(fù))
select distinct depart from Teacher5.列出student表中所有記錄的name、sex和class列
select name,sex,class from Student6.輸出student中不姓王的同學(xué)的姓名
select name from Student where name <> '王%'7.輸出成績(jī)?yōu)?5或86或88或在60-80之間的記錄(no,cno,degree)
select *from Score where degree=85 or degree=86 or degree between 60 and 808.輸出班級(jí)為95001或性別為‘女’ 的同學(xué)(student表全部屬性)
select *from Student where class=95001 or sex='女'9.以cno升序、degree降序輸出score的所有記錄(score表全部屬性)
select *from Score order by cno , degree DESC10.輸出男生人數(shù)及這些男生分布在多少個(gè)班級(jí)中
select COUNT(class),COUNT(distinct class) from Student where sex='男'11.列出存在有85分以上成績(jī)的課程編號(hào)
select distinct cno from Score where degree>8512.輸出95001班級(jí)的學(xué)生人數(shù)
select COUNT(class) from Student where class=9500113.輸出‘3-105’號(hào)課程的平均分
select AVG(degree) from Score where cno='3-105'14.輸出student中最大和最小的birthday日期值
select MAX(birthday),MIN(birthday) from Student15.顯示95001和95004班全體學(xué)生的全部個(gè)人信息(不包括選課)。(student表全部屬性)
select *from Student where class=95001 or class=95004聚合查詢
16.輸出至少有5個(gè)同學(xué)選修的并以3開頭的課程的課程號(hào),課程平均分,課程最高分,課程最低分
命令:
17.輸出所選修課程中最低分大于70分且最高分小于90分的學(xué)生學(xué)號(hào)及學(xué)生姓名
命令:
18.顯示所教課程選修人數(shù)多于5人的教師姓名
命令:
19.輸出’95001’班級(jí)所選課程的課程號(hào)和平均分
命令:
20.輸出至少有兩名男同學(xué)的班級(jí)編號(hào)
命令:
多表查詢
21.列出與108號(hào)同學(xué)同年出生的所有學(xué)生的學(xué)號(hào)、姓名和生日
命令:
22.列出存在有85分以上成績(jī)的課程名稱
命令:
23.列出“計(jì)算機(jī)系”教師所教課程的成績(jī)表(課程編號(hào),課程名,學(xué)生名,成績(jī))
命令:
24.列出所有可能的“計(jì)算機(jī)系”與“電子工程系”不同職稱的教師配對(duì)信息,要求輸出每個(gè)老師的姓名(name)和(職稱)
命令:
25.列出所有處于不同班級(jí)中,但具有相同生日的學(xué)生,要求輸出每個(gè)學(xué)生的學(xué)號(hào)和姓名。(提示:使用datediff函數(shù),具體用法可以參考:
命令:
26.顯示‘張三’教師任課的學(xué)生姓名,課程名,成績(jī)
select Student.name,cname,degree from Teacher,Student,Course,Score where Teacher.no=Course.tno and Score.cno=Course.cno and Student.no=Score.no and Teacher.name='張三'27.列出所講課已被選修的教師的姓名和系別
select distinct name,depart from teacher,course,score where teacher.no=course.tno and course.cno=score.cno28.輸出所有學(xué)生的name、no和degree。(degree為空的不輸出和為空的輸出兩種情況)
select name,Student.no,degree from Student left join Score on Student.no=Score.noselect name,Student.no,degree from Student inner join Score on Student.no=Score.no29.列出所有任課教師的name和depart。(從課程選修和任課兩個(gè)角度考慮)
select distinct name,depart from Teacher,Score,Course where Teacher .no=Course.tno and Course.cno=Score.cnoselect distinct name,depart from Teacher inner join Course on Teacher.no=Course.tno30.輸出男教師所上課程名稱
select cname from Teacher join Course on Teacher.no=Course.tno where sex='男'31.輸出與“李軍”同性別的所有同學(xué)的name
select name from Student where sex=(select sex from Student where name='李軍')32.輸出選修“數(shù)據(jù)結(jié)構(gòu)”課程的男同學(xué)的成績(jī)
select degree from Course,Student,Score where Course.cno=Score.cno and Student.no=Score.no and sex='男' and cname='數(shù)據(jù)結(jié)構(gòu)'33.列出選修編號(hào)為‘3-105’課程并且該門課程成績(jī)比課程 ‘3-111’的最高分要高的cno,no和degree
select Score.cno,Score.no,Score.degree from Score,(select degree from Score where cno='3-111') as a where Score.cno='3-105' group by Score.cno,Score.no,Score.degree having Score.degree>MAX(a.degree)子查詢
34.輸出score中成績(jī)最高的學(xué)號(hào)和課程號(hào)
select no,cno from Score where degree=(select max(degree) from Score)35.輸出選修3-105課程,其成績(jī)高于109號(hào)同學(xué)在此課程所得成績(jī)的所有同學(xué)的學(xué)號(hào),姓名
select Student.no,Student.name from Student join Score on Student.no=Score.no where cno='3-105' and degree>(select degree from Score where Score.no='109')')
36.列出成績(jī)比該課程平均成績(jī)低的同學(xué)的學(xué)號(hào),成績(jī)和該門課的平均成績(jī)
select no,degree,a.avg_degree from score,(select cno,avg(degree)as avg_degree from score group by cno)as a(cno,avg_degree) where score.cno=a.cno and score.degree<a.avg_degree37.列出沒有實(shí)際授課的教師的姓名和系別
select name,depart from teacher where name not in(select name from teacher join course on teacher.no=course.tno join score on score.cno=course.cno)38.列出選修了編號(hào)為‘3-105’課程且其成績(jī)高于‘4-109’課程最高成績(jī)的同學(xué)的 課程編號(hào),學(xué)號(hào)和成績(jī)
select cno,no,degree from score where cno='3-105' and degree>(select max(degree) from score where cno='4-109')39.列出符合下述條件的所有可能的同學(xué)配對(duì)(sno1,sname1,sno2,sname2,difference)。其中要求學(xué)號(hào)為sno1的sname1同學(xué)的所學(xué)課程的平均分大于學(xué)號(hào)為sno2的sname2同學(xué)的所學(xué)課程平均分,兩個(gè)同學(xué)的課程平均分的差值difference為(sno1同學(xué)平均分-sno2同學(xué)平均分)
select a.no, a.name, b.no, b.name, c.avgdegree - d.avgdegree difference from Student as a, Student as b, (select no, avg(degree) as avgdegree from Score group by no) as c, (select no, avg(degree) avgdegree from Score group by no) as d where a.no = c.no and b.no = d.no and c.avgdegree > d.avgdegree總結(jié)
以上是生活随笔為你收集整理的SQL Server经典查询语句练习题及答案的全部?jī)?nèi)容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: 【OpenCV 例程200篇】78. 频
- 下一篇: python基础项目实践之: 学生通讯录