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 6201979
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 24, 20262026-05-24T04:39:00+00:00 2026-05-24T04:39:00+00:00

given a table foo of the following structure (Oracle 11g): ID | GROUP_ID 1

  • 0

given a table foo of the following structure (Oracle 11g):

ID | GROUP_ID
 1 | 100
 2 | 100
 3 | 100
 4 | 200
 5 | 300
 6 | 300
 7 | 400

I want to select the first n rows (ordered by ID) or more, such that I always get a complete group.

Example:

n = 2: I want to get at least the first two rows, but since ID 3 also belongs to group 100, I want to get that as well.

n = 4: Give me the first four rows and I am happy 😉

n = 5: Rows 1-6 are requested.

Your help is highly appreciated!

  • 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-05-24T04:39:02+00:00Added an answer on May 24, 2026 at 4:39 am

    Solution using rank():

    select id, group_id
    from (select t.*, rank() over (order by group_id) as rnk
        from t)
    where rnk <= :n;
    

    Building test data:

    SQL> create table t (id number not null primary key
      2      , group_id number not null);
    
    Table created.
    
    SQL> insert into t values (1, 100);
    
    1 row created.
    
    SQL> insert into t values (2, 100);
    
    1 row created.
    
    SQL> insert into t values (3, 100);
    
    1 row created.
    
    SQL> insert into t values (4, 200);
    
    1 row created.
    
    SQL> insert into t values (5, 300);
    
    1 row created.
    
    SQL> insert into t values (6, 300);
    
    1 row created.
    
    SQL> insert into t values (7, 400);
    
    1 row created.
    
    SQL> commit;
    
    Commit complete.
    SQL>
    

    Running…

    SQL> var n number
    SQL> exec :n := 2;
    
    PL/SQL procedure successfully completed.
    
    SQL> select id, group_id
      2  from (select t.*, rank() over (order by group_id) as rnk
      3      from t)
      4  where rnk <= :n;
    
            ID   GROUP_ID
    ---------- ----------
             1        100
             2        100
             3        100
    
    SQL> exec :n := 4;
    
    PL/SQL procedure successfully completed.
    
    SQL> select id, group_id
      2  from (select t.*, rank() over (order by group_id) as rnk
      3      from t)
      4  where rnk <= :n;
    
            ID   GROUP_ID
    ---------- ----------
             1        100
             2        100
             3        100
             4        200
    
    SQL> exec :n := 5;
    
    PL/SQL procedure successfully completed.
    
    SQL> select id, group_id
      2  from (select t.*, rank() over (order by group_id) as rnk
      3      from t)
      4  where rnk <= :n;
    
            ID   GROUP_ID
    ---------- ----------
             1        100
             2        100
             3        100
             4        200
             5        300
             6        300
    
    6 rows selected.
    

    EDIT Here is version that includes the for update clause (:n = 2):

    SQL> select id, group_id
      2  from T
      3  where rowid in (select RID
      4      from (select t.rowid as RID, t.*, rank() over (order by group_id) as rnk
      5          from t)
      6      where rnk <= :n)
      7  for update;
    
            ID   GROUP_ID
    ---------- ----------
             1        100
             2        100
             3        100
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

Given the following table structure: CREATE TABLE foo ( ID INT NOT NULL AUTO_INCREMENT,
Given the following Oracle sql query cooked for a PreparedStatement : SELECT * FROM
For a given table 'foo', I need a query to generate a set of
Given a couple of simple tables like so: create table R(foo text); create table
Let's say I have SELECT name FROM table which gives me something like foo
Given a table structure like this: CREATE TABLE `user` ( `id` int(10) unsigned NOT
I have the following table schema: CREATE TABLE [Foo]( [id] [int] NOT NULL, [name]
Given the following table and data CREATE TABLE #temps ( id int, name varchar(max)
I have a table FOO with the following definition: id INTEGER, type INTEGER, data
Given the following class public class Foo { public int FooId { get; set;

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.