Abhisql
Abhisql
--Round Function
(id number,
value number
);
--TRUNCATE
--REGEXP
--DATE
select hire_date from hr.employees;--will give all the dates of the table
( id number,
name varchar(40),
doj date);
desc datex;
id number,
name varchar(50),
doj date);
(emp_id number,
emp_name varchar(50),
doj date);
--DATE FUNCTIONS
select hire_date, LAST_DAY(hire_date) as last_date from hr.employees;--gives the last date of the
month of the date given
--TRUNCATE
select TRUNC(SYSDATE,'DAY') from dual;-- gives the first day of the week
select TRUNC(SYSDATE,'YEAR') from dual; --gives the first day of the year
select extract (YEAR FROM sysdate) FROM DUAL; --gives the year from the sysdate
select extract (DAY FROM sysdate) FROM DUAL;--gives the day(1,2,4,13,23,etc.) of the system
date/current date
sid number,
sname varchar(40)
);
sport varchar(50)
);
emp_id number,
ename varchar(40),
did number);
did number,
dname varchar(20)
);
-- JOIN QUERIES
select e.emp_id,e.ename,d.did,d.dname from emp e inner join dept d on e.did=d.did;-- inner join
select e.emp_id,e.ename,d.did,d.dname from emp e left join dept d on e.did=d.did;--left outer join
select e.emp_id,e.ename,d.did,d.dname from emp e full join dept d on e.did=d.did;--full outer join
--SELF JOIN
select
e.employee_id,e.first_name,e.last_name,m.employee_id,m.first_name,m.last_name from
hr.employees e
join
hr.employees m
--GROUP FUNCTIONS
--GROUP BY
SELECT JOB_ID, MAX(SALARY) FROM HR.EMPLOYEES GROUP BY JOB_ID;--WILL GIVE THE MAXIMUM
SALARY OF EVERY DEPT.
--ANALYTICAL FUNCTIONS
--ROW_NUMBER
--RANK
--DENSE RANK
select employee_id,salary,
--gives the row number to every value according to the column name given in the orderby clause.
But not suitable for duplicate values
select employee_id,salary,job_id,
ROW_NUMBER() OVER (ORDER BY job_id) FROM HR.EMPLOYEES;
select employee_id, salary, row_number() over (order by employee_id desc) rn from hr.employees;--
will rank the highest employee ID as 1st and continue raanking in descending oreder
select employee_id, salary, rank() over (order by salary desc) rn from hr.employees;
--if the value at rank 2 and 3 are similar, both will be assigned 2 as their rank and 3 will be skipped in
numbering. Not suitable for
--repetitive values.
select employee_id, salary, dense_rank() over (order by salary desc) rn from hr.employees;
-- if two values are same at position 2, both will be assigned the value 2 and the following as3,4,5,etc
select employee_id,job_id,salary,dense_rank()
over (partition by job_id order by salary desc) dn from hr.employees order by job_id;-- will allot
ranks according to salary in eacg department seperately.
select employee_id,job_id,salary,dense_rank()
over ( order by salary desc) dn from hr.employees ;--will give the ranks , this time, without
combining them into different departments.
--FIRST VALUE
select employee_id,job_id,salary,
-- a column dn will be formed that contains the first value of salary in every department.
-- suppose every dept is an array,containing the salaries of employees within the dept, in ascending
order.
-- so first value will return the first salary in (the lowest in this case) in every dept.'s array.
select employee_id,job_id,salary,
--LAST VALUE
select employee_id,job_id,salary,
last_value(salary) over (partition by job_id order by salary rows between unbounded preceding and
unbounded following ) from hr.employees;
--this will give the last salary in every dept. ordered in ascending order
--LAG FUNCTION
--LEAD
select listagg(first_name,':')
select job_id,listagg(first_name,':')
--SUBQUERY
-- fetch the details of employees who are earning more than Diana in hthe hr.employee table
select * from hr.employees where first_name='Diana'; --first fetch the details of Diana
select * from hr.employees where salary>
with sample_one as
--this sample_one will act as a sperate table which stores the particular values fetched from
--this sample one will be treated as variable that stores some value from the original table
--and won't execute or return any values outside this statement block,after the semicolon
with temporary_tb as
--the result of this full join will be stored in table temporary_tb and will only exist till the execution
of
--SET OPERATORS
e_id number,
e_loc varchar(20)
);
d_id number,
d_loc_name varchar(40)
);
--UNION
UNION
--UNION ALL
UNION ALL
--INTERSECT
INTERSECT
--MINUS(it works like set A- setB--> will give all the values that belong to set A and do not belong in
Set B)
select e_loc from cityemp
MINUS
--SYS_CONNECT_BY_PATH
SYS_CONNECT_BY_PATH(first_name,',')
from hr.employees