How to group joins in OpenSQL - sql

I'd like to do the equivalent of
SELECT * FROM A LEFT OUTER JOIN (B INNER JOIN C)
using OpenSQL. Is this possible and what is the syntax?

It is possible, but the grouping needs to be an inline view in its own right. So, adapting your example here.
SELECT *
FROM A
LEFT OUTER JOIN (
SELECT B.FirstColumn, C.SecondColumn, B.Thirdcolumn
FROM B
INNER JOIN C
) D ON A.Something = D.SomethingElse
No outer references - The inline view cannot reference columns from table A unless it is part of the inline-query in its own right.
Any more specifics would likely need more detail about your query and intent.

Related

Is it possible to JOIN with a CTE in PostgreSQL?

I'm trying to write a query like this
WITH a AS (SELECT key FROM table)
SELECT *
FROM a
JOIN b;
which generates a syntax error in PostgreSQL 10.4.
Why does this error?
It looks like I will be creating a view instead. Is there a better solution?
You are missing the JOIN condition:
WITH a AS (SELECT key FROM table)
SELECT *
FROM a
JOIN b ON a.key = b.key;
The problem is not the CTE, it is a simple syntax error:
SELECT *
FROM a
JOIN b
-- something missing here
Here, JOIN defaults to an INNER JOIN, which requires some condition for which rows should be joined - generally either like ON a.key = b.key or USING key. The same would be true of a LEFT OUTER JOIN or RIGHT OUTER JOIN.
If you wanted all the possible combinations (rare, but occasionally useful), you would use CROSS JOIN:
SELECT *
FROM a
CROSS JOIN b;
Or the similar comma operator:
SELECT *
FROM a, b;

Oracle join's order

I have a sql sentence.
select a.*,b.*,c.*
from a
inner join b
on a.id=b.id
left join c
on b.id=c.id
but I not konw it is execute inner join first .then create a temporary table such as temp .and finaly temp left join c. inner join ,left join and right join do they have same level of execution.Thank you!
SQL is not a procedural language. A SQL query describes the result set being produced. When interpreting a query, the join order is from left to right. So, in your query, the result set is the one produced by an inner join on a and b with that result being left joined to c.
You can add parentheses to avoid ambiguity:
from (a inner join
b
on a.id = b.id
) left join
c
on b.id = c.id
But that is unnecessary.
That is logically how the query is processed. The optimization engine can choose many different ways of executing the query, some of which might be pretty unrecognizable as relating to this particular query. The only guarantee is what the result set looks like.

What is semi-join in database?

I am having trouble while trying to understand the concept of semi-join and how it is different from conventional join. I have tried some article already but not satisfied with the explanation, could someone please help me to understand it?
Simple example. Let's select students with grades using left outer join:
SELECT DISTINCT s.id
FROM students s
LEFT JOIN grades g ON g.student_id = s.id
WHERE g.student_id IS NOT NULL
Now the same with left semi-join:
SELECT s.id
FROM students s
WHERE EXISTS (SELECT 1 FROM grades g
WHERE g.student_id = s.id)
The latter is generally more efficient (depending on concrete DBMS and query optimizer).
As far as I know SQL dialects that support SEMIJOIN/ANTISEMI are U-SQL/Cloudera Impala.
SEMIJOIN:
Semijoins are U-SQL’s way filter a rowset based on the inclusion of its rows in another rowset. Other SQL dialects express this with the SELECT * FROM A WHERE A.key IN (SELECT B.key FROM B) pattern.
More info Semi Join and Anti Join Should Have Their Own Syntax in SQL:
“Semi” means that we don’t really join the right hand side, we only check if a join would yield results for any given tuple.
-- IN
SELECT *
FROM Employee
WHERE DeptName IN (
SELECT DeptName
FROM Dept
)
-- EXISTS
SELECT *
FROM Employee
WHERE EXISTS (
SELECT 1
FROM Dept
WHERE Employee.DeptName = Dept.DeptName
)
EDIT:
Another dialect that supports SEMI/ANTISEMI join is KQL:
kind=leftsemi (or kind=rightsemi)
Returns all the records from the left side that have matches from the right. The result table contains columns from the left side only.
let t1 = datatable(key:long, value:string)
[1, "a",
2, "b",
3, "c"];
let t2 = datatable(key:long)
[1,3];
t1 | join kind=leftsemi (t2) on key
demo
Output:
key value
1 a
3 c
As I understand, a semi join is a left join or right join:
What's the difference between INNER JOIN, LEFT JOIN, RIGHT JOIN and FULL JOIN?
So the difference between a left (semi) join and a "conventional" join is that you only retrieve the data of the left table (where you have a match on your join condition). Whereas with a full (outer) join (I think thats what you mean by conventional join), you retrieve the data of both tables where your condition matches.

left join with three tables

Can anyone explain what will happen in following scenario?
SELECT *
FROM A,
B
LEFT JOIN C
ON B.FIELD1=C.FIELD1
WHERE A.FIELD1='SOME VALUE'
Here table A and table B are not joined with any condition. So my doubt is what kind of join will be applied between A and B?
A cross join (cartesian product, if you prefer) will be applied between the results of A and B left join C: each row in the first set will be tied to each row in the second set.
A cross join applies, If you don't used a join condition, get irrelavent results also.
Please try it
SELECT * FROM A INNER JOIN B ON A.IDCOLUMND=B.IDCOLUMN LEFT JOIN C ON B.FIELD1=C.FIELD1

Joins in SQL for retriving data from two tables

There are two tables A and B. You are retreiving data from both tables where all rows from B table and only matching rows from A table should be displayed. Which of the following types of joins will you apply between A and B tables?
- Inner join
- Left outer join
- Right outer join
- Self join
Use left outer hoin or right outer join.
For example, the following satisfy your requirement.
select * from tableB
Left outer join tableA
on tableB.ID= tableA.ID
Or
select * from tableA
Right outer join tableB
on tableA.ID= tableB.ID
Better way to understand:
Easy, I would go with (B).
SELECT * FROM B x
LEFT JOIN A y
on x.someColName = y.someColname
EDIT: can also use Right join
SELECT * FROM A x
RIGHT OUTER JOIN B y
on x.someColName = y.someColname
This looks like homework, but it's dead simple enough that I'll just say that you're asking for B LEFT JOIN A.
Join Left
http://www.w3schools.com/Sql/sql_join_left.asp