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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 6, 20262026-06-06T01:29:51+00:00 2026-06-06T01:29:51+00:00

If I have a table1 with columns table1.user and table1.site, how can I return

  • 0

If I have a table1 with columns table1.user and table1.site, how can I return a list of distinct users that have access to ALL of a list of sites?

Let me clarify. I could start with the following code:

    SELECT  DISTINCT user
    FROM    table1
    WHERE   (site IN ('site1','site2','site3'))

Of course, this will display all distinct users that have entries for ANY of the three listed sites. I only want the users who have entries for ALL of the three listed sites.

I feel like there’s probably an obvious way to do this, and I’m probably going to feel quite stupid once someone points it out. Still, I’m drawing a blank.

  • 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-06T01:29:52+00:00Added an answer on June 6, 2026 at 1:29 am
    SELECT  user
    FROM    table1
    WHERE   site IN ('site1','site2','site3')
    GROUP BY user
    HAVING count(*) = 3
    

    This assumes that the user cannot be assigned to the same site more than once. If that is not the case, than Gordon’s comment holds true, and the having expression must be replaced with HAVING count(distinct site) = 3

    A more dynamic approach where the sites are only specified once and the count() adjusts automatically could be this:

    WITH sites (site_name) AS (
       VALUES ('site1'), 
              ('site2'),
              ('site3')
    )
    SELECT  user
    FROM    table1
    WHERE   site IN (SELECT site_name FROM sites) 
    GROUP BY user
    HAVING count(*) = (select count(*) from sites);
    

    (This is ANSI SQL, as no DBMS has been specified)

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

Sidebar

Related Questions

I am working on a site that has tests users can take. I have
i have a search page in my site, where user can search all the
I have a table of users with three columns. Username Accepted Rejected User 1
I have a site where users can save images together with a couple of
I have a table that stores comments users make about images on the site.
I have 3 tables with the following columns : * users : user_id *
I have a table in HTML which has three columns i.e Error - User
Consider i have a user table and I have three columns mobilePhone , homePhone
Currently I have a User table that has a toys_owned column that is an
I have two tables: table1, table2. Table1 has 10 columns, table2 has 2 columns.

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.