How to use subqueries in oracle - sql

Hello I'm trying to solve this question with a subquery:
Select names, service number, jobs and salaries of
people working in the same city as HAVET. (havet is a name)
And I have only two table the first one is the emp table with the column (noserv, name, job, salaries) and the second one is the SERV table with the column (noserv, nameserv, city)
I know that I have to use a subquery but I don't know how to do it.

Semi-pseudocode (CTE won't work, obviously).
with emp (noserv, name, job, salaries),
serv (noserv, nameserv, city)
-- This is what you're looking for, I presume
select e.*
from emp e join serv s on e.noserv = s.noserv
where s.city = -- subquery returns city where HAVET lives
(select s1.city
from serv s1 join emp e1 on e1.noserv = s1.noserv
where e1.name = 'HAVET'
);

Try this:
-- This is a normal query with a left join
select *
from emp e
left join s on e.noserv = s.noserv
where s.city =
-- get Havet's city from the subquery.
(select s.city
from emp e
left join s on e.noserv = s.noserv
where e.name = 'HAVET')

Try this one, change the column name according to your table and column names.
Select e.name,e.serviceNubmer,e.Job,e.salaries
from emp e,serv s
where
e.noserv = s.noserv
and s.city ='HAVET';

Related

what is wrong with my nested subquery and substr function

I'm learning sql and I have this question that I can't solve by myself :
Select name, job, city for employees who do not work in
the same service as their direct supervisor.
I have two tables :
EMP (NOEMP, NAME, FIRST, NAME, EMPLOI, SUP, HIRING, SAL, COMM, NOSERV)
SERV (NOSERV SERVICE CITY)
After an analysis of the two tables I can know if a employee is not working in the same city as his supervisor by extracting the second figure of the SUP column.
And here you can read my proposition to solve the question.
select e1.nom, e1.prenom, e1.emploi, s1.ville, substr(sup,2,1), e1.noserv`
from emp e1 join
serv s1
on e1.noserv = s1.noserv
where e1.nom in (select distinct e3.nom
from emp e3 join
emp e4
on e3.noserv = e4.noserv
where substr(e3.sup,2,1) != e4.noserv
);
Everything is working well but I have an unexpected output as you can see here in this screen from my laptop.
output of the query
Here the sample of the data :
serv table
emp table
This answer makes the following assumptions:
sup is the empno of the supervisor
noserv represents the "service"
The second assumption means that you don't need the serv table. It is sufficient to use a join and an inequality on noserv:
select e.*
from emp e join
emp m
on m.sup = e.empno and m.noserv <> e.noserv;

How to combine two columns from different tables that have a similar name but have different values in SQL Server

I have three tables (example) STAFF, STU, EMP.
I want to combine the column EMPID in table STAFF and table EMP into 1 column?
My previous query is like this,
SELECT *
FROM STU s
FULL OUTER JOIN STAFF st ON st.STAFFID = STUID
FULL OUTER JOIN EMP e ON s.STUID = st.EMPID
The result is like this
The expected result is just like the above screenshot, but I want to join EMPID into one column only.
UPDATE:
I tried using this query:
SELECT
stu.stuid, stu.stuname, stu.stucode,
s.staffid, s.staffname, s.staffcode,
emp.empname, emp.empcode,
COALESCE (emp.empid, staff.staffid) AS col
FROM
STU, Staff, EMP
FULL OUTER JOIN
STAFF s ON s.STAFFID = stu.STUID
FULL OUTER JOIN
EMP e ON stu.STUID = s.EMPID
but it displays an error like this
Use below query to get the desired result.
SELECT s.StuID, s.StuName, s.Stucode, st.StaffId, st.StaffName, st.Staffcode, isnull(st.EmpId, e.EmpId) EmpId, e.EmpCode, e.EmpName
FROM STU s FULL outer JOIN
STAFF st
ON st.STAFFID = STUID FULL OUTER JOIN
EMP e
ON s.STUID = st.EMPID
Note: You will get the one emp Id column as needed. If Staff emp id is not null then staff emp id will be displayed else employee emp id will be displayed

How do I simplify this query?

so my main goal with this query is to select all departments and employees under a manager named Mario Souza.
SELECT d.deptname,e.name FROM employee e JOIN department d ON e.dept = d.deptnum WHERE idmanager IN(
SELECT id FROM employee WHERE name = 'Mario Souza'
) AND manager IN(
SELECT id FROM employee WHERE name = 'Mario Souza'
)
And it's working, but is there a way I could store the first IN result so I could use it later after the AND operator?
You can use EXISTS to match on multiple columns.
WHERE EXISTS
(
SELECT *
FROM employee AS manager
WHERE manager.name = 'Mario Souza'
AND manager.id = e.idmanager
AND manager.id = d.manager
)
You could use a JOIN with employee table. Simply put both manager and idmanager in ON clause.

SQL query returning cartesian product

I have some tables:
Employee: id, name, id_suc, id dep, id_sec
Suc : id_suc, name
Dep : id_dep, id_suc, name
Sec : id_sec, id_dep, id_suc, name
Don't blame on me, this is an existing application, I didn't create the Database and can't touch the structure since there is too much data inside and reports depending on it. I'm just trying to modify a report as asked.
I do a query:
SELECT DISTINCT
s.name as sucurs,
d.name as depart,
c.name as section,
e.name AS emp
FROM
employee e
join suc s on (e.id_suc = s.id_suc)
join dep d on (e.id_dep = d.id_dep)
join sec c on (e.id_sec = c.id_sec)
ORDER BY
sucurs, depart, section, emp
and brings me a cartesian product. I want:
sucurs1, depart1, section1, emp1
sucurs1, depart1, section1, emp2
.....
(then on the report I group by suc, then dep, then sec)
instead, I got:
sucurs1, depart1, section1, emp1
sucurs2, depart1, section1, emp1
and so on. It brings ALL sucurs, ALL depart, ALL section and sometimes duplicated emp.
I'm missing something, but don't know what. Any clues?
Well, you're always joining the tables to only employee - it would seem that Dep is also linked to Suc - so you need a second JOIN condition (join not only on id_dep but also on id_suc!). Table Sec even needs three JOIN conditions since it shares three id's with the Employee table.
SELECT DISTINCT
s.name as sucurs,
d.name as depart,
c.name as section,
e.name AS emp
FROM
employee e
INNER JOIN
suc s ON e.id_suc = s.id_suc
INNER JOIN
dep d ON e.id_dep = d.id_dep AND e.id_suc = d.id_suc
INNER JOIN
sec c ON e.id_sec = c.id_sec AND e.id_suc = c.id_suc AND e.id_dep = c.id_dep
ORDER BY
sucurs, depart, section, emp

SQL query how to print out manager name?

I need to create a query to display employee name and number along with their super's name and super number. Listing should also include employees who don't have any supervisor.
Select e.ename,e.empno,super.ename,super from emp e;
I don't know how to print out the manager/supervisor name, i just need that.
Join the table to itself using an outer join:
select e.ename, e.empno, super.ename, super.empno
from emp e
left join emp super on super.empno = e.super_empno
The left join will still return rows from emp that do not have a super defined.
EDIT
Due to OP's comment, here's how to do it without a join (using a nasty correlated sub-query):
select
ename,
empno,
(select super.ename from emp where empno = e.super_empno) as super_name,
super_empno
from emp e;