Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 8930649
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 15, 20262026-06-15T08:56:11+00:00 2026-06-15T08:56:11+00:00

Using CONNECT BY LEVEL seems to return too many rows when performed on a

  • 0

Using CONNECT BY LEVEL seems to return too many rows when performed on a table. What is the logic behind what’s happening?

Assuming the following table:

create table a ( id number );

insert into a values (1);
insert into a values (2);
insert into a values (3);

This query returns 12 rows (SQL Fiddle).

 select id, level as lvl
   from a
connect by level <= 2
  order by id, level

One row for each in table A with the value of column LVL being 1 and three for each in table A where the column LVL is 2, i.e.:

ID | LVL 
---+-----
 1 |  1 
 1 |  2 
 1 |  2 
 1 |  2 
 2 |  1 
 2 |  2 
 2 |  2 
 2 |  2 
 3 |  1 
 3 |  2 
 3 |  2 
 3 |  2 

It is equivalent to this query, which returns the same results.

 select id, level as lvl
   from dual
  cross join a
connect by level <= 2
  order by id, level

I don’t understand why these queries return 12 rows or why there are three rows where LVL is 2 and only one where LVL is 1 for each value of the ID column.

Increasing the number of levels that are “connected” to 3 returns 13 rows for each value of ID. 1 where LVL is 1, 3 where LVL is 2 and 9 where LVL is 3. This seems to suggest that the rows returned are the number of rows in table A to the power of the value of LVL minus 1.

I would have though that these queries would be the same as the following, which returns
6 rows

select id, lvl
  from ( select level  as lvl
           from dual
        connect by level  <= 2
                )
 cross join a
 order by id, lvl

The documentation isn’t particularly clear, to me, in explaining what should occur. What’s happening with these powers and why aren’t the first two queries the same as the third?

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-15T08:56:12+00:00Added an answer on June 15, 2026 at 8:56 am

    In the first query, you connect by just the level.
    So if level <= 1, you get each of the records 1 time. If level <= 2, then you get each level 1 time (for level 1) + N times (where N is the number of records in the table). It is like you are cross joining, because you’re just picking all records from the table until the level is reached, without having other conditions to limit the result. For level <= 3, this is done again for each of those results.

    So for 3 records:

    • Lvl 1: 3 record (all having level 1)
    • Lvl 2: 3 records having level 1 + 3*3 records having level 2 = 12
    • Lvl 3: 3 + 3*3 + 3*3*3 = 39 (indeed, 13 records each).
    • Lvl 4: starting to see a pattern? 🙂

    It’s not really a cross join. A cross join would only return those records that have level 2 in this query result, while with this connect by, you get the records having level 1 as well as the records having level 2, thus resulting in 3 + 3*3 instead of just 3*3 record.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have a hierarchy table with the following data : SOURCE TARGET Level ID
I am trying to connect couchdb using node-couchdb-api at nodejs level as mentioned in
I'm trying to integrate passport into my nodejs server using connect, but can't seem
I'm trying to recursively get some data using connect by, which for each row
When using ftpClient.connect with an existing host who has no ftp service active, timeout
I'm using Facebook Connect along with the Facebook Graph API to fetch user's email
I am using Facebook connect and JS client library to retrieve my photos in
I am using Facebook Connect for sharing something on my appliction. So I want
I'm using Facebook Connect's Comments Box ( http://wiki.developers.facebook.com/index.php/Comments_Box ) to handle comments on my
I am using Facebook Connect and showing the users Facebook profile pic on the

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.