Is it possible to join a table to itself - sql

I have a query that joins two tables together. In table O I have an employee ID, which I join to the HR table to retrieve the employee name:
inner join hr AS hr on o.syscreator = hr.res_id
Also on the HR table is the employees manager, but the value is only the employee ID of the manager. Therefore to retrieve the full name of the manager, would I need to use a self join? I could only find examples of self join using the FROM table, in this case it's the join table.
select o.ordernr,
o.refer,
o.orddat,
o.afldat,
o.magcode,
o.user_id,
o.status,
o.represent_id,
o.crdnr,
o.ord_debtor_name,
o.ord_AddressLine1,
ord_PostCode,
ord_City,
ord_StateCode,
o.ord_landcode,
o.ord_Phone,
o.ord_contactperson,
o.ord_contactemail,
o.syscreated,
h.fullname AS RepName,
h.repto_id AS ManagerID
from order o
inner join humres AS hr on o.syscreator = hr.res_id
Any help would be appreciated.

You can sure bring the same table twice - you just need to use a different alias.
I think that you want:
select
o.ordernr,
...,
h.fullname AS RepName,
h.repto_id AS ManagerID,
hr_mgr.fullname as MgrName
from order o
inner join humres AS hr on o.syscreator = hr.res_id
inner join humres AS hr_mgr on hr_mgr.res_id = hr.repto_id

Related

Sql server: Join two tables and select multiple column values with subquery

Table OpenPO
Shopping_Cart_No
Goods_Recipient_Emp_ID
accassgnmtownerid
1001958413
160213
65658
1001661570
61875
61855
Table Employee
employee_number
Email
160213
Quentin_Walker#gmail.com
61875
Mihaela_Balseanu#gmail.com
65658
siva#gmail.com
61855
mohan#gmail.com
I have these two tables, both are linked by the employee_number column.
I want to display data as shown below .
Expected result
Goods_Recipient_Email
Goods_Recipient_Emp_ID
accassgnmtownerid
accassgnmtownerid_Email
Quentin_Walker#gmail.com
160213
65658
siva#gmail.com
Mihaela_Balseanu#gmail.com
61875
61855
mohan#gmail.com
Tried with left join and able to compare and select only one email column Goods_Recipient_Emp_ID or op.accassgnmtownerid
SELECT
op.Goods_Recipient_Emp_ID, op.accassgnmtownerid,
te.Email AS accassgnmtownerid_Email
FROM
OpenPO op
LEFT JOIN
Employee te ON te.Employee_Number = op.Goods_Recipient_Emp_ID
Tried with subquery for accassgnmtownerid_Email, but it didn't work out.
Can we apply subquery for accassgnmtownerid_Email or is there any other solution?
You need to join the table employee twice, using two different aliases.
For example:
select
r.email as Goods_Recipient_Email,
o.Goods_Recipient_Emp_ID,
o.accassgnmtownerid,
a.email as accassgnmtownerid_Email
from openpo o
join employee r on r.employee_number = o.Goods_Recipient_Emp_ID
join employee a on a.employee_number = o.accassgnmtownerid
Other than joining the tables for each Email, you can also use a correlated subquery for each, such as
select
(select Email from Employee e where e.employee_number = po.Goods_Recipient_Emp_ID) Goods_Recipient_Email,
po.Goods_Recipient_Emp_ID,
po.accassgnmtownerid,
(select Email from Employee e where e.employee_number = po.accassgnmtownerid) accassgnmtownerid_Email
from OpenPO po;

Single SQL query on many to many relationship using join

I have a simple database with few tables (and some sample columns)
Employee (ID, Title, Content)
Project (ID, Title)
ProjectEmployee(ID ,EMPLOYEE_ID,PROJECT_ID)
Need to find employee's whose don't have any project using join query
I am able to do it using subquery
select * from employee
where id not in (select id from project_employee);
The LEFT JOIN with NOT NULL give you the expected results.
select e.*
from employee e
LEFT JOIN project_employee pe on pe.id = e.id
where pe.id is not null;
This would be best implemented using not exists, joining is not the correct solution if you do not need to return any columns from the joined table.
select *
from employee e
where not exists (select * from project_employee pe where pe.id = e.id)

SQL Joins and Corelated subqueries with column data

I am facing an issue in terms of understanding the joins. Lets say for an example we have two tables employee and sales and now I have a query where we have sales of an employee using the id of the employee
select e.employeename
,s.city
,SUM(s.sales)
from employee e
left join (select sales,eid from sales) s on s.eid = e.id
group by 1,2
I'd like to understand why s.city wasn't showing up? and also would like to understand what is this concept called? Is it co related sub queries on Joins? Please help me down over here.
select
e.employeename
,s.city
,SUM(s.sales)
from employee e
left join (select sales,eid,city from sales) s on s.eid = e.id
group by 1,2
in the left join above you have to add city as well. The query Imagine select sales,eid,city from sales is a table itself and then from this table you are selecting city (your second column s.city) this will run error as your table doesn't have a city column yet.
It is much easier to use CTE (common table expressions than CTE's) You can also do the above question as
select
e.employeename
,s.city
,SUM(s.sales)
from employee e
left join sales as s
on e.id = s.id
group by 1,2
here I have added e.id = s.id instead of s.id = e.id it is better to reference the key of the main table first.
you could use CTE (although used when you have to do a lot of referencing but you can see how it works):
With staging as (
select
e.employeename
,s.city
,s.sales
from employee e
left join sales as s
on e.id = s.id
),
sales_stats as (
select
staging.employeename,
staging.city,
sum(staging.sales)
from staging
group by 1,2
#here you will select from staging again consider staging as a separate table so you will have to have all the columns in the staging that you want to use further. Also you will have to reference columns using staging.x
)
select * from sales_stats
-- here you could have combined the steps but I wanted to show you how cte works, Hope this works for you

How to join one to many tables in Oracle database

I have four tables:
Order, Employee, Supply, Supply_company
Order
------------------
Order_id
Order name
Emp_id
Employee
------------------
Emp_id
Emp_name
Supply
--------------------
Supply_id
Order_id
SupplierName
Supply_company
----------------------
Supply_company_id
Supply_id
Supplier_desc
address
In these 4 tables one employee has more than one order and one order has many supply ID's and for that one supply ID we have one supplier desc. I wanted to display Supplier_desc based on Emp_id. I am getting all the descriptions associated with all orders but I need to get specific desc for specific order, I have tried distinct, listagg, inner join and left outer join and used subquery in where clause but I didn't find any solution.
SELECT
O.EMP_ID,SC.*
FROM
SUPPLY_COMPANY SC
INNER JOIN
SUPPLY S
ON S.Supply_id=SC.Supply_id
INNER JOIN
Order O
ON O.Order_id=S.Order_id
WHERE O.Emp_id=123
Assuming that you have a particular EMP_ID and an ORDER_ID the following query will get you the EMP_NAME and all SUPPLIER_DESCs associated with that employee and order:
SELECT DISTINCT e.EMP_ID, e.EMP_NAME, s.SUPPLIER_DESC
FROM EMPLOYEE e
INNER JOIN ORDER o
ON o.EMP_ID = e.EMP_ID
INNER JOIN SUPPLY s
ON s.ORDER_ID = o.ORDER_ID
INNER JOIN SUPPLY_COMPANY c
ON c.SUPPLY_ID = s.SUPPLY_ID
WHERE e.EMP_ID = your_emp_id AND
o.ORDER_ID = your_order_id
Here we just walk down from EMPLOYEE, through ORDER and SUPPLY, to SUPPLY_COMPANY where the SUPPLIER_DESC can be found, then use the WHERE clause to filter the results down to the particular employee and order we care about - and since you probably don't want repeated rows we put a DISTINCT on the SELECT criteria to tell us we only want a single example of each unique combination. Just replace your_emp_id and your_order_id with the EMP_ID and ORDER_ID values you're interested in. Note that if you supply and EMP_ID and ORDER_ID which do not have anything in common on the EMPLOYEE and ORDER tables you'll get nothing returned by this query.
Best of luck.

Three tables join given me the all combination of records

When i written the query like the following.. It's written the combination of all the records.
What's the mistake in the query?
SELECT ven.vendor_code, add.address1
FROM vendor ven INNER JOIN employee emp
ON ven.emp_fk = emp.id
INNER JOIN address add
ON add.emp_name = emp.emp_name;
Using inner join, you've to put all the links (relations) between two tables in the ON clause.
Assuming the relations are good, you may test the following queries to see if they really make the combination of all records:
SELECT count(*)
from vendor ven
inner join employee emp on ven.emp_fk = emp.id
inner join address add on add.emp_name = emp.emp_name;
SELECT count(*)
add.address1
from vendor ven, employee emp, address add
If both queries return the same result (which I doubt), you really have what you say.
If not, as I assume, maybe you are missing a relation or a restriction to filter the number of results.