Leetcode1412.查找成績(jī)處于中游的學(xué)生(困難)

題目
Table: Student

+---------------------+---------+
| Column Name         | Type    |
+---------------------+---------+
| student_id          | int     |
| student_name        | varchar |
+---------------------+---------+

student_id is the primary key for this table.
student_name is the name of the student.

Table: Exam

+---------------+---------+
| Column Name   | Type    |
+---------------+---------+
| exam_id       | int     |
| student_id    | int     |
| score         | int     |
+---------------+---------+

(exam_id, student_id) is the primary key for this table.
Student with student_id got score points in exam with id exam_id.

A "quite" student is the one who took at least one exam and didn't score neither the high score nor the low score.

Write an SQL query to report the students (student_id, student_name) being "quiet" in ALL exams.

Don't return the student who has never taken any exam. Return the result table ordered by student_id.

The query result format is in the following example.

Student table:

+-------------+---------------+
| student_id  | student_name  |
+-------------+---------------+
| 1           | Daniel        |
| 2           | Jade          |
| 3           | Stella        |
| 4           | Jonathan      |
| 5           | Will          |
+-------------+---------------+

Exam table:

+------------+--------------+-----------+
| exam_id    | student_id   | score     |
+------------+--------------+-----------+
| 10         |     1        |    70     |
| 10         |     2        |    80     |
| 10         |     3        |    90     |
| 20         |     1        |    80     |
| 30         |     1        |    70     |
| 30         |     3        |    80     |
| 30         |     4        |    90     |
| 40         |     1        |    60     |
| 40         |     2        |    70     |
| 40         |     4        |    80     |
+------------+--------------+-----------+

Result table:

+-------------+---------------+
| student_id  | student_name  |
+-------------+---------------+
| 2           | Jade          |
+-------------+---------------+

For exam 1: Student 1 and 3 hold the lowest and high score respectively.
For exam 2: Student 1 hold both highest and lowest score.
For exam 3 and 4: Studnet 1 and 4 hold the lowest and high score respectively.
Student 2 and 5 have never got the highest or lowest in any of the exam.
Since student 5 is not taking any exam, he is excluded from the result.
So, we only return the information of Student 2.

生成數(shù)據(jù)

CREATE TABLE Student3(
student_id         INT,     
student_name       VARCHAR(20));

CREATE TABLE Exam(
exam_id       INT,
student_id    INT,
score         INT);

INSERT INTO Student3 VALUE(1, 'Daniel'),(2, 'Jade'),(3, 'Stella'),(4, 'Jonathan'),(5, 'Will');

INSERT INTO Exam VALUE(10, 1, 70),(10, 2, 80),(10, 3, 90),(20, 1, 80),
(30, 1, 70),(30, 3, 80),(30, 4, 90),
(40, 1, 60),(40, 2, 70),(40, 4, 80);

解答
就是選出在每一門課中都沒有取到最高分也沒有取到最低分的同學(xué) 但是他得參加考試
對(duì)exam_id進(jìn)行分組 可以得到最高分和最低分
用二元in可以選出得到過最低分和最高分的同學(xué)
在exam中排除以上的同學(xué)即可
最后和Student 表左連接

先選出最高分和最低分

SELECT E.`exam_id`, MAX(E.`score`) AS max_sc
FROM Exam AS E
GROUP BY E.`exam_id`
UNION
SELECT E.`exam_id`, MIN(E.`score`) AS min_sc
FROM Exam AS E
GROUP BY E.`exam_id`;

再選出得到過最高分或最低分的同學(xué)

SELECT DISTINCT EEE.`student_id`
FROM Exam AS EEE
WHERE (EEE.`exam_id`, EEE.`score`) IN (SELECT E.`exam_id`, MAX(E.`score`) AS max_sc
FROM Exam AS E
GROUP BY E.`exam_id`
UNION
SELECT E.`exam_id`, MIN(E.`score`) AS min_sc
FROM Exam AS E
GROUP BY E.`exam_id`);

排除以上的同學(xué)

SELECT DISTINCT EE.`student_id`
FROM Exam AS EE
WHERE EE.`student_id` NOT IN (SELECT DISTINCT EEE.`student_id`
FROM Exam AS EEE
WHERE (EEE.`exam_id`, EEE.`score`) IN (SELECT E.`exam_id`, MAX(E.`score`) AS max_sc
FROM Exam AS E
GROUP BY E.`exam_id`
UNION
SELECT E.`exam_id`, MIN(E.`score`) AS min_sc
FROM Exam AS E
GROUP BY E.`exam_id`));

最后兩表連接即可 左連接

SELECT S.`student_id`, S.`student_name`
FROM (SELECT DISTINCT EE.`student_id`
FROM Exam AS EE
WHERE EE.`student_id` NOT IN (SELECT DISTINCT EEE.`student_id`
FROM Exam AS EEE
WHERE (EEE.`exam_id`, EEE.`score`) IN (SELECT E.`exam_id`, MAX(E.`score`) AS max_sc
FROM Exam AS E
GROUP BY E.`exam_id`
UNION
SELECT E.`exam_id`, MIN(E.`score`) AS min_sc
FROM Exam AS E
GROUP BY E.`exam_id`))) AS tmp
LEFT JOIN Student3 AS S
ON tmp.student_id = S.`student_id`;
?著作權(quán)歸作者所有,轉(zhuǎn)載或內(nèi)容合作請(qǐng)聯(lián)系作者
  • 序言:七十年代末,一起剝皮案震驚了整個(gè)濱河市荞胡,隨后出現(xiàn)的幾起案子妈踊,更是在濱河造成了極大的恐慌,老刑警劉巖泪漂,帶你破解...
    沈念sama閱讀 219,270評(píng)論 6 508
  • 序言:濱河連續(xù)發(fā)生了三起死亡事件廊营,死亡現(xiàn)場(chǎng)離奇詭異歪泳,居然都是意外死亡,警方通過查閱死者的電腦和手機(jī)露筒,發(fā)現(xiàn)死者居然都...
    沈念sama閱讀 93,489評(píng)論 3 395
  • 文/潘曉璐 我一進(jìn)店門呐伞,熙熙樓的掌柜王于貴愁眉苦臉地迎上來,“玉大人慎式,你說我怎么就攤上這事伶氢。” “怎么了瘪吏?”我有些...
    開封第一講書人閱讀 165,630評(píng)論 0 356
  • 文/不壞的土叔 我叫張陵癣防,是天一觀的道長。 經(jīng)常有香客問我掌眠,道長蕾盯,這世上最難降的妖魔是什么? 我笑而不...
    開封第一講書人閱讀 58,906評(píng)論 1 295
  • 正文 為了忘掉前任扇救,我火速辦了婚禮刑枝,結(jié)果婚禮上,老公的妹妹穿的比我還像新娘迅腔。我一直安慰自己装畅,他們只是感情好,可當(dāng)我...
    茶點(diǎn)故事閱讀 67,928評(píng)論 6 392
  • 文/花漫 我一把揭開白布沧烈。 她就那樣靜靜地躺著掠兄,像睡著了一般。 火紅的嫁衣襯著肌膚如雪锌雀。 梳的紋絲不亂的頭發(fā)上蚂夕,一...
    開封第一講書人閱讀 51,718評(píng)論 1 305
  • 那天,我揣著相機(jī)與錄音腋逆,去河邊找鬼婿牍。 笑死,一個(gè)胖子當(dāng)著我的面吹牛惩歉,可吹牛的內(nèi)容都是我干的等脂。 我是一名探鬼主播,決...
    沈念sama閱讀 40,442評(píng)論 3 420
  • 文/蒼蘭香墨 我猛地睜開眼撑蚌,長吁一口氣:“原來是場(chǎng)噩夢(mèng)啊……” “哼上遥!你這毒婦竟也來了?” 一聲冷哼從身側(cè)響起争涌,我...
    開封第一講書人閱讀 39,345評(píng)論 0 276
  • 序言:老撾萬榮一對(duì)情侶失蹤粉楚,失蹤者是張志新(化名)和其女友劉穎,沒想到半個(gè)月后,有當(dāng)?shù)厝嗽跇淞掷锇l(fā)現(xiàn)了一具尸體模软,經(jīng)...
    沈念sama閱讀 45,802評(píng)論 1 317
  • 正文 獨(dú)居荒郊野嶺守林人離奇死亡伟骨,尸身上長有42處帶血的膿包…… 初始之章·張勛 以下內(nèi)容為張勛視角 年9月15日...
    茶點(diǎn)故事閱讀 37,984評(píng)論 3 337
  • 正文 我和宋清朗相戀三年,在試婚紗的時(shí)候發(fā)現(xiàn)自己被綠了撵摆。 大學(xué)時(shí)的朋友給我發(fā)了我未婚夫和他白月光在一起吃飯的照片底靠。...
    茶點(diǎn)故事閱讀 40,117評(píng)論 1 351
  • 序言:一個(gè)原本活蹦亂跳的男人離奇死亡,死狀恐怖特铝,靈堂內(nèi)的尸體忽然破棺而出暑中,到底是詐尸還是另有隱情,我是刑警寧澤鲫剿,帶...
    沈念sama閱讀 35,810評(píng)論 5 346
  • 正文 年R本政府宣布鳄逾,位于F島的核電站,受9級(jí)特大地震影響灵莲,放射性物質(zhì)發(fā)生泄漏雕凹。R本人自食惡果不足惜,卻給世界環(huán)境...
    茶點(diǎn)故事閱讀 41,462評(píng)論 3 331
  • 文/蒙蒙 一政冻、第九天 我趴在偏房一處隱蔽的房頂上張望枚抵。 院中可真熱鬧,春花似錦明场、人聲如沸汽摹。這莊子的主人今日做“春日...
    開封第一講書人閱讀 32,011評(píng)論 0 22
  • 文/蒼蘭香墨 我抬頭看了看天上的太陽逼泣。三九已至,卻和暖如春舟舒,著一層夾襖步出監(jiān)牢的瞬間拉庶,已是汗流浹背。 一陣腳步聲響...
    開封第一講書人閱讀 33,139評(píng)論 1 272
  • 我被黑心中介騙來泰國打工秃励, 沒想到剛下飛機(jī)就差點(diǎn)兒被人妖公主榨干…… 1. 我叫王不留氏仗,地道東北人。 一個(gè)月前我還...
    沈念sama閱讀 48,377評(píng)論 3 373
  • 正文 我出身青樓夺鲜,卻偏偏與公主長得像廓鞠,于是被迫代替她去往敵國和親。 傳聞我的和親對(duì)象是個(gè)殘疾皇子谣旁,可洞房花燭夜當(dāng)晚...
    茶點(diǎn)故事閱讀 45,060評(píng)論 2 355