Search code examples
sqloracle-databaseora-00904rownum

Sql query on rownum


SELECT instmax
FROM
  (SELECT instmax ,rownum r
  FROM
    ( SELECT instmax FROM pswlinstmax ORDER BY instmax DESC NULLS LAST
    )
  WHERE r = 2
  );

After execution it's giving this error:

ORA-00904: "R": invalid identifier
00904. 00000 -  "%s: invalid identifier"
*Cause:    
*Action:
Error at Line: 39 Column: 8

why it's giving this error??


Solution

  • Because aliases are not supported in the WHERE clause of the same query. So instead write your query like:

    SELECT instmax
    FROM 
      (SELECT instmax ,rownum r 
      FROM 
        ( SELECT instmax FROM pswlinstmax ORDER BY instmax DESC NULLS LAST 
        )  
    ) WHERE r = 2;