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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 23, 20262026-05-23T03:05:26+00:00 2026-05-23T03:05:26+00:00

I have the following: id | name | Book Read | Date Read 1

  • 0

I have the following:

id | name  | Book Read        | Date Read
1    Maria   Dogs love cats     12/31/2008
1    Maria   Cats and Dogs      12/31/2007
1    Maria   Cowboys            12/31/2006
2    Tom     Cowboys            12/31/2008
2    Tom     Indians            12/31/2005
2    Tom     Cats and Dogs      12/31/2003
3    Harry   Raining hard       12/31/2005
3    Harry   Cats and Dogs      12/31/2002
3    Harry   Indians            12/31/2001

If I do “SELECT * FROM table GROUP BY id”, I get:

id | name  | Book Read         | Date Read
1    Maria   Dogs love cats      12/31/2008
2    Tom     Cowboys             12/31/2008
3    Harry   Raining hard        12/31/2005

But how do I get the first 2 books for each person? i.e.:

id | name  | Book Read         | Date Read
1    Maria   Dogs love cats      12/31/2008
1    Maria   Cats and Dogs       12/31/2007
2    Tom     Cowboys             12/31/2008
2    Tom     Indians             12/31/2005
3    Harry   Raining hard        12/31/2005
3    Harry   Cats and Dogs       12/31/2002
  • 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-23T03:05:26+00:00Added an answer on May 23, 2026 at 3:05 am

    Supposing you have an autoincrement field, in my example called i

    select * from table as t1
    where (select count(*) from table as t2
           where t1.id = t2.id and t2.i < t1.i) <2
    

    This is another alternative

    select tmp.i,tmp.id,tmp.name,tmp.book_read
    from (
      select 
        id,i,name,book_read,
        if( @prev <> id, @r:=1,@r:=@r+1 ) as rank,
        @prev := id
      from table as t
      join (select @r:=null,@prev:=0) as rn
      order by t.id,t.i
    ) as tmp
    where tmp.rank <= 2
    order by i,id; 
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

Suppose i have the following table book ----- id name genre check_out_date location_id Can
I have the following models class Book(models.Model): name = models.CharField(max_length=140) class UserProfile(models.Model): favorites =
I have following XML file - with a Book Name and respective Authors. The
If i have the following XML: <Books> <Book> <Name>Test</Name> ... </Book> <Book> <Name>Another one</Name>
I have the following XML: <bookstore> <author name=King> <book id=book1><title>Title1</title></book> <book id=book2><title>Title2</title></book> <book id=book3><title>Title3</title></book>
I have following code class User attr_accessor :name end u = User.new u.name =
I have following databases: test table1 fields: id, password, name, lastname test2 table2 fields:
I have the following class public class Car { public Name {get; set;} }
I have the following SQL-statement: SELECT DISTINCT name FROM log WHERE NOT name =
I have a following SQL QUERY: SELECT articles.name, articles.price, users.zipcode FROM articles INNER JOIN

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.