Get the App
SLTechnology News&Howtos  ›  Database  › 

Restrict the recording of Top-N query results

Shulou Source: shulou.com Published: 2022-06-01 13:41:58 09月29日 Update

In previous versions, there were a variety of indirect means to get Top-N query results for top or bottom records. In 12c, the process is simplified and made more straightforward with a new FETCH FIRST | NEXT | PERCENT statement. Retrieve the top 10 salary records from the EMP table SQL > SELECT empno,ename,sal FROM emp ORDER BY SAL DESC FETCH FIRST 10 ROWS ONLY; EMPNO ENAME SAL 7839 KING 5000 7902 FORD 3000 7566 JONES 2975 7698 BLAKE 2850 7782 CLARK 2450 7499 ALLEN 1600 7844 TURNER 1500 7934 MILLER 1300 7521 WARD 1250 7654 MARTIN 1250

10 rows selected.

Original method

SQL > select * from (SELECT empno,ename,sal FROM emp ORDER BY SAL DESC) where rownum SELECT empno,ename,sal FROM emp ORDER BY SAL DESC offset 2 rows fetch next 3 rows only

EMPNO ENAME SAL 7566 JONES 2975 7698 BLAKE 2850 7782 CLARK 2450

Get the top 10% records from the EMP table

SQL > SELECT empno,ename,sal FROM emp ORDER BY SAL DESC FETCH FIRST 10 PERCENT rows only

EMPNO ENAME SAL 7839 KING 5000 7902 FORD 3000 get all similar records in the top 9 SQL > SELECT empno,ename,sal FROM emp ORDER BY SAL DESC FETCH FIRST 9 ROWS WITH TIES EMPNO ENAME SAL 7839 KING 5000 7902 FORD 3000 7566 JONES 2975 7698 BLAKE 2850 7782 CLARK 2450 7499 ALLEN 1600 7844 TURNER 1500 7934 MILLER 1300 7521 WARD 1250 7654 MARTIN 1250

10 rows selected.

Tags: Salary search result query similarity multiple bottom means method version statement procedure Top restriction Apple Docker Huawei Linux macOS MariaDB Microsoft MySQL NVidia OPPO Reno Microsoft Shulou Information Docker Xiaomi NVidia