Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Will Inner join allow duplicates?

Tags:

sql

if join two tables using inner join method will it return duplicate values ?

like image 393
velmurugan sammantham Avatar asked Oct 24 '16 12:10

velmurugan sammantham


People also ask

Can inner join have duplicates?

Yes, if there are duplicate values.

Does LEFT join allow duplicates?

Again, if we perform a left outer join where date = date, each row from Table 5 will join on to every matching row from Table 4. However, in this case, the join will result in 4 rows of duplicate dates in the joined DataSet (see Table 6).

Does full outer join have duplicates?

Inner join will give you the records that have the same values in the joined columns between the 2 tables. From what you are saying, the 2 tables you are comparing are more or less the same, and full outer join giving you records from both tables, chances are you are going to get a lot of duplicates.


2 Answers

Here is an example with duplicate rows in both tables.

select * from customers;
 id |    name     | age |  address
----+-------------+-----+-----------
  1 | Ramesh      |  32 | Ahmedabad
  2 | Khilan      |  25 | Delhi
  3 | kaushik     |  23 | Kota          <-- id 3 "kaushik"
  3 | kaushik_two |  23 | Ahmedabad     <-- appears twice
  4 | Chaitali    |  25 | Mumbai
  5 | Hardik      |  27 | Bhopal
  6 | Komal       |  22 | MP
  7 | Muffy       |  24 | Indore
(8 rows)


select * from orders;
 oid |        date         | customer_id | amount
-----+---------------------+-------------+--------
 102 | 2009-10-08 00:00:00 |           3 |   3000    <-- reference to customer 3 
 100 | 2009-10-08 00:00:00 |           3 |   1500    <-- also appears twice
 101 | 2009-11-20 00:00:00 |           2 |   1560
 103 | 2008-05-20 00:00:00 |           4 |   2060
 104 | 2022-01-01 00:00:00 |         100 |   3900
(5 rows)

Inner Join

Produces duplicated rows for "kaushik" and "kaushik_two".

select id, name, amount, date 
from customers 
inner join orders on customers.id = orders.customer_id;

 id |    name     | amount |        date
----+-------------+--------+---------------------
  2 | Khilan      |   1560 | 2009-11-20 00:00:00
  3 | kaushik     |   1500 | 2009-10-08 00:00:00  <-- first pair
  3 | kaushik     |   3000 | 2009-10-08 00:00:00
  3 | kaushik_two |   1500 | 2009-10-08 00:00:00  <-- second pair
  3 | kaushik_two |   3000 | 2009-10-08 00:00:00
  4 | Chaitali    |   2060 | 2008-05-20 00:00:00
(6 rows)
like image 140
shj Avatar answered Oct 13 '22 01:10

shj


Yes, if there are duplicate values.

If you have CUSTOMERS table:

+----+----------+-----+-----------+----------+
| ID | NAME     | AGE | ADDRESS   | SALARY   |
+----+----------+-----+-----------+----------+
|  1 | Ramesh   |  32 | Ahmedabad |  2000.00 |
|  2 | Khilan   |  25 | Delhi     |  1500.00 |
|  3 | kaushik  |  23 | Kota      |  2000.00 |
|  4 | Chaitali |  25 | Mumbai    |  6500.00 |
|  5 | Hardik   |  27 | Bhopal    |  8500.00 |
|  6 | Komal    |  22 | MP        |  4500.00 |
|  7 | Muffy    |  24 | Indore    | 10000.00 |
+----+----------+-----+-----------+----------+

and ORDERS table as follows:

+-----+---------------------+-------------+--------+
| OID | DATE                | CUSTOMER_ID | AMOUNT |
+-----+---------------------+-------------+--------+
| 102 | 2009-10-08 00:00:00 |           3 |   3000 |
| 100 | 2009-10-08 00:00:00 |           3 |   1500 |
| 101 | 2009-11-20 00:00:00 |           2 |   1560 |
| 103 | 2008-05-20 00:00:00 |           4 |   2060 |
+-----+---------------------+-------------+--------+

Then inner join will give result:

SELECT  ID, NAME, AMOUNT, DATE
     FROM CUSTOMERS
     INNER JOIN ORDERS
     ON CUSTOMERS.ID = ORDERS.CUSTOMER_ID;

This would produce the following result:

+----+----------+--------+---------------------+
| ID | NAME     | AMOUNT | DATE                |
+----+----------+--------+---------------------+
|  3 | kaushik  |   3000 | 2009-10-08 00:00:00 |
|  3 | kaushik  |   1500 | 2009-10-08 00:00:00 |
|  2 | Khilan   |   1560 | 2009-11-20 00:00:00 |
|  4 | Chaitali |   2060 | 2008-05-20 00:00:00 |
+----+----------+--------+---------------------+
like image 39
Arti Avatar answered Oct 13 '22 01:10

Arti