20

I know that questions related to 'limit' have been asked before here, and I have already referred to them. My question is somewhat different.

Here's my query:

select id,somecol from sometable where someval=2 order by id desc limit 3 

I'm getting an error saying 'SQL command not properly ended'. How do I resolve this? If you need additional information, feel free to tell me so.

3
  • 2
    See the answers here. Is this Oracle 12c? If so, replace "limit 3" with "FETCH FIRST 3 ROWS ONLY". If not, I think the standard Oracle-y thing to do is "SELECT * FROM (select id,somecol from sometable where someval=2 order by id desc) WHERE rownum <= 3" Commented Feb 14, 2018 at 6:37
  • what is your oracle version Commented Feb 14, 2018 at 6:42
  • My version is 11g. Express edition Commented Feb 14, 2018 at 7:07

2 Answers 2

28

Generally, we use LIMIT in MYSQL database and Rownum in Oracle.

MySQL Syntax:

SELECT column_name(s) FROM table_name WHERE condition LIMIT number;

Oracle Syntax:

SELECT column_name(s) FROM table_name WHERE ROWNUM <= number;

References:

https://www.w3schools.com/sql/sql_top.asp

Sign up to request clarification or add additional context in comments.

2 Comments

'select id,somecol from sometable where someval=2 order by id desc where rownum<=3' should work, right?
okay, this worked! thank you!
15

If you are running Oracle 12c, you could use FETCH FIRST n ROWS ONLY:

SELECT id, somecol FROM sometable WHERE someval = 2 ORDER BY id DESC FETCH FIRST 3 ROWS ONLY; 

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.