---
# System prepended metadata

title: 12/20
tags: [database]

---

---
tags: database
---
# 12/20

1
查詢學生有任何一門課程成績在 70 分(含)以下的姓名、課程名稱和分數
```
SELECT s.s_name,c.c_name,r.r_result
FROM students as s,results as r,classes as c
WHERE s.s_no=r.s_no and r.c_no=c.c_no and r.r_result<=70
```
2
查詢平均成績大於 85 的所有學生的學號、姓名和平均成績；
```
SELECT s.s_no,s.s_name,AVG(r.r_result) 
FROM students as s,results as r
WHERE s.s_no=r.s_no
GROUP BY s.s_no,s.s_name
HAVING AVG(r.r_result)>85
```
3
查詢各課程分數大於總平均成績的所有學生的學號、姓名和課程、成績；
```
SELECT s.s_no,s.s_name,r.c_no,r.r_result
FROM students as s,results as r
WHERE r.r_result>(
SELECT AVG(r.r_result)
FROM results as r
) and r.s_no=s.s_no
```
4
查詢所有的學生平均成績及其名次；
```
SELECT s.s_no,AVG(r_result) '平均成績',RANK() OVER (ORDER BY AVG(r_result) DESC) 'rank'
FROM results as r,students as s
WHERE r.s_no=s.s_no
GROUP BY s.s_no
```
5
請查詢所有學生的姓氏及名字，顯示學號、姓氏及名字。
```
SELECT s.s_no,left(s.s_name,1),RIGHT(s.s_name,len(s.s_name)-1)
FROM students AS s
```