Why can I reference a field from an outer query in SQL - sql

Given theses tables:
create temporary table a(pid integer, cid integer);
create temporary table b(id integer, code varchar);
This works but returns wrong results:
select pid
from a
where cid in (select cid from b where code like 'AE%')
I just had a query like that where I use the wrong field, and it surprised me that that query even works. Doesn't a query like this just return all the rows from table a ?
Do you have any example of a query written like this that would be useful ?
Maybe I'm missing something.

You regulary need fields from the outer query in the where clause of the inner query:
select * from a
where exists ( select 1 from b where id = cid );
or
select * from a
where 'HELLO' in (select code from b where id = cid );
We can also construct examples where the outer fields are (kind of) useful in the select clause:
select * from a
where 1 = any (select id-cid from b where code like 'HE%');
Therefore, access to the fields of the outer query is absolutely necessary.

Related

How to insert data from CTE to a Temp Table?

I am trying to create a some logic using CTE and then instead of using DML statement after CTE, I am trying to create a temp table using CTE. This is possible in T-SQL. Is it possible in GBQ?
I know I can create temp table instead of CTE in the below example, but just want to know the possibility!
WITH xyz AS
(SELECT * FROM table1)
CREATE TEMP TABLE temp1 AS (
SELECT * FROM xyz INNER JOIN table2 on ...);
Use below instead
CREATE TEMP TABLE temp1 AS (
WITH xyz AS
(SELECT * FROM table1)
SELECT * FROM xyz INNER JOIN table2 on ...
);
So in 2022 I believe that no longer works without a script or session in GBQ:
You could write your query as follows:
WITH xyz AS (
SELECT
*
FROM table1
)
SELECT
*
FROM xyz
INNER JOIN table2
ON ...
and then click the More Button -> Query Settings as shown below:
After that you can set a destination for your results a a temporary table and here you can define the name of your table etc. in your case it's temp1:
This way you can just save the results of your query into a temporary table. Hope it helps!

SQL select from column where column equals table variable?

I'm a serious SQL noob so any help is appreciated. I'm having a hard time even explaining what I'm trying to do so I'll lay out what I have so far:
DECLARE #UserIDInt table (ID int);
INSERT into #UserIDInt
SELECT UserId
FROM [LcsCDR].[dbo].[Users]
WHERE [LcsCDR].[dbo].[Users].[UserUri] LIKE '%example';
SELECT *
FROM [LcsCDR].[dbo].[SessionDetails]
WHERE [LcsCDR].[dbo].[SessionDetails].[User1Id] = #UserIDInt;
"DECLARE #UserIDInt table (ID int);"
This creates my variable with a column called "ID"
INSERT into #UserIDInt
SELECT UserId
FROM [LcsCDR].[dbo].[Users]
WHERE [LcsCDR].[dbo].[Users].[UserUri] LIKE '%example';
This adds numeric values into the ID column based on whether or not the WHERE statement matched
SELECT *
FROM [LcsCDR].[dbo].[SessionDetails]
WHERE [LcsCDR].[dbo].[SessionDetails].[User1Id] = #UserIDInt;
This is where I am lost. I am trying to return all rows from [LcsCDR].[dbo].[SessionDetails] if the column [LcsCDR].[dbo].[SessionDetails].[User1Id] matches anything in my variable. The problem (I think) I'm having is that SQL can't look within the variable's column to find multiple values. Basically, the ID column in my variable #UserIDInt will contain a bunch of numeric values.
How do I perform the final SELECT statement and have SQL return all results if [LcsCDR].[dbo].[SessionDetails].[User1Id] matches anything within my #UserIDInt.ID column?
I am using SQL Server 2014.
Apologies if I explained it badly. Not sure how else to ask the question :)
using inner join:
select sd.*
from [lcscdr].[dbo].[sessiondetails] sd
inner join #useridint i
on i.id = sd.user1id;
or using exists():
select sd.*
from [lcscdr].[dbo].[sessiondetails] sd
where exists (
select 1
from #useridint i
where i.id = sd.user1id
);
or using in():
select sd.*
from [lcscdr].[dbo].[sessiondetails] sd
where sd.user1id in (
select id
from #useridint i
);
rextester demo: http://rextester.com/UVCB28056
Use EXISTS:
SELECT T1.*
FROM [lcscdr].[dbo].[sessiondetails] T1 WHERE EXISTS (
SELECT 1
FROM #useridint T2
WHERE T2.id = T1.user1id
);

return one table from two tables where condition exists in one or the other

This is a little weird, but I need to select from one of two tables that have a given unique identifier. Let's say that table A looks like:
ID CallNumber Caller
and table B looks like:
ID CallNumber Caller
and I have a unique identifier that could be in either one. How could I write a select statement that would return these columns and display the data from either table A or table B? What I have come up with so far is:
SELECT
coalesce(a.ID,t.id) as ID
,coalesce(a.CallNumber,t.CallNumber) as CallNumber
,coalesce(a.Caller,t.Caller) as Caller
FROM tableA a
right join tableB b on b.ID = a.ID
where a.ID = '' or b.ID = ''
but this will only return the unique identifier if the ID lives in table A.
If it needs to be one query then you could query both tables and mash up the results. You don't need to bother with joining tables or other logic.
select
ID,
CallNumber,
Caller
from
tableA
where
ID = theID
union all -- adding 'all' avoids unnecessary sorting operation
select
ID,
CallNumber,
Caller
from
tableB
where
ID = theID

Create writeable multi table select query in Access

I have two tables with below schema
Table 1 -
id, Serial Code, Hours
xxxx-aa, 1
xxxx-bb, 2
yyyy-aa, 1
Table 2 -
Stage , Description
aa, foo
bb, bar
Right now my joins are like this.
SELECT x.*, y.Description
FROM table1 x LEFT JOIN
table2 y
ON MID(serial, 6, 2) = y.stage
this gave me the desire result. however, it is readable only. I know I can create a writeable query if i separate out the foreign key in table 1 so the joining condition is
SELECT x.*, y.Description
FROM table1 x LEFT JOIN
table2 y
ON x.stage = y.stage
but is there a better way to create a writeable query without .. un-normalize.. table 1?
thanks.
One approach would be to create a calculated column called [stage] in table 1 and define it MID(serial,6,2). As you fill up the table, this column is automatically populated and allows you to query it simply as if you "un-normalized" table 1.

SQL : build a query that reads from a file

i have a table with column name,add,no tel. ( millions of rows)
i need to pull a list of names ( like hundreds) from the table.
the straight forward way is like this...
select * from table where name = name1 or name = name2 .....or name = name300
i tried to make it more simpler,
i import the list of name into another table,say table2.
then i do a subquery.
select * from table where name like '%'+(select * from table2)+'%'
but it did not work.
please help how i can accomplish this efficiently.
thank u.
Just join the two tables:
Select a.* from tableA a join tableB b on a.name = b.name