Is there any way I can do a join between two tables where the resulting table has only the columns of the left table without the need to discriminate all the columns names in the select?
You can do this:
Select LeftTable.*
From LeftTable
Inner Join RightTable
On LeftTable.Id = RightTable.Id
Select T.* from tbl1 T, tbl2 J
You mean something like
Select t1.id, t1.name, t1.age FROM t1 INNER JOIN t2 ON t1.id = t2.id
WHERE t2.Something = Something
Related
Table t1
Table t2
I tried running the query
select CountyCode, ContactPerson
from table1 t1
inner join select * from table 2 t2 on t1.CountyCode[1] = Code[0]
Any one help me please.
Use this syntax:
select CountyCode, ContactPerson
from t1
inner join t2 on t1.CountyCode = t2.Code
I have 3 tables in BigQuery.
I need to join first one (contains ids), to others (contains list of values for ids). I want to have sum of values by ids from two tables:
SELECT t0.id, sum(values) FROM t0
LEFT JOIN t1 ON t0.id = t1.id
LEFT JOIN t2 ON t0.id = t2.id
GROUP BY id
It does not work with an error Column name values is ambiguous
What is the best way to make it?
SELECT t0.id, sum(t1.values) + sum(t2.values) as sumOfValues FROM t0
LEFT JOIN t1 ON t0.id = t1.id
LEFT JOIN t2 ON t0.id = t2.id
GROUP BY id
I think below better reflect your original idea
#standardSQL
SELECT id, sum(u.values)
FROM t0
LEFT JOIN (
SELECT id, values FROM t1 UNION ALL
SELECT id, values FROM t2
) u
USING (id)
GROUP BY id
This is somewhat of a followon to SQL JOIN where to place the WHERE condition?
I would prefer to use the USING clause on the join, but am not yet able to put the field value condition with the join.
SELECT 1
FROM table1 t1
JOIN table2 t2 USING(id) AND t2.field = 0;
ORA-00933: SQL command not properly ended
Is it possible to have USING and another condition as part of the JOIN clause?
You can use:
SELECT 1
FROM table1 t1
JOIN table2 t2 USING(id)
WHERE t2.field = 0;
Using USING(id) is like using ON t1.id = t2.id except that in the JOIN result instead of two columns t1.id & t2.id there is only one id column.
For INNER JOIN USING with a condition followed by an OUTER JOIN you need a subquery to keep the WHERE with the USING:
SELECT ...
FROM (SELECT id, ...
FROM table1 t1
JOIN table2 t2 USING(id)
WHERE t2.field = 0) s
LEFT JOIN ...;
For an OUTER JOIN USING with a condition you need a subselect:
SELECT ...
FROM table1 t1
LEFT JOIN (SELECT *
FROM table2 t2
WHERE t2.field = 0) t2
USING (id);
See this re ON/WHERE with JOIN. See this re ON/WHERE when mixing INNER & OUTER JOINs.
I've got 3 tables that I want to join and filter on some conditions.
I've first wrote this query:
select * from table1 t1
left join (select * from table2 where table2.fieldX=...) t2
on t1.id_12=t2.id_12
left join table3
on t2.id_23=t3.id_23
where t1.fieldY=...
Then I wanted to make it looks like more canonical by rewritting it like that:
select * from table1 t1
left join table2 t2
on t1.id_12=t2.id_12
left join table3
on t2.id_23=t3.id_23
where table2.fieldX=...
and t1.fieldY=...
But it does not give the same result.
I dont't understand why...
Do you?
Thanks in advance.
When you put table2.fieldX=... in the where clause you eliminate all rows from table1 that do not have a corresponding row in table2. Effectively you are changing the left join into an inner join.
Instead, you can apply the table2 filter in the join itself:
SELECT
*
FROM
Table1 t1
LEFT JOIN Table2 t2 ON t1.id_12 = t2.id_12 AND t2.fieldX = ...
LEFT JOIN Table3 t3 ON t2.id_23 = t3.id_23
WHERE
t1.fieldY = ...
Did you add the inner select where on the second query?
SELECT *
FROM table1 t1
LEFT JOIN table2 t2
on t1.id_12=t2.id_12
LEFT JOIN table3
on t2.id_23=t3.id_23
WHERE t1.fieldY=...
and t2.fieldX=...
I am doing some relational algebra exercizes. On a teacher slide I saw a thing that makes me think that there can be an error.
I think, the third JOIN, should be
JOIN 'Farmacia' as F
ON 'D'.'idCF' = 'F'.'idFì
instead of
JOIN 'Farmacia' as F
ON 'F'.'idFì = 'D'.'idCF'
Using this last command you will join Farmacia on itself, isn't it?
The slide question says:
Which pharmacy does sell drug X of phramaceutic company Y?
The order of the columns in the ON part of the statement doesn't influence how the JOIN itself is done.
This:
SELECT t1.columnA, t2.columnB
FROM Table1 t1
JOIN Table2 t2 ON t1.ID = t2.ID
will yield the same results as this:
SELECT t1.columnA, t2.columnB
FROM Table1 t1
JOIN Table2 t2 ON t2.ID = t1.ID
The self-join you described would have been something like this:
SELECT t1.columnA, t2.columnB
FROM Table1 t1
JOIN Table1 t2 ON t1.managerID = t2.employeeID