SQL SELECT TOPThe SELECT TOP statement in SQL shows the limited number of records or rows from the database table. The TOP clause in the statement specifies how many rows are returned. It shows the top N number of rows from the tables in the output. This clause is used when there are thousands of records stored in the database tables. Let's take a simple example: If a Student table has a large amount of data about students, the select TOP statement determines how much student data will be retrieved from the given table. Note: All the database systems do not support the TOP keyword for selecting the limited number of records. Oracle supports the ROWNUM keyword, and MySQL supports the LIMIT keyword.Syntax of TOP Clause in SQLIn the syntax, the number denotes the number of rows shown from the top in the output. column_Name denotes the column whose record we want to show in the output. We can also specify the condition using the WHERE clause. Examples of TOP Clause in SQLThe following four SQL examples will help you how to use the Number and Percent in SQL TOP clause in the query: Example 1: In this example, we have a table called Cars with three columns:
This query shows the following table on the screen:
Example 2: In this example, we have a table called Student with three columns:
This query shows the following table on the screen in the SQL output:
Example 3: In this example, we have a table called Employee with four columns:
This query shows the following table on the screen in the SQL output:
Example 4: In this example, we have a table called Bikes with three columns:
This query shows the following table on the screen:
Syntax of LIMIT Clause in MySQLIn the syntax, we have to specify the value after the LIMIT keyword. The value denotes the number of rows to be shown from the top in the output. Example of LIMIT Clause in MySQLThe following SQL example will help you how to use the LIMIT clause in the query. In this example, we have a table called Cars with three columns:
This query shows the following table on the screen:
Syntax of ROWNUM keyword in WHERE Clause in Oracle databaseIn the syntax, we have to assign the value to ROWNUM in the WHERE clause. The value denotes the number of rows to be shown from the top in the output. Example of ROWNUM keyword in WHERE Clause in OracleThe following SQL example will help you how to use the ROWNUM keyword in the query. In this example, we have a table called Cars with three columns:
This query shows the following table on the screen:
Next TopicSQL SELECT FIRST |