Nice way to query for cross table - sql

I have 3 tables :
A(k1,A) B(k1,k2,B) and C(k2,C).
I want to filter all A that satisfy C.k2 condition. in this example, I must filter go through table B : filter all B that have same k1 attribute with A , and filter all C k2 attribute with B (that I have filtered before).
I have an ugly way to do this :
select * from A where k1 in (select * .....) // it looks ugly and hard to trace
I have though about using join function, but don't really know how to do this. Please tell me a best way for this query.
Thanks :)

Try this Query.
select * from A
join b on a.k1 = b.k1
join c on c.k2 = b.k2
Explanation for JOIN

It sounds pretty easy:
select * from A
join B on B.k1 = A.k1
join C on C.k2 = B.k2

If I'm reading your table structure correctly, the join logic would be like this:
SELECT *
FROM A
JOIN B
ON A.k1 = B.k1
JOIN C
ON B.k2 = C.k2
You could of course then specify in the SELECT which table you want values from, ie:
SELECT A.*,C.*
Or Limit results with WHERE ie:
WHERE C.C = 'something'

Using join to retrieve data from two or more tables. see Join Fundamentals
SELECT A.k1,B.k2
FROM A
JOIN B ON A.k1 = B.k1
JOIN C ON B.k2 = C.k2

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

DB2 SQL for Join: Duplicate Table Designator B Error

I have a ColdFusion query like this:
SELECT a.Description, b.MCDL01
FROM ( SELECT GMDL01 as Description from F0901_LB where GMMCU = '950ALDA77') a, F0006_LA b
Inner Join b on a.GMMCU = b.MCMCU
Basically, the value of GMMCU in F0901_LB table exists as MCMCU in F0006_LA table. And I need to grab all matching MCDL01 from the F0006_LA table. But my join above gives error: Duplicate table designator B not valid.
BTW, I am not fully sure an Inner Join is needed but trying it to see what happens.
Any ideas?
Thanks.
You have two different syntax for joins here you want this (preferred):
SELECT a.Description, b.MCDL01
FROM ( SELECT GMDL01 as Description, GMMCU
from F0901_LB
where GMMCU = '950ALDA77') a
Inner Join F0006_LA b on a.GMMCU = b.MCMCU
this is the old way of doing it which you might have seen, but is not the best way to do it:
SELECT a.Description, b.MCDL01
FROM ( SELECT GMDL01 as Description, GMMCU
from F0901_LB
where GMMCU = '950ALDA77') a, F0006_LA b
where a.GMMCU = b.MCMCU
Just do it?
SELECT a.MCDL01, b.GMDL01 Description
FROM F0006_LA a inner join F0901_LB b on a.MCMCU = b.GMMCU and b.GMMCU = '950ALDA77'

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

i m confuse about this query

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

Joining two tables on a key and then left outer joining a table on a number of criteria

I'm attempting to join 3 tables together in a single query. The first two have a key so each entry has a matching entry. This joined table will then be joined by a third table that could produce multiple entries for each entry from the first table (the joined ones).
select * from
(select a.bidentifier, a.bsession, a.symbol, b.jidentifier, b.JSession
from trade_monthly a, trade_monthly_second b
where
a.bidentifier = b.jidentifier AND
a.bsession = b.JSession)
left outer join
trade c
on c.symbol = a.symbol
order by a.bidentifier, a.bsession, a.symbol, b.jidentifier, b.JSession, c.symbol
There will be more criteria (not just c.symbol = a.symbol) on the left outer join but for now this should be useful. How can I nest the queries this way? I'm gettin gan SQL command not properly ended error.
Any help is appreciated.
Thanks
For what I know every derived table must be given a name; so try something like this:
SELECT * FROM
(SELECT a.bidentifier, ....
...
a.bsession = b.JSession) t
LEFT JOIN trade c
ON c.symbol = t.symbol
ORDER BY t.bidentifier, ...
Anyway I think you could use a simpler query:
SELECT a.bidentifier, a.bsession, a.symbol, b.jidentifier, b.JSession, c.*
FROM trade_monthly a
INNER JOIN trade_monthly_second b
ON a.bidentifier = b.jidentifier
AND a.bsession = b.JSession
LEFT JOIN trade c
ON c.symbol = a.symbol
ORDER BY a.bidentifier, a.bsession, a.symbol, b.jidentifier, b.JSession, c.symbol
Try this:
SELECT
`trade_monthly`.`bidentifier` AS `bidentifier`,
`trade_monthly`.`bsession` AS `bsession`,
`trade_monthly`.`symbol` AS `symbol`,
`trade_monthly_second`.`jidentifier` AS `jidentifier`,
`trade_monthly_second`.`jsession` AS `jsession`
FROM
(
(
`trade_monthly`
JOIN `trade_monthly_second` ON(
(
(
`trade_monthly`.`bidentifier` = `trade_monthly_second`.`jidentifier`
)
AND(
`trade_monthly`.`bsession` = `trade_monthly_second`.`jsession`
)
)
)
)
JOIN `trade` ON(
(
`trade`.`symbol` = `trade_monthly`.`symbol`
)
)
)
ORDER BY
`trade_monthly`.`bidentifier`,
`trade_monthly`.`bsession`,
`trade_monthly`.`symbol`,
`trade_monthly_second`.`jidentifier`,
`trade_monthly_second`.`jsession`,
`trade`.`symbol`
Why don't you just create a view of the two inner joined tables. Then you can build a query that joins this view to the trade table using the left outer join matching criteria.
In my opinion, views are one of the most overlooked solutions to a lot of complex queries.