0

I am trying get information of 2 lowest salary holders from default "scott" table.

This is the query I am trying:

SELECT TOP 2 * FROM emp ORDER BY sal ASC

But I'm getting this error:

ORA-00923: FROM keyword not found where expected

Screenshot:

enter image description here

Kamil Gosciminski
  • 14,490
  • 4
  • 39
  • 60

3 Answers3

1

In the most recent versions of Oracle, you can use the ANSI standard:

SELECT emp.*
FROM emp
ORDER BY sal ASC
FETCH FIRST 2 ROWS ONLY;

In older versions:

SELECT e.*
FROM (SELECT emp.*
      FROM emp
      ORDER BY sal ASC
     ) e
WHERE rownum <= 2;
Gordon Linoff
  • 1,122,135
  • 50
  • 484
  • 624
0

You can use ROWNUM in Oracle to get two first rows of your query results.

SELECT EMP1.* FROM (SELECT * FROM EMP ORDER BY SAL ASC) EMP1 WHERE ROWNUM < 3;
evilone
  • 21,386
  • 7
  • 73
  • 103
0
  • TOP X is used by SQL Server
  • Fetch first is used by Oracle and DB2 (rownum is also available in Oracle)
  • Limit is used by mysql
  • Many can also use a windowing function (ROW_NUMBER()) in a sub query.

You want

select * from emp ORDER BY sal ASC
fetch first 2 rows only
Hogan
  • 63,843
  • 10
  • 75
  • 106