Latest updates

[5]

SQL TOP Clause




The TOP clause is used to specify the number of records to return.
The TOP clause can be very useful on large tables with thousands of records. Returning a large number of records can impact on performance.
Note: Not all database systems support the TOP clause.
SQL Server Syntax
SELECT TOP number|percent column_name(s)
FROM table_name

 

SQL TOP Example

“Persons” TABLE:

P_Id
LastName
FirstName
Address
City
1
Suhail
Osmani
Maharaj Nagar
Sitapur
2
Maxwel
Tom
Biswan
Sitapur
3
Rahi
Ayon
Okhla
Delhi
4
Nyla
Tom
Rohini
Delhi
Now we want to select only the two first records in the table above.
We use the following SELECT statement:
SELECT TOP 2 * FROM Persons
Output:
P_Id
LastName
FirstName
Address
City
1
Suhail
Osmani
Maharaj Nagar
Sitapur
2
Maxwel
Tom
Biswan
Sitapur

 

SQL TOP PERCENT Example

“Persons” TABLE:

P_Id
LastName
FirstName
Address
City
1
Suhail
Osmani
Maharaj Nagar
Sitapur
2
Maxwel
Tom
Biswan
Sitapur
3
Rahi
Ayon
Okhla
Delhi
4
Nyla
Tom
Rohini
Delhi
Now we want to select only 50% of the records in the table above.We use the following SELECT statement:
SELECT TOP 50 PERCENT * FROM Persons
Output:
P_Id
LastName
FirstName
Address
City
1
Suhail
Osmani
Maharaj Nagar
Sitapur
2
Maxwel
Tom
Biswan
Sitapur


SQL TOP Clause SQL TOP Clause Reviewed by Admin on 04:21:00 Rating: 5

No comments:

Sora Templates