Network Security Internet Technology Development Database Servers Mobile Phone Android Software Apple Software Computer Software News IT Information

In addition to Weibo, there is also WeChat

Please pay attention

WeChat public account

Shulou

Restrict the recording of Top-N query results

2025-03-29 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Database >

Share

Shulou(Shulou.com)06/01 Report--

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.

Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.

Views: 0

*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.

Share To

Database

Wechat

© 2024 shulou.com SLNews company. All rights reserved.

12
Report