Joining two tables where id does not equal - sql

I'm struggling getting this query to produce the results I want.
I have:
table1, columns=empid, alt_id
table2, columns=empid, alt_id
I want to get the empid, and alt_id from table 1 where the alt_id does not match the alt_id in table2. They will both have alt_id numbers I just want to get the ones that do not match.
Any ideas?

SELECT * FROM table1
INNER JOIN table2 ON table2.empid = table1.empid AND table2.alt_id <> table1.alt_id

What does that really mean though? Normally when this is asked, it is of the form "I want all rows from A that have no row matching in B and all in B that have no match in A"
Which looks like this:
SELECT * FROM
A
FULL OUTER JOIN
B
ON
a.id = b.id
You'll see a null for any row data where there isn't a matching row on the other side:
A.id
1
2
B.id
1
3
Result of full outer join:
A.id B.id
1 1
2 null
null 3
You, however have asked for A-B join where the IDs aren't equal, which would be the more useless query of:
SELECT * FROM
A
INNER JOIN
B
ON
a.id != b.id
And it would look like:
A.id B.id
1 3
2 1
2 3

You seem to want not exists:
select t1.*
from table1 t1
where not exists (select 1 from table2 t2 where t2.alt_id = t1.alt_id);
It is unclear whether or not you also want to join on empid, so you might really want:
select t1.*
from table1 t1
where not exists (select 1 from table2 t2 where t2.alt_id = t1.alt_id and t2.empid = t1.empid);

A left join will find all records in Table A that do not match those in Table B. Then use a Where filter to find the Nulls from Table B. That will give you all those in Table A that do not have a matching ID in Table B.
Select A.*
from Table A
Left Join
Table B
on a.altid = b.altid
where b.altid is null;

select *
from [Login] L inner join Employee E
on l.EmployeeID = e.EmployeeID
where l.EmployeeID not in (select EmployeeID from Employee)

Related

Query to output not existing data

Table A:
id Name
1 a
2 b
3 c
4 d
5 e
Table B:
id Name
3 c
4 d
5 e
Here, id is the primary key connected to Table B.
I need output like this:-
id
1
2
That means, which ids in Table A are not present in Table B
Use EXCEPT operator:
select id from tableA
except
select id from tableB
You can use a left join, which will preserve all records on the left side and associate them with null if no matching record is available on the right side.
This way you can then filter on the right side columns to be null to get the desired outcome
select t1.id
from tableA t1
left join
tableB t2
on t1.id = t2.id
where t2.id is null
Use NOT EXISTS in WHERE clause
SELECT id FROM TableA A
WHERE NOT EXISTS(SELECT 1 FROM TableB B WHERE A.id = B.Id )
Using Not in statement.
Try this:-
Select id from TableA
where id not in (Select id from TableB);
You can use minus:
select * from tableA
minus
select * from tableB

SQL - not sure how to join tables

I'm trying to join two tables like this:
Table A
ID Value1
1 A
2 B
3 C
Table B
ID Value2
1 A
3 B
4 C
Result should be:
ID Value1 Value2
1 A A
2 B null
3 C B
4 null C
I.e. join Table A to Table B on ID. If ID doesn't exist in Table A, add the ID from Table B.
The closest I've come is:
SELECT
a.ID, a.Value1, b.Value2
FROM
TableA a
OUTER JOIN
TableB b ON a.ID = b.ID
That gives me the new rows from TableB, but the ID is null.
How can I accomplish this?
You are very close, you just need a little push in the right direction:
SELECT COALESCE(a.ID, B.ID) As ID, a.Value1, b.Value2
FROM TableA a
FULL OUTER JOIN TableB b ON a.ID=b.ID
The COALESCE function returns the first parameter it gets that is not null. since this is a full outer join, a.id will be null on one row and b.id would be null on a different row.
Try this:
SELECT *
FROM TableA A
FULL OUTER JOIN TableB B
ON A.ID = B.ID;
Just a note: you should not name your tables in SQL with spaces in them.
Remember the basic for joining different tables
SELECT column_name(s)
FROM table1
FULL OUTER JOIN table2
ON table1.column_name=table2.column_name;
For your case:
SELECT a.value1, b.value2
FROM TableA a
FULL OUTER JOIN TableB b ON a.ID=b.ID
remember full outer join
The FULL OUTER JOIN keyword returns all rows from the table (tableA) and from the table (tableB) and the FULL OUTER JOIN keyword combines the result of both LEFT and RIGHT joins.

Select from two different tables by value in third table

I have next tables.
First one is A.
A have two columns: A_ID and A_VALUE.
Second table is B. B too have two columns: B_ID and B_VALUE
In additional I have table C. Table C have C_ID and bool columns C_BOOL
If C_BOOL value == true i need select value from A with given ID.
If C_BOOL value == false i need select value from B.
How I can write SELECT for this?
I use oracle db.
Thanks in advice.
SELECT CASE C.BOOL WHEN 1 THEN A.ID ELSE B.ID END
FROM A
JOIN B
ON B.ID = A.ID
JOIN C
ON C.ID = A.ID
Try this query:
SELECT C_ID,CASE WHEN C_BOOL = 1 THEN T3.A_VALUE ELSE T2.B_VALUE END
FROM TABLE_C T1 LEFT OUTER JOIN TABLE_B T2 ON T1.C_ID = T2.B_ID
LEFT OUTER JOIN TABLE_A T3 T2 ON T1.C_ID = T3.A_ID
select decode(C.BOOL,1,A.ID,B.ID) FROM C
JOIN A
ON A.ID=C.ID
JOIN B
ON B.ID=C.ID;
I consider T McKeown answer as valid this is just equivalent (but more compact) I suppose.

Getting data from one table to another table using join

I have a table name "a"
Id name
1 abc
2 xyz
3 mmm
4 xxx
and Other table name is "b"
Id suId
3 2
3 1
My requirement is get detail from "a" table from "b" table where id=3. Any help?
SELECT a.Id, a.name, b.Id, b.suId FROM b JOIN a ON b.suId = a.Id WHERE b.Id = 3;
I wont recommend join for this kind of scenarios(you want all details from Table A whose ids are in Table B suId column, where Table B id should be 3., Its bad English but hope you got me and may be i got you too.)
SELECT a.name FROM a
WHERE
a.id IN(SELECT b.suId FROM b WHERE b.id = 3);
If you want to use join only then,
SELECT a.name FROM a,b
WHERE a.id = b.suId
AND
b.id = 3;
Simple answer:
SELECT a.Id,a.name FROM a,b
WHERE a.Id=b.suId AND b.Id=3
It will give you the result:
Id Name
1 abc
2 xyz
See result in SQL Fiddle
This should get the job done:
SELECT * FROM table_a a JOIN table_b b ON b.suId = a.Id WHERE b.Id = 3;
You can try this...You can try different JOIN clauses like INNER JOIN, LEFT OUTER JOIN or just simply JOIN etc. You will get different number of rows depending on field connections from 1 table to the other.
SELECT T1.*
FROM a T1
INNER JOIN b T2
ON T1.Id = T2.Id
WHERE T1.Id='3'

SQL "All" Functionality?

This is probably a really easy question, it's just very hard to google a word like "All".
SELECT a.Id, Max(b.Number)
FROM Table1 a
JOIN Table2 b
ON a.FK = b.Id
GROUP BY a.Id
But I want to add a where clause that specifies that all b.Id's linked to an a.FK must have values. So basically I don't want to select the a.Id grouping of b.Id's where any of those b.Id's are null. Hope I made that clear, let me know if I need to elaborate. Thanks.
Edit - For some clarification (Changed the query above as well):
Table1
Id, FK
1 1
1 2
2 3
3 4
3 5
3 6
Table 2
Id Number
1 1
2 NULL
3 10
4 20
5 30
6 40
I would want my query to show:
a.Id Max Number
2 10
3 40
(Notice that a.Id = 1 doesn't show up because one of the b.Number fields is null)
select t1.Id, max(Number) as [Max Number]
from Table1 t1
left join Table2 t2 ON t1.FK=t2.Id and t2.Number is not null
group by t1.Id
having count(distinct t1.FK) = count(distinct t2.Id)
Okay, you are asking a totally different question from the one I thought you were. I'm replacing my answer.
The way I would handle this is to join a to b twice -- once to get all matching rows in b, and a second join to search for rows in b where Number is null. If no such row exists, then we know they're all non-null.
SELECT a.Id, Max(b1.Number)
FROM Table1 a
JOIN Table2 b1 ON a.FK = b1.Id
LEFT OUTER JOIN Table2 b2 ON a.FK = b2.Id AND b2.Number IS NULL
WHERE b2.Id IS NULL
GROUP BY a.Id
b2.Id will be null only if no row is found where b2.Number is null.
SELECT a.Id, Max(b.Id)
FROM Table1 a
JOIN Table2 b
ON a.FK = b.Id
WHERE b.Id is NOT NULL
GROUP BY a.Id