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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 13, 20262026-06-13T23:54:21+00:00 2026-06-13T23:54:21+00:00

I need help from a SQL guru here… I have data in the following

  • 0

I need help from a SQL guru here…

I have data in the following format:

Key  Value
100  A
100  B
300  A
400  B
500  A
500  B
500  C
600  B
600  W

If I want to find all keys with A or B and A and B, but not AB with anything else…how would I do this? Would I need a temp table or can I somehow join the table to itself?

EDIT: The desired output would be Key 100 since it has A & B, Key 300 since it has A, Key 400 since it has B. Not Key 500 because it also contains a C, and not key 600 because it also contains a W

  • 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-13T23:54:24+00:00Added an answer on June 13, 2026 at 11:54 pm

    This should work for you:

    SELECT DISTINCT t1.`key`
    FROM table1 t1
    WHERE t1.value in('A', 'B')
    GROUP BY t1.`key`
    HAVING COUNT(t1.value) = (SELECT COUNT(t2.value) 
                              FROM Table1 t2 
                              WHERE t1.`key` = t2.`key`)
    

    SQL Fiddle Demo

    This returns only the keys:

    • Has A and B only, and nothing else so key = 500 shouldn’t be returned. But key = 100 should be included.
    • Has only A. As in key = 300.
    • Has only B so key = 600 shouldn’t be returned since it contains one more value than B which is w. But key = 400 should be included.

    Update: How is this working

    If any key has a value IN('A', 'B') then it could also contains other values as well.

    Thats why I added the HAVING clause:

    HAVING COUNT(t1.value) = (SELECT COUNT(t2.value) 
                              FROM Table1 t2 
                              WHERE t1.`key` = t2.`key`)
    

    The COUNT(t1.value) is compared to the total COUNT of the values for the same key, by using a correlated subquerySELECT COUNT(t2.value) FROM Table1 t2 WHERE t1.key = t2.key , for each value t1.value. So if the current key contains values other than 'A', 'B' then the COUNT(t1.value) won’t equal to the COUNT of all values for the same key. For example the key = 500 has a count = 3 but using:

    SELECT DISTINCT t1.`key`
    FROM table1 t1
    WHERE t1.value in('A', 'B')
    GROUP BY t1.`key`
    

    Without the HAVING clause will include it as well, since it has either A' or ‘B’. But the HAVING clause eliminates this key since it has a count = 3 not equal to 2.

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

Sidebar

Related Questions

I need an SQL guru to help me speed up my query. I have
I need help inserting xml files into SQL Server 2008. I have the following
I am new to SVN. Need help from you guys. I have a link
Need some help from javascript gurus. I have one page where http://www.google.com/finance/converter is embedded
Need help writing a script downloads data from google insight using c# this is
Hi need help getting repeated value from give array,below is the code which im
Here i need help with joins. I have two tables say articles and users.
I need help with a SQL script. I have an orders table with a
I need some help with make a datatable from sql. I'm a Newbie. I
I need help reproducing the following SQL statement to a statement that SQLite understands.

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.