18apr2025 Nasim
18apr2025 Nasim
select
*from employees;
--nullif if length is equal then return null otherwise first expression will return
select employee_id, first_Name,last_name, salary,
nullif(length(first_name),length(last_name)) "Length"
from employees;
---Coalesce(parameter unlimited) search until get actual value
---list aggregate
select department_id, listagg(last_name,',') within group (order by last_name)
"Listagg"
from employees group by department_id;
from employees
group by to_char(hire_date,'yyyy');
select department_id,
sum(CASE when to_char(hire_date,'yyyy') = 2001 then 1 else 0 end ) "2001",
sum(CASE when to_char(hire_date,'yyyy') = 2002 then 1 else 0 end ) "2002" ,
sum(CASE when to_char(hire_date,'yyyy') = 2003 then 1 else 0 end ) "2003" ,
sum(CASE when to_char(hire_date,'yyyy') = 2004 then 1 else 0 end ) "2004" ,
sum(CASE when to_char(hire_date,'yyyy') = 2005 then 1 else 0 end ) "2005" ,
sum(CASE when to_char(hire_date,'yyyy') = 2006 then 1 else 0 end ) "2006" ,
sum(CASE when to_char(hire_date,'yyyy') = 2007 then 1 else 0 end ) "2007" ,
sum(CASE when to_char(hire_date,'yyyy') = 2008 then 1 else 0 end ) "2008"
from employees
group by department_id order by 1;
---using count
select department_id,sum(CASE when to_char(hire_date,'yyyy') = 2001 then 1 else 0
end ) "2001",
count(CASE when to_char(hire_date,'yyyy') = 2002 then 1 end ) "2002" ,
count(CASE when to_char(hire_date,'yyyy') = 2003 then 1 end ) "2003" ,
count(CASE when to_char(hire_date,'yyyy') = 2004 then 1 end ) "2004" ,
count(CASE when to_char(hire_date,'yyyy') = 2005 then 1 end ) "2005" ,
count(CASE when to_char(hire_date,'yyyy') = 2006 then 1 end ) "2006" ,
count(CASE when to_char(hire_date,'yyyy') = 2007 then 1 end ) "2007" ,
count(CASE when to_char(hire_date,'yyyy') = 2008 then 1 end ) "2008"
from employees
group by department_id order by 1;
from employees;
select max(avg(salary))