Follow along with the video below to see how to install our site as a web app on your home screen.
Note: This feature may not be available in some browsers.
SQL> select 'hello' from dual;
'HELL
-----
hello
SQL> select 'hello' from dual connect by level <= 10;
'HELL
-----
hello
select 'hello'
from (select null from dual connect by level <= 10)
/
'HELL
-----
hello
hello
hello
hello
hello
hello
hello
hello
hello
hello
10 rows selected.
select 'hello' from all_tab_columns where rownum <= 10;
'HELL
-----
hello
hello
hello
hello
hello
hello
hello
hello
hello
hello
10 rows selected.
Here is an example that may clarify the need for your wanting a specific number of rows. Let's assume that you want to create a report that shows how many people have birthdays in each month of the year. Let's also assume the following results from your query:LoveKang said:I haven't met the situation before.
select to_char(birthdate,'MON')Month,count(*) Birthdays
from person
group by to_char(birthdate,'MM'), to_char(birthdate,'MON')
/
MON BIRTHDAYS
--- ----------
JAN 2
FEB 3
APR 3
MAY 3
JUN 7
JUL 8
SEP 2
OCT 3
NOV 3
DEC 3
10 rows selected.
select to_char(to_date(Month_data.Month_num,'MM'),'MON') Month
,count(Person_data.Month_num) Birthdays
from (select to_char(birthdate,'MM') month_num
,to_char(birthdate,'MON') month_name
from person
) person_data
,(select rownum Month_num
from dual connect by level <= 12) Month_data
where Month_data.Month_num = person_data.Month_num(+)
group by Month_data.Month_num, Month_name
/
MON BIRTHDAYS
--- ----------
JAN 2
FEB 3
MAR 0
APR 3
MAY 3
JUN 7
JUL 8
AUG 0
SEP 2
OCT 3
NOV 3
DEC 3
12 rows selected.
SELECT TO_CHAR(m.mnth,'Mon') hire_month, count(*)
FROM hr.employees e
RIGHT OUTER JOIN (SELECT TO_DATE(LEVEL,'MM') mnth
FROM DUAL
CONNECT BY LEVEL<=11) m
ON TO_CHAR(m.mnth,'Mon') = to_char(e.hire_date,'Mon')
GROUP BY m.mnth
ORDER BY m.mnth
SQL> select sysdate+level
2 from dual
3 connect by level <= 10000;
10000 rows selected.
Execution Plan
----------------------------------------------------------
Plan hash value: 1236776825
-----------------------------------------------------------------------------
| Id | Operation | Name | Rows | Cost (%CPU)| Time |
-----------------------------------------------------------------------------
| 0 | SELECT STATEMENT | | 1 | 2 (0)| 00:00:01 |
| 1 | CONNECT BY WITHOUT FILTERING| | | | |
| 2 | FAST DUAL | | 1 | 2 (0)| 00:00:01 |
-----------------------------------------------------------------------------
Statistics
----------------------------------------------------------
0 recursive calls
0 db block gets
0 consistent gets
0 physical reads
0 redo size
177083 bytes sent via SQL*Net to client
7707 bytes received via SQL*Net from client
668 SQL*Net roundtrips to/from client
1 sorts (memory)
0 sorts (disk)
10000 rows processed
SQL> select sysdate+lvl
2 from tenkrows;
10000 rows selected.
Execution Plan
----------------------------------------------------------
Plan hash value: 4197133435
------------------------------------------------------------------------------
| Id | Operation | Name | Rows | Bytes | Cost (%CPU)| Time |
------------------------------------------------------------------------------
| 0 | SELECT STATEMENT | | 10000 | 126K| 6 (0)| 00:00:01 |
| 1 | TABLE ACCESS FULL| TENKROWS | 10000 | 126K| 6 (0)| 00:00:01 |
------------------------------------------------------------------------------
Note
-----
- dynamic sampling used for this statement
Statistics
----------------------------------------------------------
28 recursive calls
0 db block gets
708 consistent gets
16 physical reads
0 redo size
177081 bytes sent via SQL*Net to client
7707 bytes received via SQL*Net from client
668 SQL*Net roundtrips to/from client
0 sorts (memory)
0 sorts (disk)
10000 rows processed