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;
Related
I have to re-write a SQL statement with a subquery so that it has a join for my job. So far, this is what I have.
SELECT * FROM Table_A
WHERE TABLE_A.A_ID NOT IN
(SELECT LK.A_ID FROM Link_Table LK
LEFT JOIN Table_B B
ON B.B_ID = LK.B_ID)
I am really having a hard time with this. I feel like this is because of the link tables though. Can anyone give me advice on altering this query?
Seems like you want a LEFT JOin with a IS NULL in the where:
SELECT {Column list} --Don't use *
FROM dbo.Table_A A
LEFT JOIN dbo.Link_Table LK ON A.A_ID = LK.A_ID
WHERE LK.A_ID IS NULL;
You don't need the reference to Table_B at all here.
Personally, however, I would prefer an EXISTS, but that is a subquery again:
SELECT {Column List}
FROM dbo.Table_A A
WHERE NOT EXISTS (SELECT 1
FROM dbo.Link_Table LK
WHERE A.A_ID = LK.A_ID);
I have three tables. Table A Table B and Table C they have id1,id2,id3 as primary key respectively.
id2 is foreign key in both A and C.
id1 is foreign key in C.
For following query we get five rows
select *
from A
where id3=123;
For following query we get three rows
select *
from C
where id3=123;
To get two remaining rows I performed outer join like
select *
from A,B,C
where C.id3=123
AND A.id1=C.id1(+)
I am getting required output
Is there any simpler way like in line query or use of "not exist" using which I can replace outer join ?
You syntax would generate a syntax error.
I speculate that you want to run something like this:
select a.*
from a left join
c
on a.id3 = c.id3 and a.id1 = c.id1
where a.id3 = 123;
However, I don't know what B is doing there; it is in the query but no where else in the question.
An outer join is the proper mechanism for what you want, but a more standard syntax would be:
select *
from A
LEFT JOIN C
ON A.id1=C.id1
WHERE id3=123
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.
I am trying to left join to tables using a query like this
SELECT * FROM table1 a, table2 b WHERE (a.ID = b.ID OR b.ID IS NULL)
In Oracle, this is equivalent to a LEFT JOIN (and in other databases as well, afaik).
Doing the same thing in DB2 (z/OS) produces an inner join - the b.ID IS NULL clause has no effect on the result, removing it does not change anything.
Is there a way to make this work in DB2? Is this something that should work according to ANSI SQL?
PS: I am aware that I can use the JOIN syntax, I'm just interested in why this doesn't work and if there is a way around this.
You can use
SELECT a.*, b.*
FROM tbl1 a LEFT JOIN tbl2 b ON a.id=b.id;
I have two tables, one has primary key other has it as a foreign key.
I want to pull data from the primary table, only if the secondary table does not have an entry containing it's key. Sort of an opposite of a simple inner join, which returns only rows that join together by that key.
SELECT <select_list>
FROM Table_A A
LEFT JOIN Table_B B
ON A.Key = B.Key
WHERE B.Key IS NULL
Full image of join
From aticle : http://www.codeproject.com/KB/database/Visual_SQL_Joins.aspx
SELECT
*
FROM
primarytable P
WHERE
NOT EXISTS (SELECT * FROM secondarytable S
WHERE
P.PKCol = S.FKCol)
Generally, (NOT) EXISTS is a better choice then (NOT) IN or (LEFT) JOIN
use a "not exists" left join:
SELECT p.*
FROM primary_table p LEFT JOIN second s ON p.ID = s.ID
WHERE s.ID IS NULL
Another solution is:
SELECT * FROM TABLE1 WHERE id NOT IN (SELECT id FROM TABLE2)
SELECT P.*
FROM primary_table P
LEFT JOIN secondary_table S on P.id = S.p_id
WHERE S.p_id IS NULL
If you want to select the columns from First Table "which are also present in Second table, then in this case you can also use EXCEPT. In this case, column names can be different as well but data type should be same.
Example:
select ID, FName
from FirstTable
EXCEPT
select ID, SName
from SecondTable
This was helpful to use in COGNOS because creating a SQL "Not in" statement in Cognos was allowed, but it took too long to run. I had manually coded table A to join to table B in in Cognos as A.key "not in" B.key, but the query was taking too long/not returning results after 5 minutes.
For anyone else that is looking for a "NOT IN" solution in Cognos, here is what I did. Create a Query that joins table A and B with a LEFT JOIN in Cognos by selecting link type: table A.Key has "0 to N" values in table B, then added a Filter (these correspond to Where Clauses) for: table B.Key is NULL.
Ran fast and like a charm.