SQL – SELECT TOP

Prev     Next

  • SELECT…TOP clause in SQL is used to specify the number of rows or percentage of rows to be selected from a table by SELECT query.
  • This TOP clause is very useful in larger tables where there is a chance for performance issue while selecting thousands of records. So, we can select only a few records from the larger tables as per our need.Syntax for SQL select statement is given below.

SQL Syntax for SELECT…TOP statement:

Syntax for SELECT…TOP clause using number of rows to be selected SELECT TOP number * FROM table_name;
Syntax for SELECT…TOP clause using percentage of rows to be selected SELECT TOP number PERCENT * FROM table_name;

Note: * is a wild card which selects all columns from a table.

Please consider the following table with few records as given below.

Table name (for example): student
Column names in this table: Student_ID, Student_name, City and Age
Available records: 4 rows

sql-table1
Example1: how to use SQL SELECT…TOP by number:

To select only top 3 records/rows from this table, please execute below query. This query will fetch only 1st three records available in this table:

SQL query:

SELECT TOP 3 * from student;

SQL query Output:

sql-table32

Example2: how to use SQL SELECT…TOP by Percentage:

  • To select only 50% of records/rows from this table (i.e. If a table contains 100 records, then 50% means 50 records and if a table contains 50 records, then 50% means 25 records etc.),
  • Please execute below query. This query will fetch only 1st two records available in this table which is 50%:

SQL query:

SELECT TOP 50 PERCENT * from student;

SQL query Output:

sql-table33

Prev     Next

Ask your questions or clarify your/others doubts from,

✅ Online Discussion Forums by Fresh2Refresh.com





Share this website !!!
Facebook Twitter Linkedin

Close
We Support JALLIKATTU !!!
We Support JALLIKATTU!
By Fresh2Refresh.com
Show Your Support as well. Thanks!