Oracle select top 10 records group by oracle năm 2024

For example, what if I wanted to retrieve the first 3 records from my query results. How can I do this?

Answer: To retrieve the Top N records from a query, you can use the following syntax:

SELECT * FROM (your ordered query) alias_name WHERE rownum <= Rows_to_return ORDER BY rownum;

For example, if you wanted to retrieve the first 3 records from the suppliers table, sorted by supplier_name in ascending order, you would run the following query:

SELECT FROM (select from suppliers ORDER BY supplier_name) suppliers2 WHERE rownum <= 3 ORDER BY rownum;

If you wanted to retrieve the first 5 records from the suppliers table, sorted by supplier_id in descending order, you would run the following query:

The SELECT TOP clause is useful on large tables with thousands of records. Returning a large number of records can impact performance.

Example

Select only the first 3 records of the Customers table:

SELECT TOP 3 * FROM Customers;

Try it Yourself »

Note: Not all database systems support the SELECT TOP clause. MySQL supports the LIMIT clause to select a limited number of records, while Oracle uses FETCH FIRST n ROWS ONLY and ROWNUM.

SQL Server / MS Access Syntax:

SELECT TOP number|percent columnname(s) FROM tablename WHERE condition;

MySQL Syntax:

SELECT columnname(s) FROM tablename WHERE condition LIMIT number;

Oracle 12 Syntax:

SELECT columnname(s) FROM tablename ORDER BY columnname(s) FETCH FIRST number ROWS ONLY;

Older Oracle Syntax:

SELECT columnname(s) FROM tablename WHERE ROWNUM <= number;

Older Oracle Syntax (with ORDER BY):

`SELECT TOP`0


Demo Database

Below is a selection from the Customers table used in the examples:

CustomerID CustomerName ContactName Address City PostalCode Country 1 Alfreds Futterkiste Maria Anders Obere Str. 57 Berlin 12209 Germany 2 Ana Trujillo Emparedados y helados Ana Trujillo Avda. de la Constitución 2222 México D.F. 05021 Mexico 3 Antonio Moreno Taquería Antonio Moreno Mataderos 2312 México D.F. 05023 Mexico 4 Around the Horn Thomas Hardy 120 Hanover Sq. London WA1 1DP UK 5 Berglunds snabbköp Christina Berglund Berguvsvägen 8 Luleå S-958 22 Sweden



LIMIT

The following SQL statement shows the equivalent example for MySQL:

Example

Select the first 3 records of the Customers table:

SELECT * FROM Customers LIMIT 3;

Try it Yourself »


FETCH FIRST

The following SQL statement shows the equivalent example for Oracle:

Example

Select the first 3 records of the Customers table:

SELECT * FROM Customers FETCH FIRST 3 ROWS ONLY;


SQL TOP PERCENT Example

The following SQL statement selects the first 50% of the records from the "Customers" table (for SQL Server/MS Access):

The following SQL statement shows the equivalent example for Oracle:

Example

SELECT * FROM Customers FETCH FIRST 50 PERCENT ROWS ONLY;


ADD a WHERE CLAUSE

The following SQL statement selects the first three records from the "Customers" table, where the country is "Germany" (for SQL Server/MS Access):

The following SQL statement shows the equivalent example for MySQL:

The following SQL statement shows the equivalent example for Oracle:

Example

SELECT * FROM Customers WHERE Country='Germany' FETCH FIRST 3 ROWS ONLY;


ADD the ORDER BY Keyword

Add the `SELECT TOP`1 keyword when you want to sort the result, and return the first 3 records of the sorted result.

I have a big problem with an SQL Statement in Oracle. I want to select the TOP 10 Records ordered by STORAGE_DB which aren't in a list from another select statement.

This one works fine for all records:

SELECT DISTINCT

APP_ID,

NAME,

STORAGE_GB,

HISTORY_CREATED,

TO_CHAR(HISTORY_DATE, 'DD.MM.YYYY') AS HISTORY_DATE

FROM HISTORY WHERE

STORAGE_GB IS NOT NULL AND

APP_ID NOT IN (SELECT APP_ID

FROM HISTORY

WHERE TO_CHAR(HISTORY_DATE, 'DD.MM.YYYY') = '06.02.2009')

But when I am adding

AND ROWNUM <= 10 ORDER BY STORAGE_GB DESC

I'm getting some kind of "random" Records. I think because the limit takes in place before the order.

Does someone have a good solution? The other problem: This query is really slow (10k+ records)

1 Answer

Jul 22, 2019 by Soni Kumari (40.7k points)

Try to put your current query in subquery as follows :

SELECT * FROM (

SELECT DISTINCT

APP_ID,

NAME,

STORAGE_GB,

HISTORY_CREATED,

TO_CHAR(HISTORY_DATE, 'DD.MM.YYYY') AS HISTORY_DATE

FROM HISTORY WHERE

STORAGE_GB IS NOT NULL AND

APP_ID NOT IN (SELECT APP_ID FROM HISTORY WHERE TO_CHAR(HISTORY_DATE, 'DD.MM.YYYY') ='06.02.2009')

ORDER BY STORAGE_GB DESC )

WHERE ROWNUM <= 10

In Oracle, rownum is used to the result after it is returned. You just have to filter the result after it has been returned, therefore the subquery will be required.

How to get the top 10 rows in Oracle?

Get the top-N rows per group.

Statement 1. Get the 10 most recent orders select * from co.orders order by order_datetime desc fetch first 10 rows only. ... .

Statement 2. Add a row number for each customer select o.*, row_number () over ( partition by customer_id order by order_datetime desc ) rn from co.orders o. ... .

Statement 3..

How to SELECT the top

Getting the top-N rows for a group is a two-step process:.

Assign row numbers starting at one for each group..

Filter the result of this down to the rows less than or equal to the number you want..

How to SELECT first record of each group in Oracle?

To obtain the first record in a group of records, you can use the with clause to create a common table expression including row_number() to obtain the position of each row in the group. Later with an outer query you can filter rows by position.

How to find top 10 tables in Oracle?

Click on 'Table Row Count' in the navigation bar. The tables will be listed with the number of records displayed beside them in descending order. To limit the number of tables displayed, click the 'wrench' icon to open the macro options window.