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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 18, 20262026-05-18T10:24:10+00:00 2026-05-18T10:24:10+00:00

i want to select first 10 rows then select 5 random rows of them.

  • 0

i want to select first 10 rows then select 5 random rows of them.

  • 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-18T10:24:10+00:00Added an answer on May 18, 2026 at 10:24 am

    Have to define “first 10” and it’s “something” in this snippet. This is for SQL Server 2000+

    select top 5 
        * 
    from 
        (
        select top 10 * from <table>
        order by something --got to have soemthing here to define "first 10"
        ) as a
    order by 
        newid()
    

    Edit:

    Why you need ORDER BY in the derived table

    -- large row, same result with and without index/PK
    CREATE TABLE #foo (bar int /*PRIMARY KEY NONCLUSTERED (bar)*/, eejit char(8000))
    
    --create rows with value 1-10 + some others
    INSERT #foo (bar) VALUES (1)
    INSERT #foo (bar)  VALUES (10)
    INSERT #foo (bar)  VALUES (20)
    INSERT #foo (bar)  VALUES (2)
    INSERT #foo (bar)  VALUES (5)
    INSERT #foo (bar)  VALUES (45)
    INSERT #foo (bar)  VALUES (99)
    INSERT #foo (bar)  VALUES (3)
    INSERT #foo (bar)  VALUES (9)
    INSERT #foo (bar)  VALUES (7)
    INSERT #foo (bar)  VALUES (6)
    INSERT #foo (bar)  VALUES (4)
    INSERT #foo (bar)  VALUES (8)
    
    --create logical fragmentation
    DELETE #foo WHERE bar IN (1, 3, 5, 7, 9)
    INSERT #foo (bar) VALUES (1)
    INSERT #foo (bar) VALUES (3)
    INSERT #foo (bar) VALUES (5)
    INSERT #foo (bar) VALUES (7)
    INSERT #foo (bar) VALUES (9)
    
    -- run this a few times, you will see values > 10
    -- "first 10" surely means values between 1 and 10?
    select top 5 
        * 
    from 
        (
        select top 10 * from #foo
        ) as a
    order by 
        newid()
    
    -- always <= 10 because of ORDER BY
    select top 5 
        * 
    from 
        (
        select top 10 * from #foo
        order by bar --got to have soemthing here to define "first 10"
        ) as a
    order by 
        newid()
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

Let's say I want to first select rows which have download_link the same. Then,
I need to select rows with common IDs first, then the remaining rows without
I want to select a handful of random rows from the results of a
Not sure if you can do this, but I want to select the first
I want to implement chained select boxes: the first select box determines the values
I want to select the top N rows of a table, ranked by how
I want to echo only the first 10 rows, but I need to count
I'm trying to grab multiple rows from SQL Server and then manipulate them in
Using MySQL 5.x I want to efficiently select all rows from table X where
Say I have 50 rows in a MySQL table. I want to select the

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.