基本表結構:
student(sno,sname,sage,ssex)學生表
course(cno,cname,tno) 課程表
sc(sno,cno,score) 成績表
teacher(tno,tname) 教師表
1,查詢課程1的成績比課程2的成績高的所有學生的學號
select a.sno from
(select sno,score from sc where cno=1) a,
(select sno,score from sc where cno=2) b
where a.score>b.score and a.sno=b.sno
2,查詢平均成績大於60分的同學的學號和平均成績
select a.sno as "學號", avg(a.score) as "平均成績"
from
(select sno,score from sc) a
group by sno having avg(a.score)>60
3,查詢所有同學的學號、姓名、選課數、總成績
select a.sno as 學號, b.sname as 姓名,
count(a.cno) as 選課數, sum(a.score) as 總成績
from sc a, student b
where a.sno = b.sno
group by a.sno, b.sname
或者:
select student.sno as 學號, student.sname as 姓名,
count(sc.cno) as 選課數, sum(score) as 總成績
from student left Outer join sc on student.sno = sc.sno
group by student.sno, sname
4,查詢姓“張”的老師的個數
select count(distinct(tname)) from teacher where tname like '張%‘
或者:
select tname as "姓名", count(distinct(tname)) as "人數"
from teacher
where tname like'張%'
group by tname
5,查詢沒學過“張三”老師課的同學的學號、姓名
select student.sno,student.sname from student
where sno not in (select distinct(sc.sno) from sc,course,teacher
where sc.cno=course.cno and teacher.tno=course.tno and teacher.tname='張三')
6,查詢同時學過課程1和課程2的同學的學號、姓名
select sno, sname from student
where sno in (select sno from sc where sc.cno = 1)
and sno in (select sno from sc where sc.cno = 2)
或者:
selectc.sno, c.sname from
(select sno from sc where sc.cno = 1) a,
(select sno from sc where sc.cno = 2) b,
student c
where a.sno = b.sno and a.sno = c.sno
或者:
select student.sno,student.sname from student,sc where
student.sno=sc.sno and sc.cno=1
and exists( select * from sc as sc_2 where sc_2.sno=sc.sno and sc_2.cno=2)
7,查詢學過“李四”老師所教所有課程的所有同學的學號、姓名
select a.sno, a.sname from student a, sc b
where a.sno = b.sno and b.cno in
(select c.cno from course c, teacher d where c.tno = d.tno and d.tname = '李四')
或者:
select a.sno, a.sname from student a, sc b,
(select c.cno from course c, teacher d where c.tno = d.tno and d.tname = '李四') e
where a.sno = b.sno and b.cno = e.cno
8,查詢課程編號1的成績比課程編號2的成績高的所有同學的學號、姓名
select a.sno, a.sname from student a,
(select sno, score from sc where cno = 1) b,
(select sno, score from sc where cno = 2) c
where b.score > c.score and b.sno = c.sno and a.sno = b.sno
9,查詢所有課程成績小於60分的同學的學號、姓名
select sno,sname from student
where sno not in (select distinct sno from sc where score > 60)
10,查詢至少有一門課程與學號爲1的同學所學課程相同的同學的學號和姓名
select distinct a.sno, a.sname
from student a, sc b
where a.sno <> 1 and a.sno=b.sno and
b.cno in (select cno from sc where sno = 1)
或者:
select s.sno,s.sname
from student s,
(select sc.sno
from sc
where sc.cno in (select sc1.cno from sc sc1 where sc1.sno=1)and sc.sno<>1
group by sc.sno)r1
where r1.sno=s.sno
11. 請列出所有超過或等於5名學生的課
有一個courses 表 ,有: student (學生) 和 class (課程)。
請列出所有超過或等於5名學生的課。
例如,表:
±--------±-----------+
| student | class |
±--------±-----------+
| A | Math |
| B | English |
| C | Math |
| D | Biology |
| E | Math |
| F | Computer |
| G | Math |
| H | Math |
| I | Math |
±--------±-----------+
應該輸出:
±--------+
| class |
±--------+
| Math |
±--------+
Note:
學生在每個課中不應被重複計算。
SELECT class FROM courses GROUP BY class having count(DISTINCT student)>=5
12. 獲取 Employee 表中第 n 高的薪水(Salary)
編寫一個 SQL 查詢,獲取 Employee 表中第 n 高的薪水(Salary)。
±—±-------+
| Id | Salary |
±—±-------+
| 1 | 100 |
| 2 | 200 |
| 3 | 300 |
±—±-------+
例如上述 Employee 表,n = 2 時,應返回第二高的薪水 200。如果不存在第 n 高的薪水,那麼查詢應返回 null。
±-----------------------+
| getNthHighestSalary(2) |
±-----------------------+
| 200 |
±-----------------------+
CREATE FUNCTION getNthHighestSalary(@N INT) RETURNS INT AS
BEGIN
Declare M INT;
Set M = N-1;
RETURN (
/* Write your T-SQL query statement below. */
select DISTINCT Salary from Employee order by Salary DESC limit M, 1
);
END
13. 編寫一個 SQL 查詢來實現分數排名
編寫一個 SQL 查詢來實現分數排名。如果兩個分數相同,則兩個分數排名(Rank)相同。請注意,平分後的下一個名次應該是下一個連續的整數值。換句話說,名次之間不應該有“間隔”。
±—±------+
| Id | Score |
±—±------+
| 1 | 3.50 |
| 2 | 3.65 |
| 3 | 4.00 |
| 4 | 3.85 |
| 5 | 4.00 |
| 6 | 3.65 |
±—±------+
例如,根據上述給定的 Scores 表,你的查詢應該返回(按分數從高到低排列):
±------±-----+
| Score | Rank |
±------±-----+
| 4.00 | 1 |
| 4.00 | 1 |
| 3.85 | 2 |
| 3.65 | 3 |
| 3.65 | 3 |
| 3.50 | 4 |
±------±-----+
SELECT Score,
(SELECT count(DISTINCT score) FROM Scores WHERE score >= s.score) AS Rank
FROM Scores s ORDER BY Score DESC ;