Does memsql support Full Outer Join? - outer-join

I wanted to have a full outer join in memsql. Something like
SELECT *
FROM A FULL OUTER JOIN B
ON A.id = B.id
Is it possible ?

It appears that MemSQL does not have a FULL OUTER JOIN syntax. However, you should be able to simulate a full outer join in MemSQL using a combination of LEFT and RIGHT OUTER JOIN operations:
SELECT * FROM A
LEFT OUTER JOIN B ON A.id = B.id
UNION ALL
SELECT * FROM A
RIGHT OUTER JOIN B on A.id = B.id
WHERE ISNULL(A.id)
The first SELECT covers the orange area, namely matching records between A and B along with records in A which do not match to anything in B. The second query obtains only records in B which do not match to anything in A. Using UNION ALL instead of UNION ensures that duplicates are not removed.

Related

LEFT JOIN with a dataset that might or might not exist

I'm trying to JOIN a Master Dataset, via a left join with 2 other Datasets, all of them have the same Key field. So nothing special there.
One of those secondary Datasets is the result of another Query and therefor might or might not exist. Obviously my JOIN statement fails when this table doesn't exist.
Below a really simplified version of the code, the JOIN is used to exclude rows from the table_a that exist in table b or c (if they exist).
SELECT a.id, a.name
FROM table_a a
LEFT JOIN table_b b
ON a.id = b.id
LEFT JOIN table c c
ON a.id = c.id
WHERE b.id IS NULL
AND c.id IS NULL;
I am not sure that I understand your question well, but I think that you should better do:
SELECT a.id,a.name
FROM table_a a
WHERE a.id NOT IN
(SELECT id FROM table_b)
AND a.id NOT IN
(SELECT id FROM table_c)
Any query optimizer should have the exact same performance with this request, and I find it much more readable.

Is it possible to include ALL fields from joined table EXCEPT joined one?

Very often join fields have the same name in joined tables. If just join
SELECT a.*, b.* FROM a INNER JOIN b ON a.id=b.id
it will produce id field twice.
Is it possible to include ALL fields from joined table EXCEPT joined one?
UPDATE
I am using MySQL but standard way is also interesting to me!
UPDATE 2
Regarding USING syntax, how to use it with multiple joins?
SELECT * FROM
a INNER JOIN b USING (b_id)
INNER JOIN c USING (c_id)
swears table b doesn't contain c_id field, which is true, since it is inside a.
Normally I would write
SELECT * FROM
a INNER JOIN b ON a.b_id = b.b_id
INNER JOIN c ON a.c_id = c.c_id
In standard SQL this is achieved through USING
select *
from a
join b using (id);
This will return the id column only once.

How this queries will be evaluated? and, join syntax

How this queries will be evaluated?
(what i ask is: what will be the logic that the db-engine will use to gather the data?)?
A:
SELECT tableA.* FROM tableA
LEFT JOIN tableB ON tableB.key1 = tableA.key1
INNER JOIN tableC ON tableC.key2 = tableB.key2
B:
SELECT tableA.* FROM tableA
INNER JOIN tableC
LEFT JOIN tableB
ON (tableB.key1 = tableA.key1) AND (tableC.key2 = tableB.key2)
C:
What is the syntax, for joining multiple tables? (A and B for example)
D:
What is the logic behind the order of joins?
(How different joins (left, and inner) should be combines in a query?)
ANY-one?
SELECT tableA.* FROM tableA
LEFT JOIN tableB ON tableB.key1 = tableA.key1
INNER JOIN tableC ON tableC.key2 = tableB.key2
Since no brackets are used this should be evaluated left-to-right, so:
SELECT tableA.*
FROM
(tableA LEFT JOIN tableB ON tableB.key1 = tableA.key1)
INNER JOIN tableC ON tableC.key2 = tableB.key2
Meaning you first select all records from table A, with the matching records from B if they exist (outer join). That result set is then joined with C, but since you join on B.Key, all previous records where B = null will now disappear.
I am quite sure that the first join could be an inner join, giving the same result.
SELECT tableA.* FROM tableA
INNER JOIN tableC
LEFT JOIN tableB
ON (tableB.key1 = tableA.key1) AND (tableC.key2 = tableB.key2)
Now we first cross-join every record from A with every record from C (cartesian product).
That (possibly huge and possibly meaningless) resultset we extend with data from B where we can find it (meaning we add a record from B wherever we have a match with either A or B).
In general, when joining several tables, just take it step by step and always try to realize what you are joining with what. When in doubt, use brackets :)

full outer join in mysql [duplicate]

This question already has answers here:
Closed 11 years ago.
Possible Duplicate:
Full Outer Join in MySQL
There are two tables tableA and table B
both the tables have common column id
We want to get the results which are having all records in A but not in B
and all records which exists in B but not in A
Regards,
Chinta kiran
You can use UNION operator as follow
SELECT * FROM tablea
UNION
SELECT * FROM tableb
if you want to read more about
UNION operator
This is best accomplished with a LEFT OUTER JOIN where the predicate (WHERE clause) ensures that the joined row is NULL; something like:
SELECT A.* FROM A LEFT OUTER JOIN B ON A.id = B.a_id WHERE B.a_id IS NULL;
I suggest you to read the article A Visual Explanation of SQL Joins by Jeff Atwood of Coding Horror.
Full outer join produces the set of all records in Table A and Table B, with matching records from both sides where available. If there is no match, the missing side will contain null.
You are looking for MINUS SET OPERATOR:
"We want to get the results which are having all records in A but not in B"
Easy way:
SELECT A.*
FROM A
WHERE A.id not in (SELECT id FROM B)
With Full Outer Join
SELECT A.*
FROM A full outer join B on A.id = B.id
WHERE B.id is Null
The right way:
SELECT A.*
FROM A left outer join B on A.id = B.id
WHERE B.id is Null
Change A to B and B to A for in order to get the results which are having all records in B but not in A.

How can I do a FULL JOIN of MULTIPLE TABLES in MySQL

we have been searching for it but all we see is 2 tables by the left and right inner/outer joins.
I love you guys.
MySQL doesn't support FULL OUTER JOIN.
As you mention, you can simulate a FULL OUTER JOIN of two tables using a combination of LEFT and RIGHT OUTER joins.
SELECT * FROM tableA LEFT JOIN tableB ON tableA.b_id = tableB.id
UNION ALL
SELECT * FROM tableA RIGHT JOIN tableB ON tableA.b_id = tableB.id
WHERE tableA.b_id IS NULL
The same technique can in theory be extended to more than two tables. I'd suggest first using the above approach to join two of the tables as a view. Then use the same approach again to join the view to the third table.
I don't know what to say about the love part, but
Having tables named a and b:
SELECT a.*, b.* FROM a, b
Does this the trick?