How to write a MySQL “LIMIT” in SQL Server?


You need to use TOP(1) in SQL Server. The syntax is as follows −

SELECT TOP(1) *FROM yourTableName WHERE yourCondition;

To understand the above syntax, let us create a table. The query to create a table is as follows −

create table TopDemoInSQLServer
(
   Id int,
   Name varchar(10)
);

The snapshot of creation of table is as follows −

Insert some records in the table using insert command. The query is as follows −

insert into TopDemoInSQLServer values(10,'John');
insert into TopDemoInSQLServer values(14,'Carol');
insert into TopDemoInSQLServer values(1,'Sam');
insert into TopDemoInSQLServer values(11,'Bob');
insert into TopDemoInSQLServer values(18,'David');
insert into TopDemoInSQLServer values(20,'Sam');

The snapshot of insert record in the table is as follows −

Display all records from the table using select statement. The query is as follows −

select *from TopDemoInSQLServer;

The snapshot of displaying all records from the table is as follows −

Output

Here is the query to implement TOP(1) instead of LIMIT 1 −

select TOP(1) *from TopDemoInSQLServer where Name = 'Carol';

Here is the snapshot of query −

Here is the snapshot of sample output −

Samual Sam
Samual Sam

Learning faster. Every day.

Updated on: 30-Jul-2019

245 Views

Kickstart Your Career

Get certified by completing the course

Get Started
Advertisements