Tuesday, 7 March 2017
LIMIT and TOP in select query
LIMIT and TOP in select query
In this post we will learn the usage of LIMIT and TOP in select query.In order to limit the records from the select query each database provides different syntax. For example :
MySQL has LIMIT – which will be used to limit the records from the select query. In the same way SQL Server provides the TOP syntax in order to limit the records returned from the query.
TOP Syntax in SQL Server :-
This is mainly useful in querying very large tables. In a SELECT statement, always use an ORDER BY clause with the TOP clause. This is the only way to predictably indicate which rows are affected by TOP.
Examples :-
To fetch top 3 records of having high salaries.
SELECT TOP 3 * FROM USERS ORDER BY salary DESC;
Returning the top 20% of rows from a table called USERS:SELECT TOP 20 PERCENT * FROM USERS;
LIMIT Syntax in MySQL Server :-Similar to the TOP in SQL Server in MySQL we use LIMIT.The LIMIT is used in SELECT query to specify the number of rows in the result set. It accepts one or two arguments.The value of both arguments must be greater than or equal to 0.
SELECT * FROM Users LIMIT offset,count.
offset – specify the starting/first row. In general the first row is 0.count – The number of rows to return.
SELECT * FROM User LIMIT 2;
The above query will return the 2 records.
Subscribe to:
Post Comments
(
Atom
)
No comments :
Post a Comment