taking a join with output of other sql - sql

I have 2 tables as follows T1 and T2.
T1 has one field as A and T2 has one field B.
Now i want to do following: for each value of T1.A I want to join with T2.B
Something like :
select * from T1 ,(select * from where T2 where T2.B = T1.A)
Is this correct? When i try this I get an error saying T1.A is invalid indentifier.
I know that i can do select * from T1,T2 where T1.A = T2.B
But my use case is very complex. The query (select * from where T2 where T2.B = T1.A) is very complex.
So how do I go ahead with this?

You just need to JOIN the tables:
select *
from T1
inner join T2
on T2.B = T1.A
If you need help learning JOIN syntax, here is a great visual explanation of joins.
I used an INNER JOIN which will return the rows that match between T1 and T2. You might need to use a LEFT JOIN which will return all rows in T1 even if there is not a matching row in T2
If you have another query to select from, then you can use a subquery:
select *
from T1
inner join
(
-- place your query here
select *
from T2
) T2
on T2.B = T1.A
If your subquery is only returning one column, then you could use:
select t1.*, (select t2.col1 from T2 t2 where t2.B = t1.A)
from T1 t1

Unless I'm mistaken, can't you just use JOIN:
select *
from t1
join t2 on t1.field = t2.field
Good luck.

You can do
select *
from T1
inner join T2
on T2.B = T1.A
as other people have said.
However, this "ON" version is preferred only for readability.
You can also go ahead an use
select * from T1,T2 where T1.A = T2.B
The optimizer will figure it out, and do the exact same thing,
as the queries are equivalent.
You can go ahead an use it, as there is nothing wrong with it.

select * from first_table inner join second_table on first_table.X = second_table.Y

select A,T1.B,(select * from where T2 where T2.B = T1.A) FROM T1 .Will this help?

Related

Is it possible to share the same filter parameters (WHERE CLAUSE) with two more fields in SQL?

Consider below
SELECT * FROM table1 t1
LEFT JOIN table2 t2 ON t1.id = t2.id
WHERE
t1.A NOT IN ('111111','222222','33333')
AND
t2.B NOT IN ('111111','222222','33333')
Is there another way to use the same filter parameters for two different fields?
Something like that
SELECT * FROM table1 t1
LEFT JOIN table2 t2 ON t1.id = t2.id
WHERE (t1.A and t2.B) NOT IN ('111111','222222','33333')
Seems pretty simple, but I couldn't find anything in the docs.
You could use the array intersection operator (&&) to simulate that condition - create an array of the columns and an array of the values to test, and have a where clause that checks there's no intersection between them:
SELECT *
FROM table1 t1
LEFT JOIN table2 t2 ON t1.id = t2.id
WHERE NOT ARRAY[t1.A, t2.B] && ARRAY['111111', '222222', '33333']
A CTE would be one possibility.
WITH cte
AS
(
SELECT c
FROM (VALUES ('111111'),
('222222'),
('33333')) v
(c)
)
SELECT *
FROM table1 t1
LEFT JOIN table2 t2
ON t1.id = t2.id
WHERE t1.a NOT IN (SELECT c
FROM cte)
AND t2.c NOT IN (SELECT c
FROM cte);

access sql query to select from multiple tables

I am new to sql and have a simple question. I have two tables, t1 and t2, both have same fields A, B, C,D
I want to create a sql that
select
*
from t1
where (t1.A exist in t2.A)
AND (combination (ABC) from t1 not exist in t2)
I am using access 2013
Thank you!
You may try using exists logic:
SELECT t1.*
FROM table1 t1
WHERE
EXISTS (SELECT 1 FROM table2 t2 WHERE t2.A = t1.A) AND
NOT EXISTS (SELECT 1 FROM table2 t2 WHERE t2.B = t1.B AND t2.C = t1.C);
Something like this may work:
SELECT *
FROM t1
WHERE
A in (SELECT A from t2) AND
NOT EXISTS (
SELECT *
FROM t2
WHERE t1.B = t2.B AND
t1.C = t2.C AND
t1.D = t2.D
);

Inner join differences

I have a table1 (a,b,c) and table2(a,b,c)
What's the difference between
select * from table1 T1 inner join table2 T2 on
T1.a=T2.a and T1.b = t2.b and T1.c = T2.c
and
select * from table1 T1 inner Join table2 T2 on T1.a = T2.a where
T1.b= T2.b and T1.c = T2.C
Is is the same ? and which one is better?
Thanks
With inner joins there are no difference. It is only when you start using left/right joins, that you will see differences.
For LEFT JOINS, if you had
select *
from table1 T1 LEFT join
table2 T2 on T1.a=T2.a
and T1.b = t2.b
and T1.c = T2.c
It would include all rows fromo table1 and only rows from table2 where fields a,b and c matched.
If you had
select *
from table1 T1 inner Join
table2 T2 on T1.a = T2.a
where T1.b= T2.b
and T1.c = T2.C
This would include rows from table1 and those from table2 where a is equal, and then filter on b and c.
SQL Fiddle DEMO
I always find this visual representation usefull.
SQL SERVER – Introduction to JOINs – Basic of JOINs
For your queries, this doesn't change anything.
And in term of performance, your RDBMS is able to understand that it's the same.
But considering you have more joins, this would change the readability of the query.
Example :
SELECT
*
FROM
table1 T1
INNER JOIN table2 T2
ON T1.a = T2.a
AND T1.b = T2.b
AND T1.c = T2.c
LEFT JOIN table3 T3
ON T3.x = T1.a
AND T3.status = 1
WHERE
T1.a > 100
You can understand faster which condition works with which table in INNER/LEFT JOIN

multiple condition in join

how can i use two or more condition on join?
i want to replace this query with join version of it:
select *
from t1,t2
where t1.a=t2.b and t1.c=t2.d and t1.e=t2.f
how could it be?
This should work
select *
from t1
join t2
on t1.a = t2.b and t1.c = t2.d and t1.e = t2.f

Sql Server : How to use an aggregate function like MAX in a WHERE clause

I want get the maximum value for this record. Please help me:
SELECT rest.field1
FROM mastertable AS m
INNER JOIN (
SELECT t1.field1 field1,
t2.field2
FROM table1 AS T1
INNER JOIN table2 AS t2 ON t2.field = t1.field
WHERE t1.field3=MAX(t1.field3)
-- ^^^^^^^^^^^^^^ Help me here.
) AS rest ON rest.field1 = m.field
As you've noticed, the WHERE clause doesn't allow you to use aggregates in it. That's what the HAVING clause is for.
HAVING t1.field3=MAX(t1.field3)
You could use a sub query...
WHERE t1.field3 = (SELECT MAX(st1.field3) FROM table1 AS st1)
But I would actually move this out of the where clause and into the join statement, as an AND for the ON clause.
The correct way to use max in the having clause is by performing a self join first:
select t1.a, t1.b, t1.c
from table1 t1
join table1 t1_max
on t1.id = t1_max.id
group by t1.a, t1.b, t1.c
having t1.date = max(t1_max.date)
The following is how you would join with a subquery:
select t1.a, t1.b, t1.c
from table1 t1
where t1.date = (select max(t1_max.date)
from table1 t1_max
where t1.id = t1_max.id)
Be sure to create a single dataset before using an aggregate when dealing with a multi-table join:
select t1.id, t1.date, t1.a, t1.b, t1.c
into #dataset
from table1 t1
join table2 t2
on t1.id = t2.id
join table2 t3
on t1.id = t3.id
select a, b, c
from #dataset d
join #dataset d_max
on d.id = d_max.id
having d.date = max(d_max.date)
group by a, b, c
Sub query version:
select t1.id, t1.date, t1.a, t1.b, t1.c
into #dataset
from table1 t1
join table2 t2
on t1.id = t2.id
join table2 t3
on t1.id = t3.id
select a, b, c
from #dataset d
where d.date = (select max(d_max.date)
from #dataset d_max
where d.id = d_max.id)
SELECT rest.field1
FROM mastertable as m
INNER JOIN table1 at t1 on t1.field1 = m.field
INNER JOIN table2 at t2 on t2.field = t1.field
WHERE t1.field3 = (SELECT MAX(field3) FROM table1)
yes you need to use a having clause after the Group by clause ,
as the where is just to filter the data on simple parameters ,
but group by followed by a Having statement is the idea to group the data and filter it on basis of some aggregate function......
But its still giving an error message in Query Builder. I am using SqlServerCe 2008.
SELECT Products_Master.ProductName, Order_Products.Quantity, Order_Details.TotalTax, Order_Products.Cost, Order_Details.Discount,
Order_Details.TotalPrice
FROM Order_Products INNER JOIN
Order_Details ON Order_Details.OrderID = Order_Products.OrderID INNER JOIN
Products_Master ON Products_Master.ProductCode = Order_Products.ProductCode
HAVING (Order_Details.OrderID = (SELECT MAX(OrderID) AS Expr1 FROM Order_Details AS mx1))
I replaced WHERE with HAVING as said by #powerlord. But still showing an error.
Error parsing the query. [Token line number = 1, Token line offset = 371, Token in error = SELECT]