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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 24, 20262026-05-24T17:46:17+00:00 2026-05-24T17:46:17+00:00

I have a table with data like so: [ID, Name] 1, Bob 1, Joe

  • 0

I have a table with data like so:

[ID, Name]
1, Bob
1, Joe
1, Joe
1, Bob

I want to retrieve a list of records showing the relationship between the records with the same ID.
For instance, I want the following result set from my query:

Bob, Joe
Joe, Bob
Bob, Bob
Joe, Joe

This shows me the “from” and “to” for every item in the table.

I can get this result by using the following query:

SELECT DISTINCT [NAME] 
FROM TABLE A
INNER JOIN TABLE B ON A.ID = B.ID

Is there anyway for me to achieve the same result set without the use of the “distinct” in the select statement? If I don’t include the distinct, I get back 16 records, not 4.

  • 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-24T17:46:18+00:00Added an answer on May 24, 2026 at 5:46 pm

    The reason you get duplicate rows without DISTINCT is because every row of ID = x will be joined with every other row with ID = x. Since the original table has (1, “Bob”) twice, both of those will be joined to every row in the other table with ID = 1.

    Removing duplicates before doing a join will do two things: decrease the time to run the query, and prevent duplicate rows from showing up in the result.

    Something like (using MySQL version of SQL):

    SELECT L.NAME, R.NAME
    FROM (SELECT DISTINCT ID, NAME FROM A) AS L
    INNER JOIN (SELECT DISTINCT ID, NAME FROM B) AS R
    ON L.ID = R.ID
    

    Edit: is B an alias for table A?

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

Sidebar

Related Questions

let say i have one table, which have data like: name status bob single
I have a table like this... CustomerID DBColumnName Data 1 FirstName Joe 1 MiddleName
I have three tables, table one (tableA) containing users data like name and email,
I have table one which looks like this. And I want to get data
I have one table in MYSQL It has data like Id Name 1 test
I have table with data about tasks like Id, Name, Date, WorkerId, VehicleId (and
I have table data like this id id1 name 1 1 test1 1 1
I have a mysql table with data like (name, address, sex, city state, zip,
Suppose I have a table with data like: Tab(id, name) 1, 'Name 1' 2,
Let's say I have a table which can contains data like this: id name

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.