0% found this document useful (0 votes)
2 views4 pages

18apr2025 Nasim

The document contains SQL queries related to employee data management, focusing on data selection, conversion functions, and aggregation. It covers various SQL operations such as joins, null handling, and salary calculations using case and decode functions. Additionally, it demonstrates grouping and filtering of employee records based on hire dates and department information.

Uploaded by

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

18apr2025 Nasim

The document contains SQL queries related to employee data management, focusing on data selection, conversion functions, and aggregation. It covers various SQL operations such as joins, null handling, and salary calculations using case and decode functions. Additionally, it demonstrates grouping and filtering of employee records based on hire dates and department information.

Uploaded by

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

---18042025(class 04)

select
*from employees;

select to_char(sysdate, 'dd-mm-yyyy HH12:MI:SS') from dual;


---Chapter-05
---Using conversion funtion
select employee_id, last_name, to_char(salary,'99,99,999.00') "Salary", job_id,
hire_date from employees ;
select employee_id, last_name, to_char(salary,'99,99,999.99') "Salary", job_id,
hire_date from employees ;
---G for Group
select employee_id, last_name, to_char(salary,'99G99G999D99') "Salary", job_id,
hire_date from employees ;
select employee_id, last_name, to_char(salary,'99,99,999.00') "Salary", job_id,
to_char(hire_date,'DD/MM/YYYY') from employees ;
---Release extra space
select employee_id, last_name, to_char(salary,'99,99,999.00') "Salary", job_id,
to_char(hire_date,'fmDay "of" Month, YYYY') HIREDATE
from employees ;
select employee_id, last_name, to_char(salary,'99,99,999.00') "Salary", job_id,
to_char(hire_date,'fmddspth "of" Month, YYYY') HIREDATE
from employees ;
----Convert number
select employee_id, last_name, to_char(salary,'99,99,999.00') "Salary", job_id,
hire_date
from employees where salary> to_number('10,000','99,999');
---Character only equal expect

select employee_id, last_name, to_char(salary,'99,99,999.00') "Salary", job_id,


hire_date
from employees where trim(to_char(salary,'99,999'))= '10,000';
---convert Date
---conversion is two type i. auto conversion(implecit) ii. manual(explecit)
conversion
---Auto conversion
select employee_id, last_name, to_char(salary,'99,99,999.00') "Salary", job_id,
hire_date
from employees where department_Id= '40';

select employee_id, last_name, to_char(salary,'99,99,999.00') "Salary", job_id,


hire_date
from employees where hire_date = '07/june/02';
---Date convsion
select employee_id, last_name, to_char(salary,'99,99,999.00') "Salary", job_id,
hire_date
from employees where hire_date = to_date('07-06-02','dd-mm-yy');

select employee_id, last_name, to_char(salary,'99,99,999.00') "Salary", job_id,


hire_date
from employees where to_char(hire_date,'yyyymm') = '200305';
--Null convert actual value(number,date, character) value control
select employee_id, last_name, salary, nvl(commission_pct,0) "Comm"
from employees;
select employee_id, last_name, salary, nvl(commission_pct,0) "Comm",
salary*nvl(commission_pct,0) "Comm" , salary*nvl(commission_pct,0)+salary "Total
Salary"
from employees;
---nvl2(three parameters) Condition Check kore first argument is true then then
third expreesion execute hbe othewise second expression execute hbe.
select employee_id, last_name, salary, nvl2(commission_pct, salary +
salary*commission_pct,salary) "Comm", salary*nvl(commission_pct,0) "Comm" ,
salary*nvl(commission_pct,0)+salary "Total Salary"
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

select employee_id, first_Name,last_name, salary, manager_id, commission_pct,


coalesce(manager_id,commission_pct,salary) "coalesce"
from employees;
---case is expression decode is function
select employee_id, first_Name,last_name, job_id,salary,
case when job_id= 'IT_PROG' then salary*1.10
when job_id like '%MAN%' then salary*1.15
when job_id in ('SA_REP', 'ST_CLERK') then salary*1.12
else salary*1.20 end "New Salary"
from employees;

select employee_id, first_Name,last_name, job_id,salary,


case job_id when 'IT_PROG' then salary*1.10
when 'SA_MAN' then salary*1.15
when 'SA_REP' then salary*1.12
else salary*1.20 end "New Salary"
from employees;
---Decode only work with equl condition
select employee_id, first_Name,last_name, job_id,salary,
decode(job_id, 'IT_PROG', salary*1.10
,'SA_MAN', salary*1.15
,'SA_REP', salary*1.12
,salary*1.20 ) "New Salary"
from employees;
--Difference between yy and rr
--0-49 current century or 50-99
--achi 0-49 disee 50-99 se bujbe prvious century
select employee_id, first_Name,last_name, job_id,salary,
decode(job_id, 'IT_PROG', salary*1.10
,'SA_MAN', salary*1.15
,'SA_REP', salary*1.12
,salary*1.20 ) "New Salary"
from employees where hire_date = to_char('24-Jul-05');
--Chapter -06
--Aggregated Function get group row return single row.
--group function automatically avoid null value.
select count(*), count(commission_pct),sum(salary) "Total Salary",
max(salary), min(salary), round(avg(salary)) "Avg Salary",
round(avg(commission_pct),2) "Agv Commission"
from employees;

select Department_id, count(*), count(commission_pct),sum(salary) "Total Salary",


max(salary), min(salary), round(avg(salary)) "Avg Salary",
round(avg(commission_pct),2) "Agv Commission"
from employees group by department_id;

---salary is greater than 50000


select Department_id, count(*), count(commission_pct),sum(salary) "TotalSalary",
max(salary), min(salary), round(avg(salary)) "Avg Salary",
round(avg(commission_pct),2) "Agv Commission"
from employees group by department_id having sum(salary)>=50000;

---list aggregate
select department_id, listagg(last_name,',') within group (order by last_name)
"Listagg"
from employees group by department_id;

-- joinig after 2004, department_id, sum salary

select Department_id, sum(salary) "TotalSalary", to_char(hire_date,'yyyy')


from employees where to_char(hire_date,'yyyy') > '2004'
group by department_id, to_char(hire_date,'yyyy');

select sum(salary) "TotalSalary", to_char(hire_date,'yyyy')

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;

select count(distinct department_id)

from employees;

select distinct department_id

from employees where department_id is not null;

select max(avg(salary))

from employees group by department_id;


--Chapter 7
---joning
--inner join(matched data retrieved)
select e.employee_id, e.last_name, d.department_name, e.salary
from employees e, departments d
where e.department_id = d.department_id;

select e.employee_id, e.last_name, d.department_name, e.salary


from employees e join departments d
on (e.department_id = d.department_id);

select e.employee_id, e.last_name, d.department_name, e.salary


from employees e join departments d
using(department_id);

select e.employee_id, e.last_name, j.job_title, e.salary


from employees e natural join jobs j ;

select e.employee_id, e.last_name, d.department_name, e.salary, j.job_title,


l.city
from employees e, departments d,jobs j, locations l
where e.department_id = d.department_id
and e.job_id = j.job_id
and d.location_id = l.location_id;
---outer join
---left, right, full outer join
--left outer join
select e.employee_id, e.last_name, d.department_name, e.salary
from employees e left join departments d
on ( e.department_id = d.department_id);
select e.employee_id, e.last_name, d.department_name, e.salary
from employees e , departments d
where e.department_id = d.department_id(+);--left outer join

select e.employee_id, e.last_name, d.department_name, e.salary


from employees e , departments d, jobs j
where e.department_id = d.department_id(+)
and e.job_id = j.job_id;

select e.employee_id, e.last_name, d.department_name, e.salary


from employees e right join departments d
on ( e.department_id = d.department_id);

select e.employee_id, e.last_name, d.department_name, e.salary


from employees e full outer join departments d
on ( e.department_id = d.department_id);
--self join(like inner join)
select e.last_name, e.salary, m.last_name "Manager"
from employees e, employees m
where e.manager_id = m.employee_id(+);

You might also like