0% found this document useful (0 votes)
37 views11 pages

Rank/: Functii Pentru Ranking Si Windowing - de Pe E-Learning

The document discusses various SQL analytic functions like RANK(), DENSE_RANK(), ROW_NUMBER() and provides examples of using these functions to return ranked results based on partitions and ordering. It also provides exercises with solutions to help understand these analytic functions in different contexts like finding the highest paid employee in each department, the top 3 highest paid employees across departments, minimum salary earners for each job etc.

Uploaded by

IoanaMiruna
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
37 views11 pages

Rank/: Functii Pentru Ranking Si Windowing - de Pe E-Learning

The document discusses various SQL analytic functions like RANK(), DENSE_RANK(), ROW_NUMBER() and provides examples of using these functions to return ranked results based on partitions and ordering. It also provides exercises with solutions to help understand these analytic functions in different contexts like finding the highest paid employee in each department, the top 3 highest paid employees across departments, minimum salary earners for each job etc.

Uploaded by

IoanaMiruna
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
You are on page 1/ 11

Functii pentru Ranking si Windowing – de pe e-learning

https://elearning.unitbv.ro/mod/page/view.php?id=23537
https://elearning.unitbv.ro/mod/page/view.php?id=23538
https://elearning.unitbv.ro/mod/page/view.php?id=23539
https://www.oracletutorial.com/oracle-analytic-functions/oracle-
rank/

ROW_NUMBER() OVER (PARTITION BY… ORDER BY…), ORDER BY E optional

The RANK() function is useful for top-N and bottom-N queries.

EXEMPLU: NU

select last_name, salary,department_id,

Rank() over(order by salary) rnk,

dense_rank() over(order by salary) d_rnk,

row_number() over( order by salary) rn,

Rank() over(partition by department_id order by salary) p_did_rnk,

dense_rank() over(partition by department_id order by salary) p_did_d_rnk,

row_number() over(partition by department_id order by salary) p_did_rn

,Rank() over(partition by department_id order by salary) p_did_rnk_avg

,sum(salary) over (partition by department_id order by salary) sum_sal_p_did

,round(avg(salary) over (partition by department_id order by salary)) sum_sal_p_did

from employees

order by department_id;
EXERCITIU 1 : Salariile celor mai bine platiti salariati din fiecare dep (fiecare – partition by):

 CE AM FACUT EU  fata de ce e mai sus, la mine nu ia cel cu dep=null


SELECT * FROM (SELECT

LAST_NAME,

D.DEPARTMENT_ID,

D.DEPARTMENT_NAME,

SALARY,

RANK() OVER(PARTITION BY D.DEPARTMENT_ID ORDER BY SALARY DESC) AS


CEL_MAI_BINE_PLATIT_ANG

FROM employees INNER JOIN DEPARTMENTS D

ON d.department_id=employees.department_id)

WHERE CEL_MAI_BINE_PLATIT_ANG=1;
EXERCITIU 2: TOP 3 ANGAJATI BINE PLATITI (cred ca din fiecare dep):

 Ce am facut eu:
SELECT * FROM (SELECT

LAST_NAME,

D.DEPARTMENT_ID,

D.DEPARTMENT_NAME,

SALARY,

RANK() OVER(PARTITION BY D.DEPARTMENT_ID ORDER BY SALARY DESC) AS CEL_MAI_BINE_PLATIT_ANG

FROM employees INNER JOIN DEPARTMENTS D

ON d.department_id=employees.department_id)

WHERE CEL_MAI_BINE_PLATIT_ANG<=3;
😊 EXERCITIU 3 : Angajatii cu salariu min pt fctia lor si nu sunt manager, president:

 CE AM FACUT EU:

select * from (select

first_name,

last_name,

job_id,

salary,

rank() over(partition by job_id order by salary) as sal_min_per_job

from employees

where job_id not like ('%MAN') AND job_id not like ('%MGR') AND job_id not like ('%PRESIDENT'))

where sal_min_per_job=1;
😊 EXERCITIU 4 : SALARIUL MIN PT UN JOB DIN CADRUL UNUI DEP:

select * from (select

first_name,

last_name,

job_id,

department_id,

salary,

rank() over(partition by job_id, department_id order by salary desc) as sal_min_per_job_si_dep

from employees)

where sal_min_per_job_si_dep=1;
EXERCITIU 5 - ULTIMII ANGAJATI (DIN FIECARE DEP CU PARTITION) SI GLOBAL(FARA PARTITION):

☹ EXERCITIU 6: DEPARTAMENTUL CU CEL MAI MARE TOTAL AL SALARIILOR: TODO

 PAS1 :

 PAS2 :

 PAS3:
 PAS4:

SELECT * FROM( SELECT DEPARTMENT_ID, SAL_TOTAL,

RANK() OVER(ORDER BY SAL_TOTAL DESC) RNK,

ROW_NUMBER() OVER(ORDER BY SAL_TOTAL DESC) RN

FROM(

SELECT DEPARTMENT_ID,

SUM(SALARY) OVER(PARTITION BY DEPARTMENT_ID) AS SAL_TOTAL

FROM EMPLOYEES))

WHERE RNK=1 AND RN=1;

Acelasi lucru cu group by:


EXERCITIU 7:In ce an s-au angajat cei mai multi in companie? TODO
select * from (

select AnAng,

max(NrCrtPersAng) as maxim,

row_number() over(order by max(NrCrtPersAng)) as nr

from (

select last_name, to_char(hire_date, 'yyyy') as AnAng,

row_number() over(partition by to_char (hire_date, 'yyyy') order by to_char(hire_date, 'yyyy')) as


NrCrtPersAng

from employees)

group by AnAng

order by maxim desc

) where nr=1;
select distinct to_char (hire_date, 'yyyy') as AnAng,

count(*) over(partition by to_char(hire_date, 'yyyy')) nr

from employees

order by nr desc;

 CE AM FACUT EU – MI-A DAT CA MAI SUS

SELECT * FROM(

SELECT ROW_NUMBER() OVER(ORDER BY NR_ANGAJATI_PER_AN DESC) AS NR_RAND,

AN_ANG,
NR_ANGAJATI_PER_AN

FROM(

SELECT DISTINCT TO_CHAR(HIRE_DATE, 'YYYY') AS AN_ANG,

COUNT(*) OVER(PARTITION BY TO_CHAR(HIRE_DATE, 'YYYY')) AS NR_ANGAJATI_PER_AN

FROM employees)

ORDER BY NR_ANGAJATI_PER_AN DESC)

WHERE NR_RAND=1;

EXERCITIU 8 : ORASELE CU CELE MAI MULTE DEPARTAMENTE TODO

You might also like