SELECT st.s_name, sc.s_score, avg(sc.s_score) from student st left JOIN score sc on st.s_id = sc.s_id GROUP BY sc.s_score HAVING sc.s_score < "60" OR sc.s_score is null
时间: 2024-03-04 14:52:52 浏览: 126
这是一个 SQL 查询语句,目的是查询学生姓名、分数以及平均分数,其中包括分数小于60或者没有分数的情况。具体解释如下:
- SELECT st.s_name, sc.s_score, avg(sc.s_score):选择要查询的列,分别为学生姓名、分数和平均分数。
- FROM student st LEFT JOIN score sc ON st.s_id = sc.s_id:从学生表和分数表中选择数据,使用学生表的s_id和分数表的s_id进行连接。
- GROUP BY sc.s_score:按照分数进行分组。
- HAVING sc.s_score < "60" OR sc.s_score is null:筛选出分数小于60或者没有分数的数据。
希望能够解答你的问题。
相关问题
SELECT st.s_name, sc.s_score, avg(sc.s_score), COUNT(*) as count FROM student st LEFT JOIN score sc ON st.s_id = sc.s_id WHERE sc.s_score < 60 OR sc.s_score is null GROUP BY st.s_name, sc.s_score ORDER BY sc.s_score LIMIT 2;
这是一条 SQL 查询语句,用于查询学生的姓名、成绩、平均成绩和不及格科目数。具体来说,它的查询条件是成绩小于60或者为空的记录,使用了LEFT JOIN连接了学生表和成绩表,按照成绩升序排列,最后只返回前两条记录。
需要注意的是,这条语句可能只在特定的数据库环境下才能正确执行,因为不同的数据库系统可能有不同的语法和函数。如果需要更详细的解释,可以提供上下文和数据库类型等信息。
我给你四张表,表结构是第一张表名称是student,字段是 s_id,s_name,s_birth,s_sex; 第二张表名称是teacher字段是t_id,t_name 第三张表名称是course,字段是 c_id,c_name,t_id; 第四张表名称是score,表字段是 s_id,c_id,s_score,根据以上表结构生成难度高的oracle sql题目及答案
假设四张表的表结构如下:
```
CREATE TABLE student (
s_id INT PRIMARY KEY,
s_name VARCHAR2(50),
s_birth DATE,
s_sex VARCHAR2(10)
);
CREATE TABLE teacher (
t_id INT PRIMARY KEY,
t_name VARCHAR2(50)
);
CREATE TABLE course (
c_id INT PRIMARY KEY,
c_name VARCHAR2(50),
t_id INT,
CONSTRAINT fk_teacher FOREIGN KEY (t_id) REFERENCES teacher(t_id)
);
CREATE TABLE score (
s_id INT,
c_id INT,
s_score INT,
CONSTRAINT pk_score PRIMARY KEY (s_id, c_id),
CONSTRAINT fk_student FOREIGN KEY (s_id) REFERENCES student(s_id),
CONSTRAINT fk_course FOREIGN KEY (c_id) REFERENCES course(c_id)
);
```
现在给出以下复杂的 Oracle SQL 题目和答案:
1. 查询每门课程的平均分和最高分,按照平均分从高到低排序。
答案:
```
SELECT c.c_id, c.c_name, AVG(s.s_score) AS avg_score, MAX(s.s_score) AS max_score
FROM course c
LEFT JOIN score s ON c.c_id = s.c_id
GROUP BY c.c_id, c.c_name
ORDER BY avg_score DESC;
```
2. 查询每个学生的平均成绩,按照平均成绩从高到低排序。
答案:
```
SELECT s.s_id, s.s_name, AVG(sc.s_score) AS avg_score
FROM student s
LEFT JOIN score sc ON s.s_id = sc.s_id
GROUP BY s.s_id, s.s_name
ORDER BY avg_score DESC;
```
3. 查询没有选修任何课程的学生的姓名和性别。
答案:
```
SELECT s.s_name, s.s_sex
FROM student s
LEFT JOIN score sc ON s.s_id = sc.s_id
WHERE sc.s_id IS NULL;
```
4. 查询同时选修了编号为 1 和编号为 2 课程的学生的姓名。
答案:
```
SELECT s.s_name
FROM student s
INNER JOIN score sc ON s.s_id = sc.s_id
WHERE sc.c_id IN (1, 2)
GROUP BY s.s_id, s.s_name
HAVING COUNT(DISTINCT sc.c_id) = 2;
```
5. 查询选修了所有课程的学生的姓名。
答案:
```
SELECT s.s_name
FROM student s
INNER JOIN score sc ON s.s_id = sc.s_id
GROUP BY s.s_id, s.s_name
HAVING COUNT(DISTINCT sc.c_id) = (SELECT COUNT(*) FROM course);
```
这些题目和答案都比较复杂,需要熟练掌握 SQL 查询和连接的知识才能解决。
阅读全文