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 βˆ’

Updated on: 2019-07-30T22:30:25+05:30

342 Views

Kickstart Your Career

Get certified by completing the course

Get Started
Advertisements