How can I put join in 3 tables? - sql

I am having three tables. Main table which I want to show contain all the data whereas the other two tables contain the values whose reference ID's are available in main table. I want to show all the values from main table along with the values whose references are available in other two tables.
PART_ID and SERIAL_ID values are available in Table B and Table C respectively. How can i show the data?
I am using inner join which shows result but not as per my requirement.
Here is my SQL code:
SELECT
TEMP_RMA_ENQUIRY.TEMP_ID,
PART_NUMBER_TBL.PART_NO,
PART_SERIAL.SERIAL_NUM
FROM TEMP_RMA_ENQUIRY
INNER JOIN PART_NUMBER_TBL
ON TEMP_RMA_ENQUIRY.RMA_PART_NO=PART_NUMBER_TBL.PARTID
INNER JOIN PART_SERIAL
ON TEMP_RMA_ENQUIRY.RMA_SERIAL_NO=PART_SERIAL.SERIAL_ID

Use Left Join Instead of Inner JOIN
Try this
SELECT TEMP_RMA_ENQUIRY.TEMP_ID,
PART_NUMBER_TBL.PART_NO,
PART_SERIAL.SERIAL_NUM
FROM TEMP_RMA_ENQUIRY
LEFT JOIN PART_NUMBER_TBL
ON TEMP_RMA_ENQUIRY.RMA_PART_NO=PART_NUMBER_TBL.PARTID
LEFT JOIN PART_SERIAL
ON TEMP_RMA_ENQUIRY.RMA_SERIAL_NO=PART_SERIAL.SERIAL_ID

Related

SQL column added twice during INNER JOIN

I am trying to join two tables from a database; energyImport and sunAlt.
energyImport has three columns: timestamp_id, energy, duration.
sunAlt has two columns: timestamp_id, altitude
I am doing an inner join on these two tables using the SQL:
SELECT *
FROM energyImport
INNER JOIN sunAz ON sunAz.timestamp_id = energyImport.timestamp_id;
The output from this is:
timestamp_id,duration,energy,timestamp_id,altitude
1601769600,1800,81310,1601769600,0.0
1601771400,1800,78915,1601771400,0.0
1601773200,1800,78305,1601773200,0.0
The problem is that the timestamp_id column is repeated. How can I join these columns and only include the first timestamp_id?
Replace the * with
energyImport.timestamp_id,energyImport.duration, energyImport.energy,
sunAz.altitude
Either you specify the columns that you want in the results:
SELECT e.timestamp_id, e.duration, e.energy, s.altitude
FROM energyImport e INNER JOIN sunAz s
ON s.timestamp_id = e.timestamp_id;
Or, use NATURAL instead of INNER join, so that the join is based on the columns(s) with the same names of the 2 tables (if this fits your requirement), because NATURAL join returns only 1 of each pair of these columns:
SELECT *
FROM energyImport NATURAL JOIN sunAz;
See the demo.

How to Inner join two anonymous tables [SQL]

as the title states, Let's say i have three tables: person, autos, numbers.
Created
First table with: INNER JOIN of person and autos
Second Table with: INNER JOIN of autos and numbers
Question: Is it possible to INNER JOIN first and second table ?
You can join the two tables based on the autos part. Assuming it has an ID:
SELECT *
FROM first_table f
JOIN second_table s ON f.auto_id = s.auto_id
You can do 2 joins (with 3 tables) together. Which are the keys for the joins? personID for person and autos and on numbers
SELECT *
FROM person p
JOIN autos a ON p.keyA= a.keyA
JOIN numbers n ON a.keyB = n.keyB
Where keyA and keyB are the corresponding field on which you want to do the join.

When would you INNER JOIN a LEFT JOINed table /

I came across below code today.
SELECT StaffGroup.*
FROM StaffGroup
LEFT OUTER JOIN StaffByGroup
ON StaffByGroup.StaffGroupId = StaffGroup.StaffGroupId
INNER JOIN StaffMember
ON StaffMember.StaffMemberId = StaffByGroup.StaffMemberId
WHERE StaffByGroup.StaffGroupId IS NULL
The main table StaffGroup is being LEFT JOINed with StaffByGroup table and then StaffByGroup table is being INNER JOINed with StaffMember table.
I thought the INNER JOIN is trying to filter out the records which exist in StaffGroup and StaffByGroup but do not exist in StaffMember.
But this is not how it is working. The query does not return any records.
Am I missing something in understanding the logic of the query ? Have you ever used INNER JOIN with a table which has been used with LEFT JOIN in earlier part of the query ?
Actually you are missing one concept:
The main table StaffGroup is being LEFT Joined with StaffByGroup table and then this creates a virtual table say VT1 with all records from StaffGroup and matching records from StaffByGroup based on your match/filter condition in ON predicate.Then not StaffByGroup table but this VT1 is being INNER Joined with StaffMember table based on match/filter condition in ON predicate.
So basically the inner join is trying to filter out those records from StaffGroup and hence StaffByGroup which do not have a StaffMemberId.
Adding your where condition adds a final filter like from the final virtual table created by all the above joins remove all such records which don't have a StaffGroupId which in turn might be removing all rows collected in VT1 as all of them will be having some value for StaffGroupId.
To get all records from StaffGroup which have no StaffGroupId along with details from StaffMember for all such records you can add condition in ON predicate as:
SELECT StaffGroup.*
FROM StaffGroup
LEFT OUTER JOIN StaffByGroup
ON StaffByGroup.StaffGroupId = StaffGroup.StaffGroupId and StaffByGroup.StaffGroupId IS NULL
INNER JOIN StaffMember
ON StaffMember.StaffMemberId = StaffByGroup.StaffMemberId
This query looks fundamentally flawed - I guess what was originally intended is
SELECT StaffGroup.*
FROM StaffGroup
LEFT OUTER JOIN
(SELECT * FROM StaffByGroup
INNER JOIN StaffMember
ON StaffMember.StaffMemberId = StaffByGroup.StaffMemberId) StaffByGroup
ON StaffByGroup.StaffGroupId = StaffGroup.StaffGroupId
WHERE StaffByGroup.StaffGroupId IS NULL
which returns all groups from StaffGroup that dont' have existing staffmembers assigned to them (the INNER JOIN with StaffMember filters out those rows from StaffByGroup that don't have a matching row in StaffMember - probably because there exists no foreign key between them)
You are getting 0 records because of your where clause
where StaffByGroup.StaffGroupId is null
The left join links all the records from tbl A which are contained in tbl B and since you have specified StaffGROUPID as your key and then looked for Nulls values in your key, its 100% clear that you will end up with no results

Why does this query produce the same information?

I am using the Lyric Database. I am wondering why this query produces the same information:
SELECT M.LastName, A.ArtistName
FROM members M
LEFT JOIN XRefArtistsMembers X USING(MemberID)
JOIN Artists A on X.ArtistID = A.ArtistID;
And
SELECT M.LastName, A.ArtistName
FROM members M
INNER JOIN XRefArtistsMembers X USING(MemberID)
JOIN Artists A on X.ArtistID = A.ArtistID;
There are no errors. I can post relevent records from the tables by request.
INNER JOIN only chooses only those entries from the two joined tables, where the values from the fields in the JOIN clause match in both tables.
LEFT JOIN chooses all entries from the first table, combined with either the matched entries from the second table or with NULL values instead, if there was no match.
If every row from the first table had a match in the second table on the field mentioned in the JOIN clause, then both INNER JOIN and LEFT JOIN will produce the same results.

I want to retrieve data from 4 tables in SQL Server

I want to retrieve data from 4 tables. Patient table has id as PK which is the foreign key in other three tables ett, phar and ssc. Where a patient lie in only one category. i.e patient id pt1 exists in either of the 3 tables. now I want to retrieve patient info along with its associated category.
My query is:
SELECT *
FROM Patient p
INNER JOIN ETT t
ON p.Patient_ID = t.Patient_ID || INNER JOIN Pharmacological ph
ON p.Patient_ID = ph.Patient_ID
I used OR clause because I want only 1 inner join executing at one time. but its not giving me results, any suggestions??
....Patient table has ID as PK which is the foreign key in other three
tables name: ett, phar and ssc where a patient lie in only one
category. Example, patient id pt1 exists in either of the 3 tables.
Based on your statement, you can join all the tables in table Patient using LEFT JOIN since a record can only exist on one table. The query below uses COALESCE which returns the first non-null value with int the list.
The only thing you need is to manually specify the column names that you want to be shown on the list as shown below.
SELECT a.*,
COALESCE(t.colA, p.ColA, s.ColA) ColA,
COALESCE(t.colB, p.ColB, s.ColB) ColB,
COALESCE(t.colN, p.ColN, s.ColN) ColN
FROM Patient a
LEFT JOIN ETT t
ON a.Patient_ID = t.Patient_ID
LEFT JOIN Phar p
ON a.Patient_ID = p.Patient_ID
LEFT JOIN SSC s
ON a.Patient_ID = s.Patient_ID
To further gain more knowledge about joins, kindly visit the link below:
Visual Representation of SQL Joins
For or - do not ise ||, use "or"
You cannot join with or, you need re-format your query.