SQL 基础练习题组-19题

SQL 基础练习题组-19题


题目来源于 willcaty。在此特别感谢作者提供练习材料。

题面

有两张表student和score,两张表中的字段定义如下:
student:

字段名 字段描述 数据类型 主键 外键 非空 唯一 自增
id 学号 INT(10)
name 姓名 VARCHAR(20)
sex 性别 VARCHAR(4)
birth 出生年份 YEAR
department 院系 VARCHAR(20)
address 家庭住址 VARCHAR(50)

score:

字段名 字段描述 数据类型 主键 外键 非空 唯一 自增
id 编号 INT(10)
stu_id 学号 INT(10)
c_name 课程名 VARCHAR(20)
grade 分数 INT(10)

问题与解答

  1. 创建student和score表
create table student(
id int(10) not null unique primary key,
name varchar(20) not null,
sex varchar(4),
birth year,
department varchar(20),
address varchar(50)
);

create table score(
id int(10) not null unique primary key auto_increment,
stu_id int(10) not null,
c_name varchar(20),
grade int(10)
);

  1. 为两张表格增加记录
    student:
id name sex birth department address
901 张老大 1985 计算机系 北京市海淀区
902 张老二 1986 中文系 北京市昌平区
903 张三 1990 中文系 湖南省永州市
904 李四 1990 英语系 辽宁省阜新市
905 王五 1991 英语系 福建省厦门市
906 王六 1988 计算机系 湖南省衡阳市
insert into student values(901,'张老大','男',1985, '计算机系','北京市海淀区');
insert into student values(902,'张老二','男',1986,'中文系','北京市昌平区');
insert into student values(903,'张三','女',1990,'中文系','湖南省永州市');
insert into student values(904,'李四','男',1990,'英语系','辽宁省阜新市');
insert into student values(905,'王五','女',1991,'英语系','福建省厦门市');
insert into student values(906,'王六','男',1998,'计算机系','湖南省衡阳市');

score:

id stu_id c_name grade
NULL 901 计算机 98
NULL 901 英语 80
NULL 902 计算机 65
NULL 902 中文 88
NULL 903 中文 95
NULL 904 计算机 70
NULL 904 英语 92
NULL 905 英语 94
NULL 906 计算机 90
NULL 906 英语 85
insert into score values(null,901,'计算机',98);
insert into score values(null,901,'英语',80);
insert into score values(null,902,'计算机',65);
insert into score values(null,902,'中文',88);
insert into score values(null,903,'中文',95);
insert into score values(null,904,'计算机',70);
insert into score values(null,904,'英语',92);
insert into score values(null,905,'英语',94);
insert into score values(null,906,'计算机',90);
insert into score values(null,906,'英语',85);
  1. 查询student表的所有记录
select * from student;
  1. 查询student表的第2条到4条记录
select * from student limit 1,3;
  1. 从student表查询所有学生的学号(id)、姓名(name)和院系(department)的信息
select id,name,department from student;
  1. 从student表中查询计算机系和英语系的学生的信息
select * from student where department in ('计算机', '英语');
  1. 从student表中查询年龄18~22岁的学生信息
select * from student where year(curdate())-birth between 18 and 22;
  1. 从student表中查询每个院系有多少人
select department, count(*) as student_num from student group by department;
  1. 从score表中查询每个科目的最高分
select c_name, max(score) from score group by c_name;
  1. 查询李四的考试科目(c_name)和考试成绩(grade)
select c_name, grade from score,student where score.stu_id = student.id and student.name = '李四';
  1. 用连接的方式查询所有学生的信息和考试信息
select * from student inner join score on student.id= score.stu_id;
  1. 计算每个学生的总成绩
select stu_id, sum(score) as total_score from score group by stu_id;
  1. 计算每个考试科目的平均成绩
select c_name, avg(score) as avg_score from score group by c_name;
  1. 查询计算机成绩低于95的学生信息
select * from student where id in (select stu_id from score where c_name = '计算机' and score < 95);
  1. 查询同时参加计算机和英语考试的学生的信息
select * from student where id in (select stu_id from score
where c_name in ('计算机','英语') group by stu_id having count(*) = 2);
  1. 将计算机考试成绩按从高到低进行排序
select * from score where c_name = '计算机' order by score desc;
  1. 从student表和score表中查询出学生的学号,然后合并查询结果
select id from student
union
select stu_id from score;
  1. 查询姓张或者姓王的同学的姓名、院系和考试科目及成绩
select name, department, c_name, score from student inner join score
on student.id = score.stu_id where name like ‘[张王]%’;
  1. 查询都是湖南的学生的姓名、年龄、院系和考试科目及成绩
select name, year(curdate()) - birth as age, department,c_name, score from student inner score on student.id = score.stu_id where address like '湖南%';

猜你喜欢

转载自blog.csdn.net/Minervar/article/details/84798911