hce_kmu
111年
計算機概論與程式設計
第 24 題
Refer to Table 3 and Table 4, please indicate the CORRECT SQL statement that can output as depicted in Table 5.
Table 3 movies
| id | movie | year | actor |
|---|---|---|---|
| m_001 | The Matrix | 1999 | a_002 |
| m_002 | Iron Man | 2008 | a_010 |
| m_003 | Doctor Strange | 2016 | a_003 |
Table 4 actors
| id | name | salary |
|---|---|---|
| a_001 | Brad Pitt | 250M |
| a_002 | Keanu Reeves | 200M |
| a_003 | Benedict Cumberbatch | 150M |
Table 5 output
| movie | name |
|---|---|
| The Matrix | Keanu Reeves |
Table 3 movies
| id | movie | year | actor |
|---|---|---|---|
| m_001 | The Matrix | 1999 | a_002 |
| m_002 | Iron Man | 2008 | a_010 |
| m_003 | Doctor Strange | 2016 | a_003 |
Table 4 actors
| id | name | salary |
|---|---|---|
| a_001 | Brad Pitt | 250M |
| a_002 | Keanu Reeves | 200M |
| a_003 | Benedict Cumberbatch | 150M |
Table 5 output
| movie | name |
|---|---|
| The Matrix | Keanu Reeves |
- A SELECT * FROM movies
- B SELECT TOP 1 movies.movie, actors.name FROM movies INNER JOIN actors ON movies.actor = actors.id ORDER BY actors.salary DESC
- C SELECT movies.movie, actors.name FROM movies INNER JOIN actors ON movies.actor = actors.id ORDER BY actors.salary
- D All of the above can output the same results, as shown in Table 5.
- E None of the above can output the same results, as shown in Table 5.
思路引導 VIP
如果你面前有兩張彼此關聯的清單,而你被要求「只取出」合併之後排名最首位的一筆資料,你會如何下達指令,讓資料庫先決定好資料的先後順序,再精確地切下最上方的那份結果呢?
🤖
AI 詳解
AI 專屬家教
恭喜你精準地選出了正確答案!這顯示你對於 SQL 的多表查詢與結果集處理有著非常清晰的邏輯概念。
關聯式查詢與排序邏輯
在本題中,我們必須結合 movies 與 actors 兩張資料表。首先,透過 內部連接 (INNER JOIN),程式會比對 movies.actor 與 actors.id。觀察數據可以發現,只有演員編號 a_002 與 a_003 在兩表中都有紀錄,而 m_002 因為對應的 a_010 不存在於演員表中而被排除。接著,由於輸出結果(Table 5)僅顯示一筆資料,且該資料對應的是薪資較高(200M > 150M)的記錄,因此必須先以 salary 進行 降序排列 (DESC),再配合 TOP 1 語句擷取第一筆結果,這正是選項 (B) 的邏輯。
▼ 還有更多解析內容