Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

LIMIT not working in SQL

Tags:

sql-server

In my below Query:

Select * from DimCustomer 
order by MiddleName desc LIMIT 5

Getting below error:

Msg 102, Level 15, State 1, Line 3 Incorrect syntax near 'LIMIT'.

like image 863
user3652369 Avatar asked Mar 05 '17 17:03

user3652369


People also ask

How do I limit rows in SQL Server?

The way to perform row limiting in SQL Server is different from doing it in MySQL. In SQL Server, you use the SQL TOP keyword rather than LIMIT. The SQL TOP keyword goes at the start of the query in the SELECT clause.

What is LIMIT clause in SQL?

Summary: this tutorial shows you how to use the SQL LIMIT clause to constrain the number of rows returned by a SELECT statement. Notice that LIMIT clause is not standard SQL. Only some database systems support it including MySQL and PostgreSQL.

How to use the limit keyword in SQL?

How to Use the LIMIT Keyword The LIMIT keyword is used to LIMIT the number of rows of a result set returned Any number from zero (0) and up can be the LIMIT number. No rows are returned from the set result if zero (0) is set as the LIMIT

How to use limit 1 offset 1 in SQL Server?

To do so, you use the LIMIT OFFSET clauses as follows. SELECT employee_id, first_name, last_name, salary FROM employees ORDER BY salary DESC LIMIT 1 OFFSET 1; See it in action. The ORDER BY clause sorts the employees by salary in descending order. And the LIMIT 1 OFFSET 1 clause gets the second row from the result set.


1 Answers

Sql Server doesn't use limit like that, it uses top instead.

 select top 5 * from DimCustomer order by MiddleName desc

If you are looking for pagination, you can use offset and fetch in sql server 2012+

select * 
from DimCustomer 
order by MiddleName desc
offset 0 rows
fetch next 5 rows only;

For more patterns and options for pagination, check here: Pagination with offset / fetch : A better way - Aaron Betrand

like image 111
SqlZim Avatar answered Oct 19 '22 03:10

SqlZim