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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 19, 20262026-05-19T12:12:18+00:00 2026-05-19T12:12:18+00:00

Suppose you have a table like: create table user_news ( user_id int unsigned not

  • 0

Suppose you have a table like:

create table user_news (
  user_id int unsigned not null, 
  article_id int unsigned not null, 
  article_date timestamp, 
  primary key (user_id, article_id), 
  key (user_id, article_date));

How can you ensure that every user_id has no more than 30 rows? That is, how can you delete all the rows ordered by article_date offset by 30 per user_id?

  • 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-19T12:12:19+00:00Added an answer on May 19, 2026 at 12:12 pm

    Note: The queries below use >= 2 since the test dataset is small. Change it to >= 30 for your solution.

    This is the DELETE statement.

    delete from x
    using  user_news x inner join
        (
        select a.user_id, a.article_id
        from user_news a
        where (
          select count(*)
          from user_news b
          where a.user_id = b.user_id
            and ((a.article_date < b.article_date)
              or (a.article_date = b.article_date
              and a.article_id < b.article_id))
           ) >= 2
        ) C on c.user_id = x.user_id and c.article_id = x.article_id
    

    Based on this test table

    create table user_news(
        user_id int,
        article_id int,
        article_date timestamp,
        primary key(user_id, article_id));
    insert into user_news select 1,2,'2010-01-02';
    insert into user_news select 1,3,'2010-01-03';
    insert into user_news select 1,4,'2010-01-01'; # article_id order != date order
    insert into user_news select 2,1,'2010-01-01';
    insert into user_news select 2,2,'2010-01-02'; # 3 ties on date
    insert into user_news select 2,3,'2010-01-02';
    insert into user_news select 2,4,'2010-01-02';
    insert into user_news select 4,5,'2010-01-05';
    

    And here is the inner SELECT query on its own to see what is happening

    select a.user_id, a.article_id
    from user_news a
    where (
      select count(*)
      from user_news b
      where a.user_id = b.user_id   # correlate the user_ids
            # earlier articles. for tie on date, break on article_id
        and ((a.article_date < b.article_date)
          or (a.article_date = b.article_date and a.article_id < b.article_id))
       ) >= 2
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

Suppose I have a 2 column table like this: | user_id | int(11) |
Suppose we have a table A: itemid mark 1 5 2 3 and table
Suppose I have a table called Companies that has a DepartmentID column. There's also
Suppose I have a table . Now, I'm interested in Getting Useful Data Easily.
Suppose I have a database table with two fields, foo and bar. Neither of
Suppose I have a tags table with two columns: tagid and contentid . Each
Suppose I have a database table that has a timedate column of the last
suppose that I have this RDBM table ( Entity-attribute-value_model ): col1: entityID col2: attributeName
suppose I have the following source table (called S): name gender code Bob 0
Suppose I have a table a with one column b and three rows(1,2,3), I

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.