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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 13, 20262026-06-13T14:15:53+00:00 2026-06-13T14:15:53+00:00

In the following db, I need all users and their locations, without duplicates, and

  • 0

In the following db, I need all users and their locations, without duplicates, and with those in NJ listed first.
(If they have a NJ address, I do not need their other addresses. Database simplified for clarity).

> table address,

|  id  |  state |   City   |
|------|--------|----------|
|  01  |   NY   |  Gotham  |
|  02  |   NY   |  Uye     |
|  03  |   NJ   |  Hoboken |
|  04  |   NJ   |  Newark  |

> table contact

|  user  |  address  |
|--------|-----------|
|   01   |     01    |
|   02   |     02    |
|   02   |     03    |
|   03   |     04    |

Below are some of my attempts and their outputs.
From what I have read [I have spent hours on this], I think this is called a ambiguous GROUP BY query, and is only allowed by MySQL.

I can’t figure out the correct way to do this, though. Please help!

DESIRED RESULT:

|  user  |  state  |
|--------|---------|
|   02   |   NJ    |
|   03   |   NJ    |
|   01   |   NY    |

OTHER ATTEMPTS:

SELECT user, state FROM contact, address WHERE id = address;
// Duplicate users, and addresses I do not need.

|  user  |  state  |
|--------|---------|
|   01   |   NY    |
|   02   |   NY    |
|   02   |   NJ    |
|   03   |   NJ    |  

SELECT user, state FROM contact, address WHERE id = address GROUP BY user;
// NY address. I need the NJ address.

|  user  |  state  |
|--------|---------|
|   01   |   NY    |
|   02   |   NY    |
|   03   |   NJ    |

SELECT user, state FROM contact, address WHERE id = address GROUP BY user HAVING state = 'NJ';
//Worse, now I lose my NY users, and one of my NJ users doesn't even show 

|  user  |  state  |
|--------|---------|
|   03   |   NJ    |
  • 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-13T14:15:54+00:00Added an answer on June 13, 2026 at 2:15 pm

    It may not be pretty, but this solution works. I’ve updated the query here to reflect changes in the fiddle:

    SELECT user, state, `state` LIKE 'NJ' ismatch
    FROM contact, address WHERE id = address
    AND (state = 'NJ' OR `user` NOT IN 
        (SELECT `user` FROM contact, address WHERE address = id AND `state` LIKE 'NJ'))
    GROUP BY user
    ORDER BY ismatch DESC;
    

    Simple explanation: Join every user with his address where the address is in NJ or with any address of his if he has no addresses in NJ. The GROUP BY is needed to prevent duplicate users from showing up in the results.

    Essentially what the problem boils down to is wanting to retrieve a matching address for a user if it exists, and otherwise just to get any address for that user. Thus the second condition in the where clause, which will pull an NJ address only if that user has one, otherwise it will just take the first normally. The ismatch field is for sorting purposes.

    Note: I originally wrote this using JOINs between the tables. They have been removed for clarity purposes.

    Link to SQL Fiddle

    EDIT: It was pointed out to me that this solution does not work in all cases (see comments). To fix this, I added a GROUP BY, and now it appears to work. I also replaced the CASE with a simple comparison to improve performance: SQLFiddle

    In addition, an alternate answer has been suggested to me by someone I know with experience in database management. His suggestion wast to do it in two queries (at least), so I built those queries and used a UNION:

    SELECT user, state, 1 ismatch FROM contact, address
    WHERE id = address AND state = 'NJ' GROUP BY user
    UNION
    SELECT user, state, 0 ismatch FROM contact, address
    WHERE id = address AND `user` NOT IN (SELECT `user` FROM contact, address WHERE address = id AND `state` LIKE 'NJ')
    GROUP BY user ORDER BY ismatch DESC
    

    Simple Explanation: Fetch every user with an NJ address, then fetch every user with any address if he has no NJ addresses, and UNION the two result sets together. GROUP BY is used in both queries to prevent duplicates.

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

Sidebar

Related Questions

I need a codeigniter route so all of the following urls: admin/users/page/:num admin/accounts/page/:num members/results/page/:num
i have a string which has the following format (3,1,Mayer,Jack). I need all 4
I need to find all CIDs (Contracts) meet the following conditions They are active
With the following object hierarchy, I need to confirm whether or not all string
Need help with following situation: Users can generate their own data structures which are
I have the following entities: User Company Organization Users need to be able to
I need to convert all of the following forms into .NET Uri object: hello.world
I've got the following code, and need to strip all non alpha numeric characters.
I need an sql query to give me the following: All the values in
I need a VBA function that will perform the following: Find all unique values

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.