Search Row not exist in another table - sql

Can anyone help me to write the query for below condition.
Table 1
ID Key
1 A
2 A
4 C
5 D
6 A
Table 2
ID Key
2 B
3 B
5 D
6 A
These are the two tables
I want a query in which the ID, which is not exist in Table1 corresponding to Table2, where deleted from table.
Example: ID = 1 row was completely deleted from Table1, and in which key are not match were also deleted
Example: ID = 2, exists in both tables but key are not same so the complete row also delete from Table1 not in Table2, I need a single query which is applicable to both condition
Thanks in advance

SELECT id, [Key]
FROM table1
EXCEPT
SELECT id, [Key]
FROM table2

You can try to check if exists in following:
DELETE FROM Table1
WHERE NOT EXISTS (
SELECT 1
FROM Table2 t2
WHERE Table1.Id = t2.Id
)
OR EXISTS (
SELECT 1
FROM Table2 t2
WHERE Table1.Id = t2.Id AND Table1.[Key] <> t2.[Key]
)
OUTPUT
ID Key
5 D
6 A

Related

How to delete rows from two tables which are in common with single query in postgresql

I have two tables t1 and t2
table t1 as follows:
id name
1 x
2 y
3 z
table t2 as follows:
id name
1 a
121 b
131 c
Here I am selecting rows that are common in both the tables i.e.,
SELECT *
from t1,t2
where t1.id=t2.id;
Now I want to delete the rows when id=1 in both the tables at once. I have tried to delete the rows but I am able to delete only in one table but not both. Can anyone help me out in solving this.
You can do that with a data modifying common table expression
with common_ids as (
select id
from t1
intersect
select id
from t2
), t1_delete as (
delete from t1
where id in (select id from common_ids)
)
delete from t2
where id in (select id from common_ids);
Online example: http://rextester.com/NAQ26877

After Table Joining, need specific row value based on another Row with same ID

I have 2 tables as follows:
Table 1:
ID FName
1 Basics
2 Machine1
3 master
4 Machine2
15 Machine3
16 Machine16
Table 2:
ParentID Name InitialValue
1 Active 1
2 MachineName Entrylevel
2 Active 1
3 Active 1
4 MachineName Midlevellevel
4 Active 1
15 MachineName Endlevel
15 Active 1
16 MachineName Miscellenious
16 Active 0
Here, ID of Table 1 is referred as Parent ID at Table 2. I want "Initial Value" of Table 2 for MachineName Rows (of Table 2) provided "InitialValue" of Table 2 for Active Rows (of Table 2) are 1
Result should be like
ID InitialValue
2 Entrylevel
4 Midlevellevel
15 Endlevel
You could join the second table twice, once for MachineName, and once for Active:
SELECT t.ID, machine.InitialValue
FROM table1 t
INNER JOIN table2 machine
ON t.ID = machine.ParentId
AND machine.Name = 'MachineName'
INNER JOIN table2 active
ON t.ID = active.ParentId
AND active.Name = 'Active'
AND active.InitialValue = 1;
About Joins
The JOIN syntax allows you to link records to the previous table in your FROM list, most of the time via a relationship of foreign key - primary key. In a distant past, we used to do that with a WHERE condition, but that really is outdated syntax.
In the above query, that relationship of primary key - foreign key is expressed with t.ID = machine.ParentId in the first case. Note the alias that was defined for table2, so we can refer to it with machine.
Some extra condition(s) are added to the join condition, such as machine.Name = 'MachineName'. Those could just as well have been placed in a WHERE clause, but I like it this way.
Then the same table is joined again, this time with another alias. This time it filters the "Active" 1 records. Note that if the ID in table1 does not have a matching record with those conditions, that parent record will be excluded from the results.
So now we have the table1 records with a matching "MachineName" record and are sure there is an "Active" 1 record for it as well. This is what needs to be output.
Not sure if this is standard SQL but it should work using MySQL.
select T1.ID, T2.InitialValue
from Table1 T1 inner join Table2 T2 on T1.ID = T2.ParentId
where
T2.Name <> 'Active'
and exists (
select * from Table2 T3 where T3.ParentId = T1.ID and T3.Name = 'Active' and T3.InitialValue = 1
)
SELECT t1.ID, t2.InitialValue
FROM table1 t1 join table2 t2 on t1.ID=t2.ParentID
WHERE t2.name LIKE 'MachineName'AND t1.ID= ANY(SELECT t22.ParentID
FROM table2 t22
WHERE t22.InitialValue=1)
I think this should work
//slightly changed the condition in WHERE clausule (t2.parentID changed to t1.ID)

Lookup two columns using SQL

I have a task. For Instance, I have 2 tables.
Table 1 columns ID, Name
Table 2 columns ID, Name
Data is like in Table 1:
ID Name
1 A
2 B
3 C
Data is like in Table 2:
ID Name
1 D
2 B
3 E
I want to write a SQL query which lookup two tables in both columns. I want to have the count, which records not matching (both columns) with table 2.
Here only one record was matched (2 B). So, I should get the count 2.
Thanks.
Use not exists to count the # of rows in table1 that are not in table2
select count(*) from mytable t1
where not exists (
select 1 from mytable t2
where t2.id = t1.id
and t2.name = t1.name
)

Getting results to display that exist in table 1 but not table 2

I'm new to SQL and am having trouble getting results to display that exist in table 1 but not table 2. I need to display how many times each ID from table 1 has been used in table 2 (including 0 if it has not been used) I can get the ID's that exist in Table 1 to display, but not the ID's that don't exist in Table 2.
I am getting:
ID Count
1
1
1
1
1
1
2
but need:
ID Count
1
1
1
0
1
1
0
1
2
I have tried:
SELECT COUNT (PID) AS [ID Count]
FROM SalesOrderProduct
WHERE PID > = 0
GROUP BY PID;
(just for this column that i can't get the 0 values to display in)
Table 1: PID, Description
Table 2: PID, Status
How can I get the results to display showing all the counts for ID in Table 2, including when the count is 0 using UNION?
Thanks everyone
Try this, you can change the attribute name based on your table structure.
Select t1.id, count(t2.id)
From t1 left join t2
on (t1.id = t2.id)
Group By t1.id;
in this case that your ids are not unique use exists with a count plus a union like:
select distinct tbl.id, 0 cnt --for ids not exists in table2
from table1 tbl
where not exists (select t.id from table2 t where t.id=tbl.id)
union
select t1.id, count(t1.id) cnt ----for ids exists in table2
from table1 t1
where exists (select t2.id from table2 t2 where t1.id=t2.id)
group by t1.id

Select data from multiple table

I have the following:
Table1 IdTable1, IdTable2, IdTable3
Table2 IdTable2, Title
Table3 IdTable3, FName, LName
I Need the content of Table1 with the Title, FName, LName
I have tried
SELECT T2.Title, T3.FName, T3.LName
FROM Table1 T1
LEFT JOIN Table2 T2 ON T1.IdTable2 = T2.IdTable2
LEFT JOIN Table3 T3 ON T1.IdTable3 = T3.IdTable3
All what I get is the Table2 with the other columns NULL or the 2 columns of Table3 with the first column NULL.
Table 1 Contains
1 2 1
2 2 1
3 2 5
Table 2 Contains
1 Mr
2 Madame
Table3 Contains
1 A B
2 C D
3 E F
4 G H
5 I J
The Results of all my Queries are
Madame Null Null
Madame Null Null
Madame Null Null
or
NULL A B
NULL A B
NULL I J
Your SQL is fine as long as you want to return partial results.
Have you verified the actual data is correct? You are using a LEFT JOIN. This means rows in T1 will be returned even if there is no matching data in T2 or T3. If there are no matching rows in one of those tables, the columns in that table will be NULL.
If you include all of the columns in T1 in your result set, you will see that there is indeed data in T1, but there is no matching data in T2 or T3. In cases where T3 columns are NULL, that means there is data in T1 and T2 but not T3.
It looks to me like you've got a referential integrity problem. You may need to add foreign keys with drop constraints or fix some application logic problems when creating or deleting records.
I guess your Table1 is a pivot table for Table2 and Table3. I will do something like below.
SELECT T2.Title, T3.FName, T3.LName
FROM Table1 T1, Table2 T2, Table3 T3 WHERE T2.IdTable2 = T1.IdTable2 and T3.IdTable3 = T1.IdTable3