(完整版)sql练习题+答案
(一) 新建以下几个表
student(学生表):
sno sname sex dept birth age
其中约束如下:
(1) 学号不能存在相同的
(2) 名字为非空
(3) 性别的值只能是’男’或’女’
(4) 系包括这几个:信息系,计算机科学系,数学系,管理系,中文系,外语系,法学系
(5) 出生日期为日期格式
(6) 年龄为数值型,且在0~100之间
create table student(
sno smallint constraint a primary key,----设置学生学号为student的主键
sname varchar(10) not null,
sex varchar(2) constraint b check(sex in('男','女')),----检查约束——性别的值只能是’男’或’女’
dept varchar(20) constraint c check(dept in('信息系','计算机科学系','数学系','管理系','中文系','外语系','法学系')),----检查约束——系包括这几个:信息系,计算机科学系,数学系,管理系,中文系,外语系,法学系
birth datetime,
age smallint constraint d check(age between 0
and 100)----检查约束——年龄为数值型,且在~100之间
)
cs(成绩表):
sno cno cj
其中约束如下:
(1)sno和cno分别参照student和course表中的sno,cno的字段
(2)cj(成绩)只能在0~100之间,可以不输入值
create table cs(
sno smallint not null references
student(sno),----定义成外键
cno smallint not null references
course(cno),----定义成外键
cj smallint constraint e check(cj between
0 and 100),----检查约束——cj(成绩)只能在~100之间,可以不输入值
constraint f primary key(sno,cno)----定义学生学号和课程号为sc表的主键
)
course(课程表)
cno cname
其约束如下:
(1)课程号(cno)不能有重复的
(2)课程名(cname)非空
create table course(
cno smallint not null constraint g primary
key,----设置课程号为course的主键
cname varchar(20) not null
)
(三)针对学生课程数据库查询
(1) 查询全体学生的学号与姓名。
Select sno,sname from student
(2) 查询全体学生的姓名、学号、所在系,并用别名显示出结果。
Select sname as '姓名',sno as '学号',dept as '所在地' from student
(3) 查询全体学生的详细记录。
select * from student
(4) 查全体学生的姓名及其出生年份。
select sname,birth from student
(5) 查询学校中有哪些系。
select distinct dept from student
(6) 查询选修了课程的学生学号。
select sno from cs where cno is not null
(7) 查询所有年龄在20岁以下的学生姓名及其年龄。
select sname,age from student where age < 20
(8) 查询年龄在20~23岁(包括20岁和23岁)之间的学生的姓名、系别和年龄。
select sname,dept,age from student where age
between 20 and 23
(9) 查询年龄不在20~23岁之间的学生姓名、系别和年龄。
select sname,dept,age from student where
age<20 or age>23
(10) 查询信息系、数学系和计算机科学系生的姓名和性别。
select sname,sex from student where dept='信息系' or dept='数学系' or dept='计算机科学系'
(11) 查询既不是信息系、数学系,也不是计算机科学系的学生的姓名和性别。
select sname,sex from student where dept!='信息系' and dept!='数学系' and dept!='计算机科学系'
(12) 查询所有姓刘学生的姓名、学号和性别。
select sname,sno,sex from student where sname
like('刘%')
(13) 查询学号为2009011的学生的详细情况。(具体的学号值根据表中数据确定)
select * from student where sno=5
(14) 查询姓“欧阳”且全名为三个汉字的学生姓名
select sname from student where sname like('欧阳_')
(15) 查询名字中第2个字为“晨”字的学生的姓名和学号
select sname,sno from student where sname
like('_晨')
(16) 查询所有不姓刘的学生姓名。
select sname,sno from student where sname not
like('刘%')
(17) 查询sql课程的课程号和学分。
select cno from course where cname='sql'
(18) 查询以"DB_"开头,且倒数第3个字符为 i的课程的详细情况。
select * from course where cname
like('DB[_]%i__')
(19) 查询缺少成绩的学生的学号和相应的课程号。
select sno,cno from cs where cj is null
(20) 查所有有成绩的学生学号和课程号。
select sno,cno from cs where cj is not null
(21) 查询计算机系年龄在20岁以下的学生姓名。
select sname from student where age < 20 and
dept='计算机科学系'
(22) 查询信息系、数学系和计算机科学系学生的姓名和性别。(使用多个条件表达式)
select sname,sex from student where dept='信息系' or dept='数学系' or dept='计算机科学系'
(23)
查询年龄在20~23岁(包括20岁和23岁)之间的学生的姓名、系别和年龄。(使用多个条件表达式)
select sname,dept,age from student where age
between 20 and 23
(24) 查询选修了3号课程的学生的学号及其成绩,查询结果按分数降序排列。
select sno,cj from cs where cno=3 order by cj
desc
(25)
查询全体学生情况,查询结果按所在系的系号升序排列,同一系中的学生按年龄降序排列。
select * from student order by dept asc,age
desc
(26) 查询学生总人数。
select count(*) from student
(27) 查询选修了课程的学生人数。
select count(sno) from cs where cno is not null
(28) 计算1号课程的学生平均成绩。
select avg(cj) from cs where cno=1
(29) 查询选修1号课程的学生最高分数。
select max(cj) from cs where cno=1
(30) 求各个课程号及相应的选课人数。
select o,count(cs.sno) from course
left join cs
on o=o group by o
(31) 查询选修了3门以上课程的学生学号。
select sno, count(cno) from cs group by sno
having count(cno)>3
(32) 查询有3门以上课程是90分以上的学生的学号及(90分以上的)课程数。
select sno, count(cno) as '课程数' from cs
where cj>90
group by sno having count(cno)>=3
(33) 查询学生2006011选修课程的总学分。
select sum(course) from course,cs where
o=cs.sno and cs.sno=2006011
(34) 查询每个学生选修课程的总学分。
select sno,sum(cj)from cs,course
where o=o
group by sno
union
sql练习题+答案
(一) 新建以下几个表
student(学生表):
sno sname sex dept birth age
其中约束如下:
(1) 学号不能存在相同的
(2) 名字为非空
(3) 性别的值只能是’男’或’女’
(4) 系包括这几个:信息系,计算机科学系,数学系,管理系,中文系,外语系,法学系
(5) 出生日期为日期格式
(6) 年龄为数值型,且在0~100之间
create table student(
sno smallint constraint a primary key,----设置学生学号为student的主键
sname varchar(10) not null,
sex varchar(2) constraint b check(sex in('男','女')),----检查约束——性别的值只能是’男’或’女’
dept varchar(20) constraint c check(dept in('信息系','计算机科学系','数学系','管理系','中文系','外语系','法学系')),----检查约束——系包括这几个:信息系,计算机科学系,数学系,管理系,中文系,外语系,法学系
birth datetime,
age smallint constraint d check(age between 0 and
100)----检查约束——年龄为数值型,且在~100之间
)
cs(成绩表):
sno cno cj
其中约束如下:
(1)sno和cno分别参照student和course表中的sno,cno的字段
(2)cj(成绩)只能在0~100之间,可以不输入值
create table cs(
sno smallint not null references
student(sno),----定义成外键
cno
smallint not null references course(cno),----定义成外键
SQL经典习题及答案(新手必看)
Student(S#,Sname,Sage,Ssex) 学生表
Course(C#,Cname,T#) 课程表
SC(S#,C#,score) 成绩表
Teacher(T#,Tname) 教师表
问题:
1、查询“001”课程比“002”课程成绩高的所有学生的学号;
select a.S# from (select s#,score from SC where C#='001') a,(select s#,score
from SC where C#='002') b
where a.score>b.score and a.s#=b.s#;
2、查询平均成绩大于60分的同学的学号和平均成绩;
select S#,avg(score)
from sc
group by S# having avg(score) >60;
3、查询所有同学的学号、姓名、选课数、总成绩;
select Student.S#,Student.Sname,count(SC.C#),sum(score)
from Student left Outer join SC on Student.S#=SC.S#
group by Student.S#,Sname
4、查询姓“李”的老师的个数;
select count(distinct(Tname))
from Teacher
where Tname like '李%';
5、查询没学过“叶平”老师课的同学的学号、姓名;
select Student.S#,Student.Sname
from Student
where S# not in (select distinct( SC.S#) from SC,Course,Teacher where SC.C#=Course.C#
sql练习题+答案 2
(一) 新建以下几个表
student(学生表):
sno sname sex dept birth age
其中约束如下:
(1) 学号不能存在相同的
(2) 名字为非空
(3) 性别的值只能是’男’或’女’
(4) 系包括这几个:信息系,计算机科学系,数学系,管理系,中文系,外语系,法学系
(5) 出生日期为日期格式
(6) 年龄为数值型,且在0~100之间
create table student(
sno smallint constraint a primary key,----设置学生学号为student的主键
sname varchar(10) not null,
sex varchar(2) constraint b check(sex in('男','女')),----检查约束——性别的值只能是’男’或’女’
dept varchar(20) constraint c check(dept in('信息系','计算机科学系','数学系','管理系','中文系','外语系','法学系')),----检查约束——系包括这几个:信息系,计算机科学系,数学系,管理系,中文系,外语系,法学系
birth datetime,
age smallint constraint d check(age between 0 and
100)----检查约束——年龄为数值型,且在~100之间
)
cs(成绩表):
sno cno cj
其中约束如下:
(1)sno和cno分别参照student和course表中的sno,cno的字段
(2)cj(成绩)只能在0~100之间,可以不输入值
create table cs(
sno smallint not null references
student(sno),----定义成外键
cno smallint not null references
sql语句练习题完整版
1、 查询Student表中的所有记录的Sname、Ssex和Class列
2、 查询教师所有的单位即不重复的Depart列。
3、 查询Student表的所有记录。
4、 查询Score表中成绩在60到80之间的所有记录。
5、 查询Score表中成绩为85,86或88的记录。
6、 查询Student表中“95031”班或性别为“女”的同学记录。
7、 以Class降序查询Student表的所有记录。
8、 以Cno升序、Degree降序查询Score表的所有记录。
9、 查询“95031”班的学生人数。
10、查询Score表中的最高分的学生学号和课程号。
11、查询„3-105‟号课程的平均分。
12、查询Score表中至少有5名学生选修的并以3开头的课程的平均分数。
13、查询最低分大于70,最高分小于90的Sno列。
14、查询所有学生的Sname、Cno和Degree列。
15、查询所有学生的Sno、Cname和Degree列。
16、查询所有学生的Sname、Cname和Degree列。
17、查询“95033”班所选课程的平均分。
18、假设使用如下命令建立了一个grade表:
create table grade(low number(3,0),upp number(3),rank char(1));
insert into grade values(90,100,‟A‟); insert into grade values(80,89,‟B‟);
insert into grade values(70,79,‟C‟) insert into grade values(60,69,‟D‟);
insert into grade values(0,59,‟E‟); commit;
Sql Server SQL练习题
测试表格
--1.学生表
Student(S#,Sname,Sage,Ssex)
--S# 学生编号,Sname 学生姓名,Sage 出生年月,Ssex 学生性别
--2.课程表
Course(C#,Cname,T#)
--C# --课程编号,Cname 课程名称,T# 教师编号
--3.教师表
Teacher(T#,Tname)
--T# 教师编号,Tname 教师姓名
--4.成绩表
SC(S#,C#,score)
--S# 学生编号,C# 课程编号,score 分数
创建测试数据
学生表 Student
create table Student(S# varchar(10),Sname nvarchar(10),Sage
datetime,Ssex nvarchar(10))
insert into Student values('01' , N'赵雷' , '1990-01-01' , N'男')
insert into Student values('02' , N'钱电' , '1990-12-21' , N'男')
insert into Student values('03' , N'孙风' , '1990-05-20' , N'男')
insert into Student values('04' , N'李云' , '1990-08-06' , N'男')
insert into Student values('05' , N'周梅' , '1991-12-01' , N'女')
insert into Student values('06' , N'吴兰' , '1992-03-01' , N'女')
insert into Student values('07' , N'郑竹' , '1989-07-01' , N'女')
insert into Student values('08' , N'王菊' , '1990-01-20' , N'女')
SQL语句练习题答案
SQL练习题及答案:
请用SQL语句实现以下功能。(1至6题每题10分,7至8题每题20分)
1. 如何用SQL语句求带辅助核算206科目12月的余额在贷方的合计值?cendd_c为余额方向字段,字符型,me为余额。
SELECT sum(me)
FROM gl_accass
WHERE ccode="206" and cendd_c="贷" and iperiod=12
2.如何查找GL_accass中的科目在科目表中不是末级的?(如下3种写法均正确)
SELECT ODE ,B.BEND
FORM GL_ACCASS AS A JOIN CODE AS B
ON ODE=ODE
WHERE B.BEND =0
select distinct ccode
from gl_accass
where ccode in(select ccode from code where bend=0)
select distinct ccode
from gl_accass
where ccode not in(select ccode from code where
bend=1) 3. 如何将AA库中的department表用语句把数据导入BB库中?
INSERT INTO BB..DEPARTMENT
SELECT * FROM AA..DEPARTMENT
4.如何查辅助总帐中的部门编号(cDEPT_ID)在部门目录(department 表cdepcode)中不存在的记录?
SELECT cDEPT_ID FROM GL_ACCASS
WHREE cDEPT_ID NOT IN
(SELECT cdepcode FROM DEPARTMENT)
5. 如何在部门目录表中查找部门名称(CDEPNAME)重复的记录数?
SELECT CDEPNAME, COUNT(CDEPNAME)
sql练习题 答案
(一) 新建以下几个表
student(学生表):
sno sname sex dept birth age
其中约束如下:
(1) 学号不能存在相同的
(2) 名字为非空
(3) 性别的值只能是’男’或’女’
(4) 系包括这几个:信息系,计算机科学系,数学系,管理系,中文系,外语系,法学系
(5) 出生日期为日期格式
(6) 年龄为数值型,且在0~100之间
create table student(
sno smallint constraint a primary key,----设置学生学号为student的主键
sname varchar(10) not null,
sex varchar(2) constraint b check(sex in('男','女')),----检查约束——性别的值只能是’男’或’女’
dept varchar(20) constraint c check(dept in('信息系','计算机科学系','数学系','管理系','中文系','外语系','法学系')),----检查约束——系包括这几个:信息系,计算机科学系,数学系,管理系,中文系,外语系,法学系
birth datetime,
age smallint constraint d check(age between 0 and 100)----检查约束——年龄为数值型,且在~100之间
)
cs(成绩表):
sno cno cj
其中约束如下:
(1)sno和cno分别参照student和course表中的sno,cno的字段
(2)cj(成绩)只能在0~100之间,可以不输入值
create table cs(
sno smallint not null references
student(sno),----定义成外键
cno smallint not null references
course(cno),----定义成外键
sql练习题 答案 2
1 (一) 新建以下几个表
student(学生表):
sno sname sex dept birth age
其中约束如下:
(1) 学号不能存在相同的
(2) 名字为非空
(3) 性别的值只能是’男’或’女’
(4) 系包括这几个:信息系,计算机科学系,数学系,管理系,中文系,外语系,法学系
(5) 出生日期为日期格式
(6) 年龄为数值型,且在0~100之间
create table student(
sno smallint constraint a primary key,----设置学生学号为student的主键
sname varchar(10) not null,
sex varchar(2) constraint b check(sex in('男','女')),----检查约束——性别的值只能是’男’或’女’
dept varchar(20) constraint c check(dept in('信息系','计算机科学系','数学系','管理系','中文系','外语系','法学系')),----检查约束——系包括这几个:信息系,计算机科学系,数学系,管理系,中文系,外语系,法学系
birth datetime,
age smallint constraint d check(age between 0 and 2 100)----检查约束——年龄为数值型,且在~100之间
)
cs(成绩表):
sno cno cj
其中约束如下:
(1)sno和cno分别参照student和course表中的sno,cno的字段
(2)cj(成绩)只能在0~100之间,可以不输入值
create table cs(
sno smallint not null references
student(sno),----定义成外键
cno smallint not null references course(cno),----定义成外键
sql经典练习题库(附答案)
SQL练习题库
表结构 Student(S#,Sname,Sage,Ssex) 学生表
Course(C#,Cname,T#) 课程表
SC(S#,C#,score) 成绩表
Teacher(T#,Tname) 教师表
试题:
1、查询“0001”课程比“C002”课程成绩高的所有学生的学号;
Select s# from sc
Where c#='0001' and score>any(select score from sc c#='0002')
2、查询平均成绩大于60分的同学的学号和平均成绩; select s#,avg(score) from SC
group by s#
having avg(score)>60
3、查询所有同学的学号、姓名、选课数、总成绩;
select student.s#,student.sname,count(sc.c#)as 选课数,sum(score)总成绩 from student,sc where student.s#=sc.s#
group by student.s#,student.sname
4、查询姓“张”的老师的个数;
select count(*)人数 from teacher
where tname like'张%' 5、查询没学过“叶平”老师课的同学的学号、姓名;
select student.s#,student.sname from student,course,teacher,sc
where student.s#=sc.s# and course.t#=teacher.t# and teacher.t# not in(select t# from teacher where tname='张丽芬')
group by student.s#,student.sname
6、查询学过“0001”并且也学过编号“0002”课程的同学的学号、姓名; select sc.s#,sname from sc,student
SQL考试题库及答案
SQL考试题库及答案
1. 选择题:以下哪个SQL命令用于创建一个新的数据库?
A. CREATE TABLE
B. CREATE DATABASE
C. DROP DATABASE
D. ALTER DATABASE
答案:B
2. 填空题:在SQL中,使用____关键字可以删除一个已经存在的表。
答案:DROP TABLE
3. 判断题:以下SQL语句是否正确?"SELECT * FROM users WHERE
name = 'John';"
A. 正确
B. 错误
答案:A
4. 简答题:解释SQL中的主键(PRIMARY KEY)是什么?
答案:主键是一个或多个字段的组合,用于在数据库表中唯一标识每条记录。一个表只能有一个主键,且主键的值不能为NULL。
5. 选择题:在SQL中,哪个函数用于计算表中所有记录的总数?
A. COUNT(*)
B. SUM(*)
C. AVG(*)
D. MAX(*)
答案:A
6. 填空题:在SQL中,使用____关键字可以更新表中的记录。
答案:UPDATE
7. 判断题:以下SQL语句是否正确?"INSERT INTO customers (name,
email)VALUES('Alice','*****************');"A. 正确
B. 错误
答案:A
8. 简答题:解释SQL中的外键(FOREIGN KEY)是什么?
答案:外键是一个字段(或一组字段),它在一个表中引用另一个表的主键。外键用于建立两个表之间的关系,并确保数据的引用完整性。
9. 选择题:在SQL中,哪个命令用于删除数据库中的表?
A. DELETE TABLE
B. DROP TABLE
C. REMOVE TABLE
D. CLEAR TABLE
答案:B
10. 填空题:在SQL中,使用____关键字可以添加一个新列到现有的表中。
答案:ALTER TABLE
