SQL query how to print out manager name? - sql

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;

Related

SQL Developer : Holding Multiple values in Variable From Query Result

I need help with query for sql developer.The requirement is a select statement which returns ID Column.I need to hold this ids in a variable and fetch the records associated with each id.Thanks for any help.
You can use join statement.
for example:
select id from table_1 join table_2
on table_1.id = table_2.id;
Not possible, as far as I can tell.
But, if it is the same query all over again and you use its result (those ID values you mentioned) in another queries, then you could create a view
create or replace view v_id as
select id
from some_table
where some_conditions;
and then use that view as yet another data source, e.g.
select d.dname, e.ename, e.job, e.sal
from emp e join v_id v on v.id = e.empno
join dept d on d.deptno = e.deptno;
or
select e.ename, e.job, e.sal
from emp e
where e.empno in (select v.id from v_id v);
or any other option you might choose.

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 use subqueries in oracle

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';

How do I use a value from the superquery inside a subquery?

I need to create a query that shows the last name of an employee, the employee id, the last name of the manager of that employee and the id of that manager.
The last name, id, and the manager id of that employee is easy to do because it is already in one row, which means that the following is sufficient:
SELECT last_name, employee_id, manager_id FROM employees WHERE manager_id IS NOT NULL;
But to get the last_name of the manager, you have to search the same table by the manager id you got from the employee. The solution I found is:
SELECT last_name,
employee_id,
(SELECT last_name FROM employees WHERE employee_id = manager_id),
manager_id
FROM employees
WHERE manager_id IS NOT NULL;
However, it seems that 'manager_id' doesn't work in the subquery (although I expected that) and the output is NULL (for the manager id, all the other columns do have values).
So my question is, how can I use the manager_id in the subquery?
Side note: The manager_id can be different for each employee, so using a constant value doesn't work.
What you need is a correlated subquery. I strongly, strongly recommend that you use table aliases and qualified column names in all your queries. However, these are particularly important with correlated subqueries.
You should write this query as:
SELECT e.last_name, e.employee_id,
(SELECT m.last_name
FROM employees m
WHERE m.employee_id = e.manager_id
),
e.manager_id
FROM employees e
WHERE e.manager_id IS NOT NULL;
The alias e is an abbreviation for the table reference to employees in the outer query. The alias m is an abbreviation for the table reference in the subquery.
Notice that all column references use the table alias. This makes the query unambiguous, can prevent unexpected errors, and makes the query much easier for you and others to understand.
You could use a self inner join ( a join with the same table)
SELECT
a.last_name
, a.employee_id
, b.last_name
, a.manager_id
FROM employees a
INNER JOIN employees b ON b.employee_id = a.manager_id;
The inner join work only if a.manager_id is not null so you can avoid this where condition
When you want to refer to a table in the outer query, you need to either use the full table name like table.field or, as in your case, if the outer query table is same as the subquery table, you need to assign an alias to the outer query table and use it in the subquery like this:
SELECT
last_name, employee_id,
(SELECT last_name FROM employees WHERE employee_id = emp_outer.manager_id),
manager_id
FROM employees emp_outer
WHERE manager_id IS NOT NULL;

Oracle IN vs Exists difference?

I am confused with oracle IN and EXISTS. I have below requirement.
I need to get all the employees whose names are in-
select * from emp where ename in('smith','brown','john','johnson');
Can i use EXISTS here? Also IN clause has 1000 limitation. Does EXISTS also has any such limitation?
Thanks!
simply put, EXISTS is usually used for checking whether rows that meet a criteria exist in another (or the same) table.
your SQL using EXISTS would look like this:
select *
from emp e
where exists(select * from emp e2 where e.empno = e2.empno and e2.ename in ('smith', 'brown', 'john', 'johnson'))
so you can see it's not what you need here
IN picks the list of matching values. EXISTS returns the boolean values like true or false. Exists is faster than in.
Example
IN
select ename from emp e where mgr in(select empno from emp where ename='KING');
EXISTS
select ename from emp e
where exists (select 1 from emp where e.mgr = empno and ename = 'KING');