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

  • Home
  • SEARCH
  • 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 337841
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 12, 20262026-05-12T10:22:25+00:00 2026-05-12T10:22:25+00:00

we have a DB which stores users who may have pictures. I am looking

  • 0

we have a DB which stores users who may have pictures.

I am looking for an elegant way in SQL to get the following results:
Select n users. Of those n users e.g. 60% should have an associated picture and 40% should not have a picture. If there are less than 60% users having a picture the result should be filled up with users wihtout an image.

Is there some elegant way in SQL without firing multiple SELECTs to the DB?

Thank you very much.

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

    Ugly code:

    SELECT TOP @n * FROM
    (
    
    
          //-- We start selecting users who have a picture (ordered by HasPicture)
          //-- If there is no more users with a picture, this query will fill the 
          //-- remaining rows with users without a picture
          SELECT TOP 60 PERCENT * FROM tbUser
          ORDER BY HasPicture DESC
    
          UNION
    
          //-- This is to make sure that we select at least 40% users without a picture
          //-- AT LEAST because in the first query it is possible that users without a 
          //-- picture have been selected
          SELECT TOP 40 PERCENT * FROM tblUser
          WHERE HasPicture = 0
    
          //-- We need to avoid duplicates because in the first select query we haven't 
          //-- specified HasPicture = 1 (and we didn't want to).
          AND UserID not IN
          (
            SELECT TOP 60 PERCENT UserID FROM tbUser
            ORDER BY HavePicture DESC
          )
     )
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I am developing a webapp for teachers which may have hundreds of users who
I have a SQL Server 2008 table called users which stores users details. I
I have a table which stores test results like this: user | score |
I have a database which stores (among other things), the following pieces of information:
INFORMIX-SQL 7.32 (SE) Linux Pawnshop App. I have some users who own several pawnshops
I have application which is for paranoic users who used to store their sensitive
I have a select statement: select a, b, [...]; which returns the results: a|b
I have a table in which users store scores and other information about said
I have a boolean setting in my Properties/Settings.settings file, which basically stores per user
I have a MYSQL-Table which stores scores. Every time a user improves a new

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.