user379888
user379888

Reputation:

SQL error while using LIMIT

I am a beginner with SQL and I'm following the tutorial at W3 Schools SQL Exercises. I tried to run the following query but I am getting error:

SELECT * FROM Customers LIMIT 5, 5

ERROR:

Syntax error in FROM clause.

Upvotes: 1

Views: 18341

Answers (5)

Rıza Yıldırım
Rıza Yıldırım

Reputation: 1

I had the same problem. You can use rownum:

select * from Customers where rownum < 6;

Upvotes: 0

Iridio
Iridio

Reputation: 9271

Your problem is that you are using MySql syntax instead of SqlServer syntax. You should use:

SELECT TOP 5 * FROM Customers

Upvotes: 11

Marco
Marco

Reputation: 57593

You can see that they use ADODB, so you should use TOP clause:

SELECT TOP 5 * FROM Customers

Upvotes: 2

Yuck
Yuck

Reputation: 50855

If you're following the example in the link you provided...

We will use the Customers table in the Northwind database:

Northwind is a sample database in MS SQL products. And MS SQL does not have support using LIMIT queries. So that website is probably using some version of that RDBMS.

Try running:

SELECT TOP 5 * FROM customers;

This gets you the first 5 records in the result set (unordered). It does not skip any records, though, as your LIMIT 5, 5 clause would have.

Upvotes: 3

user554546
user554546

Reputation:

I suspect you need to use offset:

select * from Customers limit 5 offset 5;

Not all RDBMSs support the limit m,n syntax (I believe MySQL does, but PostgreSQL doesn't). Also, you'll probably want to use an order by clause to help ensure that you get consistent results with your query (because databases, in general, don't care about the order of rows or columns unless you specify):

select * from Customers order by col limit 5 offset 5;

Upvotes: 0

Related Questions