Full Outer Join get repeated with Union? - mysql

I'm trying to accomplish a Full Outer Join with my SQL.
Reference Link
FULL (OUTER) JOIN: Return all records when there is a match in
either left or right table
Although apparently this is not supported. I've looked around and have come across this accepted answer: https://stackoverflow.com/a/4796911/3859456
SELECT * FROM t1
LEFT JOIN t2 ON t1.id = t2.id
UNION
SELECT * FROM t1
RIGHT JOIN t2 ON t1.id = t2.id
Although won't this at least repeat the matched records twice when we do a Union? If not does a union automatically overwrite the matched records to the 2 tables?
E.g.
LEFT (OUTER) JOIN: Return all records from the left table, and the
matched records from the right table
RIGHT (OUTER) JOIN: Return all
records from the right table, and the matched records from the left
table
Union Left-Outer-Table + (left-matched = right-matched)x2 + Right-Outer-Table
I'm sure the answer works as the community trust it. But I'm still confused as to how it works and hope that someone can help me understand better.

To reiterate from the accepted answer to which you refer, I will quote both the UNION and UNION ALL versions:
SELECT * FROM t1
LEFT JOIN t2 ON t1.id = t2.id
UNION
SELECT * FROM t1
RIGHT JOIN t2 ON t1.id = t2.id
and
SELECT * FROM t1
LEFT JOIN t2 ON t1.id = t2.id
UNION ALL
SELECT * FROM t1
RIGHT JOIN t2 ON t1.id = t2.id
WHERE t1.id IS NULL
If there were no duplicates generated by the join, then these two queries would return the same result set. The reason can be explained as:
The first half of the UNION/UNION ALL returns all records in common between the two tables (no duplicates, by our assumption), and it also return those records unique to the first table t1.
The second half of the union query returns all records in common and all records unique to the second table t2. But the UNION filters out those duplicate common records without altering the result set, since we assumed there are no duplicates.
The second half of the union all query selectively removes the duplicate common records using WHERE t1.id IS NULL. This ensures that only the records unique to the second table are added by the second half of the UNION ALL.
Now, if the first table itself happened to have duplicates, this is what would happen:
In the union query, duplicate records which occurred in the first table would be filtered off. This is subtle, because duplicates can arise from two sources here. First, there could be duplicates with the first table itself. Second, there could be duplicates which arise from the join. All duplicates would be removed from a UNION.
However, in the union all query, no duplicates would be removed. The duplicate records which might happen to appear in the first table would survive intact in the final result set, as would any duplicates which resulted from the join.
This is a long winded answer, but hopefully it convinces you that in the case of duplicates, the UNION and UNION ALL versions of the accepted answer may not generate the same result set.

Related

How to do a join on 2 tables, but only return the data for one table?

I am not sure if this is possible. But is it possible to do a join on 2 tables, but return the data for only one of the tables. I want to join the two tables based on a condition, but I only want the data for one of the tables. Is this possible with SQL, if so how? After reading the docs, it seems that when you do a join you get the data for both tables. Thanks for any help!
You get data from both tables because join is based on "Cartesian Product" + "Selection". But after the join, you can do a "Projection" with desired columns.
SQL has an easy syntax for this:
Select t1.* --taking data just from one table
from one_table t1
inner join other_table t2
on t1.pk = t2.fk
You can chose the table through the alias: t1.* or t2.*. The symbol * means "all fields".
Also you can include where clause, order by or other join types like outer join or cross join.
A typical SQL query has multiple clauses.
The SELECT clause mentions the columns you want in your result set.
The FROM clause, which includes JOIN operations, mentions the tables from which you want to retrieve those columns.
The WHERE clause filters the result set.
The ORDER BY clause specifies the order in which the rows in your result set are presented.
There are a few other clauses like GROUP BY and LIMIT. You can read about those.
To do what you ask, select the columns you want, then mention the tables you want. Something like this.
SELECT t1.id, t1.name, t1.address
FROM t1
JOIN t2 ON t2.t1_id = t1.id
This gives you data from t1 from rows that match t2.
Pro tip: Avoid the use of SELECT *. Instead, mention the columns you want.
This would typically be done using exists (or in) if you prefer:
select t1.*
from table1 t1
where exists (select 1 from table2 t2 on t2.x = t1.y);
Although you can use join, it runs the risk of multiplying the number of rows in the result set -- if there are duplicate matches in table2. There is no danger of such duplicates using exists (or in). I also find the logic to be more natural.
If you join on 2 tables.
You can use SELECT to select the data you want
If you want to get a table of data, you can do this,just select one table date
SELECT b.title
FROM blog b
JOIN type t ON b.type_id=t.id;
If you want to get the data from two tables, you can do this,select two table date.
SELECT b.title,t.type_name
FROM blog b
JOIN type t ON b.type_id=t.id;

MySQL Joins of tables with different columns

I want to create a table that needs to be a combination of selected columns from three or more tables. I don't have any sample data, just added the columns for explanation.
Table 1
A|B1|C1|D1|E1|F1|G1|H1|I1|J1
Table 2
A|B2|C2|D2|E2|F2|G2
Table 3
A|B3|C3|D3|E3|F3|G3
Resultant New table must have
A|B1|E1|F1|G1|J1|C2|D2|G2|B3|D3|F3
I'm not sure if I need to use a FULL JOIN or use UNIONS.
Each of these tables contain more than 400,000 rows. Any help here on what query needs to be included would be really helpful.
You can try the below query:
select t1.A,t1.B1,t3.E1,t1.F1,t1.G1,t1.J1,t2.C2,t2.D2,t2.G2,t3.B3,t3.D3,t3.F3
from table1 t1 join table2 t2 on t1.A = t2.A
join table3 t3 join table2 t2 on t3.A = t2.A
As Palec commented correctly in the other answer, so adding a bit of explanation to the answer.
You need to use the JOINS for this problem instead of UNION. The reason why I am saying to use JOINS over UNION is UNION combines the data/result of two or more queries into a single result set which includes all the rows which exist in the queries in your UNION. But when you are using JOINs, you can retrieve data from two or more tables based on logical relationships between the tables.
Also to add that you should add an alias name to your table so that it becomes easy to retrieve the column in the select query and also while linking the table.
Join is the correct way:
select table1.A,B1,E1,F1,G1,J1,C2,D2,G2,B3,D3,F3 from table1 join table2 on table1.A = table2.A
join table3 join table2 on table3.A = table2.A

SQL "IN" combined with "=" in WHERE clause

I'm struggling with someone else's code. What might the WHERE clause do in the following (MySQL) statement?
SELECT * FROM t1, t2 WHERE t1.id = t2.id IN (1,2,3)
It's not providing the desired result in my case, but I'm trying to figure what the original author intended.
Can anyone provide an example of the use of a WHERE clause like this?
This condition starts from the right, evaluates t2.id IN (1,2,3), gets the result (0 or 1), and uses it for join with t1.id. All rows of t2 with id from the IN list are joined to the row in t1 that has id of one; all other rows of t2 are joined with the row in t1 that has id of zero. Here is a small demo on sqlfiddle.com: link.
It is hard to imagine that that was the intent of the author, however: I think a more likely check was for both items to be in the list, and also being equal to each other. The equality to each other is important, because it looks like the author wanted to join the two tables.
A more modern way of doing joins is with ANSI SQL syntax. Here is the equivalent of your query in ANSI SQL:
SELECT * FROM t1 JOIN t2 ON t1.id = t2.id IN (1,2,3)

MySQL: different results with a WHERE statement included in master-detail query?

Let's say I have a mastertable (table1) with a detailtable (table2). There can be multiple detail records for each masterrecord. Now I want a query that counts all detailrecords for each masterrecord :
SELECT t1.id, count(t2.*)
FROM table1 as t1
LEFT JOIN table2 AS t2 ON t2.id=t1.id
GROUP BY t1.id
This gives me exactly the same number of records as table1 has.
But when I add a WHERE statement to only count the records that have a checkfield that's higher than 0, I don't get all records in table1 anymore! The ones with no matching detailrecords are now left out completely. Why is this happening?
SELECT t1.id, count(t2.*)
FROM table1 as t1
LEFT JOIN table2 AS t2 ON t2.id=t1.id
WHERE t2.checkfield != 0
GROUP BY t1.id
(Maybe something else is wrong in my real query, since I tried to simplify it for this example, but I think I got it right)
The WHERE clause restricts the joined results which are being aggregated over, so while you're trying do an outer join, only those rows with t2.checkfield != 0 survive, but that excludes all the unmatched rows!
On the other hand, when you change WHERE to AND, you now have tab1 LEFT OUTER JOIN tab2 ON(tab1.id = tab2.t1_id AND some_condition) -- but this is still an outer join, i.e. records on the left which have no match on the right will be included.

How to select two seperate non-overlapping tables in MySQL

I'm looking for a query to select rows from two different tables, keeping the column names the same (I did find one result here for selecting from two different tables, but it merged the column names to have an easier query). I need to keep the original column names, but have two different tables existing within the new, larger table. There are no overlapping columns between the two tables.
A picture, to visualise:
So, how can I do this? I know the query will probably be quite convoluted, but anything half-decent is probably going to be better than my current attempt:
SELECT t1.* , t2.*
FROM table1 t1 RIGHT OUTER JOIN table2 t2
ON r.someColumn1 = rc.someColumn2
UNION
SELECT t1.* , t2.*
FROM table1 t1 LEFT OUTER JOIN table2 t2
ON r.someColumn1 = rc.someColumn2
This does work, but only as long as there are no cases where someColumn1 = someColumn2 - which can happen quite easily, of course.
Any help is appreciated, and I apologise for what is probably a very silly question to which the smart answer is "don't do it, you fool!".
You can set your join criterion to never match:
SELECT t1.* , t2.*
FROM table1 t1 RIGHT OUTER JOIN table2 t2
ON 1 = 0
UNION
SELECT t1.* , t2.*
FROM table1 t1 LEFT OUTER JOIN table2 t2
ON 1 = 0
I don't have MySQL to test, but it works in SQL Server.
Edit: my first answer was wrong:
select * from Events
left join GroupList on ID=null
union
select Events.*,GroupList.* from GroupList
left join Events on GID=null
In the above GID and ID are keyfields in the tables.