Combining two Oracle SQL queries into one - sql

I need to combine 2 query conditions in one SQL statement. I am unable to logically combine them.
1) Employee whose leave is approved by manager
select e.employee_name,r.request_from_Date,r.request_to_Date from employee_leave e,emp_leave_request r
where e.employee_id=r.request_from_id and e.employee_manager_id= r.request_to_id
and r.request_Approved_date is not null
and r.request_reject_Date is null
and r.request_cancelled_Date is null;
2) Employee who is part of manager's team
select employee_id, employee_name, employee_email, employee_username, employee_leave_normal, employee_contact_no,employee_designation
from employee_leave a
where exists(select 1 from employee_leave b where lower(b.employee_username)=lower(:APP_USER) and a.employee_manager_id=b.employee_id);

You can use INNER JOIN to achieve the same.Please learn what is an INNER join here
SELECT e1.*,
e.request_from_date,
e.request_to_date
FROM (
SELECT e.employee_id,
e.employee_name,
r.request_from_date,
r.request_to_date
FROM employee_leave e,
emp_leave_request r
WHERE e.employee_id=r.request_from_id
AND e.employee_manager_id= r.request_to_id
AND r.request_approved_date IS NOT NULL
AND r.request_reject_date IS NULL
AND r.request_cancelled_date IS NULL)e
inner join
(
SELECT employee_id,
employee_name,
employee_email,
employee_username,
employee_leave_normal,
employee_contact_no,
employee_designation
FROM employee_leave a
WHERE EXISTS
(
SELECT 1
FROM employee_leave b
WHERE Lower(b.employee_username)=Lower(:APP_USER)
AND a.employee_manager_id=b.employee_id) e1
ON e.employee_id=e1.employee_id

Use union to combine 2 sql select queries
example:
https://www.w3schools.com/sql/sql_union.asp

Related

Display the names of each employees who works in both ‘IT’ and ‘SE’

Emp(sid(pk) : integer, sname: varchar(255))
Dep(sid(fk) : integer, dep : varchar(255))
SQL:How I find the names of each employees who works in both ‘IT’ and ‘SE’?
To observe a query that Joins two tables together and get common values depend on a common column Ex: id, using INNTER JOIN will help you on that
The INNER JOIN keyword selects records that have matching values in both tables.
Solution
SELECT Emp.sid, Emp.sname FROM Emp
INNER JOIN
(SELECT sid FROM Dep WHERE dep='IT'
INTERSECT
SELECT sid FROM Dep WHERE dep='SE') as A
ON Emp.sid = A.sid
References
SQL INNER JOIN
The way I understand it, the data situation is as below. emp with sid and sname, and dep, with sid - the foreign key to emp and dep, this time not as a table, but a column containing the department's abbreviation. And the combination, in the dep table, of sid and dep, is unique.
If that is the constellation, then join the two tables using sid, filter by: dep in the set:('IT' , 'SE'); Then, put the two columns from emp into the the column list, and GROUP BY them, and finally, apply the grouping filter HAVING COUNT(*) = 2 to just get the group that has two entries when filtered by the two departments.
WITH
emp(sid, sname) AS (
SELECT 42,'Arthur'
UNION ALL SELECT 43,'Ford'
UNION ALL SELECT 44,'Zaphod'
)
,
dep(sid, dep) AS (
SELECT 42,'IT'
UNION ALL SELECT 42,'SE'
UNION ALL SELECT 42,'AC'
UNION ALL SELECT 43,'IT'
UNION ALL SELECT 43,'AC'
UNION ALL SELECT 44,'SE'
UNION ALL SELECT 44,'SA'
)
SELECT
emp.sid
, emp.sname
FROM emp JOIN dep USING(sid)
WHERE dep.dep IN ('IT','SE')
GROUP BY
emp.sid
, emp.sname
HAVING COUNT(*) = 2;
-- out sid|sname
-- out 42|Arthur

How to write this recursive query correctly in the SQL Server?

I write a recursive query. Problem: a recursive query that show chain management employees that leads to a particular employee('Maria Cameron' with empid=8).Output should be like this:
and HR.Employees is here:
and my query is here:
with Managers as
(
SELECT empid, mgrid, firstname,lastname
FROM HR.Employees as h
where mgrid IS NULL
UNION ALL
SELECT e.empid,e.mgrid,e.firstname,e.lastname
FROM HR.Employees as e INNER JOIN Managers m
ON (e.mgrid = m.empid)
)
SELECT *
FROM Managers
where firstname='Maria' and lastname='Cameron' and empid=8
but this query don't operate correctly and my output is:
This line here is not correct:
with Managers as
(
SELECT empid, mgrid, firstname,lastname
FROM HR.Employees as h
where
*******
Where what?
WHERE h.mgrid IS NULL

Representing 'not in' subquery as join

I am trying to convert the following query:
select *
from employees
where emp_id not in (select distinct emp_id from managers);
into a form where I represent the subquery as a join. I tried doing:
select *
from employees a, (select distinct emp_id from managers) b
where a.emp_id!=b.emp_id;
I also tried:
select *
from employees a, (select distinct emp_id from managers) b
where a.emp_id not in b.emp_id;
But it does not give the same result. I have tried the 'INNER JOIN' syntax as well, but to no avail. I have become frustrated with this seemingly simple problem. Any help would be appreciated.
Assume employee Data set of
Emp_ID
1
2
3
4
5
6
7
Assume Manger data set of
Emp_ID
1
2
3
4
5
8
9
select *
from employees
where emp_id not in (select distinct emp_id from managers);
The above isn't joining tables so no Cartesian product is generated... you just have 7 records you're looking at...
The above would result in 6 and 7 Why? only 6 and 7 from Employee Data isn't in the managers table. 8,9 in managers is ignored as you're only returning data from employee.
select *
from employees a, (select distinct emp_id from managers) b
where a.emp_id!=b.emp_id;
The above didnt' work because a Cartesian product is generated... All of Employee to all of Manager (assuming 7 records in each table 7*7=49)
so instead of just evaluating the employee data like you were in the first query. Now you also evaluate all managers to all employees
so Select * results in
1,1
1,2
1,3
1,4
1,5
1,8
1,9
2,1
2,2...
Less the where clause matches...
so 7*7-7 or 42. and while this may be the answer to the life universe and everything in it, it's not what you wanted.
I also tried:
select *
from employees a, (select distinct emp_id from managers) b
where a.emp_id not in b.emp_id;
Again a Cartesian... All of Employee to ALL OF Managers
So this is why a left join works
SELECT e.*
FROM employees e
LEFT OUTER JOIN managers m
on e.emp_id = m.emp_id
WHERE m.emp_id is null
This says join on ID first... so don't generate a Cartesian but actually join on a value to limit the results. but since it's a LEFT join return EVERYTHING from the LEFT table (employee) and only those that match from manager.
so in our example would be returned as e.emp_Di = m.Emp_ID
1,1
2,2
3,3
4,4
5,5
6,NULL
7,NULL
now the where clause so
6,Null
7,NULL are retained...
older ansii SQL standards for left joins would have been *= in the where clause...
select *
from employees a, managers b
where a.emp_id *= b.emp_id --I never remember if the * is the LEFT so it may be =*
and b.emp_ID is null;
But I find this notation harder to read as the join can get mixed in with the other limiting criteria...
Try this:
select e.*
from employees e
left join managers m on e.emp_id = m.emp_id
where m.emp_id is null
This will join the two tables. Then we discard all rows where we found a matching manager and are left with employees who aren't managers.
Your best bet would probably be a left join:
select
e.*
from employees e
left join managers m on e.emp_id = m.emp_id
where
m.emp_id is null;
The idea here is you're saying that you want to select everything from employees, including anything that matches in the manager table based on emp_id and then filtering out the rows that actually have something in the manager table.
Use Left Outer Join instead
select e.*
from employees e
left outer join managers m
on e.emp_id = m.emp_id
where m.emp_id is null
left outer join will preserve the rows from m table even if they do not have a match i e table based on the emp_id field. The we filter on where m.emp_id is null - give me all the rows from e where there's no matching record in m table.
A bit more on the subject can be found here:
Visual representation of joins
from employees a, (select distinct emp_id from managers) b implies cross join - all posible combinations between tables (and you needed left outer join instead)
The MINUS keyword should do the trick:
SELECT e.* FROM employees e
MINUS
Select m.* FROM managers m
Hope that helps...
select *
from employees
where Not (emp_id in (select distinct emp_id from managers));

How to create an sql command that has SELECT statements from 2 different tables?

How to create an sql command that has SELECT statements from 2 different tables? For example,
select ID from EMP_details, select job_id from Jobs_details
So how can i possibly merge both into one
Selecting from two or more tables When rows in them tables has some sort of relation so you want to extract the corresponding row you would use a JOIN something like this ....
JOIN
SELECT EMP.ID, JD.job_id
FROM EMP_details EMP INNER JOIN jobs_details JD
ON EMP.CommonColumn = JD.CommonColumn
Results From Two SELECTS
When you have two SELECT statements and just want to get the results returned from them queries into one row you can do something like this ...
SELECT X.A , Y.B
FROM (select ID AS A from EMP_details) X, (select job_id AS B from Jobs_details) Y
This is known as a JOIN in SQL. Your query might look like this:
SELECT EMP_details.ID,
EMP_details.Name,
Job_details.RefNumber
FROM EMP_details,
Jobs_details
WHERE EMP_details.ID = Jobs_details.job_id;
Having a column in both tables by which you can match the rows from the first table to the second one, like for example the job_id in Jobs_details matches job_id in Emp_details, you could do:
SELECT e.ID,j.job_id
FROM EMP_details e
INNER JOIN jobs_details j ON e.job_id = j.job_id
For more information on JOIN's see the documentation.
Have you try
SELECT id FROM (SELECT id FROM EMP_details UNION ALL SELECT id FROM Jobs_details) as temp_table;
Thanks,

SQL Server fetch alias name for query

Please check fiddle: myFiddle
Query:
create table Emp(empId int primary key, EmpName varchar(50),MngrID int)
insert into Emp(empId,EmpName,MngrID)values(1,'A',2)
insert into Emp(empId,EmpName,MngrID)values(2,'B',null)
A has mngr B but A has no mngr, so while fetching the record from query it shows me:
EmpId EmpName MngrName(alias MngrName)
1 A B
2 B null
How to fetch the above data using a query?
For some reason it doesn't work in SQLFiddle, but i ran it in my own instance of SQL Server to verify it does work:
SELECT e1.EmpID, e1.EmpName, e2.EmpName
FROM emp e1 LEFT OUTER JOIN emp e2
ON e1.MngrID = e2.EmpID
Basically, you're doing a 'self join' by declaring two instances of the table (e1 and e2), and then joining the first instance's MngrID to the second instance's EmpID.
You need to LEFT JOIN table to itself:
select A.empID, A.empName, b.empName as mgrName
from emp A left join emp B on A.mngrID = b.empID
http://sqlfiddle.com/#!3/184dc/8
select empId,EmpName,(SELECT EmpName FROM emp WHERE MngrID = amp1.MngrID) AS Manager from emp as amp1