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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 22, 20262026-05-22T22:44:36+00:00 2026-05-22T22:44:36+00:00

How to select top 2 , bottom 2 and 6 random (not in Top

  • 0

How to select top 2 , bottom 2 and 6 random (not in Top 2 and Bottom 2) records of the table using one SQL select 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-05-22T22:44:37+00:00Added an answer on May 22, 2026 at 10:44 pm

    In MS SQL 2005/2008:

    with cte
    as
    (
        select 
            row_number() over (order by name) RowNumber, 
            row_number() over (order by newid()) RandomOrder,
            count(*) over() Total,
            *
        from sys.tables
    )
    select *
    from cte
    where RowNumber <= 2 or Total - RowNumber + 1 <= 2
    union all
    select *
    from
    (
        select top 6 *
        from cte 
        where RowNumber > 2 and Total - RowNumber > 2
        order by RandomOrder
    ) tt
    

    Replace sys.tables with your table name and alter order by name to specify order condition for top 2 and bottom 2.

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

Sidebar

Related Questions

I can do SELECT TOP (200) ... but why not BOTTOM (200)? Well not
I have a query: select * from (select top 100 percent ... from [table]
I have a table for which I want to select top the 5 rows
Which SQL statement is faster? SELECT TOP 2 c1.Price, c2.Price, ..... c49.Price, c50.Price FROM
In SQL server I can say: Select top 50 percent How can I say
I am trying to add a where clause to the following query: SELECT TOP
How do I do this Select top 10 Foo from MyTable in Linq to
Some databases support commands such as: SELECT TOP 10 START AT 10 * FROM
I'm using SQL Server 2005 and I'm trying to achieve something like this: I
I am adding rectangles on a Canvas using Random because I don't know 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.