i m confuse about this query - sql

What is the name|type of this query? Like inner join, outer join.
SELECT a.tutorial_id, a.tutorial_author, b.tutorial_count
FROM tutorials_tbl a, tcount_tbl b
WHERE a.tutorial_author = b.tutorial_author

It's an Implicit INNER JOIN most commonly found in older code. It is synonymous with:
SELECT a.tutorial_id,
a.tutorial_author,
b.tutorial_count
FROM tutorials_tbl a
INNER JOIN tcount_tbl b ON a.tutorial_author = b.tutorial_author
which is also synonymous with just using JOIN:
SELECT a.tutorial_id,
a.tutorial_author,
b.tutorial_count
FROM tutorials_tbl a
JOIN tcount_tbl b ON a.tutorial_author = b.tutorial_author

Related

How to include column values as null even when condition is not met?

Write a query to show ALL building names, their metering company name and meter type for all buildings that do not have postpaid meters.
The image 1 is the result that I should get and image 2 is the results that i am getting:
USE Ultimate_DataBase
GO
SELECT [Bld_Name], [Elec_company_name], [Mtype_Name]
FROM [dbo].[Metering_Company] A
FULL OUTER JOIN [dbo].[Metering_Type] D
ON A.[MType_ID]= D.MType_ID
FULL OUTER JOIN [dbo].[Building_metering] B
ON A.[Elec_ID]= B.[Elec_ID]
FULL OUTER JOIN [dbo].[Building] C
ON C.[Bld_ID]= B.[Bld_ID]
WHERE [Mtype_Name] != 'POSTPAID'
Try moving the WHERE logic to the corresponding ON clause:
SELECT [Bld_Name], [Elec_company_name], [Mtype_Name]
FROM [dbo].[Metering_Company] A
FULL OUTER JOIN [dbo].[Metering_Type] D
ON A.[MType_ID]= D.MType_ID AND
[Mtype_Name] != 'POSTPAID' -- change is here
FULL OUTER JOIN [dbo].[Building_metering] B
ON A.[Elec_ID]= B.[Elec_ID]
FULL OUTER JOIN [dbo].[Building] C
ON C.[Bld_ID]= B.[Bld_ID];
Note: Please add aliases to your select clause. They are not mandatory, assuming no two tables ever have columns by the same name, but just having aliases would have made your question easier to answer.
FULL JOIN isn't seem necessary -- in fact FULL JOIN is almost never needed, and especially not for routine JOINs in a well-structured database.
The structure of the question suggests NOT EXISTS:
SELECT b.*
FROM dbo.Building b
WHERE NOT EXISTS (SELECT 1
FROM dbo.Building_metering bm JOIN
dbo.Metering_Company mc
ON bm.Elec_ID = mc.Elec_ID JOIN
dbo.Metering_Type mt
ON mt.MType_ID = mc.MType_ID
WHERE bm.Bld_ID = b.Bld_ID AND mt.Mtype_Name = 'POSTPAID'
);
You can also express this as a LEFT JOIN and filtering:
SELECT b.*
FROM dbo.Building b LEFT JOIN
dbo.Building_metering bm
ON bm.Bld_ID = b.Bld_ID LEFT JOIN
dbo.Metering_Company mc
ON bm.Elec_ID = mc.Elec_ID LEFT JOIN
dbo.Metering_Type mt
ON mt.MType_ID = mc.MType_ID AND
mt.Mtype_Name = 'POSTPAID'
WHERE mt.MType_ID IS NULL;
This allows you to select columns from any of the tables.
Notes:
FULL JOIN is almost never needed.
Use meaningful table aliases! Arbitrary letters mean nothing. Use table abbreviations.
Escaping column and table names with square braces just makes code harder to write and to read.
USE Ultimate_DataBase
GO
SELECT [Bld_Name], [Elec_company_name], [Mtype_Name]
FROM [dbo].[Metering_Company] A
LEFT JOIN [dbo].[Metering_Type] D
ON A.[MType_ID]= D.MType_ID
LEFT JOIN [dbo].[Building_metering] B
ON A.[Elec_ID]= B.[Elec_ID]
LEFT JOIN [dbo].[Building] C
ON C.[Bld_ID]= B.[Bld_ID]
Use this

How can I left outer join SQL Server using on?

I would like to join two tables into master table (on to many) but I keep getting an error
Incorrect syntax near C.showdepartmentinventory_id
My code:
CREATE VIEW view_transactionshowidea
AS
SELECT
A.showidea_id, A.showideaupdatetype_id, A.showidea_proposedtitle,
A.showidea_fixtitle,
B.showinventorycategory_id, B.showinventorycategory_name,
C.showinventorydepartment_id, C.showinventorydepartment_name,
A.shoidea_duration, A.showidea_segment, A.showidea_PIC,
A.showidea_concept, A.showidea_isdisabled
FROM
transaction_showidea AS A
LEFT OUTER JOIN
view_showideainventory AS B
LEFT OUTER JOIN
view_showideainventorydepartment AS C ON showinventorycategory_id = B.showinventorycategory_id
AND showinventorycategory_name = C.showinventorydepartment_id
What is the problem with my query?
Each JOIN should be followed by its ON clause. The sources of your JOIN keys are not clear, but something like this:
FROM transaction_showidea si LEFT OUTER JOIN
view_showideainventory sii
ON si.showinventorycategory_id = sii.showinventorycategory_id LEFT OUTER JOIN
view_showideainventorydepartment siid
ON sii.showinventorycategory_name = siid.showinventorydepartment_id
Note that this introduces meaninful table aliases -- abbreviations for the table names. And all column references are qualified.
You need to specify "ON" right after your join statement like this:
CREATE VIEW view_transactionshowidea AS
SELECT A.showidea_id, A.showideaupdatetype_id, A.showidea_proposedtitle,
A.showidea_fixtitle, B.showinventorycategory_id, B.showinventorycategory_name,
C.showinventorydepartment_id, C.showinventorydepartment_name,
A.shoidea_duration, A.showidea_segment, A.showidea_PIC, A.showidea_concept,
A.showidea_isdisabled
FROM transaction_showidea AS A
LEFT OUTER JOIN view_showideainventory AS B on showinventorycategory_id = B.showinventorycategory_id
LEFT OUTER JOIN view_showideainventorydepartment AS C ON showinventorycategory_name = C.showinventorydepartment_id

How can this be translated into postgresql?

I am fairly new to sql in general, and I am trying to work with a database which is extremely large. Now, on the website's examples, there is this query
SELECT m.chembl_id AS compound_chembl_id,
s.canonical_smiles,
r.compound_key,
NVL(TO_CHAR(d.pubmed_id),d.doi) AS pubmed_id_or_doi,
a.description AS assay_description, act.standard_type,
act.standard_relation,
act.standard_value,
act.standard_units,
act.activity_comment
FROM compound_structures s,
molecule_dictionary m,
compound_records r,
docs d,
activities act,
assays a,
target_dictionary t
WHERE s.molregno (+) = m.molregno
AND m.molregno = r.molregno
AND r.record_id = act.record_id
AND r.doc_id = d.doc_id
AND act.assay_id = a.assay_id
AND a.tid = t.tid
AND t.chembl_id = 'CHEMBL1827';
because of this (+) and this NVL I assumed it is Oracle. I am working on pgadmin4 and all my attempts at translating this after doing some research, resulted in errors. Example of my attempt and the error given bellow
SELECT m.chembl_id AS compound_chembl_id,
s.canonical_smiles,
r.compound_key,
COALESCE(CAST(d.pubmed_id AS varchar),d.doi) AS pubmed_id_or_doi,
a.description AS assay_description,
act.standard_type,
act.standard_relation,
act.standard_value,
act.standard_units,
act.activity_comment
FROM compound_structures s,
compound_records r,
docs d,
activities act,
assays a,
target_dictionary t
LEFT OUTER JOIN molecule_dictionary m ON s.molregno = m.molregno
WHERE m.molregno = r.molregno
AND r.record_id = act.record_id
AND r.doc_id = d.doc_id
AND act.assay_id = a.assay_id
AND a.tid = t.tid
AND t.chembl_id = 'CHEMBL1827';
ERROR: invalid reference to FROM-clause entry for table "s"
LINE 16: LEFT OUTER JOIN molecule_dictionary m ON s.molregno = m.molr...
^
HINT: There is an entry for table "s", but it cannot be referenced from this part of the
query.
SQL state: 42P01
Character: 492
Could someone help me?
Don't mix the old, ancient implicit joins and explicit JOIN operator:
SELECT m.chembl_id AS compound_chembl_id,
s.canonical_smiles,
r.compound_key,
COALESCE(CAST(d.pubmed_id AS varchar),d.doi) AS pubmed_id_or_doi,
a.description AS assay_description,
act.standard_type,
act.standard_relation,
act.standard_value,
act.standard_units,
act.activity_comment
FROM compound_structures s
LEFT JOIN molecule_dictionary m ON s.molregno = m.molregno
JOIN compound_records r ON m.molregno = r.molregno
JOIN docs d ON r.doc_id = d.doc_id
JOIN activities act ON r.record_id = act.record_id
JOIN assays a ON act.assay_id = a.assay_id
JOIN target_dictionary t ON a.tid = t.tid
WHERE t.chembl_id = 'CHEMBL1827';
I hope I got the direction of the outer join correct - I haven't used that Oracle syntax for decades (note that even Oracle recommends to stop using it).
But the inner join on the following tables effectively turns that outer join back into an inner join - I don't think the Oracle syntax changes that (meaning: I think that attempt on an outer join in the original query was wrong to begin with). So maybe you can simplify the LEFT JOIN to a JOIN. I am not sure if that outer join actually makes sense in the Oracle query.
Consider:
SELECT
m.chembl_id AS compound_chembl_id,
s.canonical_smiles,
r.compound_key,
COALESCE((d.pubmed_id)::text, d.doi) AS pubmed_id_or_doi,
a.description AS assay_description,
act.standard_type,
act.standard_relation,
act.standard_value,
act.standard_units,
act.activity_comment
FROM
compound_structures s
INNER JOIN molecule_dictionary m ON s.molregno = m.molregno
INNER JOIN compound_records r ON m.molregno = r.molregno
INNER JOIN docs d ON r.doc_id = d.doc_id
INNER JOIN activities act ON r.record_id = act.record_id
INNER JOIN assays a ON act.assay_id = a.assay_id
INNER JOIN target_dictionary t ON a.tid = t.tid AND t.chembl_id = 'CHEMBL1827'
Rationale:
use standard, explicit joins everywhere; old school joins have been deprecated 20 years ago!
COALESCE() is more standard than NVL()
a LEFT JOIN followed by INNER JOINs that relate to columns coming for the left table is actually equivalent to INNER JOIN

Improve performance of SQL query by replacing OR with UNION

To improve the performance of the following query we have to remove the OR clause and use UNION. OR clause does not allow the index to be considered that is why we need to use UNION instead. Kindly let me know if there is any other better way to improve the performance of this query and avoid using OR clause?
SELECT *
FROM A
LEFT OUTER JOIN B
ON A.NBR_CH = B.NBR_CH
LEFT OUTER JOIN C
ON B.ID = C.ID
WHERE A.LIS IN (:IdList)
AND ((C.TYP_C = :Type
AND C.R_NBR LIKE :rNbr)
OR (A.R_NBR LIKE :rNbr))
WITH UR
Would it be like this?
SELECT *
FROM A
LEFT OUTER JOIN B
ON A.NBR_CH = B.NBR_CH
LEFT OUTER JOIN C
ON B.ID = C.ID
WHERE A.LIS IN (:IdList)
AND ((C.TYP_C = :Type
AND C.R_NBR LIKE :rNbr))
WITH UR
UNION
SELECT *
FROM A
LEFT OUTER JOIN B
ON A.NBR_CH = B.NBR_CH
LEFT OUTER JOIN C
ON B.ID = C.ID
WHERE A.LIS IN (:IdList)
AND A.R_NBR LIKE :rNbr
WITH UR
The above is correct. But I wanted to know if there is any other way to do this without using the UNION

Oracle: LEFT OUTER JOIN with using construct

I can't understand the usage of the using word. Can you explain me?
SELECT 1
FROM CONF_RAGGR_OPZTAR ropt
JOIN TAR_OPZIONI_TARIFFARIE OPT using (OPT_OPZIONE_TARIFFARIA_ID)
JOIN CONF_RAGGRUPPAMENTI_FORN rgf using (RGF_RAGGRUPPAMENTO_FORN_ID)
JOIN CONF_FORNITURE_REL_RAGG forg using (RGF_RAGGRUPPAMENTO_FORN_ID)
JOIN CONF_FORNITURE forn using (FORN_FORNITURA_ID)
LEFT JOIN (
select *
from CONF_ELEMENTI_FATTURABILI
where ELF_FLAG_ANN = 'N'
AND ELF_DATA_VER_FIN = TO_DATE('31/12/9999','DD/MM/YYYY')
) elf **using** (ROPT_RAGGR_OPZTAR_ID,COID_CONTRATTUARIO_ID,ROPT_DATA_INI,EDW_PARTITION)
-- LEFT OUTER JOIN TAR_VOCI_FATTURABILI vof
-- ON (elf.VOF_VOCE_FATTURABILE_ID = vof.VOF_VOCE_FATTURABILE_ID)
-- LEFT OUTER JOIN BASE_FASCE_ORARIE fas
-- ON (fas.FAS_FASCIA_ORARIA_ID = elf.FAS_FASCIA_ORARIA_ID)
WHERE FORN_FORNITURA_ID = 'QJlXmOFZPF3eAlAG'
ORDER BY elf.ELF_VERSIONE DESC;
The using keyword indicates that this is a natural join. This means that the column names on both side of the join are identical.
In your case this means that you will join both sides on ROPT_RAGGR_OPZTAR_ID, COID_CONTRATTUARIO_ID, ROPT_DATA_INI and EDW_PARTITION.