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'.
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.
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 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
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.
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
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With