Main query results in Subquery - sql

How can we use reference of main query result set as a source table in subquery
Table A, Table C
Select
(Select * From a)
From
(Select tabA.*
From A tabA
Join C tabC
On tabA.id = tabC.id) as a
I got invalid object a error here

Presumably, you want a common table expression (CTE):
with a as (
select tabA.*
from A tabA Join
C tabC
on tabA.id = tabC.id
)
Select (Select * from a)
From a;
That said, your query makes no sense. The scalar subquery is probably going to be returning an error, either because of the number of rows or number of columns.

if you using sqlserver than modified your query based on below query.
select * from
(select A.* from TableA A inner join TableB B on A.EmployeeID = B.EmployeeID ) a

Related

DB2 SQL: How do I add columns from the 'WHERE EXISTS' table to the select statement

Letsay I have this query:
select A.ID_NUM,A.EFF_DT,B.STRT_DT,B.END_DT
FROM MEMBERSHIP A
WHERE EXISTS
(SELECT 1 FROM PLAN B
WHERE A.ID = B.ID)
How do I add columns from the PLAN table (B.STRT_DT,B.END_DT) to the select statement?
I get the error: B.STRT_DT is not valid in the context where it is used SQLCODE = 206
You use JOIN:
SELECT M.ID_NUM, M.EFF_DT, P.STRT_DT, P.END_DT
FROM MEMBERSHIP M JOIN
PLAN P
ON M.ID = P.ID;
A join would be the simplest way, although if your PLAN table is not unique on it's ID column, you would need to add a DISTINCT, either before (or after) the join.
SELECT A.ID_NUM,A.EFF_DT,B.STRT_DT,B.END_DT
FROM MEMBERSHIP A
JOIN (SELECT DISTINCT ID FROM PLAN) B
ON ( A.ID = B.ID )

Derived table dependency

When you join a table with a derived table, can the derived table query refer to columns from the other table in the join, and why?
Example :
SELECT
cr.CountryRegionCode,
cr.Name [Country Name],
crc.CurrencyCode
Currency cr
INNER JOIN
( -- there are 109 currency codes in CountryRegionCurrency
SELECT
[CountryRegionCode],
[CurrencyCode],
[ModifiedDate]
FROM [AdventureWorks2014].[Sales].[CountryRegionCurrency]
) crc ON cr.CountryRegionCode = crc.CountryRegionCode
Can the derived table query CRC refer to the columns of Currency ?
A derived table that is part of a JOIN cannot reference objects outside of the subquery's scope. A derived table that is part of an APPLY can reference columns outside of the subquery's scope.
Example:
SELECT *
FROM TableA A
CROSS JOIN (SELECT *
FROM TableB sq
WHERE A.ID = sq.A_ID) B;
This will fail, due to the object A not being defined with the scope of the subquery. The correct syntax for the above query would be this:
SELECT *
FROM TableA A
INNER JOIN (SELECT *
FROM TableB sq) B ON A.ID = B.A_ID;
On the other hand, if you were to use APPLY you could reference columns outside of the subquery's scope:
SELECT *
FROM TableA A
CROSS APPLY (SELECT *
FROM TableB sq
WHERE A.ID = sq.A_ID) B;
Edit: Not specific to the question, but it's worth noting that within a subquery if a column is not quantified then the column will always be assumed to reference the table in the subquery first. Let's assume, for example, that the columns to JOIN on are actually A.ID and B.ID. If you were therefore to do the below, it would work:
SELECT *
FROM TableA A
CROSS JOIN (SELECT *
FROM TableB sq
WHERE ID = ID) B;
That's because the WHERE might as well be WHERE B.ID = B.ID and B.ID is always going to equal the value of itself unless it has the value NULL.
It's therefore incredibly important to always quantify your columns.

SQL Get rows that doesn't appear in another table

I have this SQL problem: I have tables A and B. Table A has columns id and name, Table B amount and id which is a foreign key to table A.id.
I need to return all table A rows that don't have their id stored in table B. Any ideas?
So the complete opposite is:
SELECT *
FROM a
LEFT OUTER JOIN b ON a.id = b.id;
Here row what I need is left out of result
Just add a where clause:
SELECT a.*
FROM a LEFT OUTER JOIN
b
ON a.id = b.id
WHERE b.id IS NULL;
You can also use NOT EXISTS:
select a.*
from a
where not exists (select 1 from b where b.id = a.id);
In most databases, the two methods typically have similar performance.

Oracle SQL WITH clause select joined column

SQL:
WITH joined AS (
SELECT *
FROM table_a a
JOIN table_b b ON (a.a_id = b.a_id)
)
SELECT a_id
FROM joined
returns invalid identifier.
How can you select joined column when using WITH clause? I have tried aliases, prefixing and nothing worked. I know I can use:
WITH joined AS (
SELECT a.a_id
FROM table_a a
JOIN table_b b ON (a.a_id = b.a_id)
)
SELECT a_id
FROM joined
but I need this alias to cover all fields.
Only way I managed to meet this condition is using:
WITH joined AS (
SELECT a.a_id a_id_alias, a.*, b.*
FROM table_a a
JOIN table_b b ON (a.a_id = b.a_id)
)
SELECT a_id_alias
FROM joined
but it is not perfect solution...
You can use the effect of the USING clause when joining the tables.
When you join tables where the join columns have the same name (as it is the case with your example), the USING clause will return the join column only once, so the following works:
with joined as (
select *
from table_a a
join table_b b using (a_id)
)
select a_id
from joined;
SQLFiddle example: http://sqlfiddle.com/#!4/e7e099/2
I don't think you can do this without aliases. The result of the "joined" query has two fields, both named a_id. Unless you alias one (or both), as you did in your final query, the outer query has no idea which a_id you are referring to.
Why is your final query not a "perfect" solution?
You can probably use alias as below:
WITH JOINED AS (
SELECT A.A_ID A_A_ID, B.A_ID B_A_ID,
A.FIELD_NAME1 A_FIELDNAME1, A.FIELDNAME2 A_FIELDNAME2,A.FIELDNAME_N A_FIELDNAME_N,
B.FIELD_NAME1 B_FIELDNAME1, B.FIELDNAME2 B_FIELDNAME2,B.FIELDNAME_N B_FIELDNAME_N,
FROM TABLE_A A
JOIN TABLE_B B ON (A.A_ID = B.A_ID)
)
SELECT A_A_ID, B_A_ID
FROM JOINED
IT IS ALWAYS A GOOD PRACTICE TO AVOID USING SELECT *

sql - confusion with correlated subqueries

I have been using suggestions given to me in an earlier question regarding subqueries. See here:
sql - multiple layers of correlated subqueries
SQL Server : left join results in fewer rows than in left table
I am using ms sql server (I believe it is 2005).
What I am trying to do now is the following:
I have a result of rows (call it result A), that is obtained from doing the following:
select * from TableA as a
join (select * from TableB where RealDate = '4/20/2013 12:00:00 AM') as b
on a.id = b.id
Of this result I want to find all rows that are NOT in the result of rows returned by this query:
select * from TableA as a
join TableC as c
on c.id = a.id
Essentially I have a situation where the first query results in 246 records, while the second query results in 247 records. I was expecting the first result to return 247 records (all of which should be in the list of records returned by the second query). So now I need to investigate which record is missing so I can take proper action.
I tried to do something like the following but received various errors:
select * from (select * from TableA as a
join (select * from TableB where RealDate = '4/20/2013 12:00:00 AM') as b
on a.ul_id = b.id))
as result_A
where not exists (select 1 from (select * from TableA as a
join TableC as c
on c.id = a.ul_id) as result_B
where result_A.ul_id = result_B.id);
Do this set difference:
select a.* from TableA as a
join TableC as c
on c.id = a.id
except
select a.* from TableA as a
join (select * from TableB where RealDate = '4/20/2013 12:00:00 AM') as b
on a.id = b.id
If for some reason this does not return a row, then the first query has a duplicate.
Your narrative says you want to exclude results from:
select * from TableA as a
join TableC as c
on c.id = a.id
but your not exists has a different subquery. Also, some of your errors may have been caused by using the same alias more than once.
Finally, Pieter's approach should work. I didn't check the details.