免費開始練習
高雄醫學大學(後西醫) 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
idmovieyearactor
m_001The Matrix1999a_002
m_002Iron Man2008a_010
m_003Doctor Strange2016a_003
Table 4 actors
idnamesalary
a_001Brad Pitt250M
a_002Keanu Reeves200M
a_003Benedict Cumberbatch150M
Table 5 output
moviename
The MatrixKeanu 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) 的邏輯。

▼ 還有更多解析內容

🏷️ 相關主題

計算機組織結構與資料儲存原理
查看更多「計算機概論與程式設計」的主題分類考古題