SQL Query not equal rows - sql

There are two tables table1 and table2 i want to join these tables and expect to retrieve rows which are not equal to table1 as there are rows which are in the both tables.
Table1
ID NAME Dept
1 B Finance
2 R HR
3 B CDU
Table2
ID PASSPORT
1 Yes
2 No
it is not working
SELECT table1.ID, table1.NAME, table1.Dept FROM TABLE1 INNER JOIN TABLE2 ON
TABLE1.ID != TABLE2.ID
Expected Result
ID NAME DEPT
3 B CDU

Use a left join to and where clause to grab the values that are only in table1
select
table1.id,
table1.name,
table1.Dept
from
table1
left join table2 on table2.id = table1.id
where
table2.id is NULL;
Output for the above Query:
A left join will insert a null value if the record from the second table cannot be found. So add in the where clause to grab all results that could not be found in the second table.
Here is a better description of using a left join https://www.w3schools.com/sql/sql_join_left.asp

Try this query --
SELECT ID
,NAME
,Dept
FROM TABLE1
WHERE ID NOT IN (
SELECT DISTINCT ID
FROM Table2
WHERE ID IS NOT NULL
);

Related

Print all the data from table1 and matching data from table 2 but what if the table2 is null

As I am trying to learn SQL, I have came across this question.
Let's say I have two tables:
table1 which contains two columns
id and data1
and
table2 which also has two columns
p_id and data2.
How would I print out all the data from table1 and just matching data from table2 in two different scenarios.
Scenario 1: table1.id = 1
Scenario 2: what if the table2 is null, (but I still want to print out all the data from table1)
So my approach was
Scenario 1:
Select table1.id, table1.data1, table2.data2
From table1
Inner Join table2 On table1.id=table2.p_id
Where table1.id = 1;
Scenario 2:
Select table1.id, table1.data1, table2.data2
From table1
Inner Join table2 On table1.id=table2.p_id
Where table1.id = 1 and table2.data2 is not null;
For the second scenario, you are describing a left join:
Select table1.id, table1.data1, table2.data2
From table1 left Join
table2
On table1.id = table2.p_id
where table1.id = 1 ;
Because you are filtering on the first table, the condition remains in the where clause. If you wanted to filter on the second table, you would presumably want something like:
Select table1.id, table1.data1, table2.data2
From table1 left Join
table2
On table1.id = table2.p_id and
table2.col = 'xyz';

SQL joining of two different queries

Table1: column is( ID)
Table2: column is (NAME)
QUERY:
SELECT ID FROM TABLE1
SELECT NAME FROM TABLE2
NOW, I want output side by side , is there any possibility
Try this....
if this is without any condition
select id, name from table1, table2
I woder whether you mean CROSS JOIN
select id, name
from table1
cross join table2
You need to do an JOIN between tables on a value that exists in both tables. An INNER JOIN only takes rows that has a match in both tables.
Example:
SELECT tb1.id, tb2.name FROM table1 AS tb1 INNER JOIN table2 AS tb2 ON tb1.id = tb2.table1_id;

Find all ID's in 2 different tables that Don't have a Username

Ok, so I have two tables. Table1 and Table2.
Table1 - All ID's
ID
Table2 - All ID's with Usernames
ID
Username
I want to select all ID's that DON'T have usernames. Table2 only has ID's that have Usernames. Basically I want to do a select on table 1 join 2 table on ID that don't have usernames. Since table2 only has ID's with usernames, it's basically all of the ID's that are in Table 2 subtracted from the TOTAL ID's in table1 so that I only get ID's that aren't in table2 but are in table1
This should work for you:
select id from table1
left join table2 on table2.id = table1.id
where table2.usernames is null
or you can do a not in
select id from table1 where id not in(select id from table2)
SELECT t1.ID
FROM Table1 t1
JOIN Table2 t2 ON
t2.ID = t1.ID
WHERE t2.Usernames IS NULL
One of the ways to do this is with a compound query:
SELECT id FROM Table1
EXCEPT
SELECT id FROM Table2;

SQL LEFT JOIN with WHERE class

there are two tables TABLE1 and TABLE2 in TABLE1 there are records which does not exist in TABLE2 with left join below i wanted to query all records which are in TABLE1 if the record does not exist in table2 however.
Note: about WHERE class in my code that is required this is because, there can be several records in the name of 'IN PROGRESS' in TABLE2 with one record in the name of 'GRADUATED' i wanted to distinct records based on table 1 ID that if there is any record in the name of 'GRADUATE' it should show only that else it should show inprogress.
SELECT DISTINCT
TABLE1.ID,
TABLE2.TRAINING_STATUS_CHECK
FROM TABLE1
LEFT JOIN TABLE2
ON TABLE1.ID = TABLE2.FK_ID_CLASS
WHERE NOT EXISTS
(
SELECT DISTINCT
TABLE1.ID,
TABLE2.TRAINING_STATUS_CHECK
FROM TABLE1
LEFT JOIN TABLE2
ON TABLE1.ID = TABLE2.FK_ID_CLASS
WHERE TABLE2.TRAINING_STATUS_CHECK = 'GRADUATED')
OR TABLE2.TRAINING_STATUS_CHECK = 'GRADUATED'
I see some odds with your query:
exists part are not related with you main query. I think you need some relation
distinct in part not exists are not needed
You filter columns with the same conditions as filter main row set
As I understand you want to get all rows from table1 with state 'GRADUATED' int table2 and any row from table1 where rows in table2 are not exists or state not equal 'GRADUATED'
SELECT DISTINCT
t1.ID,
t2.TRAINING_STATUS_CHECK
FROM TABLE1 t1
LEFT JOIN TABLE2 t2 ON t1.ID = t2.FK_ID_CLASS
WHERE NOT EXISTS
(
SELECT NULL /*its not nesessary what you need*/
FROM TABLE1 sub_t1
JOIN TABLE2 sub_t2 ON sub_t1.ID = sub_t2.FK_ID_CLASS /* left join replaced to inner */
WHERE sub_t2.TRAINING_STATUS_CHECK = 'GRADUATED'
AND sub_t1.ID = t1.ID /*relation with outer query*/
)
OR t2.TRAINING_STATUS_CHECK = 'GRADUATED'
where the relatonship between tables does not exist - but only if the comparison involves rows in table that are not 'graduated' (I think)
SELECT DISTINCT
TABLE1.ID,
TABLE2.TRAINING_STATUS_CHECK
FROM TABLE1
LEFT JOIN TABLE2 ON TABLE1.ID = TABLE2.FK_ID_CLASS
AND TABLE2.TRAINING_STATUS_CHECK <> 'GRADUATED'
WHERE TABLE2.FK_ID_CLASS IS NULL
Not sure about your question but if you want all the records from table 1 who are not in table 2, you just have to do this :
SELECT TABLE1.ID
FROM TABLE1
LEFT JOIN TABLE2 ON TABLE1.ID = TABLE2.FK_ID_CLASS
WHERE TABLE2.FK_ID_CLASS IS NULL
Try this:
SELECT DISTINCT TABLE1.ID, TABLE2.TRAINING_STATUS_CHECK
FROM TABLE1
LEFT JOIN TABLE2
ON TABLE1.ID = TABLE2.FK_ID_CLASS
AND (NOT EXISTS (SELECT 1
FROM TABLE2 t
WHERE TABLE1.ID = t.FK_ID_CLASS
AND t.TRAINING_STATUS_CHECK = 'GRADUATED')
OR TABLE2.TRAINING_STATUS_CHECK = 'GRADUATED')
For the record, conditions on the right table of a LEFT JOIN need to be placed inside the ON() clause or the join will transfer into an INNER JOIN due to NULL comparison.
It seems to me you have three distinct cases that can be "ORed together" using UNION; personally I find keeping all three separated like this makes things much easier to read and understand:
--- ID with GRADUATED exists in TABLE2
( SELECT ID, 'GRADUATED' AS TRAINING_STATUS_CHECK
FROM TABLE1
INTERSECT
SELECT FK_ID_CLASS, 'GRADUATED'
FROM TABLE2
WHERE TRAINING_STATUS_CHECK = 'GRADUATED' )
UNION
--- ID without GRADUATED exists in TABLE2
( SELECT ID, 'IN PROGRESS'
FROM TABLE1
MINUS
SELECT FK_ID_CLASS, 'IN PROGRESS'
FROM TABLE2
WHERE TRAINING_STATUS_CHECK = 'GRADUATED' )
UNION
--- ID does not exist in TABLE2
( SELECT ID, '{{NONE}}'
FROM TABLE1
WHERE ID NOT IN ( SELECT FK_ID_CLASS FROM TABLE2 ) );

Joining to Same Column but Getting No Rows

I have two tables.
Table1
ID Text
Table2
ID ParentID Text
I am trying to join to the same table twice using 2 different columns. I want all the rows where Table1.ID = Table2.ID as well as all the rows where Table1.ID = Table2.ParentID. I've tried the following but it returns no rows. When I run the script below in two different select statements each with one of the joins only, I get the result I want but not in the same select statement. Any ideas on what I'm doing wrong?
SELECT *
FROM Table 1
JOIN Table2 2
on 2.ID = Table1.ID
JOIN Table2 22
on 22.ParentID = Table1.ID
You're only getting the records where Table 1 is both a Parent ID and an ID... maybe you want:
SELECT
* FROM Table 1
JOIN Table2 2 on 2.ID = Table1.ID
UNION
SELECT
* FROM Table 1
JOIN Table2 22 on 22.ParentID = Table1.ID
Or potentially UNION ALL?
You could try this query:
SELECT
*
FROM
TABLE1,
TABLE2
WHERE
TABLE1.ID = TABLE2.ID
OR
TABLE1.ID = TABLE2.ParentID
Your aliasing is a little off.
Give this a go:
SELECT *
FROM Table 1
JOIN Table2 2
on 2.ID = 1.ID
JOIN Table2 22
on 22.ParentID = 1.ID
You may still get an error on the SELECT * by not having an alias there... I suggest doing SELECT 1.*, or even doing SELECT 1.myColumn, 1.myColumn2 etc...