How to compare two tables and find the differences - sql

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

Related

Finding IDs in one table not in another

I have two tables:
table1
ID | Name | Code
1 Joe 123
2 Sam 674
3 Mike 321
table2
ID | User Name| Code
1 Joe 123
2 Sam 674
3 Mike 321
4 John 457
5 Tim 235
Desired result:
4|John|457
5| Tim|235
Tabe1 and table2 code is identical. Table 1 code is a new field added thus contains no data for any record. Using the IDs as keys I took the codes from table2 and populated them in table1. However table1 has considerably less IDs then table2 so table2 has more codes then table1. I want to query which codes did not get transferred to table1. I thought it would be as simple as:
select *
from table2 t2
where t2.Code is not null and
t2.Code not in (select t1.Code from table1 t1 where t1.Code is not null);
This returns nothing which is strange to me. What do I need to adjust in this query? This is for oracle.
You might try the following:
SELECT t2.*
FROM table2 t2
WHERE NOT EXISTS ( SELECT 1 FROM table1 t1
WHERE t1.code = t2.code );

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.

I want to use multiple select command in sql

I am Beginner in sql. I want following result using connecting two tables.
Here are details.
Table1(usertb)
id username password fname lname
---------------------------------
1 amit abc111 amit ganatra
2 rakesh r111 rakesh patel
3 sanajay s111 sanjay Mor
Table2 (friendlist)
id fid
--------
1 2
1 3
It means id no 1 has two friends whose idno is 1 and 2. When user of id=1 logged in there should be friendlist like,
2 rakesh patel
3 sanjay Mor
It's called a join.
Select t1.fname, t1.lname From table1 t1
inner join table2 t2 on t2.fid = t1.id
Where t2.id = 1
A couple of sql courses would stand you in good stead, stop what you are doing and take one.
Select table2.fid, tabel1.fname + table1.lname
from table1 innerjoin table2 on table2.fid = table1.id

Select Row from Other Table if Doesn't Exist in Current Table

I have two tables:
Table1
id name qty
1 Tedd 6
2 Jim 7
3 Sally 8
4 Victoria 1
Table2
id name qty
1 Tedd 2
2 Jim 2
3 Sally 2
4 Victoria 1
5 Alex 9
I need to select all the rows from Table1. However, if a row exists in Table2 that doesn't exist in Table1, I need to include that in the result set. So, in the end, my query should return this:
id name qty
1 Tedd 6
2 Jim 7
3 Sally 8
4 Victoria 1
5 Alex 9
Is there a way I can do this? Thanks.
You can use a FULL OUTER JOIN:
select
coalesce(t1.id, t2.id) id,
coalesce(t1.name, t2.name) name,
coalesce(t1.qty, t2.id) qty
from table1 t1
full outer join table2 t2
on t1.id = t2.id
See SQL Fiddle with Demo

SQL conditional insert on SQL Server 2000

Hi I have 2 tables below:
table1
GroupName PersonName email phone
A Tom tom# 123
A Jen jen# 223
B Kim kim# 232
table2
GroupName PersonName email phone
A Tom NULL 1
A Jen NULL 1
A Ken NULL 1
B Kim NULL 1
B Tai NULL 1
I need to insert all columns of the additional records from table2 into table1. for example, I need Ken and Tai's records from table2 added to table1. it's running on SQL Server 2000 so I cannot use EXCEPT or INTERSECT.
assuming email is unique:
insert into table1
select * from table2
where email not in (select email from table1)
alternative:
insert into table1
select a.*
from table2 a
LEFT JOIN table1 b
ON a.email = b.email
WHERE b.email IS NULL
this will do it..
Inser Table1
select GroupName, PersonName, email, phone
from Table2 where GroupName+'|'+PersonName+'|'+email+'|'+phone
not in (select GroupName+'|'+PersonName+'|'+email+'|'+phone from Table1)
Thanks!
#leo.