Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

SQL join: avoid duplicate entries while joining two tables?

I have two tables, A and B, and I am trying to select rows from A based on a join query. I am getting some records from these tables, but there are some duplicate entries. How should I filter this query to avoid duplicate rows? For now, I'm doing it via Java code by putting these into a HashSet.

like image 538
JavaUser Avatar asked Feb 21 '26 06:02

JavaUser


1 Answers

The keyword DISTINCT is used to eliminate duplicate rows from a query result:

SELECT DISTINCT ...
  FROM A
  JOIN B ON ...

However, you can sometimes (possibly even 'often', but not always) avoid the need for it if the tables are organized correctly and you are joining correctly.

To get more information, you are going to have to ask your question more clearly, with concrete examples.

like image 86
Jonathan Leffler Avatar answered Feb 24 '26 00:02

Jonathan Leffler



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!