10、select score.c_id,course.c_name,student.s_name,s_score from score join student on student.s_id=score.s_id join course on score.c_id='01' and course.c_id=score.c_id order by s_score desc limit 3; 关系代数表达式
时间: 2023-06-16 19:04:11 浏览: 103
这个 SQL 查询语句的关系代数表达式如下:
π score.c_id, course.c_name, student.s_name, score.s_score (σ score.c_id='01' (score ⨝ student ⨝ course)) ⨝ (score ⨝ student ⨝ course) ⨝ (score ⨝ student ⨝ course) -s_score
相关问题
select tb_student* from tb_student,tb_course,tb_score,tb_teacher, WHERE tb_teacher.id='张三'. and tb_student.id=tb_score.studentid and tb_score.courseid=tb_course.id and tb_course.teacherid=tb_teacher.id
这是一个 SQL 查询语句,其目的是查询学生表中所有选了张三老师教授的课程的学生信息。该查询语句中的语法存在一些错误,应该修改为:
```
SELECT tb_student.*
FROM tb_student, tb_course, tb_score, tb_teacher
WHERE tb_teacher.teacher_name = '张三' AND tb_student.id = tb_score.studentid AND tb_score.courseid = tb_course.id AND tb_course.teacherid = tb_teacher.id;
```
具体的查询条件是:在学生表、课程表、成绩表、教师表中,分别对应的字段是 tb_student.id、tb_course.id、tb_score.studentid、tb_course.teacherid、tb_teacher.teacher_name,通过这些字段的关联,筛选出教师姓名为张三的课程对应的学生信息。
需要注意的是,该查询语句中的表名和字段名应该根据实际情况进行修改,同时建议使用 JOIN 关键字来进行表的连接,可以使 SQL 查询语句更加清晰易懂。
我给你四张表,表结构是第一张表名称是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 查询和连接的知识才能解决。
阅读全文