I'm trying to get the data from 3 tables but i think i need to use inner join (to join TABLE1 and TABLE2) and right outer join because i need all information from TABLE3 but i don't know how to do it. I tried this:
select a.TABLE1_ID, e.TABLE2_ID,f.TABLE3_ID
from TABLE1 as a, TABLE2 as e, TABLE3 as f
where a.TABLE1_ID = e.TABLE2_ID and a.TABLE1_COL = f.TABLE3_ID
UNION ALL
SELECT f.TABLE3_ID, NULL
FROM TABLE3 as f
WHERE NOT EXISTS (
SELECT * FROM TABLE1 as a
WHERE a.TABLE1_COL = f.TABLE3_ID);
But i get the following error:
The operands of a set operator or a VALUES clause do not have the same number of columns.. SQLCODE=-421, SQLSTATE=42826, DRIVER=4.19.56
How could i do it? Is it possible?
**EDIT:
The following is the result that i would like to get:
a.TABLE1_ID | e.TABLE2_ID | f.TABLE3_ID
----------------------------------------
AFR123 | AFR123 | MM145FR123
AFR124 | AFR124 | MM145FR124
NULL | NULL | MM145FR125
NULL | NULL | MM145FR126
You are describing LEFT JOINs:
select a.TABLE1_ID, e.TABLE2_ID, f.TABLE3_ID
from TABLE3 f left join
TABLE1 a
ON a.TABLE1_ID = f.TABLE3_ID left join
TABLE2 e
on a.TABLE1_ID = e.TABLE2_ID ;
As a note: If there is a row in a that matches f but has no matching row in e, then this keeps the values from a. If you using INNER JOIN followed by RIGHT JOIN, you won't have the values in the intermediate table. Usually having such values is desired.
Related
I have the following columns in my table:
Title | Title Root
-------|--------------
A | null
B | A
C | D
D | null
E | null
F | E
I need to be able to have my query see that rows like B,C, and F have a value in Title_Root and display the corresponding title of that title_root. For example: B has Title_root = A. So then display the Title A. I don't want it to display B's title though.
I hope this makes sense. I just started doing queries.
Looks like a simple outer join:
select t1.title,
t2.title_root
from the_table t1
left join the_table t2 on t2.title_root = t1.title
Or if you only want to see one title:
select coalesce(t2.title_root, t1.title) as title
from the_table t1
left join the_table t2 on t2.title_root = t1.title
It seems like a simple update statement should do the trick:
UPDATE Table
SET title = title_root
WHERE title_root is not null
Hope this helps.
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)
I have a Join statement on two tables(Table 1 and 2), which returns the City and State. I have another table(Table 3) which contains columns like Name, City, State, Country. I want to fetch all the rows from Table 3 whose City and State Columns matches with the rows of the Join result.
Select * from 3rdTable where City='' AND State='';
Result from Join is like
- City | State
- A | B
- C | D
- E | F
Example Result if only 1 row of the 3rd table matches
- C | D
How can this be done?
You can use the joined table as a sub table in 3rdTable to create a where clause as follows;
select *
from 3rdTable
where City+'|'+State= (select a.City+'|'+b.State
from a
inner join b
on a.x=b.y)
Buy concatenating the fields, you can create a single equality to the joined subquery
Be sure about the joins, we have inner join, Left join, right join and outer join; maybe knowing the difference can help you to answer your question.
and also the code is not clear :)
Just join in the 3rd table...
IF we assume table1 has both city and state in it...
SELECT A.City, A.State
FROM Table1 A
INNER JOIN table2 B
on A.PK = B.A_FK
INNER JOIN table3 C
on A.City = C.City
and A.State = C.State
This is the nature of an inner join: Include all rows from all tables where the joined data matches.
If you use an OUTER join (left, right, full outer) then you get all records from one table and only those that match in the others, or full outer all records from all tables aligned where they match.
SELECT *
FROM table3 t3
INNER JOIN (SELECT city,
state
FROM table1 T1
JOIN table2 t2
ON t1.id = t2.id) a1
ON t3.city = a1.city
AND t3.state = a1.state
I think this could help you:
SELECT T3.*
FROM
table_1_2_join T12 /* replace this placeholder table with the select statement that joins your 2 tables */
JOIN table_3 T3 ON T3.City = T12.city AND T3.state = T12.state
Let me know if you need more details.
i have currently 3 tables :
Table A
Table B
Table C
There is a link between A & B and a link between B & C (A-B-C).
The thing is that :
It is possible to have a row in A but no not in B
It is possible to have a row in B but not in A
It is possible to have a row in B but not in C
In the end i would like to have a query which could give me the following row (where X represent the ID of the corresponding table) :
TableA|TableB|TableC
X | X | X
X | null | null
null | X | X
X | X | null
I managed to have the case with TableA & TableB with the following query :
SELECT A.ID, B.ID
FROM TABLEA A
LEFT JOIN TABLEB B on (join condition)
UNION
SELECT A.ID,B.ID
FROM TABLE B
LEFT JOIN TABLEA A on (join condition)
Thank you for any help you may provide
What you need is a FULL OUTER JOIN, however, you have tagged your post with sybase - it depends what you mean by that. Sybase ASE doesn't support FULL OUTER JOIN syntax, but SQL Anywhere does.
If I understood it correctly then a FULL OUTER JOIN should do your work :
SELECT a.id,b.id,c.id
FROM TableA a
FULL OUTER JOIN TableB b on a.id = b.id
FULL OUTER JOIN TableC c on COALESCE(a.id,b.id) = c.id
SQL Fiddle
Are these two queries equivalent (assuming varying/any kinds of data in the table)? Are there any scenarios in which they would return different results?
Query 1:
select * from tablea a
left join tableb b on a.keyacol = b.keybcol
inner join tablec c on c.keyccol = b.keybcol;
Query 2:
select * from tablea a
left join (
select b.*, c.* from tableb b
inner join tablec c on c.keyccol = b.keybcol
) sub on a.keyacol = sub.keybcol;
No, they are not equivalent. Example:
CREATE TABLE a
( keya int ) ;
CREATE TABLE b
( keyb int ) ;
CREATE TABLE c
( keyc int ) ;
INSERT INTO a
VALUES
(1) ;
INSERT INTO b
VALUES
(1),(2) ;
INSERT INTO c
VALUES
(2) ;
Results:
SELECT *
FROM a
LEFT JOIN b
ON a.keya = b.keyb
INNER JOIN c
ON c.keyc = b.keyb ;
Result
----------------------
| keya | keyb | keyc |
----------------------
SELECT *
FROM a
LEFT JOIN
( SELECT b.*, c.*
FROM b
INNER JOIN c
ON c.keyc = b.keyb
) sub
ON a.keya = sub.keyb ;
Result
----------------------
| keya | keyb | keyc |
----------------------
| 1 | NULL | NULL |
----------------------
As to why this happens, a LEFT JOIN b INNER JOIN c is parsed as (a LEFT JOIN b) INNER JOIN c which is equivalent to (a INNER JOIN b) INNER JOIN c because the condition on the INNER join cancels the LEFT join.
You can also write the second query in this form - without subquery - which is parsed as a LEFT JOIN (b INNER JOIN c) because of the different placing of the ON clauses:
SELECT *
FROM a
LEFT JOIN
b
INNER JOIN c
ON c.keyc = b.keyb
ON a.keya = b.keyb ;
Result
----------------------
| keya | keyb | keyc |
----------------------
| 1 | NULL | NULL |
----------------------
They're not equivalent.
Essentially, there are four scenarios here, for records on A:
corresponding records on B and C exist;
corresponding records exist on B but not C;
corresponding records exist on C but not B;
corresponding records don't exist on B or C.
Both queries will return the same values for scenario 1.
However, they will return different values for the other scenarios - the inner join on a value of B to a value C in the first query means that you will be attempting to join a null to a value of C in the other scenarios.
The INNER JOIN keyword return rows when there is at least one match in
both tables/selections. If there are rows in tableb that do not have
matches in tablec, those rows will NOT be listed.
The LEFT JOIN keyword returns all the rows from the left table
(tablea), even if there are no matches in the right table (tableb or
sub selection).
Since the left join returns all rows, the two queries might differ if there aren't any correspondent matches in the inner join from the first query. Those rows won't be selected in the first query but will appear in the second since it's used a left join.
Another thing that migh differ was the columns. But since the select * from you will select all colums from all tables/selections and:
Query 1 returns all columns from tablea, tableb and tablec
Query 2 returns all columns from tablea and selection sub (returns all columns from tableb and tablec) = returns all columns from tablea, tableb and tablec
this isn't a problem.
So NO, they are equivalent.