Join two tables based on the first table id - sql-server

I have two tables like this
table A:
ID NAME
1 TED
2 JIM
3 AJU
4 ANN
Table B :
ID AGE
1 12
2 14
3 15
4 13
5 15
6 13
7 12
8 16
9 14
10 12
And I want output like
ID NAME AGE
1 TED 12
2 JIM 14
3 AJU 15
4 ANN 13

You want to join these two tables and return all common entries present in both tables(sets), joins are done based on some common entity(usually keys), in your case it is the column 'ID' .
An inner join of A and B gives the result of A intersect B, i.e. the inner part of a Venn diagram intersection.
select A.*,B.age
from tablea A
inner join tableb b
on a.id = b.id

Just Do a simple Query and you will get your result
Select A.ID,A.NAME,(Select B.AGE FROM B WHERE B.ID = A.ID)
FROM A
Another way to do that
SELECT A.ID,A.NAME,B.AGE
FROM A JOIN B
ON A.ID = B.ID

just do a simple join
SELECT
A.*
B.Age
FROM TABLEA A
INNER JOIN TABLEB B
ON A.ID = B.ID

Related

T-SQL query to get desired output

I have 2 below tables called T1 and T2
T1 having data as follows
DateList
2021-11-06
2021-11-07
2021-11-08
2021-11-09
T2 having data as follows
EmpId ApplyDate
1 2021-11-07
1 2021-11-09
2 2021-11-09
Now i need t-sql statement that will give date which is not there in T2 for each EmpId.
Output should look as below
DateList EmpId
2021-11-06 1
2021-11-08 1
2021-11-06 2
2021-11-07 2
2021-11-08 2
We can use a cross join between the two tables to generate a reference table containing all date/employee pairs. Then, left anti-join this table to T2 to find all pairs which are not present.
SELECT d.DateList, e.EmpId
FROM T1 d
CROSS JOIN (SELECT DISTINCT EmpId FROM T2) e
LEFT JOIN T2
ON T2.DateList = d.DateList AND
T2.EmpId = e.EmpId
WHERE
T2.DateList IS NULL
ORDER BY
d.DateList,
e.EmpId;

Return sum using aggregate function in SQL Server

I have two tables.
Table1 has following data
Id1 Name Comments
--------------------
1 abc hgdhg
2 xyz mnoph
3 ysdfr jkljk
4 asdf iiuoo
5 pqrs liuoo
Table2 has following data
Id2 Id1 count date
-------------------------------
1 1 18 11/16/2005
2 1 1 11/15/2005
3 1 4 11/25/2005
4 2 4 11/22/2005
5 3 8 11/05/2005
6 3 3 11/30/2005
7 4 2 11/29/2005
8 3 0 11/04/2005
9 2 5 11/02/2005
10 3 9 11/22/2005
11 2 15 11/10/2005
12 5 12 11/19/2005
I want to return output as name, comments, sum of all count since 11/10/2005
I am trying the following query(with out date where condition)
select
Name, Comments, sum(count)
from
Table1 T1
join
Table2 T2 on T1.Id1 = T2.Id1
group by
ID1
But it is throwing error
Name is invalid in the select list because it is not contained in either an aggregate function or the Group by clause.
Can anyone help me with query (with the date where condition)? What's wrong with this?
Thanks in advance
You have to add any columns not contained in the aggregate function, and use where to filter the results:
select Name,
Comments,
sum(count)
from Table1 T1 join Table2 T2 on T1.Id1 = T2.Id1
where T2.[date] >= '11/10/2005'
group by Name, Comments
you can use below query
SELECT T1.Name ,
T1.Comments ,
SUM(T2.[count]) AS [count]
FROM Table1 T1
INNER JOIN Table2 T2 ON T1.Id1 = T2.Id1
WHERE CAST(T2.[date] AS DATE) >= CAST('11/10/2005' AS DATE)
GROUP BY T1.Name ,
T1.Comments
Every column in a select statement without an aggregate function needs to be in the group by sentence too to prevent aggregate errors, about limiting the date, use where clause to define the condition, like shows ahead.
select
Name, Comments, sum(count)
from
Table1 T1
join
Table2 T2 on T1.Id1 = T2.Id1
where
date >= '2005-11-10 00:00:00'
group by
Name, Comments

when sum two columns in different table the record sum duplicate

table 1 table2
k2 id k1 id
3 1 3 1
3 1 3 1
3 1 3 1
5 2 5 2
6 2 6 2
7 2 7 2
7 2 7 2
7 2 7 2
5 2
output i need is:
id sum k1 sum k1
1 9 9
2 37 32
Here is the query i used:
select table1.id,sum(table1.k1),table2.id,sum(table2.k1) from table1,table2
where table1.id= table2.id
but output i have not right
A simple combination of UNION and GROUP BY will give you desired result. Here you go..
SELECT id,max(SumK1) AS SumK1,max(SumK2) AS SumK2 FROM
(
SELECT ID, sum(k1) AS SumK1, NULL AS SumK2 FROM table1 GROUP BY id
UNION
SELECT ID, NULL AS SumK1, sum(k2) AS SumK2 FROM table2 GROUP BY id
) T
GROUP BY id
This should do the trick:
SELECT
COALESCE(table1_result.id1, table2_result.id2) AS id
, table1_result.sum_k1 AS sum_k1
, table2_result.sum_k2 AS sum_k2
FROM
( SELECT id AS id1, SUM(k1) AS sum_k1 FROM table1 GROUP BY id ) AS table1_result
FULL OUTER JOIN ( SELECT id AS id2, SUM(k2) AS sum_k2 FROM table2 GROUP BY id ) AS table2_result
ON table1_result.id1 = table2_result.id2
The first sub-query in the FROM clause:
SELECT id AS id1, SUM(k1) AS sum_k1 FROM table1 GROUP BY id
will give you a result of
id1 sum_k1
1 9
2 37
And likewise, the second sub-query will give the sum for table 2.
id2 sum_k2
1 9
2 32
The outer query matches the id values from both sub-queries and displays the respective sums from table 1 and table 2 alongside one another.
id sum_k1 sum_k2
1 9 9
2 37 32
I believe table1 has id and k1 column. So, you can do this:
select coalesce(t1.id, t2.id) id,
sum(t1.k1) sum_t1_k1,
sum(t2.k1) sum_t2_k1
from table1 t1 full outer join table2 t2
t1.id = t2.id
group by coalesce(t1.id, t2.id);

Inner Join on Empty Values

TableName:Emp
EmpId EmpName EmpAddressId
1 Ram 100
2 Ravi
3 Raj 102
4 Kiran
5 Bujji 101
TableName:Address
AddressId Address
101 India
102 Uk
103 US
select E.*,A.Address from EMP E inner join Address A
on E.EmpId=2 and E.EmpAddressId='' or E.EmpAddressId=A.AddressId
out put should display as for EmpId:2
------------------------------
EmpId EmpName EmpAddressId Address
2 Ravi
for EmpId:3
------------
EmpId EmpName EmpAddressId Address
3 Raj 102 Uk
For employee 2 there is no EmpAddressId but here requirement is need to display as null values or empty values.
can any one help this.
Just replace INNER JOIN with a LEFT JOIN (or LEFT OUTER JOIN). That will result in NULL for all columns from Address table if there's no match. Also, you can replace your join condition with a simpler version:
select E.EmpId, E.EmpName, E.EmpAddressId, A.Address
from EMP E left join Address A
on E.EmpAddressId=A.AddressId
where E.EmpId=2
More on outer joins on MSDN.
Inner join will yield to null if any of the joining table is null.. If all the table has value then use inner join else go for left join. It ensures that atleast you will get let joined table values.
For this ,you use left join
select e.EmpId, e.EmpName, e.EmpAddressId, a.Address
from EMP e left join Address a
on e.EmpAddressId=a.AddressId
where e.EmpId=2

SQL Server query to find parent names of child

i have a table like this
ID Name Mother Father
1 Sue NULL NULL
2 Ed NULL NULL
3 Emma 1 2
4 Jack 1 2
5 Jane NULL NULL
6 Bonnie 5 4
7 Bill 5 4
and i need output as below
ID Name Mother Father
3 Emma sue ed
4 jack sue ed
6 bonnie jane jack
7 bill jane jack
i have tried writing query with join n cte but couldnt come up with the logic , can someone please help me out
SELECT t.ID, t.Name, m.Name, f.Name
FROM your_table t
INNER JOIN your_table m ON m.ID = t.Mother
INNER JOIN your_table f ON f.ID = t.Father
Use LEFT JOIN if you want to include records without Mother and/or Father nodes:
SELECT t.ID, t.Name, ISNULL(m.Name, 'Orphan') Mother, ISNULL(f.Name, 'Orphan') Father
FROM your_table t
LEFT JOIN your_table m ON m.ID = t.Mother
LEFT JOIN your_table f ON f.ID = t.Father
try something like this:
select
p.id, p.name, p1.name as mother, p2.name as father
from people p
inner join people p1 on p1.id = p.mother
inner join people p2 on p2.id = p.father
Select Query
SELECT A.*
FROM tbl A
,tbl M
,tbl F
WHERE A.Mother = M.ID
AND A.Father = F.ID
without inner join..

Resources