SQL join eliminating repeated columns - sql

So I dispose of a few tables which were all generated out of a single, common one.
Their schemas are as
T1: A, B, C, M, N
T2: A, B, J, M
T3: A, C, M, P
and the expected result is a joined version of all tables discarding the repeated values.
R: T1.A, T1.B, T1.C, T2.J, T1.M, T1.N, T3.P
Fortunately, rows' attributes coincide on homonymous columns, ie.
T1.A == T2.A == T3.A across all rows
T1.B = T2.B across all rows
etc.
What would be a query to join those tables into a single R table keeping only uniquely-named columns as above?

select T1.A, T1.B, T1.C, T2.J, T1.M, T1.N, T3.P
from t1 join t2
on t1.A = t2.A and t1.B = t2.B and t1.M = t2.M
join t3 on
t1.A = t3.A and t1.C = t3.C and t1.M = t3.M
You have to join the tables.

The basic query would be:
SELECT T1.A, T1.B, T1.C, T2.J, T1.M, T1.N, T3.P
FROM T1
INNER JOIN T2 ON T1.A=T2.A and T1.B=T2.B and T1.M=T2.M
INNER JOIN T3 ON T1.A=T3.A and T1.C=T3.C and T1.M=T3.M
The number of conditions you put on each inner join should equal all the columns that make it a unique row for that table. For example, if A,B,and M is sufficient to make it unique then you must use those. Indexing coverage (as separate or thorough clustered) should encompass these fields that make them unique together.

Although other solutions are correct, a more elegant version would be
SELECT *
FROM T1
NATURAL JOIN T2
NATURAL JOIN T3

Related

SQL Select where two values not in the same row of another table

I need a select that return the values from T2.C from rows which combination of T2.A and T2.B doesn't exist in T1.
Something like:
select C from T2 where A,B not in (select A,B from T1)
Results from example must be:
C
--
y
z
Do you mean like
SELECT T2.*
FROM T2
LEFT OUTER JOIN T1
ON T2.ColumnA = T1.ColumnA
AND T2.ColumnB = T1.ColumnB
WHERE T1.ColumnA IS NULL;
If you do an outer join, you can easily notice a lack of a match (via nulls).

Select columns from multiple tables

I have multiple tables with identical primary key fields but no other identical field. I want to select a collection of columns from all tables matching a specific primary key, something like so:
SELECT T1.a, T2.b, ..., TN.z FROM T1, T2, ..., TN WHERE T1.pk = "abc"
SELECT T1.a, T1.b, ..., TN.z
FROM T1 INNER JOIN T2 ON T1.pk = T2.pk
INNER JOIN T3 on T1.pk = T3.pk
...
INNER JOIN TN on T1.pk = TN.pk
AND TN.pk = "abc"
Or in a shorter form (thanks #Abelisto!).
SELECT T1.a, T1.b, ..., TN.z
FROM T1 JOIN T2 using(pk)
JOIN T3 using(pk)
...
JOIN TN using(pk)
WHERE TN.pk = "abc"

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

taking a join with output of other 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?

SQL Combining These Two Tables Into One

What would be the MS Access SQL to combine these two tables? Table1 has column A, B, and C. Table2 has A, D, and E. I want the final result to be A,B,C,D,E where (join) Table1 A equals Table2 A.
Union/UnionAll tells me the columns don't match. Insert into gives me a similar error. Thanks in advance for any help. (Sorry this is probably a noob question)
UNION is for combining data with equal columns, what you need is a JOIN
SELECT t1.A,B,C,D,E FROM table1 t1 JOIN table2 t2 ON t1.A = t2.A
select a.A,a.B,a.C,b.D,b.E from Table1 a, Table2 b where a.A==b.B;
This worked for me
SELECT Table1.*, Table2.*
FROM Table1 LEFT JOIN Table2 ON Table1.A=Table2.A;
I just tested this in MS Access 2003 and it worked:
SELECT t1.A, t1.B, t1.C, t2.D, t2.E
INTO Table3
FROM Table1 t1
LEFT JOIN Table2 t2
ON t1.A = t2.A
I got all data from table1 and table2 in my new table.