I have 2 tables respectively,emp_data, role_data
EMP_DATA
ID EMPID EMPNAME ROLEID
1 A01 ABC 1
2 A01 ABC 3
ROLE_DATA
ROLEID ROLENAME EMPID
1 SE A01
2 SSE B01
When I join these 2 tables, i have to get OUTPUT OF 2 records like the below one
EMPID EMPNAME ROLEID ROLENAME
A01 ABC 1 SE
A01 ABC 3 <NULL OR EMPTY>
The query what i have written will give output where instead of null in rolename it gives me SE.
SELECT ED.EMPID,ED.EMPNAME,ED.ROLEID,RD.ROLENAME
FROM SYN.EMP_DATA ED,SYN.ROLE_DATA RD
WHERE ED.EMPID=RD.EMPID
Kindly help me in this regard as to how to get the output like i desire.
Thanks in advance
You want a left join:
select e.EMPID, e.EMPNAME, e.ROLEID, r.ROLENAME
from EMP_DATA e left join
ROLE_DATA r
on e.ROLEID = r.ROLEID;
By the way, your problem suggests an issue with your database. EMP_DATA.ROLEID should be declared as a foreign key referencing ROLE_DATA. If so, it would be an error to insert a value that is invalid.
Show us your code. You have to use LEFT JOIN
SELECT emp.EMPID
,emp.EMPNAME
,emp.ROLEID
,ROLE.ROLENAME
FROM EMP_DATA as emp
LEFT JOIN ROLE_DATA as ROLE ON emp.ROLEID = ROLE.ROLEID;
Try this query
select a.EMPID, a.EMPNAME, a.ROLEID,IFNULL(b.ROLENAME, 'SE') as ROLENAME
from EMP_DATA a
lef join ROLE_DATA b on a.ROLEID=B.ROLEID
Related
I have the below tables:
Corporate table:
CorporateId DirectorId ManagerId SalesId
1 1 1 1
2 2 2 3
3 3 4 5
Employee table:
EmployeeId FirstName LastName
1 Tim Sarah
2 Tom Paulsen
3 Tam Margo
4 Eli Lot
5 Ziva Lit
I want to display, for one corporate,the names of the Director, Manager and Sales in rows. Example with corporate 3:
EmployeeId FirstName LastName
3 Tam Margo
4 Eli Lot
5 Ziva Lit
How can I do that? I know how to display rows as columns using pivot, but unsure if pivot can be used here also.
Any help please?
You may join the two tables as the following:
SELECT E.EmployeeId, E.FirstName, E.LastName
FROM Employee E JOIN Corporate C
ON E.EmployeeID IN (C.DirectorId ,C.ManagerId ,C.SalesId)
WHERE C.CorporateId=3
See a demo.
You can first un-pivot your rows into columns by using cross apply, after which you simply join the pivoted rows to your employee table:
select e.*
from corporate c
cross apply (
select EmployeeId from (
values (Directorid), (ManagerId), (SalesId)
)r(EmployeeId)
)r
join employee e on e.EmployeeId = r.EmployeeId
where c.CorporateId = 3;
I got a customer table and each customer has 4 different employeeID (tranieeID,RepresenterID,CoridatorID and ManagerID) I want to get all these employees name rather then their ID in single row.
CustomerTable
|CustomerID|CustomerName|tranieeID|RepresenterID |CoridatorID |ManagerID
------------------------------------------------------------------------
01 Mr T 100 101 102 103
EmployeeTable
EmpID | EmpName
---------------
100 Mr A
101 Mr B
102 Mr C
103 Mr D
What I need
CustomerID | CustomerName | tranieeName | RepresenterName | CoridatorName | ManagerName
----------------------------------------------------------------------------------------
01 Mr T Mr A Mr B Mr C Mr D
I did inner join but I got 4 Rows, is there any way to get all these with a single row?
Thank you for your help!
JOIN should work. I would recommend LEFT JOIN in case any of the values are not filled in:
select c.*,
et.empname as traineeName,
er.empname as RepresenterName,
ec.empname as CoridatorIDName,
em.empname as ManagerName
from customertable c left join
employeetable et
on c.traineeID = et.empid left join
employeetable er
on c.RepresenterID = et.empid left join
employeetable ec
on c.CoridatorID = ec.empid left join
employeetable em
on c.ManagerID = em.empid
You could use a correlated subquery in each instance, such as
select c.CustomerId, CustomerName,
(select Empname from EmployeeTable e where e.EmpID=c.TranieeId) TraineeName,
(select Empname from EmployeeTable e where e.EmpID=c.RepresenterId) RepresenterName,
... etc
from CustomerTable c
I have two tables say Educational_Info and Employee_Info. Details of these tables are given below.
Table: Educational_Info
DegreeID DegreeName
1 BBA
2 BCA
3 MBA
4 MCA
Table: Employee_Info
EmpID BachelorDegree MasterDegree
1001 2 4
Desired Output:
EmpID BachelorDegree MasterDegree
1001 BCA MCA
How to get this desired output. If not possible with this table structure, please suggest me any other way.
Thanks in advance.
This this. This gives you some idea
select b.EmpID,a.DegreeName
FROM Educational_Info a
JOIN Employee_Info b on b.BachelorDegree = a.DegreeID or b.MasterDegree = a.DegreeID
SELECT E.Empid,EI1.DegreeName,EI2.DegreeName
FROM Employee_info E
INNER JOIN Educational_Info EI1 ON E.BachelorDegree=EI1.Degreeid
INNER JOIN Educational_Info EI2 ON E.MasterDegree=EI2.Degreeid
hi i have problem in sql query example
Employee
empid empname
1 gan
2 sam
Designation
id desig empid
1 sr officerr 1
2 jr officer 1
3 manager 2
i want join tables and want Employee Table repeated records Null
result like
empid name desig id
1 gan sr officerr 1
1 NULL jr officer 2
2 sam manager 3
i working on query but i not getting result
SELECT DISTINCT designatin.empid, employee.empname,designatin.desig
FROM designatin INNER JOIN employee e ON employee.empid = designatin.empid
GROUP BY employee.empid, employee.empname, designatin.desig
can anybody have solution?
Change the inner join to a left join:
SELECT DISTINCT designatin.empid, employee.empname,designatin.desig
FROM designatin LEFT JOIN employee e ON employee.empid = designatin.empid
GROUP BY employee.empid, employee.empname, designatin.desig
Let try this, it will help you
SELECT e.empid, e.empname,d.desig ,d.id
FROM employee e
INNER JOIN Designation d ON e.empid = d.empid
See DEMO
I have two tables. Employee and Qualifications.
Employee
Emp_id Name Qualification1 Qualification2 Qualification3
10001 xxxxxx 1 3 5
10002 yyyyyy 3 2
.......
......
.....
Qualifications
Qual_ID Qual_name
1 B.Tech
2 MCA
3 M.Tech
How can i use join query to get the following output
Emp_ID Name Qual1 Qual2 Qual3
10001 xxxxxxx B.Tech MCA pppp
10002 yyyyyyy B.Tech
......
.....
.....
try this
Select E.Emp_ID,E.Name,Q1.Qual_Name,Q2.Qual_Name, Q3.Qual_Name
From
Employees AS E
INNER JOIN Qualifications As Q1 ON E.Qualification1=Q1.Qual_ID
INNER JOIN Qualifications As Q2 ON E.Qualification2=Q2.Qual_ID
INNER JOIN Qualifications As Q3 ON E.Qualification3=Q3.Qual_ID
SELECT e.emp_id,
e.name,
q1.qual_name,
q2.qual_name,
q3.qual_name,
FROM employee e
INNER JOIN qualifications q1
ON e.qualification1 = q1.qual_id
INNER JOIN qualifications q2
ON e.qualification2 = q2.qual_id
INNER JOIN qualifications q3
ON e.qualification3 = q2.qual_id
Try this:
Sql Fiddle
Select
Emp_id, Name,
(Select Qual_name from Qualifications where Qualifications.Qual_ID = Employee.Qualification1) as Qual1,
(Select Qual_name from Qualifications where Qualifications.Qual_ID = Employee.Qualification2) as Qual2,
(Select Qual_name from Qualifications where Qualifications.Qual_ID = Employee.Qualification3) as Qual3
From Employee