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)
);
创建score表。SQL代码如下:
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)
);
2.为student表和score表增加记录
向student表插入记录的INSERT语句如下:
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,'王六', '男',1988,'计算机系', '湖南省衡阳市');
向score表插入记录的INSERT语句如下:
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);
3.查询student表的所有记录
select * from student;
4.查询student表的第2条到4条记录
select * from student limit 1,3;
5.从student表查询所有学生的学号(id)、姓名(name)和院系(department)的信息
select id,name,department from student;
6.从student表中查询计算机系和英语系的学生的信息
select * from student where department="计算机系" or department="英语系";
7.从student表中查询年龄18~22岁的学生信息
select name,year(now())-birth as age from student where year(now())-birth>=18 and year(now())-birth<=22;
8.从student表中查询每个院系有多少人
select department as 系,count(*) as 人数 from student group by department;
9.从score表中查询每个科目的最高分
select c_name as 系,max(grade) as 最高成绩 from score group by c_name;
10.查询李四的考试科目(c_name)和考试成绩(grade)
select c_name as 科目,grade as 成绩 from student inner join score on student.id=score.stu_id where name="李四";
11.用连接的方式查询所有学生的信息和考试信息
select * from student inner join score on student.id=score.stu_id;
12.计算每个学生的总成绩
select name,sum(grade) from student inner join score on student.id=score.stu_id group by name;
13.计算每个考试科目的平均成绩
select c_name,avg(grade) from student inner join score on student.id=score.stu_id group by c_name;
14.查询计算机成绩低于95的学生信息
select name,c_name,grade from student inner join score on student.id=score.stu_id where c_name="计算机" and grade<95 group by name,c_name,grade;
15.查询同时参加计算机和英语考试的学生的信息
select * from student,score as s1,score as s2 where student.id=s1.stu_id and s1.c_name="计算机" and student.id=s2.stu_id and s2.c_name="英语";
16.将计算机考试成绩按从高到低进行排序
select name,c_name,grade from student inner join score on student.id=score.stu_id group by name,c_name,grade having c_name="计算机" order by grade desc;
17.从student表和score表中查询出学生的学号,然后合并查询结果
select id from student union select stu_id from score;
18.查询姓张或者姓王的同学的姓名、院系和考试科目及成绩
select name,department,c_name,grade from student inner join score on student.id=score.stu_id group by name,department,c_name,grade having name like "张%" or name like "王%";
19.查询都是湖南的学生的姓名、年龄、院系和考试科目及成绩
select address,name,department,c_name,grade,year(now())-birth as age from student inner join score on student.id=score.stu_id group by address,name,name,department,c_name,grade,age;
面试题:
创建学生表
CREATE TABLE student1 (
s_id INT PRIMARY KEY,
s_name VARCHAR(50),
s_birth DATE,
s_sex VARCHAR(10)
);
创建课程表
CREATE TABLE course (
c_id INT PRIMARY KEY,
c_name VARCHAR(50),
t_id INT
);
创建教师表
CREATE TABLE teacher (
t_id INT PRIMARY KEY,
t_name VARCHAR(50)
);
创建成绩表
CREATE TABLE score1(
s_id INT,
c_id INT,
s_score1 DECIMAL(5, 2),
PRIMARY KEY (s_id, c_id),
FOREIGN KEY (s_id) REFERENCES student1(s_id),
FOREIGN KEY (c_id) REFERENCES course(c_id)
);
随机插入数据
插入学生表数据
INSERT INTO student1 (s_id, s_name, s_birth, s_sex)
VALUES
(1, '张三', '2000-01-01', '男'),
(2, '李四', '1999-02-02', '女'),
(3, '王五', '2001-03-03', '男'),
(4, '赵六', '2002-04-04', '女'),
(5, '刘七', '2003-05-05', '男');
插入课程表数据
INSERT INTO course (c_id, c_name, t_id)
VALUES
(1, '数学', 101),
(2, '英语', 102),
(3, '物理', 103),
(4, '化学', 104),
(5, '历史', 105);
插入教师表数据
INSERT INTO teacher (t_id, t_name)
VALUES
(101, '张老师'),
(102, '李老师'),
(103, '王老师'),
(104, '赵老师'),
(105, '刘老师');
插入成绩表数据
INSERT INTO score1 (s_id, c_id, s_score1)
VALUES
(1, 1, 85.5),
(2, 1, 90.0),
(3, 1, 78.5),
(4, 1, 92.0),
(5, 1, 88.5);
1.查询所有学生的学号,姓名,选课数,总成绩。
SELECT
student1.s_id,
student1.s_name,
COUNT(score1.c_id) AS num_courses,
SUM(score1.s_score1) AS total_score1
FROM
student1
LEFT JOIN
score1 ON student1.s_id = score1.s_id
GROUP BY
student1.s_id, student1.s_name;
2.查询学过"张三"老师所教的所有课的同学的学号姓名。
SELECT
student1.s_id,
student1.s_name
FROM
student1
JOIN
score1 ON student1.s_id = score1.s_id
JOIN
course ON score1.c_id = course.c_id
JOIN
teacher ON course.t_id = teacher.t_id
WHERE
teacher.t_name = '张老师'
GROUP BY
student1.s_id, student1.s_name;
3.查询和"2"号同学学习的课程完全相同的其他同学的学号和姓名。
SELECT
student1.s_id,
student1.s_name
FROM
student1
JOIN
score1 ON student1.s_id = score1.s_id
WHERE
score1.c_id IN (
SELECT
c_id
FROM
score1
WHERE
s_id = 2
)
GROUP BY
student1.s_id, student1.s_name
HAVING
COUNT(DISTINCT score1.c_id) = (
SELECT
COUNT(DISTINCT c_id)
FROM
score1
WHERE
s_id = 2
)
AND student1.s_id != 2;
4.按平均成绩,从高到低显示所有学生的"数据库"(c_id='4'),"企业管理"(c_id='1'),"英语"(c_id='5')三门课程成绩,按如下形式显示:学生ID,数据库,企业管理,英语,有效课程,有效平均分
5.使用分段[100-85],[85-70],[70-60],[<60]来统计各科成绩,分别统计各分段人数:课程id和课程名称
SELECT
course.c_id,
course.c_name,
SUM(CASE WHEN score1 >= 85 AND score1 <= 100 THEN 1 ELSE 0 END) AS score1_85_100,
SUM(CASE WHEN score1 >= 70 AND score1 < 85 THEN 1 ELSE 0 END) AS score1_70_84,
SUM(CASE WHEN score1 >= 60 AND score1 < 70 THEN 1 ELSE 0 END) AS score1_60_69,
SUM(CASE WHEN score1 < 60 THEN 1 ELSE 0 END) AS score1_below_60
FROM
score1
JOIN
course ON score1.c_id = course.c_id
GROUP BY
course.c_id,
course.c_name;
SQL作业三
最新推荐文章于 2024-11-07 15:10:30 发布