How do I get my INTERSECT code to include all rows? - sql

I am working with two tables:
Table1
Name Team ID
Robinho Brazil 1
Ronaldo Brazil 2
Totti Italy 3
Baggio Italy 4
Rooney England 5
Table2
ID Football_Club Address
1 Chelsea London
3 Fulham London
I would like a new table with all columns included but to only include the two rows where the two tables intersect. I am using the following SQL Query which is very wrong:
SELECT id,
NAME,
team
FROM table1
INTERSECT
SELECT id,
football_club,
address
FROM table2
How can I rewrite this to bring about the correct result using INTERSECT?

You want an INNER JOIN rather than an INTERSECT. Replace * with the columns you need using the table aliases.
SELECT *
FROM Table1 t1
INNER JOIN Table2 t2 ON t1.id = t2.id;

Related

Multiple table joins in Oracle SQL with same column names

Currently I have 3 tables like below
Master
ID_NUMBER
ZIPCODE
1
12341
2
12342
3
12343
4
12344
Table1
ID_NUMBER
CITYNAME
COUNTYNAME
1
NEW YORK
QUEENS
3
DETROIT
SUFFOLK
Table2
ID_NUMBER
CITYNAME
COUNTYNAME
2
ATLANTA
ROCKLAND
4
BOSTON
WINCHESTER
My desired output is like below. I want to filter based on the zipcode from master table
ID_NUMBER
ZIPCODE
CITYNAME
COUNTYNAME
2
12342
ATLANTA
ROCKLAND
How would i go about writing a query for this? Below is what i have tried but it's giving me null values if the ID_NUMBER is not found on that particular table.
SELECT mstr.id_number,
mstr.zipcode,
t1.cityname,
t1.countyname,
t2.cityname,
t2.countyname
FROM MASTER mstr
LEFT JOIN Table1 t1 ON mstr.id_number=t1.id_number
LEFT JOIN Table2 t2 ON mstr.id_number=t2.id_number
WHERE mstr.zipcode='12342'
Use COALESCE():
SELECT mstr.id_number,
mstr.zipcode,
COALESCE(t1.cityname, t2.cityname) as cityname
COALESCE(t1.countyname, t2.countyname) as countyname
FROM MASTER mstr LEFT JOIN
Table1 t1
ON mstr.id_number = t1.id_number LEFT JOIN
Table2 t2
ON mstr.id_number = t2.id_number
WHERE mstr.zipcode = '12342
'
Another approach you can try since your tables are identical is to join master with the union of the tables
with t as (
select Id_number, Cityname, Countyname
from t1
union all
select Id_number, Cityname, Countyname
from t2
)
select *
from Master m join t on m.Id_number=t.Id_Number
where m.zipcode='12342'

How to compare two tables and find the differences

I have two tables like below. I am using SQL DB.
Table1:
Id Name
1 leo
2 lin
3 kim
4 ken
5 jon
6 kelly
Table2:
Id Name Date
1 leo 2019-04-11
2 lin 2019-04-17
3 kim 2019-02-15
4 ken 2018-04-11
I am trying to compare this two table and find out which name in table1 does not have a Date.
I need this output.
Result:
Id Name
5 jon
6 kelly
Could anyone please help me with this query.
Use a LEFT JOIN:
SELECT * FROM Table1
LEFT JOIN Table2
ON Table1.Name = Table2.Name
WHERE Table2.Name is NULL
Or you can use WHERE NOT EXISTS:
SELECT * From Table1
WHERE NOT EXISTS (SELECT 1 FROM Table2 WHERE Table1.Name = Table2.Name)
Or you can use WHERE NOT IN:
SELECT * From Table1
WHERE Name NOT IN (SELECT Name FROM Table2)
Try this below:
SELECT Id,Name FROM Table1
EXCEPT
SELECT Id,Name FROM Table2

fusion 2 tables with inner join

in my database i have 2 tables.
table1
i have ID and NAMES
table2
i have ID, IDASSOCIATION, QUANTITY
so
i have 2 names in table1:
john and tom
and in table2 i have 3 lignes
john, 1
tom, 1
john, 1
nombre one is the quantity
in my result i want get
john = 2
and tom = 1
so i do this:
sql = "SELECT t1.*, t2.IDASSOCIATION, (SELECT SUM(t2.id_qte) FROM associationdepotarticle t2 WHERE t1.fusiontable = t2.fusiontable GROUP BY t2.IDASSOCIATION) as id_qte FROM articletable t1, associationdepotarticle t2";
but i not get this:
john = 2
tom = 1
why ? what i will do, i need correction please
You can just join the tables together and use sum:
select t1.name, sum(t2.quantity)
from table1 t1
join table2 t2 on t1.id = t2.idassociation
group by t1.name
It's not completely clear from your sample data what to join on, but I assume it's the idassociation field. If you want to return those names in table1 which aren't in table2, then use an outer join.

SQL Server joining two tables counting [closed]

Closed. This question needs details or clarity. It is not currently accepting answers.
Want to improve this question? Add details and clarify the problem by editing this post.
Closed 9 years ago.
Improve this question
I want to join table 1 and table 2 by counting the people in table two who visited the combo in table 1, so the result would look like
table 1
City1 City2
Rome Berlin
Moscow Berlin
....many more rows (to be clear- this was just an excerpt)
table 2
Person City
Peter Rome
Peter Paris
Peter Berlin
Peter Moscow
Paul Rome
Paul Moscow
Mary London
Mary Moscow
Mary Berlin
... many more rows
What I want it to look like:
Rome Berlin 0
Moscow Berlin 3
London Berlin 1
Any help appreciated.
Here is what I have done so far:
select a.city1
, a.city2
, count(case when city=a.city1 and city=a.city2 THEN city ELSE NULL END)
from table1 a join table2 b
One way you can do this is by joining to each of the cities and then aggregating, counting up when there is a match in both tables. This form of the query assumes there are no duplicates in the second table:
select t1.city1, t2.city2,
sum(case when t21.person is not null and t22.person is not null then 1 else 0 end) as cnt
from table1 t1 left outer join
table2 t21
on t1.city1 = t21.city left outer join
table2 t22
on t1.city1 = t22.city and t22.person = t21.person
group by t1.city1, t2.city2;
EDIT:
You can also do this with two levels of aggregation and one join. This is pretty easy to explain. The inner query counts the number of cities that a person matches for each pair of cities. The cnt is 0, 1, or 2. The outer query then counts the number where cnt = 2, because these are the ones that match both cities.
select city1, city2, sum(case when cnt = 2 then 1 else 0 end) as cnt
from (select t1.city1, t2.city2, person, count(t2.person) as cnt
from table1 t1 left outer join
table2 t2
on t2.city in (t1.city1, t1.city2);
The advantage of this approach is that adding new cities is easy, as well as getting more information, such as how many people match exactly one of the cities.
Sample data is incorrect, no London-Berlin combination in table1 and result should be
Rome Berlin 1
Moscow Berlin 2
London Berlin 1
However it can be achieved: you want the combinations of the cities of table2 so join table2 to itself based on person.
You also want to mach the combinations in table1 so join table1 to the previous result.
You need a counter so group on the last resultset:
select t2.city, t3.city, count(t1.city1) as counter
from table2 as t2
inner join table2 as t3 on t2.person = t3.person and t2.city <> t3.city
inner join table1 as t1 on t1.city1 = t2.city and t1.city2 = t3.city
group by t2.city, t3.city
The result will be as requested but you have to enter correct data in table1, otherwise you can get wrong result.

SQL Server - How to display values that differ in two similar tables

I have 2 tables in SQL Server (ver:2008). Both tables have similar structure. Second table has few extra columns. I want to write a query that displays the differences in these tables (for matching fields). Output should display columns from both tables side by side (for comparison).
I tried using EXCEPT which is displaying the results from table 1 that don't match the results in table2. But I need to display both results side-by-side.
Thank you.
You simply need to select the fields you wish to display:
select t1.*, t2.*
from table1 t1
inner join table2 t2 on t1.ID = t2.ID
where t1.field1 <> t2.field1 OR t1.field2 <> t2.field2
OR ...
For example, if you have 2 tables, Products1 and Products2, both with columns Name, Price, Color etc., and you want to list all different products, you can use this:
SELECT Name FROM Products1
UNION
SELECT Name FROM Products2
OR, you can use FULL JOIN keyword, it returns rows when there is a match in one of the tables. For example:
The “Cars” table:
P_Id Name Color Quality City
---- -------- ----- ------- -----------
1 BMW Red good Las Vegas
2 Lexus Blue bad Los Angeles
3 Mercedes Green good MIami
The "Orders" table:
O_Id OrderNo P_Id
---- ------- ----
1 123 3
2 234 3
3 345 1
4 456 1
5 689 18
Now we want to list all the cars and all the people with their orders.
We use:
SELECT Cars.Name, Cars.Color, Orders.OrderNo
FROM Cars
FULL JOIN Orders
ON Cars.P_Id=Orders.P_Id
ORDER BY Cars.Name
The result:
Cars Color OrderNo
-------- ----- -------
BMW Red 345
BMW Red 456
Mercedes Green 123
Mercedes Green 234
Lexus Blue -
- - 689
Give this a shot. It assumes that there's one column (a) that is identical between the tables, and the rest (b, c, ...) are potentially different.
SELECT t1.a, t2.a, t1.b, t2.b, t1.c, t2.c, ...
FROM table1 t1
JOIN table2 t2
ON t1.a = t2.a
AND NOT (t1.b = t2.b AND t1.c = t2.c ...)
SELECT * FROM
(
SELECT * FROM TABLE1
EXCEPT
SELECT * FROM TABLE2
) T1
UNION ALL
SELECT * FROM
(
SELECT * FROM TABLE2
EXCEPT
SELECT * FROM TABLE1
) T2