Proper way to refer to COALESCED value in SELECT statement? - sql

If I am using the "COALESCE" function in a SELECT statement. What is the proper way to refer to the column value in the JOIN? For example ..
SELECT
ID,
COALESCE(TableA.Name1, TableA.Name2)
FROM
TableA
LEFT JOIN TableB ON TableA.ID = TableB.ID
LEFT JOIN TableC ON TableA.ID = TableC.ID
LEFT JOIN TableD ON <COALESCE VALUE HERE> = TableD.Name

You would just use LEFT JOIN TableD ON COALESCE(TableA.Name1, TableA.Name2) = TableD.Name
There's no way to reference an expression from the SELECT clause anywhere within the query that clause refers to.

This is another way to expand out the COALESCE, which may work better in certain distributions of data
SELECT
ID,
COALESCE(TableA.Name1, TableA.Name2)
FROM
TableA
LEFT JOIN TableB ON TableA.ID = TableB.ID
LEFT JOIN TableC ON TableA.ID = TableC.ID
LEFT JOIN TableD ON (TableA.Name1 = TableD.Name)
OR (TableA.Name1 IS NULL AND TableA.Name2 = TableD.Name)

SELECT
ID,
COALESCE(TableA.Name1, TableA.Name2)
FROM
TableA
LEFT JOIN TableB ON TableA.ID = TableB.ID
LEFT JOIN TableC ON TableA.ID = TableC.ID
LEFT JOIN TableD ON COALESCE(TableA.Name1, TableA.Name2) = TableD.Name

To what other persons said, I'll add
SELECT Base.* FROM
(SELECT
ID,
COALESCE(TableA.Name1, TableA.Name2) Names
FROM TableA
) Base
LEFT JOIN TableB ON Base.ID = TableB.ID
LEFT JOIN TableC ON Base.ID = TableC.ID
LEFT JOIN TableD ON Base.Names = TableD.Name
And as Gabe suggested, with a CTE (I think it's a little overkill a CTE here, and I love using CTEs)
; WITH Base AS (
SELECT
ID,
COALESCE(TableA.Name1, TableA.Name2) Names
FROM TableA
)
SELECT Base.* From Base
LEFT JOIN TableB ON Base.ID = TableB.ID
LEFT JOIN TableC ON Base.ID = TableC.ID
LEFT JOIN TableD ON Base.Names = TableD.Name

The obvious ways will work:
LEFT JOIN
TableD
ON COALESCE(TableA.Name1, TableA.Name2) = TableD.Name
or:
LEFT JOIN
TableD
ON TableA.Name1 = TableD.Name
OR TableA.Name2 = TableD.Name
(EDIT: the second query won't work, like it says in the comments)

Related

SQL : left join with additional criteria

I am doing a left outer join :
select * from tableA
left join tableB on tableA.tableB_id = tableB.id
How can I add another search criteria on tableB only for matching tableB records? (like 'and tableB.firstname like 'a_firstname%')
If I do :
select * from tableA
left join tableB on tableA.tableB_id = tableB.id
where tableB.firstname like 'a_firstname%'
it doesn't display records where on tableA.tableB_id = tableB.id doesn't match.
please check this
select * from tableA
left join tableB on tableA.tableB_id = tableB.id and tableB.firstname like 'a_firstname%'

SQL full join priority

Say you have 3 tables (tableA, tableB, tableC), each with an ID column and a Value column. Some of the tables' IDs match but some don't.
If you do:
SELECT tableA.ID FROM tableA
FULL JOIN tableB ON (tableA.ID = tableB.ID)
FULL JOIN tableC ON (tableA.ID = tableC.ID)
Is this different from:
SELECT tableA.ID FROM tableA
FULL JOIN tableB ON (tableA.ID = tableB.ID)
FULL JOIN tableC ON (tableB.ID = tableC.ID)
Or:
SELECT Y.ID FROM
(SELECT tableA.ID FROM tableA
FULL JOIN tableB ON (tableA.ID = tableB.ID)) X
FULL JOIN tableC ON (X.ID = tableC.ID)) Y
??? Someone please explain if there is a difference. Thanks.
[Oracle SQL Developer version 4.02.15.21]
For starters, here are all 3 statements, syntactically cleaned up:
SELECT COALESCE(a.ID,b.ID,c.ID)
FROM tableA a
FULL JOIN tableB b ON a.ID = b.ID
FULL JOIN tableC c ON a.ID = c.ID
SELECT COALESCE(a.ID,b.ID,c.ID)
FROM tableA a
FULL JOIN tableB b ON a.ID = b.ID
FULL JOIN tableC c ON b.ID = c.ID
SELECT COALESCE(X.ID,c.ID)
FROM
( SELECT COALESCE(a.ID ,b.ID) ID
FROM tableA a
FULL JOIN tableB b ON a.ID = b.ID) X
FULL JOIN tableC c ON X.ID = c.ID
Surprisingly, the syntax of the first statement produces duplicate values, but statements 2 and 3 work as advertised.
Edit: Upon further testing, statements 1 and 2 are prone to duplicates, depending on which tables overlap. Statement 3 seems to be the only solid approach.
SQLFiddle

In SQL, What's the difference a ON condition following a Join vs at the end of multiple JOINS

I have been having a hard time googling an answer for this, but....
can someone explain to me the difference between putting the ON condition of a JOIN with the the JOIN itself vs putting the ON at the end of all the other JOINs.
here is an example http://sqlfiddle.com/#!3/e0a0f/3
CREATE TABLE TableA (Email VARCHAR(100), SomeNameA VARCHAR(100))
CREATE TABLE Tableb (Email VARCHAR(100), SomeNameB VARCHAR(100))
CREATE TABLE Tablec (Email VARCHAR(100), SomeNameC VARCHAR(100))
INSERT INTO TableA SELECT 'joe#test.com', 'JoeA'
INSERT INTO TableA SELECT 'jan#test.com', 'JaneA'
INSERT INTO TableA SELECT 'dave#test.com', 'DaveA'
INSERT INTO TableB SELECT 'joe#test.com', 'JoeB'
INSERT INTO TableB SELECT 'dave#test.com', 'DaveB'
INSERT INTO TableC SELECT 'joe#test.com', 'JoeC'
INSERT INTO TableC SELECT 'dave#test.com', 'DaveC'
SELECT TOP 2 a.*,
b.*,
c.*
FROM TableA a
LEFT OUTER JOIN TableB b
ON a.email = b.email
INNER JOIN TableC c
ON c.Email = b.email;
SELECT TOP 2 a.*,
b.*,
c.*
FROM TableA a
LEFT OUTER JOIN TableB b
INNER JOIN TableC c
ON c.Email = b.email
ON a.email = b.email;
I don't understand why these two SELECT statements produce different results.
What matters is orders of joins. Treat your expressions as if every join produced temporary "virtual" table.
So when you write
FROM TableA a
LEFT OUTER JOIN TableB b ON a.email = b.email
INNER JOIN TableC c ON c.Email = b.email ;
then order is as follows:
TableA is left joined to TableB producing temporary relation V1
V1 is inner joined to TableC.
Meanhwile when you write:
FROM TableA a
LEFT OUTER JOIN TableB b
INNER JOIN TableC c ON c.Email = b.email ON a.email = b.email;
then order is as follows:
TableB is inner joined to TableC producing temporary relation V1.
TableA is left joined to V1.
Thus results are different. It is generally recommended to use parenthesis in such situations to improve readability of the query:
FROM TableA a
LEFT OUTER JOIN
(TableB b INNER JOIN TableC c ON c.Email = b.email)
ON a.email = b.email;
In your second example, the part ON a.email = b.email belongs to the LEFT JOIN.
If written like this, it means the following:
INNER JOIN TableC with TableB and LEFT OUTER JOIN the result with TableA.
The result will be all rows from TableA joined with those rows from TableB that also have an entry in TableC.
The first example means the following:
LEFT OUTER JOIN TableB with TableA and INNER JOIN TableC with the result. This is equivalent to using an INNER JOIN for TableB.
Explanation: When you LEFT OUTER JOIN TableA with TableB you will get all rows from TableA and for matching rows in TableB you will get that data, too. In your result set you will have rows with b.email = NULL and this will now be INNER JOINed with TableC. As long as there is no entry in TableC with email = NULL you will get the results you observed.

SELECT * FROM tableA, tableB WHERE Conditions [+]

I have the following query
SELECT *
FROM tableA, tableB
WHERE Conditions [+]
What does this keyword Conditions[+] Stands for?
How this query behaves as a outer join?
That is old Oracle Join syntax.
SELECT *
FROM tableA, tableB
WHERE Conditions [+] -- this should be tableA (+) = tableB
The positioning of the + sign denotes the JOIN syntax.
If you query was:
SELECT *
FROM tableA, tableB
WHERE tableA.id (+) = tableB.Id
Then it would be showing a RIGHT OUTER JOIN so the equivalent is:
SELECT *
FROM tableA
RIGHT OUTER JOIN tableB
ON tableB.id = tableA.Id
If the + sign was on the other side then it would be a LEFT OUTER JOIN
SELECT *
FROM tableA, tableB
WHERE tableA.id = tableB.Id (+)
is equivalent to
SELECT *
FROM tableA
LEFT OUTER JOIN tableB
ON tableA.id = tableB.Id
I would advise using standard join syntax though.
If you do not specify a + sign then it will be interpreted as an INNER JOIN
SELECT *
FROM tableA, tableB
WHERE tableA = tableB
it's equivalent is:
SELECT *
FROM tableA
INNER JOIN tableB
ON tableA.id = tableB.id
A FULL OUTER JOIN would be written using two SELECT statements and a UNION:
SELECT *
FROM tableA, tableB
WHERE tableA.id = tableB.Id (+)
UNION
SELECT *
FROM tableA, tableB
WHERE tableA.id (+) = tableB.Id
It's equivalent is:
SELECT *
FROM tableA
FULL OUTER JOIN tableB
ON tableA.id = tableB.id
Here is a tutorial that explains a lot of these:
Old Outer Join Syntax
It is not important how that behaves. You should use the standard syntax for outer joins:
select *
from tableA left outer join
tableB
on . . .
The "(+)" syntax was introduced by Oracle before the standard syntax, and it is highly out of date.
'Conditions' here just means what you're using to filter all this data.
LIke here's an example:
SELECT *
FROM tableA, tableB
WHERE Name like '%Bob%'
would return names that have "Bob" anywhere inside.
About outer joins, actually you'd use that in the FROM clause:
So maybe
SELECT *
FROM tableA ta
OUTER JOIN tableB tb
ON ta.name = tb.name
WHERE ta.age <> 10
and there where here is optional, by the way
I hate to just copy & paste an answer, but this sort of thing can be found pretty easily if you do a little searching...
An outer join returns rows for one table, even when there are no
matching rows in the other. You specify an outer join in Oracle by
placing a plus sign (+) in parentheses following the column names from
the optional table in your WHERE clause. For example:
SELECT ut.table_name, uc.constraint_name
FROM user_tables ut, user_constraints uc
WHERE ut.table_name = uc.table_name(+);
The (+) after uc.table_name makes the user_constraint table optional.
The query returns all tables, and where there are no corresponding
constraint records, Oracle supplies a null in the constraint name
column.

Difficulty with sql query

I have the following tables:
TableA (id, tableB_id, tableC_id)
TableB (id, expirationDate)
TableC (id, expirationDate)
I want to retrieve all the results from TableA ordered by tableB.expirationDate and tableC.expirationDate. How can I do this?
select ta.*
from TableA ta
inner join TableB tb on ta.tableB_id = tb.id
inner join TableC tc on ta.tableC_id = tc.id
order by tb.expirationDate, tc.expirationDate
Update:
If you are not getting all the records, then you'll need to use a left outer join:
select ta.*
from TableA ta
left outer join TableB tb on ta.tableB_id = tb.id
left outer join TableC tc on ta.tableC_id = tc.id
order by tb.expirationDate, tc.expirationDate
If the result set is empty with the other suggestions, are you sure the data in the tables is actually correctly correlated to each other?
Can you post some sample rows for each table?
Have you tried:
SELECT a.* FROM TableA a
INNER JOIN TableB b on b.id = a.tableB_id
INNER JOIN TableC c on c.id = a.tableC_id
ORDER BY b.expirationDate, c.expirationDatetableB_id