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:

PLSQL Query to retrieve Top 10 Record

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.

Similar Reads

PL/SQL Query to Retrieve Top 10 Records

In this article, we will learn how to retrieve the top 10 values from the database using PL/SQL. We are often required to display specific values to get insights about the database such as top performers, top gainers, or other data....

Setting up Environment

Let’s create a GEEKS table and insert the value in it:...

1. Using Select Statement

In this approach, we will use the subquery to fetch the top 10 values from the table....

2. Using Cursor

Oracle uses a special memory space called a context area for storing and retrieving information. The context area contains all the details related to the database. The cursor is the virtual pointer to the context area, in the database....

Conclusion

Retrieving top values from databases is crucial for gaining insights. In this article, we explored two effective methods using PL/SQL: utilizing subqueries and PL/SQL Cursors. Whether it’s analyzing top performers or identifying trends, mastering these techniques empowers developers to extract valuable information efficiently from their databases....