SQL SELECT TOP

      No Comments on SQL SELECT TOP

The SQL SELECT TOP clause is used to when one wishes to monitor the number of records that are returned in the result-set.
The SQL SELECT TOP defines the number of records to return.
The SQL SELECT TOP clause is very useful when dealing with large database or tables with large number of records.
Returning large records can impact performance

SQL server/ MS-Access syntax: (SQL SELECT TOP)

SELECT TOP number|percent column_name(s)
FROM table_name;

MySQL syntax: (SQL LIMIT)

SELECT column_name(s) 
FROM table_name 
LIMIT number;

Example:

SELECT * FROM customers LIMIT 5;

Oracle syntax: (SQL ROWNUM)

SELECT column_name(s) FROM table_name WHERE ROWNUM<=number

Example:
SELECT * FROM customers WHERE ROWNUM <= 5;

Consider this demo database:

A sample database-Customer table:
C.idC.nameC.contactC.address
123eed3Ramesh987654321#12,sec-4 mohali
aa22103Tarun987650000#12,sec-14 b chd
00146hhSia812300912#212,sec-86 panchukula
144dcv4Rahul700912366#001,sec-19 c chd
Example:

SELECT * FROM customers LIMIT 2;

Result:

C.idC.nameC.contactC.address
123eed3Ramesh987654321#12,sec-4 mohali

Leave a Reply

Your email address will not be published. Required fields are marked *