I have joined a table myself and I have the duplicate pairs highlighted in the image below, how can I delete them?
select DISTINCT A.name as name1 , B.name as name2 from (select name , ratings.* from reviewers inner join ratings on reviewers.id = ratings.reviewer_id ) A , (select name , ratings.* from reviewers inner join ratings on reviewers.id = ratings.reviewer_id ) B where A.reviewer_id <> B.reviewer_id and A.book_id = B.book_id order by name1 , name2 ASC
Name 1 | Name 2 |
---|---|
Alice Lewis | Elizabeth Blake |
Chris Thomas | John Smith |
Chris Thomas | Mike White |
Elizabeth Blake | Alice Lewis |
Elizabeth Blake | Jack Green |
Jack Green | Elizabeth Blake |
Joe Martinez | Mike Anderson |
John Smith | Chris Thomas |
Mike Anderson | Joe Martinez |
Mike White | Chris Thomas |
The above table was once a picture
You can do this
See this example
I have created DDL and DML statements to reproduce the database and written queries that retrieve unique pairs. Here's the "build" code that might help others:
This is the reconstructed query:
The same subquery uses aliases A and B twice, combining the
reviewers
andratings
tables and retrieving data for each reviewer-book rating pair. p>The main outer query then selects distinct reviewer name pairs from the subquery results. We use
JOIN
between subqueries A and B under 3 conditions:A.book_id = B.book_id
So these reviewers rated the same book.A.id B.id
Used to filter out reviewer pairs with the same ID to prevent self-matching.A.name Make sure the pairs are ordered consistent with the output below, eliminating duplicates. This way, for a given pair, only one name combination is considered, such as "Elizabeth Black-Jack Green", but not "Jack Green-Elizabeth Black".
This is the output you will get from the restructured query: