Inner joining two tables returns empty result - sql

I am trying to get two tables that aren't related to pull a column of data each.
I have one table called AlphaData and one called TLAuth. Each includes a column that is labelled invoice and I need to pull both columns so I can at least start a comparison. TLAuth will include some of the invoice numbers from AlphaData, but not all of them.
Right now I am using the following code:
SELECT Alphadata.Invoice, TLAuth.Invoice
FROM Alphadata
INNER JOIN TlAuth
ON TLauth.TLAUthID = Alphadata.TLAUthID;
But every time I run this it comes up totally blank. There is definitely data in there, I can pull one column of data from each, but not both at the same time. I have even setup a relationship (1 to Many from TL Auth to Alphadata) and it doesn't seem to work so any help would be grand.

If the tables could not match you should use left join
SELECT Alphadata.Invoice, TLAuth.Invoice
From Alphadata
LEFT JOIN TlAuth ON TLauth.TLAUthID=Alphadata.TLAUthID;

Related

Refer to different id's within the same query in SQL

I need to brind a lot of columns from several tables using LEFT JOIN. My starting point is the orders table and I bring the vendor name from the "Address_table". Then I add another table with order details and then the shipping information of each order detail.
My problem is that I need to bring a different record from "Address_table" to refer onether id's detailed in shipment table as of "origin_id" and "destination_id".
In other words, "address_id", "origin_id" and "destination_id" are all records from "Address_table". I brought the first one related to the vendor, how can I retrieve the other two?
Example
Thanks in advance
Your question is not exactly clear in terms of the tables and their relationships. It is, however, clear what the problem is. You need to join against the same table twice using different columns.
In order to do that you need to use table aliases. For example, you can do:
select *
from shipment s
left join address_table a on a.address_id = s.origin_id
left join address_table b on b.address_id = s.destination_id
In this example the table address_table is joined twice against the table shipment; the first time we use a as an alias, the second time b. This way you can differentiate how to pick the right columns and make the joins work as you need them to.

Need help retrieving columns from another table

I'm a total SQL newcomer, I've been working with Access until now and it's time to expand.
I've got two tables I'm working with, Aircraft and Flights.
Aircraft is keyed so that every unique aircraft has an AircraftID and that table contains all the info about that aircraft.
Flights is a list of each individual flight, including AircraftID. I want to have that table (or a new table) listing the same info but with data from the column type in table Aircraft displayed next to the AircraftID.
I can't figure out the lookup to so I can match the AircraftID to the record in the Aircraft table... I'm sure there's an easy way but I just can't make heads or tails of the commands.
SELECT Flights.AircraftID, type --add here all the columns you want to keep from Flights
FROM Flights JOIN Aircraft ON Aircraft.AircraftID = Flights.AircraftID
How to join your tables
You need to use one of the join commands.
There are many type of join, but the two most commonly used are INNER JOIN and LEFT OUTER JOIN.
Since it's a direct mapping, use the INNER JOIN (the default in Microsoft SQL server).
SELECT * FROM Aircraft INNER JOIN Flights ON Aircraft.AircraftID = Flights.AircraftID
Note: * returns every column from the result-set. Return specific columns by comma seperated reference
Example
SELECT Aircraft.Name [Aircraft Name], Flights.Name AS [Flight Name] FROM Aircraft INNER JOIN Flights ON Aircraft.AircraftID = Flights.AircraftID

Multiple JOIN statements returning multiple rows

I believe I need a fresh set of eyes, my attention has been pulled elsewhere at work and I have not had the time to figure this out. So I'm hoping someone may be kind enough to offer a suggestion.
Here is an abbreviated version of my SQL statement:
SELECT
PR.PROJECTNUM,
PR.PROJECTNUMBER,
PR.AMRNUM,
W.WONUM,
C.PONUM,
C.POLINENUM
FROM PROJECT PR
INNER JOIN WORKORDER W
ON PR.PROJECTNUM = W.PROJECTNUM
OR PR.PROJECTNUMBER = W.PROJECTNUMBER
OR PR.AMRNUM = W.AMRNUM
INNER JOIN
(SELECT PL.WONUM, P.PONUM, PL.POLINENUM FROM PO P
INNER JOIN POLINE PL ON P.PONUM = PL.PONUM) C
ON W.WONUM = C.WONUM;
As you can see, I'm joining 4 tables here.PO to POLINE to WORKORDER to PROJECT. The issue lies with the multiple joining attributes between the WORKORDER and PROJECT table.
I do not know beforehand which attribute/field will be populated with a value in the WORKORDER table, but at least one will be...but sometimes all three. The duplication occurs when more than one of the joining attributes in the WORKORDER table is populated with a matching value in the PROJECT table.
It's almost as if I need to test for the presence of a value in the joining attribute from the WORKORDER table before I execute the above SQL....and if more than one is populated with a value, then I need to find which one of the PROJECTattributes has a matching value....geez...even typing it out is making my head spin...lol
I may need to come back in the morning and add a little more context, my brain is fried at the moment :)
Thanks for reading!

Inner join sql statement

I have two tables, Invoices and members, connected by PK/FK relationship through the field InvoiceNum. I have created the following sql and it works fine, and pulls 44 records as expected.
SELECT
INVOICES.InvoiceNum,
INVOICES.GroupNum,
INVOICES.DivisionNum,
INVOICES.DateBillFrom,
INVOICES.DateBillTo
FROM INVOICES
INNER JOIN MEMBERS ON INVOICES.InvoiceNum = MEMBERS.InvoiceNum
WHERE MEMBERS.MemberNum = '20032526000'
Now, I want to replace INVOICES.GroupNum and INVOICES.DivisionNum in the above query with GroupName and DivisionName. These values are present in the Groups and Divisions tables which also have the corresponding Group_num and Division_num fields. I have created the following sql. The problem is that it now pulls 528 records instead of 44!
SELECT
INVOICES.InvoiceNum,
INVOICES.DateBillFrom,
INVOICES.DateBillTo,
DIVISIONS.DIVISION_NAME,
GROUPS.GROUP_NAME
FROM INVOICES
INNER JOIN MEMBERS ON INVOICES.InvoiceNum = MEMBERS.InvoiceNum
INNER JOIN GROUPS ON INVOICES.GroupNum = GROUPS.Group_Num
INNER JOIN DIVISIONS ON INVOICES.DivisionNum = DIVISIONS.Division_Num
WHERE MEMBERS.MemberNum = '20032526000'
Any help is greatly appreciated.
You have at least one relation between your tables which is missing in your query. It gives you extra records. Find all common fields. Say, are divisions related to groups?
The statement is fine, as far as the SQL syntax goes.
But the question you have to ask yourself (and answer it):
How many rows in Groups do you get for that given GroupNum?
Ditto for Divisions - how many rows exist for that DivisionNum?
It would appear that those numbers aren't unique - multiple rows exist for each number - therefore you get multiple rows returned

How to combine two tables, one with 1 row and one with n rows?

I have a database with two tables
One with games
and one with participants
A game is able to have more participants and these are in a different table.
Is there a way to combine these two into one query?
Thanks
You can combine them using the JOIN operator.
Something like
SELECT *
FROM games g
INNER JOIN participants p ON p.gameid = g.gameid
Explanation on JOIN operators
INNER JOIN - Match rows between the two tables specified in the INNER
JOIN statement based on one or more
columns having matching data.
Preferably the join is based on
referential integrity enforcing the
relationship between the tables to
ensure data integrity.
o Just to add a little commentary to the basic definitions
above, in general the INNER JOIN
option is considered to be the most
common join needed in applications
and/or queries. Although that is the
case in some environments, it is
really dependent on the database
design, referential integrity and data
needed for the application. As such,
please take the time to understand the
data being requested then select the
proper join option.
o Although most join logic is based on matching values between
the two columns specified, it is
possible to also include logic using
greater than, less than, not equals,
etc.
LEFT OUTER JOIN - Based on the two tables specified in the join
clause, all data is returned from the
left table. On the right table, the
matching data is returned in addition
to NULL values where a record exists
in the left table, but not in the
right table.
o Another item to keep in mind is that the LEFT and RIGHT OUTER
JOIN logic is opposite of one another.
So you can change either the order of
the tables in the specific join
statement or change the JOIN from left
to right or vice versa and get the
same results.
RIGHT OUTER JOIN - Based on the two tables specified in the join
clause, all data is returned from the
right table. On the left table, the
matching data is returned in addition
to NULL values where a record exists
in the right table but not in the left
table.
Self -Join - In this circumstance, the same table is
specified twice with two different
aliases in order to match the data
within the same table.
CROSS JOIN - Based on the two tables specified in the join clause, a
Cartesian product is created if a
WHERE clause does filter the rows.
The size of the Cartesian product is
based on multiplying the number of
rows from the left table by the number
of rows in the right table. Please
heed caution when using a CROSS JOIN.
FULL JOIN - Based on the two tables specified in the join clause,
all data is returned from both tables
regardless of matching data.
example
table Game has columns (gameName, gameID)
table Participant has columns (participantID, participantName, gameID)
the GameID column is the "link" between the 2 tables. you need a common column you can join between 2 tables.
SELECT gameName, participantName
FROM Game g
JOIN Participat p ON g.gameID = p.gameID
This will return a data set of all games and the participants for those games.
The list of games will be redundant unless you structure it some other way due to multiple participants to that game.
sample data
WOW Bob
WOW Jake
StarCraft2 Neal
Warcraft3 James
Warcraft3 Rich
Diablo Chris