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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 26, 20262026-05-26T15:57:27+00:00 2026-05-26T15:57:27+00:00

I need help with a SQL Query I have this table likes friend friend2

  • 0

I need help with a SQL Query

I have this table

likes
 friend    friend2
      1          2
      2          1
      3          1
      4          5

I need to select only one pair of the friends that like each other.

With my current select I’m obtaining this

 id     name         id     name
1709    Cassandra    1689   Gabriel 
1689    Gabriel      1709   Cassandra
1501    Jessica      1934   Kyle    
1934    Kyle         1501   Jessica 

but the expected query result is:

 id     name        id     name
1709    Cassandra   1689    Gabriel 
1501    Jessica     1934    Kyle    
  • 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-26T15:57:28+00:00Added an answer on May 26, 2026 at 3:57 pm

    If you are sure that every pair is entered in the database twice (once as A, B and again as B,A) then you can use this:

     SELECT friend, friend2
     FROM yourtable
     WHERE friend < friend2
    

    If there can be some pairs that are only entered once, then you can use this:

     SELECT DISTINCT
         LEAST(friend, friend2) AS friend,
         GREATEST(friend, friend2) AS friend2
     FROM yourtable
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

today i need your help to get an specific sql select query. i have
I need help to build SQL query. I have 2 tables table PROJECT and
I need some help to build SQL Query. I have table having data like:
Need a little help with a SQL / ActiveRecord query. Let's say I have
I need some help with SQL Query. I am trying to select all records
Okay, I need help. I'm usually pretty good at SQL queries but this one
i need your help in access db i have a table like this ID
I need help. I have a sql table t2 related to two other tables
I'm somewhat new to SQL and need help with query syntax. My issue involves
I have an interesting SQL problem that I need help with. Here is the

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.