我使用了三个表students
,student_class
和class
。我正在使用内连接,部分得到我想要的结果。我的问题是在三个表中基于结果的平均值执行内连接。如何获得Basic Calculus学生的平均GPA? SQL
TABLES
SQL> SELECT * FROM Students;
STUDENT_ID STUDENT_NAME SCHOOL_ID GPA
---------- -------------------------------------------------- ---------- ----------
2 FREDDY FROST 11546 1.22
3 Frank Smith 11111 3.21
4 Rob S. Frost 11111 4
5 Sam John SMITH 11111 3.2
6 Sally SAvage 11546 3.54
7 Bart W. SImpson 11111 2.12
8 Franklin Q. Kafka 11111 3.2
9 Jacky Frost 11111 1.22
10 fran smith 11546 3.21
11 Ronda Frost 123134 4
12 Samuel Smith 11111 3.2
13 PAT SAMSON 123134 3.54
14 Bart Simpson 123134 2.12
15 JOHN Brown 11546 3.2
16 Robby Lee 123134 1.22
17 ABE FRANKS 123134 3.21
18 Robert Frost 11546 4
19 SAM Smith 11111 3.2
20 Fred SAvage 11546 3.54
21 Bart Simpson 123134 2.12
22 Frank Kafka 11546 3.2
21 rows selected.
SQL> SELECT * FROM Class;
CLASS_ID CLASS_NAME TEACHER_ID MAX_SEATS_AVAILABLE
---------- ------------------- ---------- -------------------
1 Intro to ALGEBRA 11 12
2 Basic CALCULUS 2 10
3 ABC and 123 1 15
4 Sharing 101 8 10
5 Good Talk, Bad Talk 9 20
6 Nap Time 1 21
7 WRITing 101 5 10
8 Finger Painting 9 14
9 Physics 230 2 20
10 Gym 5 25
10 rows selected.
SQL> SELECT * FROM STUDENT_CLASS;
CLASS_ID STUDENT_ID
---------- ----------
2 12
2 11
2 2
2 7
2 8
2 16
2 21
7 10
7 2
7 3
7 4
7 5
7 6
7 7
7 8
7 9
9 2
9 11
9 12
9 13
9 14
9 15
9 16
9 17
9 18
9 20
9 21
9 22
10 12
10 4
10 3
10 6
10 8
10 9
10 12
10 16
10 18
10 20
10 2
10 4
40 rows selected.
SQL> SELECT s.GPA,c.Class_Name,s.Student_ID,sc.Student_ID,sc.Class_ID,c.Class_ID From Students s INNER JOIN Student_c
lass sc ON sc.student_id = s.student_id INNER JOIN Class c ON c.class_ID = sc.class_ID where Upper(c.Class_name)= 'BA
SIC CALCULUS';
GPA CLASS_NAME STUDENT_ID STUDENT_ID CLASS_ID CLASS_ID
---------- ------------------- ---------- ---------- ---------- ----------
3.2 Basic CALCULUS 12 12 2 2
4 Basic CALCULUS 11 11 2 2
1.22 Basic CALCULUS 2 2 2 2
2.12 Basic CALCULUS 7 7 2 2
3.2 Basic CALCULUS 8 8 2 2
1.22 Basic CALCULUS 16 16 2 2
2.12 Basic CALCULUS 21 21 2 2
7 rows selected.
查询
SQL> SELECT s.GPA,c.Class_Name,s.Student_ID,sc.Student_ID,sc.Class_ID,c.Class_ID From Students s INNER JOIN Student_c
lass sc ON sc.student_id = s.student_id INNER JOIN Class c ON c.class_ID = sc.class_ID where Upper(c.Class_name)= 'BA
SIC CALCULUS'
答案 0 :(得分:0)
如果您只想将类名和平均GPA作为输出,则只需使用avg()
函数和按类名分组:
SELECT c.Class_Name, AVG(s.GPA) AS Avg_GPA
FROM Students s
INNER JOIN Student_class sc ON sc.student_id = s.student_id
INNER JOIN Class c ON c.class_ID = sc.class_ID
WHERE UPPER(c.Class_name)= 'BASIC CALCULUS'
GROUP BY c.Class_Name
这将是结果:
| Class_Name | Avg_GPA |
|----------------|---------|
| Basic CALCULUS | 2.44 |