2112A-day_2/sql/text.sql

62 lines
1.5 KiB
SQL
Raw Blame History

This file contains ambiguous Unicode characters!

This file contains ambiguous Unicode characters that may be confused with others in your current locale. If your use case is intentional and legitimate, you can safely ignore this warning. Use the Escape button to highlight these characters.

CREATE DATABASES day_2
USE day_2
-- 学生表
CREATE TABLE t_student(
s_id INT50 AUTO_INCREMENT COMMENT'学生ID',
s_name VALUES(255) COMMENT'学生姓名',
s_sex VALUES(255) COMMENT'性别'
)
-- 课程表
CREATE TABLE t_course(
s_id INT50 AUTO_INCREMENT COMMENT'课程编号',
c_name VALUES(255) COMMENT'课程名称'
)
-- 选课表
CREATE TABLE stu_cou(
sc_id INT50 AUTO_INCREMENT COMMENT'选课ID',
c_id VALUES(255) COMMENT'课程编号',
s_id VALUES(255) COMMENT'学生'
)
-- 学生人数不少于10人。10分
INSERT INTO t_student VALUES
(0, '张三', ''),
(0, '李四', ''),
(0, '张三', ''),
(0, '李四', ''),
(0, '张三', ''),
(0, '李四', ''),
(0, '张三', ''),
(0, '李四', '');
-- 课程数量不少于3。5分
INSERT INTO t_course VALUES
(0, '语文'),
(0, '数学'),
(0, '英语'),
(0, '政治');
-- 每个学生可以选择多个课程每门课程至少有3个学生。15分
INSERT INTO stu_cou VALUES
(0, 1,1),
(0, 1,2),
(0, 1,3),
(0, 2,2),
(0, 2,5),
(0, 3,8),
(0, 3,7),
(0, 3,6),
(0, 4,2),
(0, 4,1),
(0, 4,9)
-- 3.查询男生和女生各多少人。15分
SELECT COUNT(s_sex),t.* FROM t_student t GROUP BY s_sex
-- 4. 查询男生和女生分别占总人数的比例。20分
SELECT AVG(t.s_id)/COUNT(s_sex) '总人数的比例',t.* FROM t_student t GROUP BY s_sex
-- 5.查询每门课程下的学生总人数。15分
SELECT t.*,COUNT(s.c_id) '课程人数' FROM stu_cou s LEFT JOIN t_student t ON s.s_id = t.s_id GROUP BY s.c_id