SQL left join with where over three tables - sql

I need Help with a SQL statement :-(
Lets say have three Tables:
Table1
id / vorname / user
Table2
id / groupename / user / level
Table 3
id /groupename / description
At the moment my SQL looks like this:
select table1.vorname, table2.groupename, table2.user, table2.level from table2 left join table1 on table2.user = table1.user;
So i get the all Data (users) auf Table 2 with the right uservorname of Table 1.
And now i stuck ...
I have the id from Table 3 and need to limit the recordset to only those groupes.
The key in this case is the groupename so i have to get the groupename from Table3 where the id ist the same that i have and compare the groupename with the groupename from Table2 to limit the records only to this groupe ...
Table2.groupename = Table3.groupename
But i dont know how to solve it.
THANK YOU !

You could use something old-school like
select table1.vorname, table2.groupename, table2.user, table2.level
from table1, table2, table3
where table2.user = table1.user
and table2.groupename = table3.groupename
and table3.id = '[my id]'
Using Joins is possibly more efficient, and possibly makes the intent clearer.
select table1.vorname, table2.groupename, table2.user, table2.level
from table1
inner join table2 on table2.user = table1.user
inner join table3 on table2.groupename = table3.groupename
where table3.id = '[my id]'

select table1.vorname,
table2.groupename,
table2.user,
table2.level
from table2
inner join table3 on table2.groupname = table3.groupname
left join table1 on table2.user = table1.user

Related

Oracle SQL inner join not returning records with blank values

I am joining two tables together, but the result set does not include blank rows. The query below does not return columns with null values. Why is that?
SELECT
table1.FE_KEY
,table2.CV_VALUE
,table2.CV_UOM AS EST_ISENTROPIC_POWERUoM
,CVDATA1.CV_VALUE
,CVDATA1.CV_UOM AS VAPOUR_OR_GAS_HANDLEUoM
,CVDATA2.CV_VALUE
,CVDATA2.CV_UOM AS NORMAL_FLOW_RATEUoM
FROM
((table1 FULL LEFT JOIN table2 ON table1.ID = table2.FE_ID)
INNER JOIN table2 CVDATA1 ON table1.ID = CVDATA1.FE_ID)
INNER JOIN table2 CVDATA2 ON table1.ID = CVDATA2.FE_ID
WHERE ((table1.FE_KEY) Like '6-K-%')
AND ((table2.CV_CODE)='EST_ISENTROPIC_POWER')
AND ((CVDATA1.CV_CODE)='VAPOUR_OR_GAS_HANDLE')
AND ((CVDATA2.CV_CODE)='NORMAL_FLOW_RATE')
If you want to include rows where FE_KEY or CV_CODE are null, then you need to explicitly include those. You also seem to have some extra parentheses that aren't needed. Try this reworked version:
SELECT
TABLE1.FE_KEY
,TABLE2.CV_VALUE
,TABLE2.CV_UOM AS EST_ISENTROPIC_POWERUOM
,CVDATA1.CV_VALUE
,CVDATA1.CV_UOM AS VAPOUR_OR_GAS_HANDLEUOM
,CVDATA2.CV_VALUE
,CVDATA2.CV_UOM AS NORMAL_FLOW_RATEUOM
FROM
(
(TABLE1 FULL LEFT JOIN TABLE2 ON TABLE1.ID = TABLE2.FE_ID)
INNER JOIN TABLE2 CVDATA1 ON TABLE1.ID = CVDATA1.FE_ID
)
INNER JOIN TABLE2 CVDATA2 ON TABLE1.ID = CVDATA2.FE_ID
WHERE (TABLE1.FE_KEY IS NULL OR TABLE1.FE_KEY LIKE '6-K-%')
AND (TABLE2.CV_CODE IS NULL OR TABLE2.CV_CODE = 'EST_ISENTROPIC_POWER')
AND (CVDATA1.CV_CODE IS NULL OR CVDATA1.CV_CODE = 'VAPOUR_OR_GAS_HANDLE')
AND (CVDATA2.CV_CODE IS NULL OR CVDATA2.CV_CODE = 'NORMAL_FLOW_RATE')

SQL Server : removing duplicate column while joining tables

I have 4 tables with one column is common on all tables. Is there a way to create a view where I can join all tables by same column where I see the common column only once.
Let's say I have table1
Cust ID | Order ID | Product_Name
Table2
Cust_ID | Cust_Name | Cust_Address
Table3
Cust_ID | Cust_Acc | Acc_Type
Table4
Cust_ID | Contact_Phone | Cust_Total_Ord
Here is the code I use to join tables;
SELECT *
FROM table1
LEFT JOIN table2 ON table1.Cust_ID = table2.Cust_ID
LEFT JOIN table3 ON table2.Cust_ID = table3.Cust_ID
LEFT JOIN table4 ON table3.Cust_ID = table4.Cust_ID
I get all tables joined by I see Cust_ID from each table as below;
Cust ID| Order ID|Product_Name| Cust_ID| Cust_Name|Cust_Address| Cust_ID| Cust_Acc| Acc_Type|Cust_ID|Contact_Phone|Cust_Total_Ord
Is there a way to remove duplicate Cust_ID columns or do I need to write each column name in the SELECT? I have more than 50 columns in total so will be difficult to write all.
Sorry if it is a really dumb question, I have checked previous similar questions but couldn't figure out and thanks for help.
you have common columns on all tables so could use using(common_column) to remove duplicated columns.
SELECT *
FROM table1
LEFT JOIN table2 using(Cust_ID)
LEFT JOIN table3 using(Cust_ID)
LEFT JOIN table4 using(Cust_ID)
I hop that useful.
you need to select columns from three tables first and then make inner join like below
select
t1.cust_id, t1.col1, t1.col2,
t2.col1_table2, t2.col2_table2,
t3.col1_table3, t3.col2_table3
from
table1 t1
inner join
table2 t2 on t1.cust_id = t2.cust_id
join table3 t3 on t1.cust_id = t3.cust_id
Result as shown in below image
No, you cannot easily do what you want in SQL Server. In other databases, you can use the using clause.
One thing you can do is select the columns explicitly from all but the first table:
SELECT t1.*, . . .
FROM table1 t1 LEFT JOIN
table2 t2
ON t1.Cust_ID = t2.Cust_ID LEFT JOIN
table3
ON t1.Cust_ID = table3.Cust_ID LEFT JOIN
table4
ON t1.Cust_ID = table4.Cust_ID;
Perhaps more important than the column issue, I changed the join conditions. You are using LEFT JOIN, so the first table is the "driving" table. When you say t2.Cust_ID = t3.Cust_Id, this returns true only when there was a match to table2. In general, you want to use the columns from table1, because it is the first one in the chain of LEFT JOINs.

How to write Simple.Data query to join two tables?

I need to write Simple.data queries for following SQL queries can you help me ?
SELECT
Table1.UserID,
Table1.we, Table1.ba, Table1.re,
Table1.rtes, Table1.datae, Table1.void,
Table1.deletee
FROM
Table1
INNER JOIN
Table1 ON UserID.UserID = Table2.UserID
WHERE
Table2.clinicId = 11
I try it following way
db.Table1.FindAll()
.Where(db.Table1.UserID == db.Table2.FindAll(db.Table2.ClinicID = 11).Select(db.Table2.UserID));
but it does not work. I use mysql 4.0
db.Table2.FindAllByClinicId(11)
.Select(
db.Table2.Table1.UserId,
db.Table2.Table1.we,
db.Table2.Table1.ba,
db.Table2.Table2.re,
db.Table2.Table1.rtes,
db.Table2.Table1.datae,
db.Table2.Table1.void,
db.Table2.Table1.deletee);
That should end up sending this to the database:
SELECT Table1.UserId
, Table1.we
, Table1.ba
, Table1.re
, Table1.rtes
, Table1.datae
, Table1.void
, Table1.deletee
FROM Table1
INNER JOIN Table2 ON Table1.UserId = Table2.UserId
WHERE Table2.ClinicId = 11
you are joining with same table. so you need to join two different tables or join same table giving alias.
SELECT Table1.UserID, Table1.we, Table1.ba, Table1.re, Table1.rtes, Table1.datae,
Table1.void, Table1.deletee FROM Table1 INNER JOIN Table2 ON
UserID.UserID = Table2.UserID
where Table2.clinicId=11
OR
using alias for same table.
SELECT t1.UserID, t1.we, t1.ba, t1.re, t1.rtes, t1.datae, t1.void, t1.deletee FROM
Table1 as t1 INNER JOIN Table1 as t2 ON UserID.UserID = t2.UserID
where t2.clinicId=11
This should work, you should just say " table1 inner join tabloe2". && condition Table1.UserID = Table2.UserID
where Table2.clinicId=11
SELECT Table1.UserID, Table1.we, Table1.ba, Table1.re, Table1.rtes, Table1.datae,
Table1.void, Table1.deletee
FROM Table1
INNER JOIN Table2
ON Table1.UserID = Table2.UserID
where Table2.clinicId=11

Join table twice - on two different columns of the same table

I have a very confusing database with a table that holds two values I need in a separate table. Here is my issue:
Table1
- id
Table2
- id
- table1_id
- table3_id_1
- table3_id_2
Table3
- id
- value
I need to go from table1 and do a join that would give me back the value from table3 in two separate columns. So I want something like this:
table1.id | table2.id | table2.table3_id_1 | table2.table3_id_2 | X | Y
Where X and Y are the values for the row connected by table3_id_1 and table3_id_2 respectively.
Possibly make them variables or something so I can filter them in a WHERE clause as well?
SELECT t2.table1_id
, t2.id AS table2_id
, t2.table3_id_1
, t2.table3_id_2
, t31.value AS x
, t32.value AS y
FROM table2 t2
LEFT JOIN table3 t31 ON t31.id = t2.table3_id_1
LEFT JOIN table3 t32 ON t32.id = t2.table3_id_2;
There is no need to involve table1. table2 has all you need - assuming there is a foreign key constraint guaranteeing referential integrity (all t2.table1_id are actually present in table1). Else you may want to join to table1, thereby selecting only rows also present in table1.
I use LEFT [OUTER] JOIN (and not [INNER] JOIN) to join to both instances of table3 for a similar reason: it is unclear whether referential integrity is guaranteed - and whether any of the key columns can be NULL. An [INNER] JOIN would drop rows from the result where no match is found. I assume you would rather display such rows with a NULL value for any missing x or y.
table3.id needs to be UNIQUE, or we might multiply rows with several matches from each LEFT JOIN:
Two SQL LEFT JOINS produce incorrect result
If you join a table several times, use aliases to distinguish them:
SELECT table1.id,table2.id,table2.table3_id_1,table2.table3_id_2,t3_1.id,t3_2.id
FROM table1
JOIN table2 ON table1.id=table2.table1_id
JOIN table3 t3_1 ON table2.table3_id_1=t3_1.id
JOIN table3 t3_2 ON table2.table3_id_2=t3_2.id
WHERE ... t3_1.id=... AND ... t3_2.id=...
select t1.id as table1_id,
t2.id as table2_id,
t2.table3_id_1,
t2.table3_id_2,
t3_1.value as X,
t3_2.value as Y
from Table1 t1
inner join Table2 t2 on t1.id = t2.table1_id
inner join Table3 t3_1 on t2.table3_id_1 = t3_1.id
inner join Table3 t3_2 on t2.table3_id_2 = t3_2.id
where t3_1.value = 'some_value'
or t3_2.value = 'some_other_value'

Are "from Table1 left join Table2" and "from Table2 right join Table1" interchangeable?

For example, there are two tables:
create table Table1 (id int, Name varchar (10))
create table Table2 (id int, Name varchar (10))
Table1 data as follows:
Id Name
-------------
1 A
2 B
Table2 data as follows:
Id Name
-------------
1 A
2 B
3 C
If I execute both below mentioned SQL statements, both outputs will be the same:
select *
from Table1
left join Table2 on Table1.id = Table2.id
select *
from Table2
right join Table1 on Table1.id = Table2.id
Please explain the difference between left and right join in the above SQL statements.
Select * from Table1 left join Table2 ...
and
Select * from Table2 right join Table1 ...
are indeed completely interchangeable. Try however Table2 left join Table1 (or its identical pair, Table1 right join Table2) to see a difference. This query should give you more rows, since Table2 contains a row with an id which is not present in Table1.
Table from which you are taking data is 'LEFT'.
Table you are joining is 'RIGHT'.
LEFT JOIN: Take all items from left table AND (only) matching items from right table.
RIGHT JOIN: Take all items from right table AND (only) matching items from left table.
So:
Select * from Table1 left join Table2 on Table1.id = Table2.id
gives:
Id Name
-------------
1 A
2 B
but:
Select * from Table1 right join Table2 on Table1.id = Table2.id
gives:
Id Name
-------------
1 A
2 B
3 C
you were right joining table with less rows on table with more rows
AND
again, left joining table with less rows on table with more rows
Try:
If Table1.Rows.Count > Table2.Rows.Count Then
' Left Join
Else
' Right Join
End If
You seem to be asking, "If I can rewrite a RIGHT OUTER JOIN using LEFT OUTER JOIN syntax then why have a RIGHT OUTER JOIN syntax at all?" I think the answer to this question is, because the designers of the language didn't want to place such a restriction on users (and I think they would have been criticized if they did), which would force users to change the order of tables in the FROM clause in some circumstances when merely changing the join type.
select fields
from tableA --left
left join tableB --right
on tableA.key = tableB.key
The table in the from in this example tableA, is on the left side of relation.
tableA <- tableB
[left]------[right]
So if you want to take all rows from the left table (tableA), even if there are no matches in the right table (tableB), you'll use the "left join".
And if you want to take all rows from the right table (tableB), even if there are no matches in the left table (tableA), you will use the right join.
Thus, the following query is equivalent to that used above.
select fields
from tableB
right join tableA on tableB.key = tableA.key
Your two statements are equivalent.
Most people only use LEFT JOIN since it seems more intuitive, and it's universal syntax - I don't think all RDBMS support RIGHT JOIN.
I feel we may require AND condition in where clause of last figure of Outer Excluding JOIN so that we get the desired result of A Union B Minus A Interaction B.
I feel query needs to be updated to
SELECT <select_list>
FROM Table_A A
FULL OUTER JOIN Table_B B
ON A.Key = B.Key
WHERE A.Key IS NULL AND B.Key IS NULL
If we use OR , then we will get all the results of A Union B
select *
from Table1
left join Table2 on Table1.id = Table2.id
In the first query Left join compares left-sided table table1 to right-sided table table2.
In Which all the properties of table1 will be shown, whereas in table2 only those properties will be shown in which condition get true.
select *
from Table2
right join Table1 on Table1.id = Table2.id
In the first query Right join compares right-sided table table1 to left-sided table table2.
In Which all the properties of table1 will be shown, whereas in table2 only those properties will be shown in which condition get true.
Both queries will give the same result because the order of table declaration in query are different like you are declaring table1 and table2 in left and right respectively in first left join query, and also declaring table1 and table2 in right and left respectively in second right join query.
This is the reason why you are getting the same result in both queries. So if you want different result then execute this two queries respectively,
select *
from Table1
left join Table2 on Table1.id = Table2.id
select *
from Table1
right join Table2 on Table1.id = Table2.id
Select * from Table1 t1 Left Join Table2 t2 on t1.id=t2.id
By definition: Left Join selects all columns mentioned with the "select" keyword from Table 1 and the columns from Table 2 which matches the criteria after the "on" keyword.
Similarly,By definition: Right Join selects all columns mentioned with the "select" keyword from Table 2 and the columns from Table 1 which matches the criteria after the "on" keyword.
Referring to your question, id's in both the tables are compared with all the columns needed to be thrown in the output. So, ids 1 and 2 are common in the both the tables and as a result in the result you will have four columns with id and name columns from first and second tables in order.
*select *
from Table1
left join Table2 on Table1.id = Table2.id
The above expression,it takes all the records (rows) from table 1 and columns, with matching id's from table 1 and table 2, from table 2.
select *
from Table2
right join Table1 on Table1.id = Table2.id**
Similarly from the above expression,it takes all the records (rows) from table 1 and columns, with matching id's from table 1 and table 2, from table 2. (remember, this is a right join so all the columns from table2 and not from table1 will be considered).