1.创建学生表,学号,姓名,性别,年龄 2.为学号添加主键约束,插入15条记录 3.求出参加考试的学生的个数 4.求出总分,平均分,最高分,最低分 5.为学生表添加科目和分数列,修改数据,要求存储4个学生3门科目(语文/数学/英语)的成绩,即4个学生,3门科目,12条记录 6.求出每门科目的总分,平均分,最高分,最低分 7.求出每门科目的总分,平均分,最高分,最低分,要求只显示平均分大于70的 8.求出每门科目的总分,平均分,最高分,最低分,要求只显示平均分大于70的最后再按平均分由高到低排序 (*)9.求出每门科目中不及格和及格的人数
时间: 2023-12-12 07:02:38 浏览: 257
抱歉,作为一名语言模型,我无法执行数据库操作,如果您需要执行这些操作,请使用相应的数据库管理工具。但是,我可以告诉您如何用 SQL 语言实现这些功能。
1.创建学生表,学号,姓名,性别,年龄:
```sql
CREATE TABLE student (
id INT PRIMARY KEY,
name VARCHAR(50),
gender VARCHAR(10),
age INT
);
```
2.为学号添加主键约束,插入15条记录:
```sql
ALTER TABLE student
ADD CONSTRAINT pk_student_id PRIMARY KEY (id);
INSERT INTO student (id, name, gender, age) VALUES
(1, '张三', '男', 20),
(2, '李四', '女', 19),
(3, '王五', '男', 21),
(4, '赵六', '女', 22),
(5, '钱七', '男', 20),
(6, '孙八', '女', 19),
(7, '周九', '男', 21),
(8, '吴十', '女', 22),
(9, '郑一', '男', 20),
(10, '王二', '女', 19),
(11, '李三', '男', 21),
(12, '张四', '女', 22),
(13, '刘五', '男', 20),
(14, '赵六', '女', 19),
(15, '孙七', '男', 21);
```
3.求出参加考试的学生的个数:
```sql
SELECT COUNT(*) FROM student;
```
4.求出总分,平均分,最高分,最低分:
假设有一张成绩表 score,包含以下列:学号(student_id)、科目(subject)、分数(score)。
```sql
SELECT SUM(score) AS total_score, AVG(score) AS avg_score, MAX(score) AS max_score, MIN(score) AS min_score FROM score;
```
5.为学生表添加科目和分数列,修改数据,要求存储4个学生3门科目(语文/数学/英语)的成绩,即4个学生,3门科目,12条记录:
```sql
ALTER TABLE student
ADD COLUMN chinese INT,
ADD COLUMN math INT,
ADD COLUMN english INT;
UPDATE student SET chinese = 80, math = 90, english = 70 WHERE id = 1;
UPDATE student SET chinese = 85, math = 92, english = 75 WHERE id = 2;
UPDATE student SET chinese = 75, math = 85, english = 80 WHERE id = 3;
UPDATE student SET chinese = 90, math = 88, english = 78 WHERE id = 4;
```
6.求出每门科目的总分,平均分,最高分,最低分:
```sql
SELECT
SUM(chinese) AS chinese_total,
AVG(chinese) AS chinese_avg,
MAX(chinese) AS chinese_max,
MIN(chinese) AS chinese_min,
SUM(math) AS math_total,
AVG(math) AS math_avg,
MAX(math) AS math_max,
MIN(math) AS math_min,
SUM(english) AS english_total,
AVG(english) AS english_avg,
MAX(english) AS english_max,
MIN(english) AS english_min
FROM student;
```
7.求出每门科目的总分,平均分,最高分,最低分,要求只显示平均分大于70的:
```sql
SELECT
SUM(chinese) AS chinese_total,
AVG(chinese) AS chinese_avg,
MAX(chinese) AS chinese_max,
MIN(chinese) AS chinese_min,
SUM(math) AS math_total,
AVG(math) AS math_avg,
MAX(math) AS math_max,
MIN(math) AS math_min,
SUM(english) AS english_total,
AVG(english) AS english_avg,
MAX(english) AS english_max,
MIN(english) AS english_min
FROM student
WHERE
chinese_avg > 70 AND
math_avg > 70 AND
english_avg > 70;
```
8.求出每门科目的总分,平均分,最高分,最低分,要求只显示平均分大于70的最后再按平均分由高到低排序:
```sql
SELECT
subject,
SUM(score) AS total_score,
AVG(score) AS avg_score,
MAX(score) AS max_score,
MIN(score) AS min_score
FROM (
SELECT 'chinese' AS subject, chinese AS score FROM student
UNION ALL
SELECT 'math' AS subject, math AS score FROM student
UNION ALL
SELECT 'english' AS subject, english AS score FROM student
) s
GROUP BY subject
HAVING AVG(score) > 70
ORDER BY AVG(score) DESC;
```
9.求出每门科目中不及格和及格的人数:
```sql
SELECT
subject,
SUM(CASE WHEN score < 60 THEN 1 ELSE 0 END) AS failed_count,
SUM(CASE WHEN score >= 60 THEN 1 ELSE 0 END) AS passed_count
FROM (
SELECT 'chinese' AS subject, chinese AS score FROM student
UNION ALL
SELECT 'math' AS subject, math AS score FROM student
UNION ALL
SELECT 'english' AS subject, english AS score FROM student
) s
GROUP BY subject;
```
阅读全文