SQL Cascading Join - sql

I have the following join as part of a View:
from studies
inner join orders on orders.orderId = studies.orderId
left outer join referrers on referrers.referrerId = orders.referrerId
left outer join professionalDegrees referrerDegree on referrerDegree.professionalDegreeId = referrers.professionalDegreeId
referrers.professionalDegreeId column is NOT NULL. In queries that are limited to the referrers scope, the JOIN to professionalDegrees is INNER.
In the View above, if I INNER JOIN on professionalDegrees, there are no rows returned where orders.referrerId is NULL. When I LEFT JOIN on professionalDegrees the row is returned with NULL referrerId, as desired.
Does INNER vs LEFT matter to performance in this case? Is there a better way to write this JOIN?

Related

Duplicate results due to multiple left outer joins

My query is as below
select DISTINCT
wftransaction.PERSONID,
pr.PRNUM,
pr.DESCRIPTION,
pr.PR1,
prline.GLDEBITACCT,
wftransaction.TRANSDATE,
prstatus.CHANGEBY
prstatus.CHANGEDATE,
prstatus.STATUS,
prstatus.MEMO
from pr
left outer join wftransaction pr.PRID = wftransaction.ORNERID and wftransaction.OWNERTABLE ='PR'
left outer join prline on pr.PRNUM = prline.PRNUM
left outer join prstatus on pr.PRNUM= prstatus.PRNUM
The result given by my query has duplicate results.Please do help me eliminate the redundant/repeating outputs.
When I put distinct this is what happens, https://i.stack.imgur.com/I2jnN.jpg,
I should only see 2 outputs with the same "STATUS" i.e.(COMPOSING) or (APPR) since they have different "GLDEBITACCT", other than that, there should be no more duplicates.
This is the picture of my Code and Result Set
i think you should be using inner join because if left join where being used all data from table A will repeatedly shows as the table B has its foreign key, or might sometimes you are lacking of WHERE clauses it depends on your query, it will be more helpful to others if you can paste the whole query and their structures with expected results.
https://www.codeproject.com/kb/database/visual_sql_joins.aspx
Good idea will be to place a simple DISTINCT clause in the query
select DISTINCT
wftransaction.PERSONID,
pr.PRNUM,
pr.DESCRIPTION,
pr.PR1,
prline.GLDEBITACCT,
wftransaction.TRANSDATE,
prstatus.CHANGEBY
prstatus.CHANGEDATE,
prstatus.STATUS,
prstatus.MEMO
from pr
left outer join wftransaction pr.PRID = wftransaction.ORNERID and wftransaction.OWNERTABLE ='PR'
left outer join prline on pr.PRNUM = prline.PRNUM
left outer join prstatus on pr.PRNUM= prstatus.PRNUM

returned no of rows different on left join

i have two sql query in one of them i perform left outer join, both should return same no of records but returned no of rows are different in both the sql queries
select Txn.txnRecNo
from Txn
inner join Person on Txn.uwId = Person.personId
full outer join TxnInsured on Txn.txnRecNo = TxnInsured.txnRecNo
left join TxnAdditionalInsured on Txn.txnRecNo = TxnAdditionalInsured.txnRecNo
where Txn.visibleFlag=1
and Txn.workingCopy=1
returned 20 records
select Txn.txnRecNo
from Txn
inner join Person on Txn.uwId = Person.personId
full outer join TxnInsured on Txn.txnRecNo = TxnInsured.txnRecNo
where Txn.visibleFlag=1
and Txn.workingCopy=1
returned 15 records
I suspect that the TxnAdditionalInsured table have duplicate records. use distinct
select distinct Txn.txnRecNo
from Txn
inner join Person on Txn.uwId = Person.personId
full outer join TxnInsured on Txn.txnRecNo = TxnInsured.txnRecNo
left join TxnAdditionalInsured on Txn.txnRecNo = TxnAdditionalInsured.txnRecNo
where Txn.visibleFlag=1
and Txn.workingCopy=1
A left join will produce all rows from the left side of the join at least once in the result set.
But if your join conditions are such that there are multiple rows from the right side that match a particular row on the left, that left row will appear multiple times in the result (as many times as it is matched with a right row).
So, if the results are unexpected, your join criteria aren't are strict as they need to be or you do not understand your data as well as you thought you did.
Unlike the other answers, I would not suggest just adding distinct - I'd suggest you investigate your data and determine whether your ON clause needs strengthening or if your data is in fact incorrect. Adding distinct to "make the results look right" is usually a poor decision - prefer to investigate and get the correct query written.
Try this:
select distinct Txn.txnRecNo --> added distinct here
from Txn
inner join Person on Txn.uwId = Person.personId
full outer join TxnInsured on Txn.txnRecNo = TxnInsured.txnRecNo
left join TxnAdditionalInsured on Txn.txnRecNo = TxnAdditionalInsured.txnRecNo
where Txn.visibleFlag=1
and Txn.workingCopy=1

Outer Join and Inner Join

I used inner join and outer join the following query. When executing this query I get duplicate records. How can I get unique results?
Select *
From DeliveryOrderMaster
Inner Join DeliveryOrder ON DeliveryOrderMaster.VoucherNo = DeliveryOrder.DONo
Inner Join Customer ON DeliveryOrderMaster.Code = Customer.ar_code
Left Outer Join tblbatchSerialNos On DeliveryOrder.DoNo = tblbatchSerialNos.VoucherNo And DeliveryOrder.StockCode = tblbatchSerialNos.ProductCode
Where DeliveryOrderMaster.VoucherNo= 'DO01304'
ORDER BY DeliveryOrder.DoNo ASC,DeliveryOrder.ID ASC
The problem there arent inner or left join. The problem can be the type of table. Are you sure that you have used the correct key to Connect table? Can you give an example of wath return the query?

SQl query inner join to return the available data even if the join is missing

I am still messing around with this query, which is working but is not returning the data I need.
SELECT prod.Code,
prod.Description,
prod.Groupp,
prod.Quantity,
prod.Unit,
prod.Standard,
prod.Type,
prod.Model,
prod.GroupBKM,
prod.Note,
comp.Unit,
comp.Cost
FROM dbo.Product1 prod
INNER JOIN dbo.Components comp
ON comp.Code = prod.Code
The above query is returning the data only if a comp.code=prod.code exists while I would like to get the data prod.* in any case and obviously the data relevant comp.cost, if does not exist, will be null.
I cannot get it right! Any help will be appreciated.
Replace INNER JOIN with LEFT JOIN
SELECT prod.Code,
prod.Description,
prod.Groupp,
prod.Quantity,
prod.Unit,
prod.Standard,
prod.Type,
prod.Model,
prod.GroupBKM,
prod.Note,
comp.Unit,
comp.Cost
FROM dbo.Product1 prod
LEFT JOIN dbo.Components comp
ON comp.Code = prod.Code
By definition you cannot do this with an INNER JOIN because an INNER JOIN is defined as only returning items for which a match was found.
If you want to return rows in the base SELECT even if the JOIN predicate fails, then you want a LEFT OUTER JOIN ... which is defined as precisely that.
From Wikipedia:
An outer join does not require each record in the two joined tables to
have a matching record. The joined table retains each record—even if
no other matching record exists. Outer joins subdivide further into
left outer joins, right outer joins, and full outer joins, depending
on which table's rows are retained (left, right, or both).
In your case, replace INNER JOIN with LEFT OUTER JOIN.

SQLite - remove duplicate column in a left outer join

I am doing a left outer join over 6 tables, but I dont want the query to keep the duplicated columns. In SQLite the duplicate column are renamed with underscore and added into the view.
Is it possible to remove them in the same query?
SELECT * FROM AQ_ADRESSES
LEFT OUTER JOIN AQ_CP_ADRESSES ON AQ_ADRESSES.IdAdr = AQ_CP_ADRESSES.IdAdr
LEFT OUTER JOIN AQ_ODONYMES ON AQ_ADRESSES.Seqodo = AQ_ODONYMES.Seqodo
LEFT OUTER JOIN AQ_REFERENTIEL ON AQ_ADRESSES.NoSqNoCivq = AQ_REFERENTIEL.NoSqNoCivq
LEFT OUTER JOIN AQ_MUNICIPALITES ON AQ_ADRESSES.CodeMun = AQ_MUNICIPALITES.CodeMun
LEFT OUTER JOIN AQ_ARRONDISSEMENTS ON AQ_ADRESSES.CodeArr = AQ_ARRONDISSEMENTS.CodeArr
As your join columns have the same name, you can use the using operator to define the join. In that case the "duplicate" columns will not be part of the result set
SELECT *
FROM AQ_ADRESSES
LEFT OUTER JOIN AQ_CP_ADRESSES using (IdAdr)
LEFT OUTER JOIN AQ_ODONYMES using (Seqodo)
LEFT OUTER JOIN AQ_REFERENTIEL using (NoSqNoCivq)
LEFT OUTER JOIN AQ_MUNICIPALITES using (CodeMun)
LEFT OUTER JOIN AQ_ARRONDISSEMENTS using (CodeArr)
Tested in SQLite 3.8, don't know if this works the same in earlier versions. But this behavior is required by the SQL standard.
But in general select * is considered harmful in code that is used in production.