Why does this query produce the same information? - sql

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.

Related

Find left table and right table when we join more than 2 tables?

Suppose I have three tables here one of them is a Country table second one is a city table and the third one is a customer table, and I am writing a query here like this
SELECT country.country_name_eng, city.city_name, customer.customer_name
FROM Country
LEFT JOIN city ON city.country_id = country.id
LEFT JOIN customer ON customer.city_id = city.id
I want to know here who is the left table of the customer table and who is the left table of the city table as well as who is the right table of the country table and who is the right table of the customer table
'Left' is always the table at the left of the JOIN keyword, no matter if left, right, inner or full outer join, analogously 'right'.
Left and right joins, though, have different semantics:
With a normal (inner) join a data row is only added if there's a matching entry in both tables, e.g. if you JOIN ON l.id = r.id then if l contains an id with value 7 a row for is only added if there's a matching id with value 7 in r as well (and vice versa).
A left (outer) join instead adds a row for id 7 in l even if there's no matching id in r – then the corresponding columns for table r are filled with nulls.
Analogously a right (outer) join, all entries of r are added with nulls for l's columns if no matching entry found.
A full outer join is a combination of left and right outer join, any entry of l and r is used with nulls on either side for the counter part if none exists.
In your concrete query you'd list all countries even if there don't exist any cities – and if they do exist, for each country all of them even if there don't exist any customers there. In the latter case the columns for the customer are all filled with null values, and if no city exists either, additionally all city columns.

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.

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

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.

Effective resultset structure in case of Inner Join on more than two tables

I was going through this link to see how the effective resultset looks like if we join (Inner/Outer) two tables.
I was just curious to know how the resultset looks like, If we perform Inner/Outer join on more than two tables?
Let's say I've three tables:
1. USERS
2. ADDRESS (Store User's address contains Foreign Key USERS.USER_ID)
3. ITEMS (Contains Items that user sold contains foreign key USERS.USER_ID)
Now If I write a SQL:
SELECT ... FROM USERS users INNER JOIN ADDRESS address ON users.USER_ID = address.USER_ID
INNER JOIN ITEMS items ON users.USER_ID = items.USER_ID WHERE....
How the effective resultset will look like? Whether the DB creates two separate resultset pertaining to each JOIN condition or there will be a single resultset combining all the JOINS? When I say ResultSet, i mean how the effective DB structure looks like (as in the link I've mentioned on top).
Lets me Tell you brief description of the Inner Join And Outer Join.
Simply join is for to view columns from two or more tables together.
Inner Join :- The Matched rows Of the Two or more Tables
SELECT ... FROM USERS users INNER JOIN ADDRESS address ON users.USER_ID = address.USER_ID
INNER JOIN ITEMS items ON users.USER_ID = items.USER_ID WHERE....
What this Statement says is "SELECT ... FROM USERS users INNER JOIN ADDRESS address ON users.USER_ID = address.USER_ID" gives some rows with the filter specified.
INNER JOIN ITEMS items ON users.USER_ID = items.USER_ID WHERE.... :- statement adds columns of the table [ITEMS] with the specified criteria "users.USER_ID = items.USER_ID"
Outer Join :- Different Row of the table mean no rows matched in table.
Two type of the Outer Join
Right Outer :- Not Matched row of the right side table
Left Outer :-Not Matched row of the right side table