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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 11, 20262026-06-11T11:44:35+00:00 2026-06-11T11:44:35+00:00

For example given this table id | category 1 | 10 2 | 10

  • 0

For example given this table

id | category

1 | 10

2 | 10

3 | 10

4 | 10

5 | 20

6 | 20

7 | 20

8 | 30

9 | 30

10 | 30

11 | 30

We want obtain first 2 small id, from each category, that is we need this result

1

2

5

6

8

9

I wriite this query and his works

        SELECT MIN(id) AS id FROM mytable GROUP BY category  
        UNION 
        SELECT MIN(id) AS id FROM mytable WHERE 
        id NOT IN (SELECT MIN(id) AS id FROM mytable GROUP BY category) GROUP BY category

        ORDER BY id

but here is one problem, this works if we want first 2 id from each category, but if we want more first small ids (for example 7) from each category, query will be very difficult.
Someone has idea, how can make this easy?

  • 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-11T11:44:37+00:00Added an answer on June 11, 2026 at 11:44 am

    @Quassnoi wrote a couple of very informative blog articles on this subject, concluding that the following would be extremely performant if you have a composite index on (category, id):

    SELECT mytable.*
    FROM   mytable JOIN (
      SELECT category, (
        SELECT   id
        FROM     mytable
        WHERE    mytable.category = categories.category
        ORDER BY id
        LIMIT    1, 1
      ) AS id
      FROM (
        SELECT DISTINCT category
        FROM   mytable
      ) AS categories
    ) AS limits ON mytable.category  <= limits.category
               AND mytable.category  >= limits.category
               AND IFNULL(mytable.id <= limits.id, TRUE)
    

    See it on sqlfiddle.

    To select the first 4 records, change LIMIT 1,1 to LIMIT 3,1. More generally: to select the first n records, change LIMIT 1,1 to LIMIT n-1,1.

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

Sidebar

Related Questions

Given this example HTML that my GM script runs on, I want to make
Given this example, how would I return the result of the equation rather than
Given a DAG, in which each node belongs to a category, how can this
For example, given this table of sparse ids: |id| | 1| | 2| |
Please give me some examples of jump table usage. I have seen this example
Given this example data set: ----------------------------- | item | date | val | -----------------------------
Given this example XML file: <doc> <tag> Hello ! </tag> <tag> My name is
I'm trying to solve this problem : http://uva.onlinejudge.org/external/7/732.html . For the given example, they
Given the example below, can someone please show me how this could be called?
This is just an example, but given the following model: class Foo(models.model): bar =

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.