I am building a SQL query which compares two tables A and B by a [name] column and returns the names from table A that are not in table B
Example
Table A
ID Name Address
1 A ABC
2 B XYZ
3 C PQR
Table B
ID Name Gender
1 A F
2 B M
3 D F
The query I wrote should return third row from table A as it is not in table B and should exclude all other rows
Following is the query I built
Select * from A oa left join B gp ON oa.name!=gp.name
the above doesn't return the results I was expecting.
Can this be corrected?
Easiest way:
select * from A where name not in (select name from B)
Better way:
select * from A where not exists (select 1 from B where B.name = A.name)
"A left join B" means keeping everything in A, and associating records in B if the condition is satisfied.
In your case, if you really wanna use left join, here is what it should be ('=', not '!='):
Select * from A oa left join B gp ON oa.name=gp.name where gp.name is null
Better way would be using 'not exists' performance-wise, or 'except' if null values are not an issue.
Using excpet operator will help
select * from TableA
except
select * from TableB
SELECT a.*
FROM A a
LEFT JOIN B b
ON a.name = b.name
WHERE b.name IS NULL
Related
Table A
a_id
a_name
1
apples
2
bananas
3
cherries
4
oranges
Table B
b_id
b_name
1
Hot
2
Cold
Table C
c_id
a_id
b_id
1
1
2
2
2
1
3
3
2
4
4
1
5
4
2
I am trying to get resulting table, which should show "a_name" and "b_name" with the following condition: where a_name like '%r%' and b_name like '%o%'.
Problem is that they should be found within the same row in "Table_C".
I've tried various methods of joining tables but I'm unable to get the desired result.
Here's my best coding attempt at this problem:
SELECT a.a_name,
b.b_name
FROM Table_A a
WHERE a.a_name LIKE '%r%' IN (SELECT a.a_id
FROM Table_c
WHERE b_id LIKE '%o');
Any help would be much appreciated.
The problem with your query is that you're trying to extract values of "Table B" by using a filtering operation (WHERE clause). As long as the filtering clause "just filters" - reduces the number of rows of your table - , you can't access "b.b_name" inside your SELECT clause if the FROM contains only "Table A".
In this case you may want to use two JOIN operations in place of WHERE .. IN .. construct. The main table is "Table C" because it connects "Table A" and "Table B" together. Since you have two conditions to be applied on the names, in order to make this more efficient, you can filter on the two tables ("Table A" and "Table B") before applying the JOIN operation. In this way the DBMS will apply the matching on less rows.
SELECT A.a_name,
B.b_name
FROM tabC C
INNER JOIN (SELECT * FROM tabA WHERE a_name LIKE '%r%') A
ON C.a_id = A.a_id
INNER JOIN (SELECT * FROM tabB WHERE b_name LIKE '%o%') B
ON C.b_id = B.b_id
Check the demo here.
select
A.a_name,
B.b_name
from TableA A
inner join TableC C on C.a_id = A.a_id
inner join tableB B on B.b_id = C.b_id
where A.a_name like '%r%' and B.b_name like '%o%';
The inner join 'glues' the table together on the condition specified after the ON
The where clause is a copy provided by you. I Just added the table aliases for clarity.
Try below code:
select tableA.a_name, tableB.b_name
from tableC
left join tableA on tableA.a_id=tableC.a_id
left join tableB on tableB.b_id=tableC.b_id
where tableA.a_name like '%r%' and tableB.b_name like '%o%'
I have a table A -
SNo ID Place
1 1000 Null
2 Null Null
3 1020 CityX
And another table B -
ID Place
1000 CityY
2000 CityZ
4040 CityAA
Now, I need to join table A and B such that I can get the values of Place in table A from table B. So my final table should look like this -
SNo ID Place
1 1000 CityY
2 1020 CityX
I'm trying to create an SQL query with joins, but that is only giving me empty rows. I did -
Select * from A
left outer join B
on A.ID = B.ID
where A.ID IS NOT NULL
Where is my query breaking? How do I get the expected result?
Select A.Sno, A.ID, IF(A.Place is null, B.Place, A.place) as Place from A
left join B
on A.ID = B.ID
where A.ID IS NOT NULL
Seems you need inner join
Select A.ID, ifnull(A.Place, B.Place) from A
Inner join B
on A.ID = B.ID
where A.ID IS NOT NULL
Technically, there is nothing wrong with your query, here is a working sqlfiddle of your question
http://sqlfiddle.com/#!9/29a002/1/0
Since you didn't post your actual schema or screenshot of it, I am going to suspect incompatible column definitions or invalid data
You can just do it like this Select the Place field of table B instead.
select SNo, B.ID, B.Place
from A
left outer join B
on A.ID = B.ID
I need to make a query that selects everything from a table A, and in addition to have a column that indicates the number of times that the value of A.col1 is in B.col2.
Example:
Table A:
id name
1 "y"
2 "z"
3 "w"
Table B:
id name
15 "y"
23 "w"
14 "y"
I want a query that will give me the following:
id name numOfTimes
1 "y" 2 // y is shown twice in table B
2 "z" 0 // z isn't shown in table B
3 "w" 1 // w is shown once in table B
try this:
Select a.id, a.name, count(b.id) as numoftimes from a
left outer join b on a.name = b.name
group by a.id, a.name;
It can be done in multiple ways
select a.id as Id
,a.name as Name
,(select count(1) from b where a.id=b.id) as NumberOfTimes
from a;
or
select a.id as Id
,a.name as Name
,count(b.id) as NumberOfTimes
from a
left join b on a.id=b.id;
Try with this following you will get your expected result.
select a.id,a.col1,count(b.col1) as numOfTimes
from TableA a left join TableB b
on a.col1 = b.col1
group by a.id,a.col1;
Thanks.
Suppose I have table A, B
ID in A is unique but in table B, ID is not unique
I want to SELECT DISTINCT ID
query 1:
SELECT DISTINCT ID FROM A a LEFT JOIN B b ON a.ID = B.ID WHERE ...
query 2:
SELECT DISTINCT ID FROM A WHERE ID IN (SELECT DISTINCT ID FROM B where ...)
or
SELECT DISTINCT ID FROM A a LEFT JOIN (SELECT DISTINCT ID FROM B) b ON a.ID = B.ID WHERE ...
The end result is same but
what happens in query 1 is the space of temp table is more as multiple rows from table B will come with repeated ID
In query 2 i am able to optimize space and further processing as it will have limited rows with all distinct ID's
Isn't there any way to use DISTINCT rows from table B using join and avoiding subqueries?
Actually I have even table C which I will join with this, so I need to care for the number of rows taking part in 2nd join when taking join further with table C.
SELECT DISTINCT ID FROM A a LEFT JOIN (SELECT DISTINCT ID FROM B) b ON a.ID = B.ID WHERE ...
Is this what you want?
Edit so the answer is a bit more visible:
Since your A is unique, but B isn't you can just swap the values :
SELECT DISTINCT ID FROM B b LEFT JOIN A a on a.ID = b.ID WHERE...
I've this table (a)
And this table (b)
Now I have to get all records from A which are not present in B (a.id not present as b.idDomanda) and where B.idUser is not 1. So In this case, it should return only id 2 from a, but it returns 1 and 2.
This is my Query
SELECT a.* FROM a LEFT JOIN b ON a.id=b.idDomanda WHERE ( b.idUser <> 1 OR b.idUser IS NULL ) GROUP BY a.id
You want to move the condition on b to the on clause:
SELECT a.*
FROM a LEFT JOIN
b
ON a.id = b.idDomanda and b.idUser <> 1
WHERE b.idUser IS NULL
GROUP BY a.id;
The group by suggests that you might want to use not exists instead:
select a.*
from a
where not exists (select 1
from b
where a.id = b.idDomanda and b.idUser <> 1
);
There should be no results given your data set.
All records from A which are not present in B (a.id not present as b.idDomanda)
Given the test data set all of A is in fact IN b.idDomanda... even when filtering out userId = 1.
but as the previous person pointed out that is the query to check.