You are on page 1of 48

Subiectul 1

1. Situaţia : Total şefi 1980-1985 1986-1990 după 1990, reprezentând în prima coloană numărul
total al şefilor, iar în coloanele următoare numărul total al şefilor încadraţi în perioada ce reprezintă
antetul coloanei.
R :
select count(*) "Total Sefi" ,
sum(case when (to_char(hiredate,'YYYY')) between 1980 and 1985 then 1 else 0 end) "1980-1985",
sum(case when (to_char(hiredate,'YYYY')) between 1986 and 1990 then 1 else 0 end)"1986-1990",
sum(case when (to_char(hiredate,'YYYY'))> 1990 then 1 else 0 end) "dupa 1990"
from emp where empno in (select mgr from emp);

sau

select count(*) "Total Sefi" ,


sum(case when (to_char(hiredate,'YYYY')) between 1980 and 1985 then 1 else 0 end) "1980-1985",
sum(case when (to_char(hiredate,'YYYY')) between 1986 and 1990 then 1 else 0 end)"1986-1990",
sum(case when (to_char(hiredate,'YYYY'))> 1990 then 1 else 0 end) "dupa 1990"
from emp where exists (select * from emp b where emp.empno=b.mgr) ;

2. Numele angajaţilor care nu sunt şefi .


R :
select ename from emp where not exists (select * from emp b where emp.empno=b.mgr) ;

3. Numele angajatului, salariul şi maximul salariilor medii ale departamentelor, pentru angajaţii care
au salariul mai mare decât maximul salariilor medii ale departamentelor.
R :
select a.ename,a.sal,b.sal_med_max
from emp a,(select max(avg(sal)) sal_med_max from emp group by deptno) b
where a.sal>b.sal_med_max;

4. Numele departamentului şi numărul mediu de săptămâni lucrate în fiecare departament în anul


1981, dacă numărul mediu de săptămâni lucrate este peste 26.
R :
select dname,avg (case when to_char(hiredate,'YYYY')<1981 then 52
else (to_date('31-DEC-1981')-to_date(to_char(hiredate,'dd-mon-YYYY')))/7 end) "Saptamani"
from emp,dept
where to_char(hiredate,'YYYY')<=1981 and emp.deptno=dept.deptno
group by dname
having avg (case when to_char(hiredate,'YYYY')<1981 then 52
else (to_date('31-DEC-1981')-to_date(to_char(hiredate,'dd-mon-YYYY')))/7 end)>26;

sau

select d.dname,c.NR as "Saptamani"


from dept d,(select deptno,avg (case when to_char(hiredate,'YYYY')<1981 then 52
else (to_date('31-DEC-1981')-to_date(to_char(hiredate,'dd-mon-YYYY')))/7 end) NR from emp
where to_char(hiredate,'YYYY')<=1981
group by deptno) c
where c.NR >26 and d.deptno=c.deptno;

sau(yeti)

select d.dname, x.nr_med_sapt


from dept d , (select deptno, avg(case when to_char(hiredate,'YYYY')<1981 then 52 else
(to_date('31-DEC-1981')-hiredate)/7 END)nr_med_sapt
from emp
where to_char(hiredate,'YYYY')<=1981
group by deptno)x
where d.deptno=x.deptno and x.nr_med_sapt>26;

Subiectul 2
1. Situaţia  : Total salarii şefi Plătite în departamentul 10 Plătite în departamentul 20 Plătite în
departamentul 30
R :
select sum(sal) "Total Salarii Sefi" ,
sum(case when deptno =10 then sal else 0 end) "Platite în departamentul 10",
sum(case when deptno =20 then sal else 0 end) "Platite în departamentul 20",
sum(case when deptno =30 then sal else 0 end) "Platite în departamentul 30"
from emp where empno in (select mgr from emp);

sau

select sum(sal) "Total Salarii Sefi" ,


sum(decode(deptno,10,sal,0)) "Platite în departamentul 10",
sum(decode(deptno,20,sal,0)) "Platite în departamentul 20",
sum(decode(deptno,30,sal,0)) "Platite în departamentul 30"
from emp where empno in (select mgr from emp);

Asta e pentru cei care nu sunt sefi :


R :
select sum(sal) "Total salarii care nu sunt sefi" ,
sum(decode(deptno,10,sal,0)) "Platite în departamentul 10",
sum(decode(deptno,20,sal,0)) "Platite în departamentul 20",
sum(decode(deptno,30,sal,0)) "Platite în departamentul 30"
from emp
where not exists (select * from emp a where emp.empno=a.mgr);

2. Numele angajaţilor de pe nivelul 2 al ierarhiei organizaţiei.


R :
select ename,level
from emp
where level=2
connect by prior empno=mgr
start with mgr is null;
3. Numele angajatului, minimul salariului mediu al localitaţilor, salariul angajatului, maximul
salariului mediu al localităţilor, pentru cei ce au salariul cuprins între minimul salariului mediu al
localitaţilor şi maximul salariului mediu al localităţilor.
R :
select ename,sal,sal_med_min,sal_med_max
from emp,(select min(avg(sal)) sal_med_min,
max(avg(sal)) sal_med_max
from emp natural join dept
group by loc) x
where sal between sal_med_min and sal_med_max;

sau(yeti)

select e.ename,e.sal, a.sal_med_min, a.sal_med_max


from emp e, (select min(avg(sal))sal_med_min, max(avg(sal))sal_med_max
from emp, dept
where emp.deptno=dept.deptno
group by loc) a
where e.sal between a.sal_med_min and a.sal_med_max;

4. Numele angajatului, salariul şi comisionul pentru cei ce au salariul şi comisionul identice cu ale
vreunui angajat din departamentul cu cel mai mare număr de angajaţi.
R :
select ename, sal, comm,deptno
from emp
where (sal, nvl(comm,-1))in(select sal, nvl(comm,-1) from emp
where deptno=(select deptno from emp group by deptno
having count(*)=(select max(count(*)) from emp group by deptno)));

Subiectul 3
1. Situaţia  : Numele şefului Salariul minim subordonaţi Numele subordonatului cu salar minim
R :
select a.ename NUME_SEF, x.sal_min SAL_MIN_Subordonati,b.ename NUME_ANG_CU_SAL_MIN
from emp a,emp b, (select min(sal)sal_min, mgr from emp group by mgr )x
where exists (select * from emp b where (a.empno=b.mgr)) and (x.mgr=a.empno) and
(b.ename in (select ename from emp c where x.sal_min=c.sal ));

sau(yeti)

select a.ename sef , b.sal_min, c.ename subaltern


from emp a, emp c , (select mgr , min(sal)sal_min from emp group by mgr)b
where a.empno=b.mgr and a.empno=c.mgr and c.sal=b.sal_min;

2. Numele angajaţilor de pe nivelul 3 al ierarhiei organizaţiei.


R :
select ename,level from emp where level=3
connect by prior empno=mgr
start with mgr is null;
sau(c)

select ename from emp a where a.mgr in (select empno from emp b
where b.mgr in (select empno from emp c where c.ename = 'KING'));

3. Numele angajaţilor care au aceeaşi meserie şi lucrează în aceiaşi localitate cu cei ce au aceiaşi grupă
salarială cu şeful lor direct.
R :
select ename
from emp a, dept e
where (a.deptno=e.deptno) and (job, loc) in (select job,d.loc
from emp b, dept d, salgrade s
where b.deptno=d.deptno and sal between s.hisal and s.losal and
grade =(select grade from salgrade t, emp c
where b.mgr=c.empno and c.sal between t.losal and t.hisal));

4. Numele angajatului, salariul, grupa salarială în care se află salariul, salariul majorat cu 20%, grupa
salarială în care se află salariul majorat, pentru cei care prin majorarea salariului ajung în grupa
salarială imediat următoare aceleia în care au fost iniţial.
R :
select ename NUME, sal SALARIUL, s.grade GRUPA_SALARIALA, 1.2*sal SAL_MAJORAT,
b.grade GRUPA_SAL_MAJ
from emp a, salgrade s, salgrade b
where sal between s.losal and s.hisal
and b.grade = (select grade from salgrade c
where 1.2*a.sal between c.losal and c.hisal ) and s.grade!=b.grade;

Subiectul 4
1. Numărul angajaţilor care nu sunt şefi.
R :
select count(*) Nr_angajati_care_nu_sunt_sefi from emp
where not exists (select * from emp a where emp.empno=a.mgr);

sau(c)

select count(ename) NUME from emp a


where not exists (select *from emp b where a.empno=b.mgr);

sau(y)

SELECT count(a.ename)NUME FROM emp a


WHERE not EXISTS ( SELECT * FROM emp b WHERE a.empno=b.mgr);

Asta e pentru cei care sunt sefi  :


select count(*) Nr_angajati_care_sunt_sefi from emp
where exists (select * from emp a where emp.empno=a.mgr);
2. Situaţia: Nume angajat Colegi_S1_81 Colegi_S2_81 Colegi_S1_82 Colegi_S2_82
Colegi_S1_83 Colegi_S2_83 Total_colegi unde în linia afişaţă pentru fiecare angajat se
afişează numele acestuia, numărul de colegi încadraţi în semestrul şi respectiv anul corespunzător
fiecăreia dintre coloanele anterioare, precum şi numărul total de colegi.
R:
select ename nume,(select count(*) from emp c where to_char(c.hiredate,'yyyy')=1981 and
to_char(c.hiredate,'ddd')<182 and c.empno<>a.empno) colegi_s1_81,
(select count(*) from emp c where to_char(c.hiredate,'yyyy')=1981 and
to_char(c.hiredate,'ddd')>=182 and c.empno<>a.empno) colegi_s2_81,
(select count(*) from emp c where to_char(c.hiredate,'yyyy')=1982 and
to_char(c.hiredate,'ddd')<182 and c.empno<>a.empno) colegi_s1_82,
(select count(*) from emp c where to_char(c.hiredate,'yyyy')=1982 and
to_char(c.hiredate,'ddd')>=182 and c.empno<>a.empno) colegi_s2_82,
(select count(*) from emp c where to_char(c.hiredate,'yyyy')=1983 and
to_char(c.hiredate,'ddd')<182 and c.empno<>a.empno) colegi_s1_83,
(select count(*) from emp c where to_char(c.hiredate,'yyyy')=1983 and
to_char(c.hiredate,'ddd')>=182 and c.empno<>a.empno) colegi_s2_83,
(select count(*) from emp)"Total_colegi"
from emp a
order by hiredate;

3. Numele lunii, numărul angajaţilor încadraţi în luna respectivă şi numele angajatului încadrat în
luna reapectivă care are cea mai mică vechime.
R :
select to_char(a.hiredate,'mon') NUME_LUNA, x.nr,y.ename
from emp a,(select to_char(hiredate,'mon') data,count(*) nr
from emp b group by to_char(hiredate,'mon')) x,
(select to_char(hiredate,'mon') dat, ename, max(hiredate)
from emp d group by to_char(hiredate,'mon'),ename
having max(hiredate) in (select max(hiredate) from emp
group by to_char(hiredate,'mon')))y
where to_char(a.hiredate,'mon')=x.data and to_char(a.hiredate,'mon')=y.dat and y.dat=x.data
group by to_char(a.hiredate,'mon'), x.nr,y.ename;

sau(y)

select distinct x.luna "numele lunii", x.numar ,e.ename vechime_mare


from emp e, (select count(*) numar,(to_char(hiredate,'month'))luna
from emp group by (to_char(hiredate,'month')))x ,
(select min( sysdate-hiredate)mic,(to_char(hiredate,'month')) luna
from emp group by (to_char(hiredate,'month')))y
where x.luna=y.luna and y.luna=to_char(e.hiredate,'month') and (sysdate-hiredate)=mic;

4. Maximul salariilor medii ale localitaţilor.


R :
select max(avg(sal)) from emp a, dept where a.deptno=dept.DEPTNO group by loc;

sau(y)
select max(salmax) from emp, (select avg(sal) salmax from emp group by deptno);

Subiectul 5
1. Numărul angajaţilor de pe nivelul 2 al organizaţiei.
R :
select ename,level from emp where level=2
connect by prior empno=mgr
start with mgr is null;

sau(y)

select a.ename from emp a,emp b where a.mgr=b.empno and b.mgr is null;

sau

select ename from emp


where level=2 connect by prior empno=mgr start with mgr is null;

2. Situaţia  : Nume angajat Salar Comision Nume dep Comision_mediu_dep Loc


Comision_mediu_loc Grupă_salarială Comision_mediu_grupă în care în coloanele aferente
comisionului afişat sau calculat unde nu există o valoare se afişează « Nu s-a negociat ».
R :
select ename NUME_ANGAJAT, sal SALAR, DECODE(comm,null,'NU_S-A_NEGOCIAT',comm)
COMISION,dname NUME_DEP,
x.com_mediu_dep, d.loc LOC, y.com_mediu_loc, s.grade GRUPA_SALARIALA, z.com_mediu_grupa
from emp a, dept d,salgrade s,
(select deptno, avg(nvl(comm,0)) com_mediu_dep from emp group by deptno) x,
(select loc, avg(nvl(comm,0)) com_mediu_loc from emp m, dept n where m.deptno=n.deptno group by
loc) y,
(select grade, avg(nvl(comm,0)) com_mediu_grupa from emp m, salgrade zz
where m.sal between zz.LOSAL and zz.HISAL group by grade) z
where a.deptno=d.deptno and a.sal between s.losal and s.hisal and a.deptno=x.deptno
and d.loc=y.loc and s.grade=z.grade;

sau(p)

select e.ename "NUME ANGAJAT", e.sal "SALAR", decode(e.comm,NULL,'Nu s-a negociat',e.comm)


"COMISION",
d.dname "NUME DEPARTAMENT", a.comm_med1 "COMISION_MEDIU_DEP", d.loc
"LOCALITATE",
b.comm_med2 "COMISION_MED_LOC",s.grade "GRUPA SALARIALA",f.comm_med3
"COMISOIN_MED_GRUPA"
from emp e,dept d,salgrade s,
(select deptno,avg(NVL(comm,0)) comm_med1 from emp group by deptno)a,
(select loc,avg(NVL(comm,0)) comm_med2 from emp,dept where emp.deptno=dept.deptno group by
loc)b,
(select grade,avg(NVL(comm,0)) comm_med3 from emp,salgrade where sal between losal and hisal
group by grade)f
where e.deptno=d.deptno and e.sal between s.losal and s.hisal and e.deptno=a.deptno and d.loc=b.loc
and s.grade=f.grade order by e.ename;

sau(y)

select a.ename,a.sal,decode(a.comm,NULL,'Nu s-a negociat',a.comm) comm,d.dname,


x.commdept,d.loc,y.commloc,s.grade,z.commgrupa
from emp a,dept d,salgrade s,
(select deptno,avg(nvl(comm,0)) commdept from emp group by deptno) x,
(select loc,avg(nvl(comm,0)) commloc from emp,dept where emp.deptno=dept.deptno group by loc) y,
(select grade,avg(nvl(comm,0)) commgrupa from emp,salgrade where sal between losal and hisal
group by grade) z
where a.deptno=d.deptno and a.deptno=x.deptno and d.loc=y.loc and a.sal between s.losal and s.hisal
and z.grade=s.grade;

3. Numărul întreg de luni dintre primul angajat şi ultimul angajat.


R :
select(to_char(max(hiredate),'yyyy')-to_char(min(hiredate),'yyyy'))*12+(to_char(max(hiredate),'mm')-
to_char(min(hiredate),'mm') ) "Numar intreg luni" from emp;

4. Graficul salariului mediu al localităţilor, reprezentând în fiecare linie numele localităţii şi câte un
caracter ‘* ‘ pentru fiecare 100 de unităţi salariale în ordinea alfabetică a localităţilor.
R :
select rpad(loc,avg(sal)/100,'*') "Grafic salar mediu" from salgrade s, dept d, emp a
where a.deptno=d.deptno and a.sal between s.losal and s.hisal
group by loc order by loc;

sau(y)

select distinct d.loc,lpad(' ',x.salmed/100,'*') "Grafic salar mediu" from emp a,dept d,
(select loc,avg(sal) salmed from emp,dept where emp.deptno=dept.deptno group by loc) x
where a.deptno=d.deptno and x.loc=d.loc order by d.loc;

Subiectul 6
1. Numărul angajaţilor de pe nivelul 3 al organizaţiei
R :
select ename,level from emp where level=3
connect by prior empno=mgr
start with mgr is null;

sau(y)

select ename from emp where level=3


connect by prior empno=mgr start with mgr is null;

2. Situaţia  : Localitatea Nr_manageri Nr_analisti Nr_altii Total_angajaţi


R :
select loc, sum(decode(job,'MANAGER',1,0)) NR_MANAGERI,
sum(decode(job,'ANALYST',1,0)) NR_ANALISTI,
sum(case when job!='MANAGER' and job !='ANALYST' then 1 else 0 end)"NR_ALTII",
count(*) TOTAL_ANGAJATI
from emp a, dept d where a.deptno=d.deptno group by loc;

sau(y)

select d.loc,sum(case when a.job='MANAGER' then 1 else 0 end) nr_manager,


sum(case when a.job='ANALYST' then 1 else 0 end) nr_analist,
sum(case when a.job not in('ANALYST','MANAGER') then 1 else 0 end) rest
from emp a,dept d where a.deptno=d.deptno group by d.loc;

sau(p)

select d.loc,sum(decode(e.job,'MANAGER',1,0)) "NR MANAGERI",


sum(decode(e.job,'ANALYST',1,0)) "NR ANALISTI",
sum(case when e.job='MANAGER'or e.job='ANALYST'then 0 else 1 end) "NR ALTII",
count(e.empno) "TOTAL ANGAJATI"
from emp e,dept d where e.deptno=d.deptno group by d.loc;

3. Graficul salariului mediu al meseriilor distincte din fiecare localitate, reprezentând în fiecare linie
numele localităţii, meseria şi câte un caracter ‘* ‘ pentru fiecare 100 de unităţi salariale în ordinea
alfabetică a localităţilor şi meseriilor.
R :
select distinct d.loc,e.job, x.sal_med_mes salariu_mediu,lpad(' ',(x.sal_med_mes)/100+1,'*')
from emp e,dept d, (select loc,job,avg(sal) sal_med_mes from emp,dept where emp.deptno=dept.deptno
group by loc,job) x
where e.deptno=d.deptno and e.job=x.job and d.loc=x.loc order by d.loc, e.job;

4. Sefii care au fost angajaţi în acelaşi trimestru cu şefii lor.


R :
select ename from emp a where empno in (select distinct mgr from emp b)
and to_char(hiredate,'q') in (select to_char (hiredate,'q') from emp c
where a.mgr=c.empno and a.deptno=c.deptno);

sau(y)

select a.ename sef, b.ename supersef,a.hiredate,b.hiredate from emp a,emp b


where a.empno in (select mgr from emp) and a.mgr=b.empno and
to_char(a.hiredate,'q')=to_char(b.hiredate,'q');

Subiectul 7
1. Situaţia  : Nume_şef Nr_total_subord_direct Nr_şefi_subord Nr_subord ce nu sunt şefi
R :
select ename NUME_SEF,
(select count(*) aaa from emp c where c.mgr=a.empno) NR_TOTAL_SUBORD_DIRECT,
(select count(*) bbb from emp d where d.mgr=a.empno
and exists( select * from emp bb where bb.mgr=d.empno))NR_SEFI_SUBORD,
(select count(*) from emp e where e.mgr=a.empno
and not exists( select * from emp bbb where
bbb.mgr=e.empno))NR_SUBORD_CE_NU_SUNT_SEFI
from emp a where exists (select * from emp b where b.mgr=a.empno);

sau(profa)

select s.ename "Nume sef",(select count(*) from emp e where s.empno=e.mgr) Nr_Total_sub_direct,
(select count(*) from emp e where s.empno=e.mgr and e.empno in(select mgr from emp))
Nr_Sefi_Subordonati ,
(select count(*) from emp e where s.empno=e.mgr and not exists(select * from emp c where
e.empno=c.mgr)) Nr_Subordonati_ce_nu_sunt_sefi
from emp s where s.empno in(select mgr from emp);

sau(y)

select a.ename,count(b.ename) toti,


sum(case when b.empno in (select nvl(mgr,0) from emp) then 1 else 0 end) subalterni_sefi,
sum(case when b.empno not in (select nvl(mgr,0) from emp) then 1 else 0 end ) subalterni_nesefi
from emp a,emp b where a.empno in (select mgr from emp) and b.mgr=a.empno group by a.ename;

2. Numele zilei din săptămână, numărul angajaţilor încadraţi în ziua respectivă a săptămânii şi numele
angajatului angajat în ziua reapectivă a săptămânii care are cea mai mare vechime.
R :
select zi,nr_ang,a.ename,zi_ang
from emp a ,(select to_char(hiredate,'day') zi, count(*)nr_ang, min(hiredate) zi_ang
from emp group by to_char(hiredate,'day'))x
where to_char(a.hiredate,'day')=zi and hiredate=zi_ang;

sau(y)

select distinct zi, nr_ang,zi_ang,vechime from emp ,


(select count(*) nr_ang,to_char(hiredate,'day') zi,min(hiredate) zi_ang
from emp group by to_char(hiredate,'day')) x,
(select max( trunc(sysdate-hiredate)) vechime,(to_char(hiredate,'day')) ziua
from emp group by (to_char(hiredate,'day')))y where y.ziua=x.zi;

3. Angajaţii care au meseria şi comisionul identice cu meseria şi comisionul celor ce au salariul mai
mic decât maximul salariilor medii ale localităţilor.
R :
select a.ename,a.sal,a.comm from emp a
where (job,nvl(comm,-1)) in (select job, nvl(comm,-1)
from emp b where b.sal<(select max(avg(sal)) from emp c, dept
where c.deptno=dept.deptno group by loc));
4. Numele salariatului, localitatea, salariul mediu al localităţii, salariul minim al localităţii şi salariul
maxim al localităţii pentru localităţile în care lucrează cel puţin 4 angajaţi.
R :
select ename, d.loc,sal_med,sal_min,sal_max
from emp a, dept d,
(select loc,min(sal) sal_min,avg(sal) sal_med,max(sal) sal_max from emp b, dept e
where b.deptno=e.deptno group by e.loc having count(ename)>=4) c
where a.deptno=d.deptno and d.loc=c.loc group by d.loc,ename,sal_med,sal_min,sal_max;

sau(y)

select e.ename, d.loc, y.salar_max,y.salar_min,y.salar_med from emp e,dept d,


(select loc,max(sal) salar_max,min(sal) salar_min,avg(sal) salar_med from emp,dept
where emp.deptno=dept.deptno group by loc) y
where y.loc in (select loc from emp ,dept where emp.DEPTNO=dept.DEPTNO group by loc
having count(*)>4) and e.deptno=d.deptno and d.loc=y.loc;

Subiectul 8
1. Situaţia  : Localitatea Salar_med_gr_1 Salar_med_gr_2 Salar_med_gr_3 Salar_med_gr_4
Salar_med_gr_5 Salar_med_loc unde salariile medii afişate sunt salariile medii pe grupele
salariale în care se încadrează salariul angajaţilor din localiatea respectivă.
R :
select dd.loc,x.sal_med_gr1,y.sal_med_gr2,z.sal_med_gr3,q.sal_med_dept
from dept dd, (select avg(sal) sal_med_gr1, loc
from emp b, dept d, salgrade s
where b.deptno=d.deptno and b.sal between s.losal and s.HISAL
and grade=1
group by loc) x,
(select avg(sal) sal_med_gr2, loc
from emp b, dept d, salgrade s
where b.deptno=d.deptno and b.sal between s.losal and s.HISAL
and grade=2
group by loc)y,
(select avg(sal) sal_med_gr3, loc
from emp b, dept d, salgrade s
where b.deptno=d.deptno and b.sal between s.losal and s.HISAL
and grade=3
group by loc)z,
( select avg(sal) sal_med_dept,loc,ddd.deptno
from emp cc,dept ddd
where cc.deptno=ddd.DEPTNO
group by ddd.deptno,loc)q
where dd.deptno=q.deptno
group by dd.loc,x.sal_med_gr1,y.sal_med_gr2,z.sal_med_gr3,q.sal_med_dept;
2. Numele salariatului, salariul, localitatea şi salariul mediu al localităţii pentru acei salariaţi care s-au
angajat până cel târziu în a 200-a zi din an.
R :
select ename, sal, d.loc, x.sal_med
from emp a, dept d, (select avg(sal) sal_med,loc from emp b, dept c
where b.deptno=c.DEPTNO group by loc) x
where d.loc=x.loc and a.deptno=d.deptno and to_char(hiredate,'ddd')<200;

sau(y)

select ename,sal,d.loc,y.sal_med from emp,dept d,


(select loc,avg(sal) sal_med from emp,dept where emp.deptno=dept.deptno group by loc) y
where emp.deptno=d.deptno and y.loc=d.loc and to_char(hiredate,'ddd')<200;

3. Numele angajatului, meseria, salariul şi salariul mediu al meseriei pentru cei ce au salariul mai
mare decât maximul salariilor medii ale meseriilor.
R :
select a.ename, a.job, a.sal, x.sal_med_job from emp a,
(select avg(b.sal) sal_med_job, b.job from emp b group by b.job)x
where a.job=x.job and a.sal>(select max(avg(b.sal)) from emp b group by b.job);

sau(y)

select e.ename,e.job, e.sal,y.sal_med from emp e,


(select job,avg(sal) sal_med from emp group by job) y,
(select max(avg(sal)) sal_maxim from emp group by job) z
where e.job=y.job and e.sal>z.sal_maxim;

4. Numele departamentului şi numărul mediu de luni lucrate de salariaţi în departament.


R :
select d.dname NUME,avg(months_between(sysdate,hiredate))NR_MED_LUNI_LUCR
from emp e,dept d
where d.deptno=e.deptno group by d.dname;

Subiectul 9
1. Situaţia  : Nume_dep Salar_med_gr_1 Salar_med_gr_2 Salar_med_gr_3 Salar_med_gr_4
Salar_med_gr_5 Salar_med_dep unde salariile medii afişate sunt salariile medii pe grupele
salariale în care se încadrează salariul angajaţilor din departamentul respectiv.
R :
select dd.dname,x.sal_med_gr1,y.sal_med_gr2,z.sal_med_gr3,xx.sal_med_gr4,yy.sal_med_gr5,
q.sal_med_dept
from dept dd, (select avg(sal) sal_med_gr1, loc
from emp b, dept d, salgrade s
where b.deptno=d.deptno and b.sal between s.losal and s.HISAL
and grade=1 group by loc) x,
(select avg(sal) sal_med_gr2, loc
from emp b, dept d, salgrade s
where b.deptno=d.deptno and b.sal between s.losal and s.HISAL
and grade=2 group by loc)y,
(select avg(sal) sal_med_gr3, loc
from emp b, dept d, salgrade s
where b.deptno=d.deptno and b.sal between s.losal and s.HISAL
and grade=3 group by loc)z,
(select avg(sal) sal_med_gr4, loc
from emp b, dept d, salgrade s
where b.deptno=d.deptno and b.sal between s.losal and s.HISAL
and grade=4 group by loc) xx,
(select avg(sal) sal_med_gr5, loc
from emp b, dept d, salgrade s
where b.deptno=d.deptno and b.sal between s.losal and s.HISAL
and grade=5 group by loc) yy,
(select avg(sal) sal_med_dept,loc,ddd.deptno
from emp cc,dept ddd
where cc.deptno=ddd.DEPTNO
group by ddd.deptno,loc)q
where dd.deptno=q.deptno
group by dd.dname, x.sal_med_gr1, y.sal_med_gr2,z.sal_med_gr3, xx.sal_med_gr4,yy.sal_med_gr5,
q.sal_med_dept;

2. Numele angajatului, salariul, grupa salarială în care se află salariul, salariul majorat cu 40%, grupa
salarială în care se află salariul majorat, pentru cei care prin majorarea salariului ajung într-o grupă
salarială cu 2 faţă de aceea în care au fost iniţial.
R :
select ename, sal, s.grade, sal*1.4, b.grade
from emp a, salgrade s, salgrade b
where a.sal between s.losal and s.hisal
and sal*1.4 between b.losal and b.hisal and b.grade-s.grade=2;

sau(y)

select ename, sal, s.grade, sal+sal*40/100 sal_majorat, t.grade from emp,salgrade s,salgrade t
where (sal between s.losal and s.hisal) and ((sal+0.4*sal) between t.losal and t.hisal)
and (s.grade=t.grade-2);

3. Numele salariatului, meseria şi salariul mediu al meseriei pentru acei salariaţi care s-au angajat
până cel târziu în a 40-a săptămână din an.
R :
select ename, a.job, x.sal_med
from emp a,(select avg(sal) sal_med,job from emp group by job) x
where a.job=x.job and to_char(hiredate,'ww')<=40;

sau(y)

select e.ename, e.job, x.sal_med from emp e,


(select job,avg(sal) sal_med from emp group by job) x
where (e.job=x.job) and to_char(hiredate,'ww')<40;
4. Numele departamentului şi numărul mediu de săptămâni lucrate de salariaţii din departament în
anul 1981.
R :
select dname, x.nr_med from emp a, dept d ,
(select avg(52-to_char(hiredate,'ww'))nr_med, deptno from emp c
where to_char(hiredate,'yy')=81 group by deptno)x
where a.deptno=d.deptno and x.deptno=a.deptno group by d.deptno,dname,x.nr_med;

Subiectul 10
1. Situaţia  : Luna_angajării Salar_med_gr_1 Salar_med_gr_2 Salar_med_gr_3
Salar_med_gr_4 Salar_med_gr_5 unde salariile medii afişate sunt salariile medii pe grupele
salariale în care se încadrează salariul angajaţilor din luna respectivă.
R :
select distinct to_char(hiredate,'Mon') Luna_angajarii ,x.sal_med_gr1, y.sal_med_gr2, z.sal_med_gr3,
xx.sal_med_gr4, yy.sal_med_gr5
from emp dd, (select avg(sal) sal_med_gr1, to_char(hiredate,'MON')
from emp b, salgrade s
where b.sal between s.losal and s.HISAL and grade=1 group by to_char(hiredate,'MON')) x,
(select avg(sal) sal_med_gr2, to_char(hiredate,'MON') from emp b, salgrade s
where b.sal between s.losal and s.HISAL and grade=2 group by to_char(hiredate,'MON')) y,
(select avg(sal) sal_med_gr3, to_char(hiredate,'MON') from emp b, salgrade s
where b.sal between s.losal and s.HISAL and grade=3 group by to_char(hiredate,'MON')) z,
(select avg(sal) sal_med_gr4, to_char(hiredate,'MON') from emp b, salgrade s
where b.sal between s.losal and s.HISAL and grade=4 group by to_char(hiredate,'MON')) xx,
(select avg(sal) sal_med_gr5, to_char(hiredate,'MON') from emp b, salgrade s
where b.sal between s.losal and s.HISAL and grade=5 group by to_char(hiredate,'MON')) yy
group by to_char(hiredate,'Mon'), x.sal_med_gr1, y.sal_med_gr2, z.sal_med_gr3,
xx.sal_med_gr4, yy.sal_med_gr5;

(sau)

select distinct to_char(hiredate,'Mon') y,


decode(x.g,1,x.sal_med_sub,NULL) GR_1,
decode(x.g,2,x.sal_med_sub,NULL) GR_2,
decode(x.g,3,x.sal_med_sub,NULL) GR_3,
decode(x.g,4,x.sal_med_sub,NULL) GR_4,
decode(x.g,5,x.sal_med_sub,NULL) GR_5
from emp a, (select avg(sal) sal_med_sub,to_char(hiredate,'Mon') v,grade g
from emp c, salgrade s where c.sal between losal and hisal group by
to_char(hiredate,'Mon'),grade) x
where to_char(hiredate,'Mon')=x.v;

2. Graficul salariului mediu al departamentelor, reprezentând în fiecare linie numele departamentului


şi câte un caracter ‘*  ‘ pentru fiecare 100 de unităţi salariale în ordinea alfabetică a departamentelor.
R :
select RPAD(dname,avg(sal)/100,'*')"Departament | Grafic " from emp a, dept d
where a.deptno=d.deptno group by dname;
3. Localitatea şi meseria şi numărul mediu de zile lucrate de salariaţi în anul 1981.
R :
select loc, job, x.nr_mediu_zile
from dept d,emp a, (select avg(52-to_char(hiredate,'ww'))nr_mediu_zile from emp )x
where a.deptno=d.deptno and to_char(hiredate,'yy')=81 group by loc,job,x.nr_mediu_zile;

4. Numele şefului, salariul minim al subordonaţilor săi, numele subordonatului cu salariul minim.
R :
select a.ename, x.min_sal,b.ename from emp a,emp b,
(select min(sal) min_sal ,mgr from emp c group by mgr)x
where exists (select * from emp b where a.empno=b.mgr)
and x.mgr=a.empno and a.deptno=b.deptno and b.sal=x.min_sal
group by a.mgr,a.ename,x.min_sal,b.ename;

Subiectul 11
1. Situaţia  : Anul_angajării Salar_med_gr_1 Salar_med_gr_2 Salar_med_gr_3 Salar_med_gr_4
Salar_med_gr_5 unde salariile medii afişate sunt salariile medii pe grupele salariale în care se
încadrează salariul angajaţilor din anul respectiv.
R :
select distinct to_char(hiredate,'YYYY') y,
decode(x.g,1,x.sal_med_sub,NULL) GR_1,
decode(x.g,2,x.sal_med_sub,NULL) GR_2,
decode(x.g,3,x.sal_med_sub,NULL) GR_3,
decode(x.g,4,x.sal_med_sub,NULL) GR_4,
decode(x.g,5,x.sal_med_sub,NULL) GR_5
from emp a, (select avg(sal) sal_med_sub,to_char(hiredate,'YYYY') v,grade g
from emp c, salgrade s where c.sal between losal and hisal group by
to_char(hiredate,'YYYY'),grade) x
where to_char(hiredate,'YYYY')=x.v;

(sau)

select to_char(hiredate,'YYYY') Anul_angajarii ,x.sal_med_gr1, y.sal_med_gr2, z.sal_med_gr3,


xx.sal_med_gr4, yy.sal_med_gr5
from emp dd, (select avg(sal) sal_med_gr1, to_char(hiredate,'YYYY')
from emp b, salgrade s
where b.sal between s.losal and s.hisal and grade=1 group by to_char(hiredate,'YYYY')) x,
(select avg(sal) sal_med_gr2, to_char(hiredate,'YYYY') from emp b, salgrade s
where b.sal between s.losal and s.hisal and grade=2 group by to_char(hiredate,'YYYY')) y,
(select avg(sal) sal_med_gr3, to_char(hiredate,'YYYY') from emp b, salgrade s
where b.sal between s.losal and s.hisal and grade=3 group by to_char(hiredate,'YYYY')) z,
(select avg(sal) sal_med_gr4, to_char(hiredate,'YYYY') from emp b, salgrade s
where b.sal between s.losal and s.hisal and grade=4 group by to_char(hiredate,'YYYY')) xx,
(select avg(sal) sal_med_gr5, to_char(hiredate,'YYYY') from emp b, salgrade s
where b.sal between s.losal and s.hisal and grade=5 group by to_char(hiredate,'YYYY')) yy
group by to_char(hiredate,'YYYY'), x.sal_med_gr1, y.sal_med_gr2, z.sal_med_gr3, xx.sal_med_gr4,
yy.sal_med_gr5;
2. Numele angajatului, meseria şi comisionul pentru cei ce au meseria şi comisionul identice cu ale
vreunui angajat din departamentul cu cel mai mic număr de angajaţi.
R :
select ename, job, comm from emp a
where (job, nvl(comm,-1)) in (select job, nvl(comm,-1) from emp b
where deptno=(select deptno from emp c
group by deptno
having count(*) in (select min(count(*)) from emp d
group by deptno)));

3. Numele şefului, salariul mediu al subordonaţilor.


R :
select ename, (select avg(sal) from emp c where c.mgr=a.empno) sal_mediu_subord
from emp a where exists (select * from emp b where a.empno=b.mgr);

4. Graficul salariului pentru angajaţii de pe nivelul 3 al ierarhiei, constând în precizarea pe fiecare linie
a numelui angajatului şi a câte unui caracter ‘$’ pentru fiecare 100 de unităţi salariale.
R :
select RPAD(ename,sal/100,'$') "Nume | Grafic" from emp where level=3
CONNECT BY PRIOR empno = mgr
START WITH mgr IS NULL;

Subiectul 12
1. Situaţia  : Trimestrul_angajării Salar_med_gr_1 Salar_med_gr_2 Salar_med_gr_3
Salar_med_gr_4 Salar_med_gr_5 unde salariile medii afişate sunt salariile medii pe grupele
salariale în care se încadrează salariul angajaţilor din trimestrul respectiv.
R :
select distinct to_char(hiredate,'Q') y,
decode(x.g,1,x.sal_med_sub,NULL) GR_1,
decode(x.g,2,x.sal_med_sub,NULL) GR_2,
decode(x.g,3,x.sal_med_sub,NULL) GR_3,
decode(x.g,4,x.sal_med_sub,NULL) GR_4,
decode(x.g,5,x.sal_med_sub,NULL) GR_5
from emp a, (select avg(sal) sal_med_sub,to_char(hiredate,'Q') v,grade g from emp c, salgrade s
where c.sal between losal and hisal group by to_char(hiredate,'Q'),grade) x
where to_char(hiredate,'Q')=x.v;

(sau)

select to_char(hiredate,'Q') Trimestrul_angajarii ,x.sal_med_gr1, y.sal_med_gr2, z.sal_med_gr3,


xx.sal_med_gr4, yy.sal_med_gr5
from emp dd, (select avg(sal) sal_med_gr1, to_char(hiredate,'Q')
from emp b, salgrade s
where b.sal between s.losal and s.hisal and grade=1 group by to_char(hiredate,'Q')) x,
(select avg(sal) sal_med_gr2, to_char(hiredate,'Q') from emp b, salgrade s
where b.sal between s.losal and s.hisal and grade=2 group by to_char(hiredate,'Q')) y,
(select avg(sal) sal_med_gr3, to_char(hiredate,'Q') from emp b, salgrade s
where b.sal between s.losal and s.hisal and grade=3 group by to_char(hiredate,'Q')) z,
(select avg(sal) sal_med_gr4, to_char(hiredate,'Q') from emp b, salgrade s
where b.sal between s.losal and s.hisal and grade=4 group by to_char(hiredate,'Q')) xx,
(select avg(sal) sal_med_gr5, to_char(hiredate,'Q') from emp b, salgrade s
where b.sal between s.losal and s.hisal and grade=5 group by to_char(hiredate,'Q')) yy
group by to_char(hiredate,'Q'), x.sal_med_gr1, y.sal_med_gr2, z.sal_med_gr3, xx.sal_med_gr4,
yy.sal_med_gr5;

2. Numele departamentului şi numărul mediu de zile lucrate în fiecare departament în anul 1981, dacă
numărul mediu de zile lucrate este peste 180.
R :
select dname, x.avg_zile
from dept d , (select avg((52-to_char(hiredate,'ww'))*7)avg_zile, deptno
from emp b where to_char(hiredate,'yyyy')=1981 group by deptno)x
where x.avg_zile>180 and d.deptno=x.deptno
group by dname,x.avg_zile;

3. Numele şefului, salariul maxim al subordonaţilor, subordonatul care are salariul maxim.
R :
select a.ename,x.sal_max, b.ename
from emp a,(select max(sal) sal_max,mgr from emp c group by mgr)x ,emp b
where exists (select * from emp b where a.empno=b.mgr) and b.sal=x.sal_max and x.mgr=a.empno;

4. Graficul salariului pentru angajaţii de pe nivelul 2 al ierarhiei, constând în precizarea pe fiecare linie
a numelui angajatului şi a câte unui caracter ‘$’ pentru fiecare 100 de unităţi salariale.
R :
select ename, RPAD(' ',sal/100,'*') from emp
where level=2
connect by prior empno=mgr
start with mgr is null;

Subiectul 13
1. Situaţia  : Nume şef Salar_med_gr_1 Salar_med_gr_2 Salar_med_gr_3 Salar_med_gr_4
Salar_med_gr_5 unde salariile medii afişate sunt salariile medii ale subordonaţilor în funcţie de
grupa salarială în care se încadrează salariul acestora.
R :
select ename,decode(x.g,1,x.sal_med_sub,NULL) GR_1,
decode(x.g,2,x.sal_med_sub,NULL) GR_2,
decode(x.g,3,x.sal_med_sub,NULL) GR_3,
decode(x.g,4,x.sal_med_sub,NULL) GR_4,
decode(x.g,5,x.sal_med_sub,NULL) GR_5
from emp a, (select avg(sal) sal_med_sub,mgr,grade g from emp c, salgrade s
where c.sal between losal and hisal group by mgr,grade)x
where exists (select * from emp b where a.empno=b.mgr) and a.empno=x.mgr;

2. Trimestrul, numărul angajaţilor încadraţi în trimestrul respectiv şi numele angajatului încadrat în


trimestrul reapectiv care are cea mai mare vechime.
R:
select rpad(to_char(hiredate,'Q'),10) Semestrul, Nr_angajati, rpad(ename,30)
Angajat_cu_vechime_maxima
from emp,(select to_char(hiredate,'Q') aaaa,count(*) nr_angajati from emp
group by to_char(hiredate,'Q'))
where to_char(hiredate,'Q')=aaaa and sysdate-hiredate in (select max(sysdate-hiredate) from emp
group by to_char(hiredate,'Q')) order by to_char(hiredate,'Q');

3. Graficul salariului mediu al grupelor salariale, reprezentând în fiecare linie numărul grupei
salariale, salariul minim din grupă, salariul maxim din grupă şi câte un caracter ‘*  ‘ pentru fiecare
100 de unităţi salariale în ordinea grupelor salariale.

select grade, min(sal), max(sal), lpad(' ',avg(sal)/100,'*') from emp, salgrade


where sal between losal and hisal
group by grade
order by grade;
4. Numele angajatului, departamentul, salariul şi salariul mediu al departamentului pentru cei ce au
salariul mai mare decât maximul salariilor medii ale departamentelor.
select e.ename,d.dname,e.sal, x.sal_med from emp e natural join dept d,
(select dname nume_dep,avg(sal) sal_med from emp natural join dept group by dname) x
where dname=x.nume_dep and sal>(select max(avg(sal)) from emp natural join dept group by dname);

Subiectul 14
1. Situaţia  : Localitatea Nr_ang_gr_1 Nr_ang gr_2 Nr_ang gr_3 Nr_ang gr_4 Nr_ang gr_5
Nr_ang loc unde numărul angajaţilor afişat reprezintă numărul de angajaţi pe grupele salariale în
care se încadrează salariul angajaţilor din localiatea respectivă.
R :
select loc Localitate, sum(decode(grade,1,1,0)) Nr_gr_1,
sum(decode(grade,2,1,0)) Nr_gr_2,
sum(decode(grade,3,1,0)) Nr_gr_3,
sum(decode(grade,4,1,0)) Nr_gr_4,
sum(decode(grade,5,1,0)) Nr_gr_5,
count(*) Nr_ang_loc
from emp,dept,salgrade
where emp.deptno=dept.deptno and sal between losal and hisal
group by loc;

2. Numărul şefilor.
R :
select count(distinct mgr) from emp;

3. Numele salariatului, salariul, grupa salarială şi salariul mediu al grupei salariale pentru acei
salariaţi care s-au angajat în zilele de luni, marţi, miercuri şi joi şi în săptămânile pare din an.
R :
select ename, sal, x.grade, salmedgr
from emp,salgrade,
(select grade,avg(sal) salmedgr from emp,salgrade where sal between losal and hisal group by grade) x
where sal between losal and hisal and salgrade.grade=x.grade and
to_char(hiredate,'day') in ('luni','marti','miercuri','joi') and
mod(to_number(to_char(hiredate,'WW')),2)=0;

4. Graficul salariului pentru angajaţii de pe ultimul nivel al ierarhiei, constând în precizarea pe fiecare
linie a numelui angajatului şi a câte unui caracter ‘$’ pentru fiecare 100 de unităţi salariale.
R :
select rpad(ename,10)||rpad(' ',sal/100+1,'$') Grafic
from emp
where level=(select max(level) from emp
connect by prior empno=mgr start with mgr is NULL)
connect by prior empno=mgr
start with mgr is NULL;

Subiectul 15
1. Situaţia  : Nume_dep Nr_ang_gr_1 Nr_ang gr_2 Nr_ang gr_3 Nr_ang gr_4 Nr_ang gr_5
Nr_ang dep unde numărul angajaţilor afişat reprezintă numărul de angajaţi pe grupele salariale în
care se încadrează salariul angajaţilor din departamentul respectiv.
R: select dname Departament, sum(decode(grade,1,1,0)) Nr_gr_1,
sum(decode(grade,2,1,0)) Nr_gr_2,
sum(decode(grade,3,1,0)) Nr_gr_3,
sum(decode(grade,4,1,0)) Nr_gr_4,
sum(decode(grade,5,1,0)) Nr_gr_5,
count(*) Nr_ang_dept
from emp,dept,salgrade
where emp.deptno=dept.deptno and sal between losal and hisal
group by dname;

2. Numele angajatului, meseria, salariul şi maximul salariilor minime ale departamentelor, pentru
angajaţii care au salariul mai mare decât maximul salariilor minime ale departamentelor.
select e.ename,e.job,e.sal,x.minsal from emp e,
(select deptno,min(sal) minsal from emp group by deptno
having min(sal)>=all(select min(sal) minsal from emp group by deptno)) x
where e.deptno=x.deptno and sal>x.minsal;

3. Numele şefului, salariul mediu al subordonaţilor, graficul salariului mediu al subordonaţilor


constând din reprezentarea pentru fiecare 100 de unităţi salariale a unui caracter $.

R: select a.ename, b.salmin,lpad(' ',b.salmin/100,'*') from emp a,


(select x.empno numar, min(y.sal) salmin from emp x,emp y where y.mgr=x.empno group by x.empno)
b
where a.empno=b.numar
4. Angajaţii care au aceleaşi număr şef şi meserie cu a celor ce au aceiaşi grupă salariala cu vreunul
din subordonaţii lor.

select ename
from emp
where (mgr,job) in (select mgr,job
from emp a , salgrade s
where a.sal between s.losal and s.hisal
and grade in (select grade
from emp b,salgrade ss
where a.empno=b.mgr
and b.sal between s.losal and s.hisal));

Subiectul 16
1. Situaţia  : Luna_angajării Nr_ang_gr_1 Nr_ang_gr_2 Nr_ang_gr_3 Nr_ang_gr_4
Nr_ang_gr_5 unde numărul angajaţilor afişat reprezintă numărul de angajaţi pe grupele salariale
în care se încadrează salariul angajaţilor din luna respectivă.
R :
select to_char(hiredate,'month') Luna, sum(decode(grade,1,1,0)) Nr_gr_1,
sum(decode(grade,2,1,0)) Nr_gr_2,sum(decode(grade,3,1,0)) Nr_gr_3,
sum(decode(grade,4,1,0)) Nr_gr_4,sum(decode(grade,5,1,0)) Nr_gr_5
from emp,salgrade
where sal between losal and hisal
group by to_char(hiredate,'month');

2. Numele angajatului, salariul, grupa salarială în care se află salariul, salariul majorat cu 60%, grupa
salarială în care se află salariul majorat, pentru cei care prin majorarea salariului ajung într-o grupă
salarială cu 3 faţă de aceea în care au fost iniţial.
R :
select ename,sal,a.grade,sal*1.6,b.grade
from emp,salgrade a,salgrade b
where sal between a.losal and a.hisal and
sal*1.6 between b.losal and b.hisal and
b.grade=a.grade+2;

3. Numele şefului, salariul minim al subordonaţilor, graficul salariului minim al subordonaţilor


constând din reprezentarea pentru fiecare 100 de unităţi salariale a unui caracter $.
R :
select rpad(b.ename,10)||rpad(min(a.sal),30) ||x.Grafic
from emp a, emp b,(select rpad(' ',min(a.sal)/100+1,'$') Grafic from emp a,emp b ) x
where a.mgr=b.empno
group by b.ename,x.Grafic;

4. Angajaţii care au aceleaşi comision şi meserie cu a celor ce au aceiaşi grupă salariala cu vreunul din
subordonaţii lor.
R :
select distinct b.ename
from emp a, emp b
where a.mgr=b.empno and nvl(b.comm,-1)=nvl(a.comm,-1) and b.job=a.job;

Subiectul 17
1. Situaţia  : Anul_angajării Nr_ang_gr_1 Nr_ang_gr_2 Nr_ang_gr_3 Nr_ang_gr_4
Nr_ang_gr_5 unde numărul angajaţilor afişat reprezintă numărul de angajaţi pe grupele salariale
în care se încadrează salariul angajaţilor din anul respectiv.
R :
select to_char(hiredate,'yyyy') Luna, sum(decode(grade,1,1,0)) Nr_gr_1,
sum(decode(grade,2,1,0)) Nr_gr_2,sum(decode(grade,3,1,0)) Nr_gr_3,
sum(decode(grade,4,1,0)) Nr_gr_4,sum(decode(grade,5,1,0)) Nr_gr_5
from emp,salgrade
where sal between losal and hisal
group by to_char(hiredate,'yyyy');

2. Numele angajatului, numele departamentului şi comisionul pentru cei ce au departamentul şi


comisionul identice cu ale vreunui angajat din localitatea cu cel mai mare număr de angajaţi
R :
select ename,dname,comm
from emp,dept
where emp.deptno=dept.deptno and
(emp.deptno,nvl(comm,-1)) = any (select emp.deptno,nvl(comm,-1) from emp,dept,
(select max(count(*)) maxnrang from emp,dept where emp.deptno=dept.deptno group by loc) x,
(select loc,count(*) nrang from emp,dept where emp.deptno=dept.deptno group by loc) y
where emp.deptno=dept.deptno and dept.loc=y.loc and nrang=maxnrang);

3. Graficul salariului mediu al anilor în care s-au produs angajări, reprezentând în fiecare linie anul şi
câte un caracter ‘*  ‘ pentru fiecare 100 de unităţi salariale în ordinea descrescătoare a anilor.
R :
select lpad(to_char(hiredate,'yyyy'),10)||' '||lpad(' ',trunc(avg(sal)/100,0),'*') Grafic
from emp
group by to_char(hiredate,'yyyy')
order by to_char(hiredate,'yyyy') desc;

4. Numele şefului şi numărul de subordonaţi ai acestuia.


R :
select b.ename Nume_sef, count(*) Nr_subordonati
from emp a,emp b
where a.mgr=b.empno
group by b.ename;

Subiectul 18
1. Situaţia  : Trimestrul_angajării Nr_ang_gr_1 Nr_ang_gr_2 Nr_ang_gr_3 Nr_ang_gr_4
Nr_ang_gr_5 unde numărul angajaţilor afişat reprezintă numărul de angajaţi pe grupele salariale
în care se încadrează salariul angajaţilor din trimestrul respectiv.
R :
select to_char(hiredate,'Q') Trimestrul, sum(decode(grade,1,1,0)) Nr_gr_1,
sum(decode(grade,2,1,0)) Nr_gr_2,sum(decode(grade,3,1,0)) Nr_gr_3,
sum(decode(grade,4,1,0)) Nr_gr_4,sum(decode(grade,5,1,0)) Nr_gr_5
from emp,salgrade
where sal between losal and hisal
group by to_char(hiredate,'Q');

2. Numele angajatului, minimul salariului maxim al localitaţilor, salariul angajatului, maximul


salariului maxim al localităţilor, pentru cei ce au salariul cuprins între minimul salariului maxim al
localitaţilor şi maximul salariului maxim al localităţilor.
R :
select ename,minmaxloc,sal,maxmaxloc
from emp, (select min(max(sal)) minmaxloc, max(max(sal)) maxmaxloc from emp,dept where
emp.deptno=dept.deptno group by loc) x
where sal between minmaxloc and maxmaxloc;

3. Anul, numărul angajaţilor încadraţi în anul respectiv şi numele angajatului încadrat în anul
reapectiv care are cea mai mare vechime.
R :
select to_char(hiredate,'yyyy') Anul, Nr_angajati, ename Angajat_cu_vechime_maxima
from emp,(select to_char(hiredate,'yyyy') aaaa,count(*) nr_angajati from emp
group by to_char(hiredate,'yyyy'))
where to_char(hiredate,'yyyy')=aaaa and sysdate-hiredate in (select max(sysdate-hiredate) from emp
group by to_char(hiredate,'yyyy'));

4. Minimul salariilor medii ale grupelor salariale.


R :
select min(avg(sal)) from emp,salgrade where sal between losal and hisal group by grade;

Subiectul 19
1. Situaţia  : Nume şef Nr_ang_gr_1 Nr_ang_gr_2 Nr_ang_gr_3 Nr_ang_gr_4 Nr_ang_gr_5
unde numărul angajaţilor afişat reprezintă numărul subordonaţilor în funcţie de grupa salarială în
care se încadrează salariul acestora.
R :
select b.ename Sef, sum(decode(grade,1,1,0)) Nr_gr_1,
sum(decode(grade,2,1,0)) Nr_gr_2,sum(decode(grade,3,1,0)) Nr_gr_3,
sum(decode(grade,4,1,0)) Nr_gr_4,sum(decode(grade,5,1,0)) Nr_gr_5
from emp a,emp b,salgrade
where a.mgr=b.empno and a.sal between losal and hisal
group by b.ename;

sau(profa)

select s.ename "Nume Sef",


sum(decode(grade,1,1,0)) "Ang_gr1",
sum(decode(grade,2,1,0)) "Ang_gr2",
sum(decode(grade,3,1,0)) "Ang_gr3",
sum(decode(grade,4,1,0)) "Ang_gr4",
sum(decode(grade,5,1,0)) "Ang_gr5"
from emp s,emp a,salgrade
where s.empno=a.mgr and a.sal between losal and hisal group by s.ename;

2. Numele salariatului, data angajării, salariul şi salariul mediu al anului în care s-a angajat pentru
acei salariaţi care s-au angajat în zilele de vineri sau sâmbătă şi în săptămânile impare din an.
R :
select ename,hiredate,sal,salmedan
from emp, (select to_char(hiredate,'yyyy') aaaa, avg(sal) salmedan from emp group by
to_char(hiredate,'yyyy')) x
where to_char(hiredate,'yyyy')=aaaa and
to_char(hiredate,'d') in (5,6) and mod (to_char(hiredate,'ww'),2)=0;

sau(profa)

select a.ename,a.hiredate,a.sal,x,salmediu
from emp a,(select to_char(hiredate,'YYYY')an,avg(sal) salmediu
from emp a group by to_char(hiredate,'YYYY')) x
where to_char(a.hiredate,'YYYY')=x.an
and to_char(a.hiredate,'d')in (5,6) and mod (to_char(a.hiredate,'ww'),2)<>0;

3. Numele şefului, salariul maxim al subordonaţilor, graficul salariului maxim al subordonaţilor


constând din reprezentarea pentru fiecare 100 de unităţi salariale a unui caracter $.
R:

4. Angajaţii care au aceleaşi comision şi localitate cu a celor ce au aceiaşi grupă salariala cu vreunul
din subordonaţii lor.

Subiectul 20
1. Situaţia  : Numele _zilei Nr_sefi Salar_mediu_sefi unde în coloana referitoare la numărul de
şefi se afişează numărul şefilor încadraţi în ziua respectivă, iar în coloana aferentă salariului mediu
se repreazintă salariul mediu al şefilor încadraţi în ziua respectivă.

R :

select to_char(hiredate,'day') "Ziua",count(*) "Numar Sefi",avg(sal) "Salar Mediu" from emp where
empno in (select mgr from emp) group by to_char(hiredate,'day');

2. Numele salariatului, departamentul, salariul mediu al departamentului, salariul minim al


departamentului şi salariul maxim al departamentului pentru departamentele în care lucrează cel
puţin 3 persoane angajate în aceiaşi lună.
R:
select ename, dname, avg(sal), min(sal), max(sal)
from emp, dept
where emp.deptno=dept.deptno
group by dname,ename
having dname in(select dname
from emp b, dept e
where b.deptno=e.DEPTNO
group by dname
having count(ename)>=3);

3. Angajaţii care au aceleaşi comision şi departament cu a celor ce au aceiaşi grupă salariala cu


vreunul din subordonaţii lor.

select ename,dname from emp natural join dept where


( nvl(comm,-1),dname) in
(select nvl(a.comm,-1),dname from emp a,emp b,salgrade s,salgrade t,dept
where a.empno=b.mgr and a.empno=dept.deptno
and (a.sal between s.losal and s.hisal)
and (b.sal between t.losal and t.hisal)
and s.grade=t.grade);

4. Numele şefului, salariul maxim al subordonaţilor, numele subordonatului care are salariul maxim.
select e.ename e, x.sal_max,f.ename from emp e,emp f,
(select a.empno numar, max(b.sal)sal_max from emp a,emp b
where b.mgr=a.empno group by a.empno) x
where x.numar=e.empno and f.mgr=e.empno and f.sal=x.sal_max;

Subiectul 21
1. Situaţia : Trimestrul Nr_sefi Total salarii_sefi unde în coloana referitoare la numărul de şefi se
afişează numărul şefilor încadraţi în trimestrul respectiv, iar în coloana aferentă salariului total se
repreazintă suma salariilor şefilor încadraţi în trimestrul respectiv.
SELECT TO_CHAR(hiredate,'Q') "Trimestrul", COUNT(empno)"Nr sefi", SUM(e.sal)"Total sal sefi"
FROM emp e
WHERE e.empno IN (SELECT DISTINCT mgr FROM emp x)
GROUP BY TO_CHAR(hiredate, 'Q');
2.Numele angajatului, localitatea, salariul şi salariul minim al localităţii, salariul maxim al localităţii şi
salariul mediu al localităţii pentru cei ce au salariul mai mare decât media salariilor minime ale
localităţilor.
select a.ename,d.loc,a.sal,b.sal_min,b.sal_max, b.sal_med
from emp a,dept d,(select y.loc localit, min(sal) sal_min,max(sal) sal_max, AVG(sal) sal_med
from emp x, dept y
where x.deptno=y.deptno
group by y.loc)b
where a.deptno=d.deptno and b.localit=d.loc
having a.sal>AVG(b.sal_min)
group by a.ename,d.loc,b.sal_min,b.sal_max,b.sal_med,a.sal;
3. Angajaţii care au aceleaşi comision şi lună a angajării cu a celor ce au aceiaşi grupă salariala cu
vreunul din subordonaţii lor
select a.ename "Nume", NVL(a.comm,0) "Comision", TO_CHAR(a.hiredate,'MONTH') "Luna ang"
from emp a
where (NVL(a.comm,-1),TO_CHAR(a.hiredate,'MONTH'))IN(select NVL(s.comm,-
1),TO_CHAR(s.hiredate,'MONTH')
from emp s,emp e,salgrade x,salgrade y
where s.empno=e.mgr
and s.sal between x.losal and x.hisal
and e.sal between y.losal and y.hisal
and y.grade=x.grade);

Subiectul 22
1. Situaţia : Grupa salarială 1980-1982 1983-1985 din 1986 unde pentru fiecare grupă salarială se
afişează numărul salariaţilor care au salariul în grupa respectivă separaţi în perioadele specificate în
antetele coloanelor afişate
select d.grade "Grupa Salariala",Sum(case when to_char(e.hiredate,'yyyy')=1980 or
to_char(e.hiredate,'yyyy')=1981
or to_char(e.hiredate,'yyyy')=1982
then 1 else 0 end) "1980-1982",
Sum(case when to_char(e.hiredate,'yyyy')=1983 or to_char(e.hiredate,'yyyy')=1984
or to_char(e.hiredate,'yyyy')=1985
then 1 else 0 end) "1983-1985",
Sum(case when to_char(e.hiredate,'yyyy')>1986 then 1 else 0 end) "din 1986"
from emp e,salgrade d
where e.sal between d.losal and d.hisal
group by d.grade;
2. Graficul salariului mediu al subordonaţilor unui şef, reprezentând în fiecare linie numele şefului şi câte
un caracter ‘* ‘ pentru fiecare 100 de unităţi salariale în ordinea alfabetică a şefilor
select RPAD(b.nume_sef,10) ||RPAD(' ',b.sal_med_subord/100+1,'*') GRAFIC
from (select x.ename nume_sef,AVG(s.sal)sal_med_subord
from emp x,emp s
where x.empno=s.mgr
group by x.ename)b;
3. Numele salariatului, numărul departamentului, numele departamentului şi salariul mediu al
departamentului pentru acei salariaţi care s-au angajat în zilele de luni, marţi sau joi şi în zilele pare din
an.
select e.ename,e.deptno,d.dname,x.sal_med
from emp e,dept d,(select deptno,avg(sal) sal_med from emp group by deptno)x,
(select empno from emp where to_char(hiredate,'D')=1
OR to_char(hiredate,'D')=2 OR
to_char(hiredate,'D')=4
AND MOD(to_char(hiredate,'DDD'),2)=1)y
where e.deptno=d.deptno and x.deptno=d.deptno and e.empno=y.empno;
4. Angajaţii care au aceleaşi comision şi an al angajării cu a celor ce au aceiaşi grupă salariala cu vreunul
din subordonaţii lor.
select a.ename "Nume", NVL(a.comm,0) "Comision", TO_CHAR(a.hiredate,'YYYY') "Anul ang"
from emp a
where (NVL(a.comm,-1),TO_CHAR(a.hiredate,'YYYY'))IN(select NVL(s.comm,-
1),TO_CHAR(s.hiredate,'YYYY')
from emp s,emp e,salgrade x,salgrade y
where s.empno=e.mgr
and s.sal between x.losal and x.hisal
and e.sal between y.losal and y.hisal
and y.grade=x.grade);

Subiectul 23
1.Numele salariatului, meseria, salariul mediu al meseriei, salariul minim al meseriei şi salariul maxim al
meseriei pentru meseriile în care lucrează cel puţin 2 persoane angajate în acelaşi trimestru.
NU E BINE :
select e.ename, e.job, avg(e.sal)sal_med_job, min(e.sal)sal_min_job, max(e.sal)sal_max_job
from emp e
having 2<=(select count(*) from emp b
where b.job=e.job
having TO_CHAR(b.hiredate,'Q')=TO_CHAR(e.hiredate,'Q')
group by TO_CHAR(b.hiredate,'Q'))
group by e.ename,e.job;
2. Situaţia : Total şefi Ian Feb Mar Apr Mai Iun Iul Aug Sep Oct Nov Dec unde în
coloana rezervată fiecărei luni calendaristice se vor înscrie numărul total de şefi angajaţi în luna
respectivă.
select count(*) "TOTAL SEFI",SUM(CASE WHEN to_char(e.hiredate,'MM')=1
then 1 else 0 end ) "Ian",
SUM(CASE WHEN to_char(e.hiredate,'MM')=2
then 1 else 0 end ) "Feb",
SUM(CASE WHEN to_char(e.hiredate,'MM')=3
then 1 else 0 end ) "Mar",
SUM(CASE WHEN to_char(e.hiredate,'MM')=4
then 1 else 0 end ) "Apr",
SUM(CASE WHEN to_char(e.hiredate,'MM')=5
then 1 else 0 end ) "Mai",
SUM(CASE WHEN to_char(e.hiredate,'MM')=6
then 1 else 0 end ) "Iun",
SUM(CASE WHEN to_char(e.hiredate,'MM')=7
then 1 else 0 end ) "Iul",
SUM(CASE WHEN to_char(e.hiredate,'MM')=8
then 1 else 0 end ) "Aug",
SUM(CASE WHEN to_char(e.hiredate,'MM')=9
then 1 else 0 end ) "Sep",
SUM(CASE WHEN to_char(e.hiredate,'MM')=10
then 1 else 0 end ) "Oct",
SUM(CASE WHEN to_char(e.hiredate,'MM')=11
then 1 else 0 end ) "Nov",
SUM(CASE WHEN to_char(e.hiredate,'MM')=12
then 1 else 0 end ) "Dec"
from emp e
where e.empno in( select mgr from emp);
3. Angajaţii care au aceleaşi comision şi număr de departament cu a celor ce au aceiaşi grupă salariala cu
vreunul din subordonaţii lor.
select a.ename "Nume", NVL(a.comm,0) "Comision", a.deptno "Nr depart"
from emp a
where (NVL(a.comm,-1),a.deptno)IN(select NVL(s.comm,-1),s.deptno
from emp s,emp e,salgrade x,salgrade y
where s.empno=e.mgr
and s.sal between x.losal and x.hisal
and e.sal between y.losal and y.hisal
and y.grade=x.grade);
4. Numele salariatului care prin majorarea salariului cu 30% se încadrează în grupa salarială următoare
celei în care se afla salariul său înaintea majorării.
select e.ename,e.sal*1.3,e.sal,s.grade,y.grade
from emp e,salgrade s,salgrade y
where e.sal between s.losal and s.hisal and e.sal*1.3 between y.losal and y.hisal and s.grade+1=y.grade;

Subiectul 24
1. Graficul salariului mediu al angajaţilor care nu sunt şefi, reprezentând în fiecare linie numele
angajatului şi câte un caracter ‘* ‘ pentru fiecare 100 de unităţi salariale în ordinea alfabetică a
angajaţilor.
R :
select ename,lpad(' ',x.sal_med/100+1,'*') from emp,
(select avg(sal) sal_med from emp
where empno not in (select nvl(mgr,-1) from emp)) x
where empno not in (select nvl(mgr,-1) from emp);

(sau)

select RPAD(ename,10)||RPAD(' ',sal/100+1,'*') GRAFIC


from emp
where empno NOT IN (SELECT DISTINCT mgr FROM emp WHERE mgr IS NOT NULL)
order by ename;

2. Situaţia  : Total şefi Angajaţi în trimestrul I Angajaţi în trimestrul II Angajaţi în trimestrul III
Angajaţii în trimestrul IV.
select count(*) "TOTAL SEFI",SUM(CASE WHEN to_char(e.hiredate,'Q')=1
then 1 else 0 end ) "Ang in trimestrul I",
SUM(CASE WHEN to_char(e.hiredate,'Q')=2
then 1 else 0 end ) "Ang in trimestrul II",
SUM(CASE WHEN to_char(e.hiredate,'Q')=3
then 1 else 0 end ) "Ang trimestrul III",
SUM(CASE WHEN to_char(e.hiredate,'Q')=4
then 1 else 0 end ) "Ang trimestrul IV"
from emp e
where e.empno in( select mgr from emp);

3. Angajaţii care au aceleaşi comision şi trimestru al angajării cu a celor ce au aceiaşi grupă salariala
cu vreunul din subordonaţii lor.

select a.ename "Nume", NVL(a.comm,0) "Comision", TO_CHAR(a.hiredate,'Q')"Trim angajarii"


from emp a
where (NVL(a.comm,-1),TO_CHAR(a.hiredate,'Q'))IN(select NVL(s.comm,-1),
TO_CHAR(s.hiredate,'Q')
from emp s,emp e,salgrade x,salgrade y
where s.empno=e.mgr
and s.sal between x.losal and x.hisal
and e.sal between y.losal and y.hisal
and y.grade=x.grade);

4. Graficul salariului mediu în funcţie de trimestrul în care s-a produs angajarea, reprezentând pe
fiecare linie numărul trimestrului şi câte un caracter ‘$’ pentru fiecare 100 unităţi salariale.

select distinct to_char(a.hiredate,'Q') ||rpad(' ',avg(e.sal)/100+1,'$') trimestru


from emp a,emp e
where to_char(a.hiredate,'Q')=to_char(e.hiredate,'Q')
group by to_char(a.hiredate,'Q');

Subiectul 25
1. Situaţia  : Trimestrul 1981-1982 1983-1984 din 1985 unde pentru fiecare trimestru în care s-au
produs angajări se afişează numărul de angajări produse în perioadele specificate ca şi titluri de
coloane.

select TO_CHAR(hiredate,'Q') "Trim ang",Sum(case when to_char(hiredate,'yyyy')=1981 or


to_char(hiredate,'yyyy')=1982
then 1 else 0 end) "1981-1982",
Sum(case when to_char(hiredate,'yyyy')=1983 or to_char(hiredate,'yyyy')=1984
then 1 else 0 end) "1983-1984",
Sum(case when to_char(hiredate,'yyyy')>1985 then 1 else 0 end) "din 1985"
from emp
group by TO_CHAR(hiredate,'Q');

2. Grupa salarială, număr şefi, numele şefului cu cei mai puţini subordonaţi.

select s.grade,x.numar_sefi,e.ename from emp e,salgrade s,


(select grade,count(*) numar_sefi from emp,salgrade
where empno in (select mgr from emp) and sal between losal and hisal group by grade ) x,
(select distinct s.grade grad, a.empno numar_sef,count(b.ename) from emp a,emp b,salgrade s where
a.empno=b.mgr
and a.sal between s.losal and s.hisal
group by a.empno,s.grade
having count(b.ename)<=all(select count(b.ename) from emp a,emp b,salgrade s where a.empno=b.mgr
and a.sal between s.losal and s.hisal
group by a.empno,s.grade )) z
where e.sal between s.losal and s.hisal
and e.empno=z.numar_sef and s.grade=z.grad and x.grade=s.grade;

3. Numele şefilor care au salariul mai mare decât minimul salariului mediu al departamentelor în care
sunt angajaţi din 1983.
select ename from emp where
sal>(select min(sal_dept) from (select deptno,avg(sal) sal_dept from emp
where to_char(hiredate,'yyyy')=1981
group by deptno)) and empno in (select mgr from emp);

(sau)

select e.ename, e.sal,e.hiredate


from emp e
where e.empno IN(select mgr from emp)
AND e.sal>(select min(avg(a.sal))
from emp a
where a.deptno IN(select x.deptno
from emp x
where TO_CHAR(x.hiredate,'YYYY')='1981')
group by a.deptno);

4. Numele angajaţilor care au mai puţine săptămâni lucrate decât şeful lor.

select b.ename from emp b,emp a


where b.mgr=a.empno and (sysdate-b.hiredate)/7<(sysdate-a.hiredate)/7;

(sau)

select a.ename "Angajat",a.hiredate"Data ang",b.ename"Seful",b.hiredate"Data sef"


from emp a, emp b
where a.mgr=b.empno and (b.hiredate-a.hiredate)/7<0;

Subiectul 26
1.Numele salariatului, salariul, grupa salarială şi salariul mediu al grupei salariale pentru acele grupe
salariale în care se află salariile a cel puţin 3 persoane angajate în aceiaşi săptămână a anului.
select e.ename,e.sal,x.grade,a.sal_med
from emp e,salgrade x,(select y.grade gr_sal,AVG(t.sal) sal_med
from emp t, salgrade y
where t.sal between y.losal and y.hisal
group by y.grade)a
where x.grade=a.gr_sal
and e.sal between x.losal and x.hisal
and x.grade=ANY(select d.grade
from emp b, salgrade d
where b.sal between d.losal and d.hisal
and 3>=any(select count(*)
from emp c,emp b
where c.deptno=b.deptno
group by to_char(c.hiredate,'WW')));
2.Angajaţii care au aceleaşi comision şi semestru al angajării cu a celor ce au aceiaşi grupă salariala cu
vreunul din subordonaţii lor.
select a.ename
from emp a
where(NVL(a.comm,-1), TO_CHAR(a.hiredate,'Q'))IN
(select NVL(s.comm,-1),TO_CHAR(s.hiredate,'Q')
from emp s, emp e, salgrade x, salgrade y
where s.empno=e.mgr
AND s.sal BETWEEN x.losal AND x.hisal
AND e.sal BETWEEN y.losal AND y.hisal
AND y.grade=x.grade);
3. Situaţia : Numele zilei 1981-1982 1983-1984 din 1985 unde pentru fiecare nume al zilei din
săptămână în care s-au produs angajări se afişează numărul de angajări produse în perioadele specificate
ca şi titluri de coloane.
select TO_CHAR(hiredate,'DAY') "Numele zilei",Sum(case when to_char(hiredate,'yyyy')=1981 or
to_char(hiredate,'yyyy')=1982
then 1 else 0 end) "1981-1982",
Sum(case when to_char(hiredate,'yyyy')=1983 or to_char(hiredate,'yyyy')=1984
then 1 else 0 end) "1983-1984",
Sum(case when to_char(hiredate,'yyyy')>1985 then 1 else 0 end) "din 1985"
from emp
group by TO_CHAR(hiredate,'DAY');
4. Localitatea, număr şefi, numele şefului cu cei mai puţini subordonaţi.
select x.loc"Localitatea", COUNT(DISTINCT a.mgr) "Nr sefi", a.ename"Seful cu cei mai putini subord"
from emp a, emp b, dept x
where a.empno=b.mgr AND x.deptno=a.deptno
group by x.loc, a.ename
having COUNT(b.empno)=(select min(COUNT(c.empno))
from emp c, emp d
where c.mgr=d.empno
group by d.empno);

Subiectul 27
1. Graficul salariului mediu al meseriilor, reprezentând în fiecare linie meseria şi câte un caracter ‘*  ‘
pentru fiecare 100 de unităţi salariale în ordinea alfabetică a meseriilor.
R :
select lpad(job,10)||' '||lpad(' ',trunc(avg(sal)/100+1),'*') as "Meserie GRAFIC salar"
from emp group by job;

2. Luna angajării, numărul de şefi angajaţi în luna şi salariul mediu al şefilor angajaţi în luna.

select distinct to_char(hiredate,'mon') luna,x.nr_sef,x.sal_med from emp,


(select to_char(hiredate,'mon')luna,count(*) nr_sef,avg(sal) sal_med
from emp where empno in (select mgr from emp) group by to_char(hiredate,'mon')) x
where to_char(hiredate,'mon')=x.luna;

3. Situaţia  : Total angajaţi Ian Feb Mar Apr Mai Iun Iul Aug Sep Oct Nov Dec
unde în coloana rezervată fiecărei luni calendaristice se vor înscrie numărul total de angajaţi în luna
respectivă.
R :
SELECT count(*) "Total angajati",
SUM(DECODE(TO_CHAR(hiredate,'mon'),'ian',1,0)) as "Ian",
SUM(DECODE(TO_CHAR(hiredate,'mon'),'feb',1,0)) AS "Feb",
SUM(DECODE(TO_CHAR(hiredate,'mon'),'mar',1,0)) as "Mar",
SUM(DECODE(TO_CHAR(hiredate,'mon'),'apr',1,0)) AS "Apr",
SUM(DECODE(TO_CHAR(hiredate,'mon'),'mai',1,0)) as "Mai",
SUM(DECODE(TO_CHAR(hiredate,'mon'),'iun',1,0)) as "Iun",
SUM(DECODE(TO_CHAR(hiredate,'mon'),'iul',1,0)) as "Iul",
SUM(DECODE(TO_CHAR(hiredate,'mon'),'aug',1,0)) as "Aug",
SUM(DECODE(TO_CHAR(hiredate,'mon'),'sep',1,0)) as "Sept",
SUM(DECODE(TO_CHAR(hiredate,'mon'),'oct',1,0)) AS "Oct",
SUM(DECODE(TO_CHAR(hiredate,'mon'),'noi',1,0)) as "Noi",
SUM(DECODE(TO_CHAR(hiredate,'mon'),'dec',1,0)) as "Dec"
FROM emp;

4. Angajaţii care au aceleaşi comision şi săptămână din an a angajării cu a celor ce au aceiaşi grupă
salariala cu vreunul din subordonaţii lor.

select ename Nume,comm,to_char(hiredate,'ww') Sapt_ang from emp


where (nvl(comm,-1),to_char(hiredate,'ww')) in
(select nvl(a.comm,-1),to_char(a.hiredate,'ww') from emp a,emp b,salgrade s,salgrade t
where b.mgr=a.empno and a.sal between s.losal and s.hisal
and b.sal between t.losal and t.hisal and s.grade=t.grade);
Subiectul 28
1. Situaţia  : Departamentul 1980-1982 1983-1985 din 1986 unde pentru fiecare departament se
afişează maximul salariilor salariaţilor care lucrează în departamentul respectiv separaţi în
perioadele specificate în antetele coloanelor afişate.

select d.dname departament,MAX(case when to_char(e.hiredate,'yyyy')=1980 or


to_char(e.hiredate,'yyyy')=1981
or to_char(e.hiredate,'yyyy')=1982
then sal else 0 end) "1980-1982",
MAX(case when to_char(e.hiredate,'yyyy')=1983 or to_char(e.hiredate,'yyyy')=1984
or to_char(e.hiredate,'yyyy')=1985
then sal else 0 end) "1983-1985",
MAX(case when to_char(e.hiredate,'yyyy')>1986 then sal else 0 end) "din 1986"
from emp e,dept d
where e.deptno=d.deptno
group by d.dname;
2. Media salariilor medii ale meseriilor.

select avg(x.sal_med) from (select avg(sal) sal_med from emp group by job) x;

3. Numele angajatului, localitatea, salariul şi salariul mediu al localităţii pentru cei ce au salariul mai
mare decât maximul salariilor medii ale localităţilor.

select ename,loc,sal, x.sal_medloc from emp natural join dept,


(select loc local, avg(sal) sal_medloc from emp natural join dept group by loc) x
where loc=x.local
and sal>(select max(y.sal_med) from (select avg(sal) sal_med from emp natural join dept group
by loc) y)

4. Angajaţii care au acelaşi număr al şefului şi comision cu cei ce au aceiaşi grupă salarială cu şeful
lor.

select *
from emp e
where (e.mgr,NVL(e.comm,-1)) in(select b.mgr,NVL(b.comm,-1)
from emp b,emp c,salgrade s,salgrade k
where c.empno=b.mgr
and b.sal between s.losal and s.hisal
and c.sal between k.losal and k.hisal
and s.grade=k.grade);

Subiectul 29
1. Angajaţii care au aceiaşi lună a angajării şi comision identice cu a celor ce au grupa salarială
superioară grupei salariale a celor ce lucrează în New York.

select ename from emp where (to_char(hiredate,'mm'),nvl(comm,-1)) in


(select to_char(hiredate,'mm'),nvl(comm,-1) from emp a,salgrade s
where (a.sal between s.losal and s.hisal ) and
(s.grade >all(select grade from emp,dept,salgrade where emp.deptno=dept.deptno
and sal between losal and hisal and loc='NEW YORK')));

(sau)

select distinct e.ename Nume, TO_CHAR(e.hiredate, 'MONTH')Luna, NVL(comm,0)Comm


from emp e,(select grade
from emp,dept,salgrade
where dept.deptno=emp.deptno and dept.loc='NEW YORK' and sal between losal and hisal)x
where (to_char(e.hiredate,'MONTH'),NVL(e.comm,-1)) in(Select
to_char(e.hiredate,'MONTH'),NVL(a.comm,- 1)
from emp a,salgrade s
where a.sal between s.losal and s.hisal
and (s.grade+1)=x.grade);

2. Situaţia  : Luna 1981-1982 1983-1984 din 1985 unde pentru fiecare lună în care s-au produs
angajări se afişează numărul de angajări produse în perioadele specificate ca şi titluri de coloane.

select TO_CHAR(hiredate,'MONTH') "Trim ang",Sum(case when to_char(hiredate,'yyyy')=1981 or


to_char(hiredate,'yyyy')=1982
then 1 else 0 end) "1981-1982",
Sum(case when to_char(hiredate,'yyyy')=1983 or to_char(hiredate,'yyyy')=1984
then 1 else 0 end) "1983-1984",
Sum(case when to_char(hiredate,'yyyy')>1985 then 1 else 0 end) "din 1985"
from emp
group by TO_CHAR(hiredate,'MONTH');

3. Departamentul, număr şefi, numele şefului cu cei mai puţini subordonaţi.

select d.dname"Departament",count(*) "Nr sefi",h.ename"Seful cu cei mai putini subord"


from dept d,emp e,emp h
where e.empno in (select distinct mgr from emp )
and d.deptno=e.deptno
and h.ename=any (select a.ename
from emp a,dept q,emp k
where a.empno in (select distinct mgr from emp )
and q.deptno=a.deptno and a.deptno=e.deptno
and a.empno=k.mgr
group by a.ename
having count(k.empno)=(select min(count(m.empno))
from emp x,dept f,emp m
where x.empno in (select distinct mgr from emp )
and x.deptno=f.deptno and f.deptno=e.deptno
and m.mgr=x.empno
group by x.empno ))
group by d.dname,h.ename;

4. Numele şefilor care au salariul mai mare decât maximul salariului minim al departamentelor în care
sunt angajaţi din 1982.
select e.ename, e.sal,e.hiredate
from emp e
where e.empno IN(select mgr from emp)
AND e.sal>=(select max(min(a.sal))
from emp a
where a.deptno IN(select x.deptno
from emp x
where TO_CHAR(x.hiredate,'YYYY')='1982')
group by a.deptno);

(sau)

select ename,sal from emp where


sal>=(select max(min(sal)) from emp where to_char(hiredate,'yyyy')=1982 group by deptno)
and empno in (select mgr from emp);

Subiectul 30
1. Maximul din salariile medii ale semestrelor în care s-au făcut angajări.

select max(x.sal_med) from (select avg(sal) sal_med from emp group by to_char(hiredate,'q')) x;
sau(paty)

select max(case when a.sal_med1>b.sal_med2 then a.sal_med1 else b.sal_med2 end )


from (select max(avg(sal)) sal_med1
from emp
where to_char(hiredate,'DDD')<183
group by to_char(hiredate,'DDD'))a,
(select max(avg(sal)) sal_med2
from emp
where to_char(hiredate,'DDD')>182
group by to_char(hiredate,'DDD'))b ;

2. Graficul salariului mediu al angajaţilor de pe nivelul 3 al ierarhiei, reprezentând în fiecare linie


numele angajatului şi câte un caracter ‘*  ‘ pentru fiecare 100 de unităţi salariale în ordinea
alfabetică a angajaţilor.

select ename, lpad(' ',sal_med/100,'*') from emp,


(select avg(sal) sal_med from emp
where level=3 connect by prior empno=mgr start with mgr is null)
where level=3 connect by prior empno=mgr start with mgr is null;

sau(paty)

select RPAD(ename,10) ||RPAD(' ',avg(sal)/100+1,'*') "GRAFIC SALRIAL"


from emp
where level=3
connect by prior empno=mgr
start with mgr is null
group by ename
order by ename;

3. Numele salariatului, data angajării, salariul mediu al departamentului, numărul săptămânii din an
în care s-a făcut angajarea salariatului pentru salariaţii care lucrează în departamente în care sunt
încadrate cel puţin 3 persoane angajate în aceiaşi lună.

select dname,
sum(case when to_char(hiredate,'yyyy')between 1980 and 1981 then 1 else 0 end) "1980-1881",
sum(case when to_char(hiredate,'yyyy')between 1982 and 1983 then 1 else 0 end) "1982-1883",
sum(case when to_char(hiredate,'yyyy')>1983 then 1 else 0 end) "1983- "
from emp,dept where emp.deptno=dept.deptno
group by dname;

sau(paty)

select e.ename,e.hiredate,a.sal_med,To_char(hiredate,'WW')
from emp e,(select deptno,AVG(sal) sal_med
from emp
group by deptno)a
where e.deptno=a.deptno and e.deptno=ANY(select b.deptno
from emp b
where 3>=any(select count(*)
from emp c,emp b
where c.deptno=b.deptno
group by to_char(c.hiredate,'MM')));

4. Situaţia  : Departamentul 1981-1982 1983-1984 din 1985 unde pentru fiecare departament în
care s-au produs angajări se afişează numărul de angajări produse în perioadele specificate ca şi
titluri de coloane.
R :
select d.dname departament,Sum(case when to_char(e.hiredate,'yyyy')=1981 or to_char(e.hiredate,'yyyy')=1982
then 1 else 0 end) "1981-1982",
Sum(case when to_char(e.hiredate,'yyyy')=1983 or to_char(e.hiredate,'yyyy')=1984
then 1 else 0 end) "1983-1984",
Sum(case when to_char(e.hiredate,'yyyy')>1981 then 1 else 0 end) "din 1985"
from emp e,dept d
where e.deptno=d.deptno
group by d.dname

Subiectul 31
1. Angajaţii care au acelaşi an al angajării şi comision identice cu a celor ce au salariul mai mare decât
media salariilor maxime ale grupelor salariale.
R :
SELECT ename FROM emp
WHERE TO_CHAR(hiredate,'YYYY')=ANY(SELECT TO_CHAR(hiredate,'YYYY') FROM emp
WHERE sal>ALL(SELECT AVG(MAX(sal)) FROM emp,salgrade WHERE sal BETWEEN losal
AND hisal GROUP BY grade))
AND comm=ANY(SELECT comm FROM emp WHERE sal>ALL(SELECT AVG(MAX(sal))
FROM emp,salgrade WHERE sal BETWEEN losal AND hisal GROUP BY grade))

sau(yeti)

select ename from emp where (to_char(hiredate,'yyyy'),nvl(comm,-1)) in


(select to_char(hiredate,'yyyy'),nvl(comm,-1) from emp where
sal >(select avg(max(sal)) from emp,salgrade
where sal between losal and hisal group by grade));

sau(paty)

Select *
from emp
where (to_char(hiredate,'yyyy'),NVL(comm,-1)) in(Select to_char(a.hiredate,'yyyy'),NVL(a.comm,-1)
from emp a
where sal>(select avg(max(sal))
from emp,salgrade
where sal between losal and hisal
group by grade));

2. Numele salariatului, numărul departamentului, numele departamentului şi salariul mediu al


departamentului pentru acei salariaţi care s-au angajat în zilele de luni, marţi sau joi şi în zilele
impare din an.
R :
select ename,deptno,dname,x.sal_med from emp natural join dept,
(select deptno nrdep,avg(sal) sal_med from emp group by deptno) x
where to_char(hiredate,'d') in (1,2,4) and mod(to_char(hiredate,'d') ,2)=1
and deptno=x.nrdep;

sau(paty)

select e.ename,e.deptno,d.dname,x.sal_med
from emp e,dept d,(select deptno,avg(sal) sal_med from emp group by deptno)x,
(select empno from emp where to_char(hiredate,'D')=1
OR to_char(hiredate,'D')=2 OR
to_char(hiredate,'D')=4
AND MOD(to_char(hiredate,'DDD'),2)=1)y
where e.deptno=d.deptno and x.deptno=d.deptno and e.empno=y.empno;

3. Situaţia  : Localitatea 1981-1982 1983-1984 din 1985 unde pentru localitate departament în care
s-au produs angajări se afişează numărul de angajări produse în perioadele specificate ca şi titluri de
coloane.
R :
SELECT loc Localitatea, SUM(DECODE(TO_CHAR(hiredate,'YYYY'),'1981',1,0))+
SUM(DECODE(TO_CHAR(hiredate,'YYYY'),'1982',1,0)) AS "1981_1982",
SUM(DECODE(TO_CHAR(hiredate,'YYYY'),'1983',1,0))+
SUM(DECODE(TO_CHAR(hiredate,'YYYY'),'1984',1,0)) AS "1983-1984",
SUM(DECODE(TO_CHAR(hiredate,'YYYY'),'1985',1,0))+
SUM(DECODE(TO_CHAR(hiredate,'YYYY'),'1986',1,0))+
SUM(DECODE(TO_CHAR(hiredate,'YYYY'),'1987',1,0))+
SUM(DECODE(TO_CHAR(hiredate,'YYYY'),'1988',1,0))+
SUM(DECODE(TO_CHAR(hiredate,'YYYY'),'1989',1,0))+
SUM(DECODE(TO_CHAR(hiredate,'YYYY'),'1990',1,0)) AS "dupa_1985"
FROM emp,dept
WHERE emp.deptno=dept.deptno
GROUP BY loc;

sau(yeti)

select loc,
sum(case when to_char(hiredate,'yyyy')between 1980 and 1981 then 1 else 0 end) "1980-1881",
sum(case when to_char(hiredate,'yyyy')between 1982 and 1983 then 1 else 0 end) "1982-1883",
sum(case when to_char(hiredate,'yyyy')>1983 then 1 else 0 end) "1983- "
from emp,dept where emp.deptno=dept.deptno
group by loc;

sau(paty)

select d.dname localitate,Sum(case when to_char(e.hiredate,'yyyy')=1981 or to_char(e.hiredate,'yyyy')=1982


then 1 else 0 end) "1981-1982",
Sum(case when to_char(e.hiredate,'yyyy')=1983 or to_char(e.hiredate,'yyyy')=1984
then 1 else 0 end) "1983-1984",
Sum(case when to_char(e.hiredate,'yyyy')>1985 then 1 else 0 end) "din 1985"
from emp e,dept d
where e.deptno=d.deptno
group by d.dname;

4. Numele şefilor care au venitul (salariul plus comisionul) mai mare decât maximul salariului mediu
al departamentelor în care sunt cel puţin doi şefi.

R :
SELECT b.ename FROM emp a,emp b
WHERE a.mgr=b.empno
AND b.sal+b.comm>ANY(SELECT MAX(AVG(sal)) FROM emp WHERE 2<ANY(SELECT
COUNT(b.empno) FROM emp a,emp b WHERE a.mgr=b.empno GROUP BY b.deptno)
GROUP BY sal);

Sau(paty)

select distinct e.ename


from emp e,(select deptno,avg(sal) sal_med
from emp
group by deptno)x,
(select deptno,count(distinct mgr) nr_sefi
from emp
group by deptno)y
where x.deptno=y.deptno and 2>=y.nr_sefi and(e.sal+NVL(comm,0))>(select max(x.sal_med)from emp x);

Subiectul 32
1. Situaţia  : Localitatea 1980-1982 1983-1985 din 1986 unde pentru fiecare localitate se afişează
salariul minim al salariaţilor care lucrează în localitatea respectivă separaţi în perioadele specificate
în antetele coloanelor afişate.
R :
SELECT loc Localitatea,
(SELECT MIN(sal) FROM emp WHERE TO_CHAR(hiredate,'YYYY') IN ('1980','1981','1982'))
"1980-1982",
(SELECT MIN(sal) FROM emp WHERE TO_CHAR(hiredate,'YYYY') IN ('1983','1984','1985'))
"1983-1985",
(SELECT MIN(sal) FROM emp WHERE TO_CHAR(hiredate,'YYYY') IN
('1986','1987','1988','1989','1990','1991','1992','1993','1994','1995','1996','1997','1998','1999','2000',
'2001','2002','2003','2004','2005','2006')) "din_1986"
FROM emp,dept
WHERE emp.deptno=dept.deptno
GROUP BY loc;

Sau(paty)

select d.dname localitate,Sum(case when to_char(e.hiredate,'yyyy')=1980 or to_char(e.hiredate,'yyyy')=1982


or to_char(e.hiredate,'yyyy')=1981 then sal else 0 end) "1980-1982",
Sum(case when to_char(e.hiredate,'yyyy')=1983 or to_char(e.hiredate,'yyyy')=1984
or to_char(e.hiredate,'yyyy')=1985 then sal else 0 end) "1983-1985",
Sum(case when to_char(e.hiredate,'yyyy')>1986 then sal else 0 end) "din 1986"
from emp e,dept d
where e.deptno=d.deptno
group by d.dname;

2. Suma salariilor medii ale departamentelor.


R :
SELECT SUM(AVG(sal)) FROM emp GROUP BY deptno;

sau(yeti)

select sum(avg(sal)) from emp natural join dept group by dname;

sau(paty)

select sum(avg(sal))
from emp
group by deptno;

3. Angajaţii care au acelaşi număr al şefului şi meserie cu cei ce au aceiaşi grupă salarială cu şeful lor.
R :
SELECT ename FROM emp
WHERE mgr=ANY(SELECT a.mgr FROM emp a,emp b, salgrade a, salgrade b WHERE
a.mgr=b.mgr AND a.sal BETWEEN a.losal AND a.hisal AND b.sal BETWEEN b.losal AND
b.hisal AND a.grade=b.grade)
AND job=ANY(SELECT a.job FROM emp a,emp b, salgrade a, salgrade b WHERE a.mgr=b.mgr
AND a.sal BETWEEN a.losal AND a.hisal AND b.sal BETWEEN b.losal AND b.hisal AND
a.grade=b.grade);

sau(yeti)
select ename from emp where (mgr,job) in
(select b.mgr,b.job from emp a,emp b,salgrade s,salgrade t
where b.mgr=a.empno and (a.sal between s.losal and s.hisal)
and (b.sal between t.losal and t.hisal) and s.grade=t.grade);

sau(paty)

select *
from emp e
where (e.mgr,e.job) IN(select a.mgr,a.job
from emp a,salgrade s,salgrade f, emp b
where a.sal between s.losal and s.hisal and b.sal between f.losal and f.hisal
and s.grade=f.grade and a.mgr=b.empno);

4. Numele şefilor care au mai puţine luni lucrate decât unii din subordonaţii lor.
R :
SELECT DISTINCT(b.ename) FROM emp a,emp b,SYS.DUAL
WHERE a.mgr=b.empno
AND ((SYSDATE-b.hiredate)/30)<((SYSDATE-a.hiredate)/30);

sau(yeti)

select a.ename from emp a


where (sysdate-hiredate)/30<any(select (sysdate-hiredate)/30 from emp
where mgr=a.empno);

sau(paty)

select distinct e.ename


from emp e,emp a
where (to_char(sysdate,'yyyy')-to_char(e.hiredate,'YYYY'))*12 <any(select (to_char(sysdate,'yyyy')-
to_char(a.hiredate,'YYYY'))*12
from emp a
where a.mgr=e.empno);

Subiectul 33
1. Media salariilor medii ale localităţilor în care s-au făcut angajări în 1981.
R :
SELECT AVG(AVG(sal)) FROM emp,dept
WHERE emp.deptno=dept.deptno
AND loc=ANY(SELECT loc FROM dept, emp WHERE TO_CHAR(hiredate,'YYYY')='1981')
GROUP BY loc

sau(yeti)
select avg(avg(sal)) from emp natural join dept where loc in
(select distinct loc from emp natural join dept where to_char(hiredate,'yyyy')=1981);

Sau(paty)

select avg(e.sal_med)
from (select avg(sal) sal_med
from emp ,dept
where emp.deptno=dept.deptno and to_char(hiredate,'yyyy')=1981
group by dept.dname)e;

2. Angajaţii care au aceiaşi săptămână din an a angajării şi comision identice cu a celor ce au


grupasalarială superioară grupei salariale a celor ce lucrează în Dallas.
R :
SELECT ename FROM emp
WHERE TO_CHAR(hiredate,'WW')=ANY(SELECT TO_CHAR(hiredate,'WW') FROM
emp,salgrade,dept WHERE emp.deptno=dept.deptno AND sal BETWEEN losal AND hisal
AND grade>ALL(SELECT grade FROM salgrade,emp,dept WHERE sal BETWEEN losal AND
hisal AND emp.deptno=dept.deptno AND loc='DALLAS'))
AND comm=ANY(SELECT comm FROM emp,salgrade,dept WHERE emp.deptno=dept.deptno
AND sal BETWEEN losal AND hisal AND grade>ALL(SELECT grade FROM salgrade,emp,dept
WHERE sal BETWEEN losal AND hisal AND emp.deptno=dept.deptno AND loc='DALLAS'));

Sau(paty)

select distinct *
from emp e,(select grade
from emp,dept,salgrade
where dept.deptno=emp.deptno and dept.loc='DALLAS' and sal between losal and hisal)x
where (to_char(e.hiredate,'WW'),NVL(e.comm,-1)) in(Select to_char(e.hiredate,'WW'),NVL(a.comm,-1)
from emp a,salgrade s
where a.sal between s.losal and s.hisal
and (s.grade+1)=x.grade);

3. Situaţia  : Grupa salarială 1981-1982 1983-1984 din 1985 unde pentru fiecare grupă salarială în
care se află salariul celor angajaţi se afişează numărul de angajări produse în perioadele specificate
ca şi titluri de coloane.
R :
SELECT grade Grupa_salariala,
SUM(DECODE(TO_CHAR(hiredate,'YYYY'),'1981',1,0))+
SUM(DECODE(TO_CHAR(hiredate,'YYYY'),'1982',1,0)) "1981-1982",
SUM(DECODE(TO_CHAR(hiredate,'YYYY'),'1983',1,0))+
SUM(DECODE(TO_CHAR(hiredate,'YYYY'),'1984',1,0)) "1983-1984",
SUM(DECODE(TO_CHAR(hiredate,'YYYY'),'1985',1,0))+
SUM(DECODE(TO_CHAR(hiredate,'YYYY'),'1986',1,0))+
SUM(DECODE(TO_CHAR(hiredate,'YYYY'),'1987',1,0))+
SUM(DECODE(TO_CHAR(hiredate,'YYYY'),'1988',1,0))+
SUM(DECODE(TO_CHAR(hiredate,'YYYY'),'1989',1,0))+
SUM(DECODE(TO_CHAR(hiredate,'YYYY'),'1990',1,0)) "din_1985"
FROM salgrade,emp
WHERE sal BETWEEN losal AND hisal
GROUP BY grade;

Sau(paty)

select s.grade "Grupa salariala",Sum(case when to_char(e.hiredate,'yyyy')=1981 or


to_char(e.hiredate,'yyyy')=1982
then 1 else 0 end) "1981-1982",
Sum(case when to_char(e.hiredate,'yyyy')=1983 or to_char(e.hiredate,'yyyy')=1984
then 1 else 0 end) "1983-1984",
Sum(case when to_char(e.hiredate,'yyyy')>1985 then 1 else 0 end) "din 1985"
from emp e,salgrade s
where e.sal between s.losal and s.hisal
group by s.grade;

4. Graficul salariului mediu al angajaţilor încadraţi într-un trimestru, reprezentând numărul


trimestrului şi câte un caracter ‘*’ pentru fiecare 100 unităţi salariale.
R :
SELECT TO_CHAR(hiredate,'Q')||' '||LPAD(' ',TRUNC(AVG(sal)/100,0),'*') FROM emp GROUP BY
TO_CHAR(hiredate,'Q');

Sau(paty)

select distinct to_char(a.hiredate,'Q') ||rpad(' ',avg(e.sal)/100+1,'*') trimestru


from emp a,emp e
where to_char(a.hiredate,'Q')=to_char(e.hiredate,'Q')
group by to_char(a.hiredate,'Q');

Subiectul 34
1. Situaţia  : Departamentul 1980-1982 1983-1985 din 1986 unde pentru fiecare departament se
afişează numărul salariaţilor care lucrează în departamentul respectiv separaţi în perioadele
specificate în antetele coloanelor afişate

R :
SELECT deptno Departamentul,
SUM(DECODE(TO_CHAR(hiredate,'YYYY'),'1980',1,0))+
SUM(DECODE(TO_CHAR(hiredate,'YYYY'),'1981',1,0))+
SUM(DECODE(TO_CHAR(hiredate,'YYYY'),'1982',1,0)) "1980-1982",
SUM(DECODE(TO_CHAR(hiredate,'YYYY'),'1983',1,0))+
SUM(DECODE(TO_CHAR(hiredate,'YYYY'),'1984',1,0))+
SUM(DECODE(TO_CHAR(hiredate,'YYYY'),'1985',1,0)) "1983-1985",
SUM(DECODE(TO_CHAR(hiredate,'YYYY'),'1986',1,0)) "din_1986"
FROM emp
GROUP BY deptno;

Sau(paty)

select d.deptno,Sum(case when to_char(e.hiredate,'yyyy')=1981 or to_char(e.hiredate,'yyyy')=1982


or to_char(e.hiredate,'yyyy')=1980 then 1 else 0 end) "1980-1982",
Sum(case when to_char(e.hiredate,'yyyy')=1983 or to_char(e.hiredate,'yyyy')=1984
or to_char(e.hiredate,'yyyy')=1985 then 1 else 0 end) "1983-1985",
Sum(case when to_char(e.hiredate,'yyyy')>1986 then 1 else 0 end) "din 1986"
from emp e,dept d
where d.deptno=e.deptno
group by d.deptno;

2. Angajaţii care au acelaşi departament şi comision identice cu a celor ce au salariul mai mic decât
media salariilor minime ale departamentelor.
R:
select e.empno,e.ename,e.sal,e.deptno
from emp e,(select avg(min(sal))sal_min
from emp
group by deptno)a
where (e.deptno,NVL(comm,-1)) in(select b.deptno,NVL(b.comm,-1)
from emp b,emp a
where b.sal<a.sal_min);

3. Numele şefilor care au venitul (salariul plus comisionul) mai mare decât minimul salariului mediu al
localităţilor în care sunt cel puţin doi şefi.
R:
select distinct e.ename
from emp e,(select loc,avg(sal) sal_med
from emp,dept
where emp.deptno=dept.deptno
group by loc)x,
(select loc,count(distinct mgr) nr_sefi
from emp,dept
where emp.deptno=dept.deptno
group by loc)y
where x.loc=y.loc and 2>=y.nr_sefi and(e.sal+NVL(comm,0))>(select max(x.sal_med)from emp x);

4. Graficul salariului mediu al angajaţilor încadraţi într-o lună, reprezentând numărul lunii cu cifre
romane şi câte un caracter ‘*’ pentru fiecare 100 unităţi salariale.
R:
select to_char(hiredate,'RM') ||RPAD(' ',AVG(sal)/100+1,'*') Grafic
from emp
group by to_char(hiredate,'RM');
Subiectul 35
1. Maximul salariilor medii ale lunilor în care s-au făcut angajări.
R:
select MAX(AVG(sal))
from emp
group by to_char(hiredate,'MM');

2. Angajaţii care au acelaşi localitate şi comision cu cei ce au aceiaşi grupă salarială cu şeful lor.
R:
select distinct *
from emp e,dept d
where d.deptno=e.deptno and(d.loc,NVL(e.comm,-1)) in(select c.loc,NVL(a.comm,-1)
from emp a,emp b,dept c,salgrade y,salgrade s
where a.mgr=b.empno AND b.sal between y.losal and y.hisal
and a.sal between s.losal and s.hisal
and y.grade=s.grade
and c.deptno=a.deptno);

3. Situaţia  : Grupa salarială Număr şefi Numele şefului cu salariul maxim


R:
select s.grade,count(*),h.ename
from salgrade s,emp e,emp h
where e.empno in (select distinct mgr from emp )
and e.sal between s.losal and s.hisal
and h.ename=any (select a.ename
from emp a,salgrade d
where a.empno in (select distinct mgr from emp )
and a.sal between d.losal and d.hisal and s.grade=d.grade
and a.sal =(select max(x.sal)
from emp x,salgrade f
where x.empno in (select distinct mgr from emp )
and x.sal between f.losal and f.hisal and f.grade=s.grade ))
group by s.grade,h.ename;

4. Graficul salariului mediu al angajaţilor încadraţi într-un an, reprezentând numărul anului şi câte un
caracter ‘*’ pentru fiecare 100 unităţi salariale.
R:
select to_char(a.hiredate,'yyyy') || RPAD(' ',AVG(e.sal)/100+1,'*') Grafic
from emp a,emp e
where to_char(a.hiredate,'yyyy')=to_char(e.hiredate,'yyyy')
group by to_char(a.hiredate,'yyyy');

Subiectul 36
1. Situaţia  : Localitatea 1980-1982 1983-1985 din 1986 unde pentru fiecare localitate se afişează
numărul salariaţilor care lucrează în localitatea respectivă separaţi în perioadele specificate în
antetele coloanelor afişate.
R :
select d.loc,sum(case when to_char(e.hiredate,'yyyy')=1980 or to_char(e.hiredate,'yyyy')=1981
or to_char(e.hiredate,'yyyy')=1982 then 1 else 0 end) "1980-1982",
sum(case when to_char(e.hiredate,'yyyy')=1983 or to_char(e.hiredate,'yyyy')=1984
or to_char(e.hiredate,'yyyy')=1985 then 1 else 0 end) "1983-1985",
sum(case when to_char(e.hiredate,'yyyy')>1986 then 1 else 0 end) "din 1986"
from emp e,dept d
where d.deptno=e.deptno(+)
group by d.loc;

2. Angajaţii care au meseria şi comisionul identice cu meseria şi comisionul celor ce au salariul mai
mic decât minimul salariilor medii ale departamentelor.
R  :
select *
from emp e,(select MIN(avg(sal))sal_med_min
from emp
group by deptno)a
where (e.job,NVL(e.comm,-1)) in(select b.job,NVL(b.comm,-1)
from emp b,emp a
where b.sal<a.sal_med_min);

3. Numele şefilor care au venitul (salariul plus comisionul) mai mare decât media salariilor minime ale
grupelor salariale în care au salariul cel puţin doi şefi.
R :
select s.ename
from emp s
where sal+nvl(comm,0) > (select avg(min(sal)) from emp,salgrade a,(select grade ,count(*) nrsefi
from emp,salgrade where sal between losal and hisal and empno in
(select mgr from emp)
group by grade
having count(*)>=2) x
where sal between losal and hisal and a.grade=x.grade group by a.grade);

sau(paty)

select distinct e.ename


from emp e,(select grade, min(sal) sal_min_gr
from emp,salgrade
where sal between losal and hisal
group by grade)a
where (e.sal+NVL(e.comm,0))>(select avg(a.sal_min_gr)
from emp a
where 2>=(select count(*)
from emp x,salgrade b,emp a
where x.sal between b.losal and b.hisal and b.grade=a.grade
and exists(select f.mgr
from emp f,emp x
where x.empno=f.mgr)));

4. Situaţia  : Număr şefi Dallas New York Alte oraşe unde în coloanele reprezentând numele
localităţilor se reprezintă numărul de şefi din localitatea respectivă.
R:
select count(e.empno),sum(decode(d.loc,'DALLAS',1,0)) Dallas,sum(decode(d.loc,'NEW YORK',1,0)) "NEW
YORK",
sum(case when d.loc='DALLAS' OR d.loc='NEW YORK' then 0 else 1 end) "Alte orase"
from emp e,dept d
where e.deptno=d.deptno and e.empno in (select distinct b.mgr
from emp b);

Subiectul 37
1. Salariul mediu al salariilor minime ale anilor în care s-au făcut angajări.
R :
select avg(b.sal_min)
from (select min(sal) sal_min
from emp
group by to_char(hiredate,'yyyy'))b;

2. Numele salariatului, departamentul, salariul mediu al departamentului pentru acei salariaţi care s-
au angajat până cel târziu în a 25 săptămână din an.
R :
select e.ename,e.deptno,b.sal_med
from emp e,(select deptno,avg(sal) sal_med
from emp
group by deptno)b
where to_char(e.hiredate,'WW')<=25 and e.deptno=b.deptno;

3. Angajaţii care au acelaşi număr al şefului şi departament cu cei ce au aceiaşi grupă salarială cu
şeful lor.
R :
select * from emp e
where (e.mgr,e.deptno) in(select b.mgr,b.deptno
from emp b,emp a,salgrade s,salgrade v
where b.mgr=a.empno and b.sal between s.losal and s.hisal
and a.sal between v.losal and v.hisal and s.grade=v.grade);

4. Situaţia  : Grupa salarială Număr şefi Numele şefului cu salariul minim


R:
select s.grade,count(*),h.ename
from salgrade s,emp e,emp h
where e.empno in (select distinct mgr from emp )
and e.sal between s.losal and s.hisal
and h.ename=any (select a.ename
from emp a,salgrade d
where a.empno in (select distinct mgr from emp )
and a.sal between d.losal and d.hisal and s.grade=d.grade
and a.sal =(select min(x.sal)
from emp x,salgrade f
where x.empno in (select distinct mgr from emp )
and x.sal between f.losal and f.hisal and f.grade=s.grade ))
group by s.grade,h.ename;

Subiectul 38
1. Minimul salariilor medii ale departamentelor în care s-au făcut angajări în 1982.
R :
select min(a.sal_med)
from (select min(sal) sal_med
from emp
where to_char(hiredate,'yyyy')=1982
group by deptno)a;

2. Angajaţii care au acelaşi nume al departamentului şi comision cu cei ce au aceiaşi grupă salarială cu
şeful lor.
R :
select *
from emp e,dept d
where d.deptno=e.deptno and (d.dname,NVL(e.comm,-1)) in(select a.dname,NVL(b.comm,-1)
from emp b,dept a,emp c,salgrade s,salgrade k
where a.deptno=d.deptno and c.empno=b.mgr
and b.sal between s.losal and s.hisal
and c.sal between k.losal and k.hisal
and s.grade=k.grade);

3. Situaţia  : Localitatea Număr şefi Numele şefului cu salariul maxim


R:
select d.loc,count(*),h.ename
from dept d,emp e,emp h
where e.empno in (select distinct mgr from emp )
and d.deptno=e.deptno
and h.ename=any (select a.ename
from emp a,dept q
where a.empno in (select distinct mgr from emp )
and q.deptno=a.deptno and a.deptno=d.deptno
and a.sal =(select max(x.sal)
from emp x,dept f
where x.empno in (select distinct mgr from emp )
and x.deptno=f.deptno and f.deptno=d.deptno ))
group by d.loc,h.ename;
4. Numele şefilor care au mai puţine săptămâni lucrate decât unii din subordonaţii lor.
R:
select s.ename,(sysdate-s.hiredate)/7 sapt_sef
from emp s
where s.empno in(select mgr from emp)
and(sysdate-s.hiredate)/7 < any (select (sysdate-e.hiredate)/7 from emp e
where s.empno=e.mgr);

sau(paty)

select e.ename
from emp e
where ((to_char(sysdate,'yyyy')-to_char(e.hiredate,'yyyy'))*52)+((to_char(sysdate,'DD')-
to_char(e.hiredate,'DD'))/7)<
any(select ((to_char(sysdate,'yyyy')-to_char(b.hiredate,'yyyy'))*52)+((to_char(sysdate,'DD')-
to_char(b.hiredate,'DD'))/7)
from emp b
where e.empno=b.mgr);

Subiectul 39
1. Graficul salariului mediu al angajaţilor de pe nivelul 2 al ierarhiei, reprezentând în fiecare linie
numele angajatului şi câte un caracter ‘*  ‘ pentru fiecare 100 de unităţi salariale în ordinea
alfabetică a angajaţilor.
R :
select RPAD(ename,10) ||RPAD(' ',sal/100+1,'*') GRAFIC
from emp
where level=2
connect by prior empno=mgr
start with mgr is null;

2. Angajaţii care au aceiaşi localitate şi comision identice cu a celor ce au salariul mai mare decât
media salariilor medii ale meseriilor.
R :
select *
from emp e,(select avg(avg(sal)) sal_med
from emp
group by job)a,dept d
where e.deptno=d.deptno and (d.loc,NVL(e.comm,-1)) in(select b.loc,NVl(c.comm,-1)
from emp c,dept b,emp a
where c.sal>a.sal_med);

3. Situaţia  : Departamentul Număr şefi Numele şefului cu cei mai mulţi subordonaţi
R:
select d.dname,count(*),h.ename
from dept d,emp e,emp h
where e.empno in (select distinct mgr from emp )
and d.deptno=e.deptno
and h.ename=any (select a.ename
from emp a,dept q,emp k
where a.empno in (select distinct mgr from emp )
and q.deptno=a.deptno and a.deptno=e.deptno
and a.empno=k.mgr
group by a.ename
having count(k.empno)=(select max(count(m.empno))
from emp x,dept f,emp m
where x.empno in (select distinct mgr from emp )
and x.deptno=f.deptno and f.deptno=e.deptno
and m.mgr=x.empno
group by x.empno ))
group by d.dname,h.ename;

4. Numele salariaţilor care prin majorarea salariului cu 70% realizează un salt de 3 grupe salariale fată
de grupa salarială în care se afla salariul lor înaintea producerii majorării.
R:
select e.ename,e.sal*1.7,e.sal,s.grade,y.grade
from emp e,salgrade s,salgrade y
where e.sal between s.losal and s.hisal and e.sal*1.7 between y.losal and y.hisal and y.grade+3=s.grade;

Subiectul 40
1. Situaţia  : Grupa salarială 1980-1982 1983-1985 din 1986 unde pentru fiecare grupă salarială
se afişează suma salariilor salariaţilor care au salariul în grupa respectivă separaţi în perioadele
specificate în antetele coloanelor afişate.
R :
select s.grade,sum(case when to_char(e.hiredate,'YYYY')=1980 or to_char(e.hiredate,'YYYY')=1981 or
to_char(e.hiredate,'YYYY')=1982 then e.sal else 0 end) "1980-1982",
sum(case when to_char(e.hiredate,'YYYY')=1983 or to_char(e.hiredate,'YYYY')=1984 or
to_char(e.hiredate,'YYYY')=1985 then e.sal else 0 end) "1983-1985",
sum(case when to_char(e.hiredate,'YYYY')>1986 then e.sal else 0 end) "din 1986"
from emp e,salgrade s
where e.sal between s.losal and s.hisal
group by s.grade;

2. Minimul salariilor medii ale trimestrelor în care s-au făcut angajări.


R :
select min(a.sal_med)
from (select avg(sal) sal_med
from emp
group by to_char(hiredate,'Q'))a;
3. Angajaţii care au meseria şi comisionul identice cu meseria şi comisionul celor ce au salariul mai
mic decât minimul salariilor medii ale meseriilor.
R :
select a.ename
from emp a
where (job,nvl(comm,-1)) in (select job,nvl(comm,-1)
from emp
where sal<(select min(avg(sal)) from emp group by job));

sau(paty)

select *
from emp e,(select min(avg(sal)) sal_min_med
from emp
group by job)a
where (e.job,NVL(e.comm,-1)) in(select b.job,NVL(b.comm,-1)
from emp b
where b.sal<a.sal_min_med);

4. Angajaţii care au acelaşi an al angajării şi comision cu cei ce au aceiaşi grupă salarială cu şeful lor.
R :
select *
from emp e
where (to_char(e.hiredate,'yyyy'),NVL(e.comm,-1)) in (select to_char(a.hiredate,'yyyy'),NVL(a.comm,-1)
from emp a,salgrade s,salgrade x,emp b
where a.mgr=b.empno and
a.sal between s.losal and s.hisal and
b.sal between x.losal and x.hisal and
x.grade=s.grade);

You might also like