How to use Select Statement In SQL
In this approach, we will use the subquery to fetch the top 10 values from the table.
SELECT * FROM table_name WHERE ROWNUM <= 10 ORDER BY column DESC;
If we use the above query it will consider only the first 10 rows from the table and log them in the descending order. It will not consider the remaining rows of the table and not order them in descending order.Hence we will use the subquery to log the top 10 values.
Syntax:
Select column1,column2 From (Select * from Table_name Order By column desc ) Where ROWNUM <=10)
- The inner subquery returns the table in descending order of the column.
- Where ROWNUM <=10 is used to limit the number of rows returned from the select query.
- The outer select statement is used to retrieve the top 10 values.
Example:
SELECT ID,NAME, SCORE FROM (SELECT * FROM GEEKS ORDER BY score DESC)
WHERE ROWNUM <= 10;
Output:
How to Get the Top 10 Values in PL/SQL?
PL/SQL stands for Procedural Language/ Structured Query Language. It has block structure programming features.PL/SQL supports SQL queries. It also supports the declaration of the variables, control statements, Functions, Records, Cursor, Procedure, and Triggers.
PL/SQL contains a declaration section, execution section, and exception-handling section. Declare and exception handling sections are optional.