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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 14, 20262026-06-14T19:08:54+00:00 2026-06-14T19:08:54+00:00

i have a newby sql related question. let’s say i have this simple table:

  • 0

i have a newby sql related question.

let’s say i have this simple table:

A      B
------ ------
a      b
a      c
b      a
m      n
m      o
n      m

i’d like to query for records which have “counterparts” only, i.e. i want to get a b only if there is b a in the table but i want to skip the “back links” (which is b ahere).
to sum up i’d like to get the following result

A       B
------  ------
a       b
m       n

this sql query does not work since in the case b a is handled the a b is removed from my result set.

SELECT DISTINCT x1.A, x1.B
FROM TEST x1, TEST x2
WHERE x1.A = x2.B AND x1.B = x2.A -- all records /w counterparts only
AND x1.A NOT IN (SELECT B from TEST where B = x1.A) -- skip the "back links"

the 2nd part of the WHERE clause does not work as desired.

do you have any hints? any help with this would be greatly appreciated.

regards
peter

p.s. i am using the derby db.

  • 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-14T19:08:55+00:00Added an answer on June 14, 2026 at 7:08 pm

    You could change your last line to:

    AND x1.A < x1.B
    

    This assumes that either your columns are never self-referential (eg: a, a) or that you don’t want circular references to appear. If you do, then:

    AND x1.A <= x1.B
    

    EDIT:

    You’re probably better off using explicit joins as well:

    SELECT DISTINCT 
        x1.A
        , x1.B
    FROM 
        TEST x1
    JOIN 
        TEST x2
        ON x1.A = x2.B 
            AND x1.B = x2.A -- all records /w counterparts only
    WHERE x1.A < x1.B --Skip backreference
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have my table columns set like this: likes(id, like_message, timestamp) id is the
I think this should be pretty simple, but I'm a SQL newb. I have
i have sql table which has partitions. I recently created one partition. Even if
I have SQL table in which I have column and Probability . I want
I have merge two array with php function merge_recursive (both came from sql query)
Scenario: I have an application (C#) that expects a SQL database and login, which
I have a working survey that pulls each question and any related answers from
Possible Duplicate: Incrementing a field in SQL using PHP I have a table (T1)
I have a code in DAO that connects to a linked table in SQL
i would like to have a query that will solve my problem in native

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.