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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 8, 20262026-06-08T07:24:39+00:00 2026-06-08T07:24:39+00:00

Consider the following table: tweets ———————– id tweet class ———————– 1 Foo bar baz!

  • 0

Consider the following table:

tweets
-----------------------
id  tweet         class
-----------------------
1   Foo bar baz!  2
2   Lorem ipsum   2
3   Foobar lorem  3
4   Activi set    1
5   Baz baz bar?  3
7   Dolor mez foo 3
8   Samet set bar 1

In reality, the table has 600,000 records, but as indicated in the example table, some ids have been deleted (so the highest id > 600,000). class can be either 1, 2 or 3.

I need a random selection 1200 tweets, 400 of each class. Is this possible to do with one query?

  • 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-08T07:24:40+00:00Added an answer on June 8, 2026 at 7:24 am

    If you have a finite number of possible values for class (3), then it is easily done with a set of UNION queries each having their own ORDER BY RAND() and LIMIT 400:

    (SELECT id, tweet, class FROM tweets WHERE class = 1 ORDER BY RAND() LIMIT 400)
    UNION
    (SELECT id, tweet, class FROM tweets WHERE class = 2 ORDER BY RAND() LIMIT 400)
    UNION
    (SELECT id, tweet, class FROM tweets WHERE class = 3 ORDER BY RAND() LIMIT 400)
    

    For the ORDER BY to be applied to each UNION group, the group must be enclosed in ().

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

Sidebar

Related Questions

Please consider the following tweets table: tweet_id user_id text ---------------------------- 1 1 lorem ipsum
Consider the following table: id foo ----------- 1 bar 2 baz 3 bat Desired
Consider the following table tweets --------------------------------- tweet_id nyse_date class type --------------------------------- 1 2011-03-12 2
Consider the following table of tweets id tweet ------------------------------------------------------ 1 alcoa inc stock analysis
Consider the following table tweets tweet_id call_id id_str timestamp text -------------------------------------------------- 1 11 12345
Consider the following tweets table tweet_id user_id text ----------------------------------------- 1 1 look at my
Consider the following tables: users and tweets user_id name tweet_id user_id tweet spam -----------------
Let's consider the following table models for sqlalchemy in python. class Worker(Base): id Column(Integer,
Consider the following table (tweets): +---------+------------+----------+ | user_id | _date | tweet_id | +---------+------------+----------+
Consider the following table which has the fields - id (int) and date_created (datetime):

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.