Two tables connected by foreign key - sql

I need to connect two tables in SQL Server: Table A has a primary key called IDTableA (integer field) and Table B has a primary key IDTableB (integer field as well).
Table A contains a foreign key IDTableB whereby I want to connect both tables, this field is of type integer and its value is 0.
My problem is that since there is no record in Table B whose ID is 0, do not show me those records from table A with a 0 stored in the foreign key.
The relationship between both tables is: a record in Table A may belong to one or no record in the table B. Therefore, the default value of the foreign key in table A is 0.
I tried connecting the tables with INNER JOIN and LEFT OUTER JOIN but it does not work and left no records show. What I can do?.
Thanks.
My SQL statement:
Select TableA.*
From TableA
inner join TableB on TableA.IdTableB = TableB.IdTableB
The Solution:
Select TableA.*
From TableA
LEFT JOIN TableB on TableA.IdTableB = TableB.IdTableB
More Info:
LEFT JOIN vs. LEFT OUTER JOIN in SQL Server

Try to use left join only
Something like this
Select *
from table_1 tbl1
left join table_2 tbl2 on tbl2.id = tbl1.id

An inner join is the most common join operation used in applications and can be regarded as the default join-type. Inner join creates a new result table by combining column values of two tables (A and B) based upon the join-predicate. The query compares each row of A with each row of B to find all pairs of rows which satisfy the join-predicate. When the join-predicate is satisfied, column values for each matched pair of rows of A and B are combined into a result row.
FROM Table A <renametablehere>
Inner Join Table B <renametablehere>
On <renamedtablename for A>.ID = <renamed table name for b>B.ID

Related

Left Join two tables with one common column and other diff columns

I have two tables where main table has 10+ columns and second table has 3 columns with one common field. My problem here is I am not able to get exact count with left outer join as main table. I am seeing more count than actual. It might be due to one of the field I am trying to get is not in main table which is in second table.
Table 1: master_table
Table 2: manager_table
Master_table :
ID,
Column1,
Column2,
...
Column10
manager_table:
ID,
Column2_different,
Column3_different
I am trying to join using Left Join to get same records as present in master table.
Select table1.columns, table2.columns
From table1
Left join table2 on table1.ID = table2.ID
The above is not giving me exact record count as in master table (table1) , it is giving me more count as the table 2 other field is not present in table 1 .
Can someone help me here ?
TIA
I believe that an INNER JOIN would be better than a LEFT JOIN. Need some sample data to be sure, but if you're getting a higher count than you'd expect upon joining the tables this is probably because the LEFT JOIN is returning everything from both tables. An INNER JOIN will only return data that appears in both tables.

Selecting Fields and Join Clause

I have two tables, TableA:
- Original_Location
- Units
and TableB:
- Original_Loc
- Adjacent_Loc
- Direction (up/down/left/right from original loc)
My goal is to return the original location, the adjacent location, the direction, and the number of units at the adjacent loc. So far, I've only been able to return the units from the original loc.
Here is what I've tried so far:
Select Original_Location,
Units,
TableB.Adjacent_Loc,
TableB.Direction
From TableA
Inner Join
Select *
From TableB
On TableA.Original_Location = TableB.Original_Loc
My thought is that I might need to change the fields I'm selecting before the inner join, or potentially join on Original_Location = Adjacent_Loc.
Your Join syntax is not right. First you don't need to "select" anything from Table B, your initial SELECT is getting data from the joined tables (A and B) as if they were 1 table. Secondly, you need to specify which fields to join the tables with.
Your join will be something like :-
From TableA Inner Join TableB
on TableA.Original_Loc = TableB.Original_Loc
Once you have got your joins right, you need to make another join to TableA to get the Units. This time you are joining the Adjacent_Loc in Table B to the Original_Location in Table A - which will have the Units value you need.
My example below uses aliases to identify each table (there are now 2 references to TableA so they need to be identified separately). So when you do the second join to TableA, this has the alias of c to differentiate it from the first TableA reference. You then need to select the Units from c.
Select a.Original_Location, c.Units, b.Adjacent_Loc, b.Direction
From TableA a Inner Join TableB b On a.Original_Location = b.Original_Loc
inner join TableA c on b.Adjacent_Loc = c.Original_Location

SQL - Need to conditionally join based on value in TableB column

I need to know the rows in TABLE A that have join records in TABLE B based a column value in TABLE B, but I also need to return rows in which a row in TABLE A has no match in TABLE B.
It seems like I need a LEFT JOIN and a LEFT OUTER JOIN, so I'm not sure what to do there. I understand how to do each, but don't understand how to do them together.
The schema looks like:
TABLE_A
pk
TABLE_B
pk
a_fk
some_value
I need the joined rows where Table_A has no join record in Table_B OR Table_A has a join record row in Table_B (it can have many) in which some_value does not equal "thisValue"
Thanks.
A Left join is a left outer join. Outer joins preserve one of the tables which is what you are after so good guess.
SELECT *
FROM Table A
LEFT JOIN Table B
ON TableA.Column = TableB.Column
AND B.SomeValue <> 'ThisValue'
All of the rows with a match will have the B information populated all of those without will have nulls in the B data

Update records only once with LEFT OUTER JOIN

I am trying to UPDATE Table A with records in Table B based on a LEFT OUTER JOIN based on two columns of data. So, for example, the JOIN from Table A might be USA-4 to match Table B's USA-4. Returning a result of XYZ to a different column. The problem I am having is the Table B contains multiple USA-4's and as a result Table A appears to be updating more than once. Is there a way to fix this? A different JOIN type?
UPDATE tablea a
SET USA-4 = (SELECT distinct USA-4 FROM tableb b WHERE a.id=b.id)

Comparing value in a table vs count of rows in other table

I have two tables A (primary key - unit_id) and B (primary key - unit_id)
I have a value (eg :4 ) in table A and has a unit_id.
I have 4 rows in table B with the same unit ID
I have to write a SQL query to check whether the value in table A matches with the count (rows) in table B with the same unit_id
You can just use inner join and you will see how many values from table A are in table B :
Select a.unit_id from Table1 a inner join Table2 b on a.unit_id = b.unit_id
Im assumin that is what you need , because as #StanislavL pointed out, you cant have more then one unique unit_id in each table.