TOP Clause in SQL Server
The TOP clause in SQL Server is used to limit the number of rows returned by a SELECT query. It is useful when you want to inspect a portion of your results to find out if your query does what you expect it to do, without taking the resources necessary to return all of the query results.
- SELECT TOP clause is used to specify the number of records to return.
- SELECT TOP clause is useful on large tables with thousands of records. Returning a large number of records can impact performance.
Here is an example of how to use the TOP clause in SQL Server:
Syntax: –
SELECT TOP number/percent column_name(s)
FROM table_nameWHERE condition;
- The following SQL statement selects the first three records from the “Customers” table
SELECT TOP 3 * FROM Customers;
- The following SQL statement selects the first 50% of the records from the “Customers” table
SELECT TOP 50 PERCENT * FROM Customers;
- The following SQL statement selects the first three records from the “Customers” table, where the country is “Germany”.
SELECT TOP 3 * FROM Customers WHERE Country=’Germany’;