「Oracle」rownumを指定してトップ行の結果を抽出するサンプル

書式
SELECT * FROM テーブル名 WHERE ROWNUM <=XXX
使用例

Plain text
Copy to clipboard
Open code in new window
EnlighterJS 3 Syntax Highlighter
SELECT
*
FROM
(
SELECT
*
FROM
TEST_TABLE1
ORDER BY
AGE DESC
)
WHERE
ROWNUM <= 3
SELECT * FROM ( SELECT * FROM TEST_TABLE1 ORDER BY AGE DESC ) WHERE ROWNUM <= 3
SELECT
  * 
FROM
  ( 
    SELECT
      * 
    FROM
      TEST_TABLE1 
    ORDER BY
      AGE DESC
  ) 
WHERE
  ROWNUM <= 3

結果
AGE USERNAME
32 user001
28 user002
25 user003

Oracle

Posted by arkgame