How to join one table with another two tables in sql? - sql

Suppose I have 3 tables and want to join three tables as Result below without repetitive values and NOT NULL.
Table-1: EMP
Id Name Client_Id
1 abc c1
2 xyz c2
3 pqr c3
Table-2: EMP_Account:
Id salary
1 10,000
2 20,000
3 30,000
Table-3: Client_Details;
Client_Id Client_Name
c1 client_1
c2 client_2
c3 client_3
Final Result:
Id Name Salary Client_Name
1 abc 10,000 client_1
2 xyz 20,000 client_2
3 pqr 30,000 client_3
I tried to do Join with Left join but not success.
So, can anyone help me or suggest that how we can do this?

You can join more than once, but each must be in its own section
select t1.id, t1.name, t2.salary, t3.client_name
from EMP t1
inner join EMP_Account t2
on t1.id = t2.id
inner join Client_Details t3
on t1.client_id = t3.client_id
Aliasing helps
If you want to return results even where there is no client info, substitute inner for left

SELECT e.id, e.name, ea.salary, cd.client_name
FROM emp e
JOIN emp_account ea ON e.id = ea.id
JOIN client_details cd ON cd.client_id = e.client_id

SELECT E.ID, E.NAME, EA.SALARY, CD.CLIENT_NAME FROM EMP E
JOIN EMP_ACCOUNT EA ON EA.ID = E.ID
JOIN CLIENT_DETAILS CD ON CD.CLIENT_ID = E.CLIENT_ID

Related

Many Columns in One Table JOIN one reference table

image. Very new so, please be kind. Using DBForge Query Builder
I have one table with a number of columns which contain abbreviations.
Another table contains the descriptions for the abbreviations.
Need to write a query to replace all the abbreviations with their descriptions. Tried a number of joins without any success. Any ideas?
T1
ID Date Type Cat Sub Cat
1 01/09/18 E F L
2 05/09/18 Cc F D
3 06/09/18 Cc C Dr
4 08/09/18 Cc C Sh
5 08/09/18 E C Sh
T2
Code Des
E Eft Payment
Cc Credit Card
F Food
C Clothes
B Breakfast
L Lunch
D Dinner
Sh Shirt
Dr Dress
Desired Output
ID Date Type D Cat D Sub Cat D
1 01/09/18 Eft Payment Food Lunch
2 05/09/18 Credit Card Food Dinner
3 06/09/18 Credit Card Clothes Dress
4 08/09/18 Credit Card Clothes Shirt
5 08/09/18 Eft Payment Clothes Shirt
Try below using multiple join by T2 with different alias
select
a.ID, a.date,
b.Des as TypeD,
c.Des as CatD,
d.Des as SubCatD
from T1 a inner join T2 b on a.type=b.code
inner join T2 c on a.Cat=c.code
inner join T2 d in a.SubCat=d.code
I will assume the columns of T1 are (ID, Date, Type, Cat, SubCat)
SELECT T1.ID, T1.Date, T2Type.Des, T2Cat.Des, T2SubCat.Des
FROM T1 INNER JOIN T2 T2Type ON T1.Type = T2Type.Code
INNER JOIN T2 T2Cat ON T1.Cat = T2Cat.Code
INNER JOIN T2 T2SubCat ON T1.SubCat = T2SubCat.Code
You need to use multiple instances of the T2 table:
select t1.id, t1.date, typ.des as type_d,ca.des as cat_d,subca.des as sub_cat_d from T1 t1
inner join T2 typ on t1.type=typ.code
inner join T2 ca on t1.cat = ca.code
inner join T2 subca on t1.subcat = subca.code;
Try this...

Selecting values from 2 columns that both are IDs of another tables rows

Let's say i have:
Table1:
ID Name
1 Ann
2 Mike
3 Stan
4 Kyle
Table2:
Pair ID Person1ID Person2ID
1 1 2
2 3 4
I want to select pairs, but with names instead of IDs, so this would be the output:
1 Ann Mike
2 Stan Kyle
I imagine a simple:
inner join Table1 on Table1.ID=Table2.Person1ID
won't work, because I want both of them, not only one.
I'm pretty new to SQL so i'm sorry if there is a simple answer.
You have to join twice and use alias to diference the tables
SELECT t2.PairID, A.Name, B.Name
FROM Table2 t2
JOIN Table1 A
ON t2.Person1ID = A.ID
JOIN Table1 B
ON t2.Person2ID = B.ID
You can join sub query like below:
Select p.PairID, p.Name, q.Name
from
(Select t2.PairID, t1.Name
from Table1 t1 inner join Table2 t2
on t1.ID = t2.Person1ID) p
inner join
(Select t2.PairID, t1.Name
from Table1 t1 inner join Table2 t2
on t1.ID = t2.Person2ID) q
on p.PairID = q.PairID
See the result here in the demo.

SQL query for getting the value ,based on a condition

Hi I need a help to get one sql Query. I have one table 'EMP' .
id name mid
1 A 1
2 B 1
3 C 2
4 D 1
5 E 2
I want the result as
id name mid
1 A A
2 B A
3 C B
4 D A
5 E B
can any one help me to do this.
It is a simple self join:
select id, t1.name, t2.name from emp t1 join emp t2 on t1.mid=t2.id
using inner join:
select EMP1.id, EMP1.name, EMP2.name AS MID from EMP AS EMP1 INNER JOIN EMP AS EMP2 on EMP1.mid=EMP2.id
ORDER BY EMP1.ID
Beside #alex answer you can also try these two too:
Method 1
SELECT T.Id ,T.Name ,D.Name AS mid
FROM EMP AS D
CROSS APPLY (SELECT Id ,Name FROM EMP AS T WHERE mid = D.id) AS T;
Method 2
SELECT Id ,Name ,(SELECT Name FROM EMP WHERE id = T.mid) AS mid
FROM EMP AS T;

Inner join - more than one table

I'm trying to write a code that will join together information from 3 tables and compare it with a "big table" and according to the comparison update a field in a test table. Look at the example:
Big Table:
Employee_Id status
2322 5
222 3
545 6
4532 2
Table 1:
S_Id status
2322 7
Table 2:
S_Id status
222 3
Table 3:
S_Id status
545 6
4532 3
Test Table:
TE_Id IsNotGood
2322 1
4532 1
222 0
545 0
The id "2322" got 1 because the status in table 1 is not like the status in the big table and same for 4532.
I started writing this:
update Test Table
set isNotGood = 0
with ids as (
select distinct Employee_Id from (
select Employee_Id,Status from BigTable as B
) as T
inner join table1 as W on W.S_ID = T.Employee_Id
inner join table2 as K on K.S_ID = T.Employee_Id
inner join table3 as R on R.S_ID = T.Employee_Id
)
I would be happy for your tips to finish this query.
Thank you very much!
You won't be able to use an inner join unless the employee_id is in all the tables.
UPDATE Test
SET isNotGood = 1
WHERE te_id in (select employee_id
from [Big Table] bt
left join table1 as W on W.s_id = bt.employee_Id
left join table2 as K on K.s_id = bt.employee_Id
left join table3 as R on R.s_id = bt.employee_Id
where (bt.status <> W.status and W.status is not null) or (bt.status <> K.status and k.status is not null) or (bt.status <> R.status and R.status is not null);

SQL Server: Subquery on a join

I have two tables with schema and data as below. Table A has an id and an associated name. Table B associates the id from Table A with a price and otherAttr. For each entry in Table A, there may be multiple entries with different prices in Table B, but otherAttr is the same for each entry.
Given an id for Table A, I would like to select the name, otherAttr, and the minimum price.
The below query returns multiple results, I need to write a query that will return a single result with the minimum price.
SELECT a.name, b.price, b.otherAttr
FROM A a
LEFT Join B b on b.idFromA = 1
WHERE a.id = 1
Table A Table B
id | name idFromA | price | otherAttr
-------- ---------------------------
1 | A 1 | 200 | abc
2 | B 1 | 300 | abc
1 | 400 | abc
2 | 20 | def
2 | 30 | def
2 | 40 | ef
I massively oversimplified my example. In addition to selecting the min price and otherAttr from Table B, I also have to select a bunch of other attributes from joins on other tables. Which is why I was thinking the Group By and Min should be a subquery of the join on Table B, as a way to avoid Grouping By all the attributes I am selecting (because the attributes being selected for vary programmatically).
The Actual query looks more like:
SELECT a.name, b.price, b.otherAttr, c.x, c.y, d.e, d.f, g.h....
FROM A a
LEFT Join B b on b.idFromA = 1
LEFT Join C c on something...
LEFT Join D d on something...
LEFT Join G g on something...
WHERE a.id = 1
To get this, you could use GROUP BY in an INNER query:
SELECT gd.name, gd.price,gd.otherAttr, c.x, c.y, d.e, d.f, g.h....
FROM
(SELECT a.id,a.name, MIN(b.price) as price, b.otherAttr
FROM A a
LEFT Join B b on b.idFromA = 1
WHERE a.id = 1
GROUP BY a.id,a.name,b.otherAttr) gd
LEFT Join B b on b.idFromA = 1
LEFT Join C c on something...
LEFT Join D d on something...
LEFT Join G g on something...
Try:-
SELECT a.name, MIN(b.price) minprice, b.otherAttr
FROM A a
LEFT Join B b ON a.Id = b.idFromA
GROUP BY a.name, b.otherAttr
HAVING a.id = 1
You could just do this instead:
SELECT a.name, MIN(b.price), MIN(b.otherAttr)
FROM TableA a
LEFT JOIN TableB b on b.idFromA = a.id
GROUP BY a.name
HAVING a.id = 1;
You need to inner join on price as well in addition to id on the subquery to intersect the right record(s) with the lowest price(s). Then TOP(1) will return only one of those records. You can avoid using TOP(1) if you can expand the conditions and group by fields in the subquery so you schema can assure only a single record is returned for that combination of attributes. Lastly, avoid left joins when intersecting sets.
SELECT TOP(1) p.id, p.price, b.OtherAttr
FROM B as b
INNER JOIN
(SELECT A.id, min(B.price) as price
FROM B
INNER JOIN A on A.id=B.idFromA and A.ID=1
GROUP BY A.id) as p on b.idFromA=p.id and b.price=p.price