SQL server 数据库面试题及答案(实操)

发布时间:2023年12月25日

使用你的名字创建一个数据库

创建表:

班级表(clazz)

数据类型

约束等

字段描述

int

主键,自增

班级序号

varchar(32)

唯一键,不能为空

班级名称

varchar(32)

可空

班主任姓名

varchar(1024)

可空

班级简介

学生表(student)

字段名称

数据类型

约束等

字段描述

id

int

主键,自增

学生序号

name

varchar(32)

不能为空

学生姓名

sex

varchar(8)

不能为空,默认男

学生性别

age

int

不为空,年龄在(18,50]之间

学生年龄

clazz_id

int

外键(关联clazz表中的id字段)

所在班级序号

stu_num

varchar(32)

唯一键

学号

课程表(course)

字段名称

数据类型

约束等

字段描述

id

int

主键.自增

课程序号

name

varchar(32)

非空

课程名称

grade_point

float

非空

课程的学分绩点

description

varchar(1024)

非空

课程简介

选课表(selection)

字段名称

数据类型

约束等

字段描述

id

int

主键.自增

选课序号

course_id

int

外键(关联course表中的id字段)

外键,课程序号

student_id

int

外键(关联student表中的id字段)

外键,学生序号

score

float

成绩大于0

成绩

问题:

1:使用SQL语句完成数据库、表、约束的创建(create.sql)

2:使用SQL给每张表添加至少5条数据

3:查询出所有的学生信息并给每列设置中文列名

4:查询所有班级信息并将班级名称、班主任姓名两个字段按照“班级名称(班主任姓名)”显示

5:查询出班级中的所有班主任姓名,要求不能有重复名字

6:查询出前3名学生信息

7:查询出前15%学生信息

8:查询出性别为“男”的学生信息

9:查询出姓名不是“张三”的学生信息

10:查询出序号在10-30之间的学生信息

11:查询出序号不在10-30之间的学生信息

12:查询出序号为 1,3,4,5,8,9的学生信息

13:查询出学生序号为2并且课程序号为3的成绩

14:查询出学生姓名里包含“国”的学生信息

15:按照成绩降序输出成绩表中课程序号为3的所有信息

16:按照成绩升序输出成绩表中学生序号为2的所有信息

17:按照成绩降序、课程序号升序查询出成绩表中学生序号为1的所有信息

3:查询出课程序号为2的总分

4:查询出课程序号为2的平均分

5:查询出成绩表中的最高分

6:查询出成绩表中的最低分

7:查询出成绩表中课程序号为2的最高分

8:查询出成绩表中课程序号为2的最低分

9:查询出参加考试的人数

10:查询每个课程参加考试的人数

11:统计平均分中大于60的课程序号,并显示出平均成绩。

答案:

/*创建数据库*/
create database gaohongyuan
on primary?
(name="gaohongyuan_data",
filename="c:\gao\gaohongyuan_data.mdf",?
size=8MB,
maxsize=100MB,?
filegrowth=10%)?
log on?
(name="gaohongyuan_log.ldf",?
filename="c:\gao\gaohongyuan_log.ldf",?
size=1MB,?
filegrowth=10%)


use gaohongyuan/*切换数据库*/

/*创建班级表*/
create table clazz(
cla_id int identity(1,1) primary key not null,
cla_name varchar(32) unique not null,
cla_tea_name varchar(32),
cla_intro varchar(1024)
)
/*创建学生表*/
create table student(
stu_id int identity(10000,1) primary key not null,
stu_name varchar(32) not null,
stu_sex varchar(8) default('男') not null,
stu_age int check(stu_age>=18 and stu_age<=50),
cla_id int references clazz(cla_id),
stu_num varchar(32) unique not null
)
/*创建课程表*/
create table course(
course_id int identity(1,1) primary key not null,
cou_name varchar(32) not null,
grade_point float not null,
descripion varchar(1024) not null
)
/*创建选课表*/
create table selection(
sele_id int identity(1,1) primary key not null,
course_id int references course(course_id),
stu_id int references student(stu_id),
score float check(score>0)
)

/*插入数据*/
select * from clazz/*查询语句*/
insert clazz values
('班级1','张强','一班'),
('班级2','李华','二班'),
('班级3','小明','三班')

select * from student/*查询语句*/
insert ?values
('张三','男','21','1','s001'),
('李四','男','20','1','s002'),
('王五','男','19','1','s003'),
('赵六','女','22','1','s004'),
('刘七','女','21','1','s005'),
('陈八','男','20','2','s006'),
('政九','女','19','2','s007'),
('孙十','女','22','2','s008'),
('冯十一','女','21','2','s009'),
('吴十二','男','22','3','s010')

select * from course/*查询语句*/
insert course values
('数学','4.0','数学课程'),
('英语','3.5','英语课程'),
('语文','4.0','语文课程')

select * from selection/*查询语句*/
insert selection(stu_id,course_id,score) values
(10000,1,'78'),
(10000,2,'89'),
(10000,3,'90'),
(10001,1,'45'),
(10001,2,'67'),
(10001,3,'90'),
(10002,1,'98'),
(10002,2,'99'),
(10002,3,'100'),
(10003,1,'98'),
(10003,2,'92'),
(10003,3,'90'),
(10004,1,'11'),
(10004,2,'24'),
(10004,3,'16'),
(10005,1,'67'),
(10005,2,'56'),
(10005,3,'66'),
(10006,1,'45'),
(10006,2,'67'),
(10006,3,'89'),
(10007,1,'90'),
(10007,2,'89'),
(10007,3,'97'),
(10008,1,'76'),
(10008,2,'87'),
(10008,3,'88'),
(10009,1,'54'),
(10009,2,'62'),
(10009,3,'61')

select stu_name 姓名,stu_sex 性别,stu_age 年龄,cla_id 班级序号, stu_num 学号 from student
select cla_name + cla_tea_name '班级名称(班主任姓名)' from clazz
select distinct cla_tea_name from clazz
select top 3 stu_id 学号,score 成绩 from selection order by score desc
select top 15 percent stu_id 学号,score 成绩 from selection order by score desc
select * from student where not stu_sex='男'
select * from student where stu_name!='张三'
select * from student where stu_id between 10003 and 10006
select * from student where stu_id not in(10003,10004,10005,10006)
select * from student where stu_id in(10001,10003,10005,10008,10009)
select * from selection where stu_id=10002 and course_id=3
select * from student where stu_name like '%十%'
select * from selection where course_id=3 ?order by score desc
select * from selection where course_id=2 order by score asc
select * from selection where stu_id=10001 order by score desc,course_id asc
select sum(score) from selection where course_id=2
select AVG(score) from selection where course_id=2
select MAX(score) from selection?
select MIN(score) from selection
select MAX(score) from selection where course_id=2
select min(score) from selection where course_id=2
select course_id, count(stu_id) from selection group by course_id
select course_id,avg(score) from selection group by course_id having avg(score)>60

文章来源:https://blog.csdn.net/m0_71071763/article/details/135197227
本文来自互联网用户投稿,该文观点仅代表作者本人,不代表本站立场。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。