MySQL LIMIT clause equivalent for SQL SERVER?


Firstly, we need to create a table to understand the limit clause (as we want for SQL server).We will create a table with the help of CREATE command.

Creating a table

mysql> CREATE table limitDemo
-> (
-> id int,
-> primary key(id)
-> );
Query OK, 0 rows affected (0.58 sec)

After that, let us insert records into the table −

mysql> INSERT into limitDemo values(1);
Query OK, 1 row affected (0.16 sec)

mysql> INSERT into limitDemo values(2);
Query OK, 1 row affected (0.12 sec)

mysql> INSERT into limitDemo values(3);
Query OK, 1 row affected (0.11 sec)

mysql> INSERT into limitDemo values(4);
Query OK, 1 row affected (0.10 sec)

mysql> INSERT into limitDemo values(5);
Query OK, 1 row affected (0.12 sec)

mysql> INSERT into limitDemo values(6);
Query OK, 1 row affected (0.13 sec)

mysql> INSERT into limitDemo values(7);
Query OK, 1 row affected (0.15 sec)

mysql> INSERT into limitDemo values(8);
Query OK, 1 row affected (0.09 sec)

mysql> INSERT into limitDemo values(9);
Query OK, 1 row affected (0.14 sec)

Displaying all the records with the help of SELECT statement −

mysql> SELECT * from limitDemo;

The following is the output

+----+
| id |
+----+
| 1  |
| 2  |
| 3  |
| 4  |
| 5  |
| 6  | 
| 7  |
| 8  |
| 9  |
+----+
9 rows in set (0.00 sec)

Let us see the query of limit clause and begin with the syntax −

SELECT column_name1……..N from yourTableName limit integervalue offset integervalue;

Now, I am applying the above query −

mysql> SELECT id from limitDemo limit 5 offset 2;

The following is the output

+----+
| id |
+----+
| 3  |
| 4  |
| 5  |
| 6  |
| 7  |
+----+
5 rows in set (0.00 sec)

Updated on: 25-Jun-2020

256 Views

Kickstart Your Career

Get certified by completing the course

Get Started
Advertisements