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

Abhisql

The document provides a comprehensive overview of SQL functions and queries, including number functions, date functions, joins, group functions, analytical functions, and subqueries. It includes examples of creating tables, inserting data, and executing various SQL commands to manipulate and retrieve data. Additionally, it covers advanced topics such as common table expressions (CTE), set operators, and hierarchical retrieval queries.

Uploaded by

kr.sakshee
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)
9 views11 pages

Abhisql

The document provides a comprehensive overview of SQL functions and queries, including number functions, date functions, joins, group functions, analytical functions, and subqueries. It includes examples of creating tables, inserting data, and executing various SQL commands to manipulate and retrieve data. Additionally, it covers advanced topics such as common table expressions (CTE), set operators, and hierarchical retrieval queries.

Uploaded by

kr.sakshee
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

-- NUMBER FUNCTIONS

--Round Function

create table roundtest

(id number,

value number

);

insert into roundtest values(1,345.43356575757);

insert into roundtest values(2,34.4335657544667);

insert into roundtest values(3,67.56575757);

insert into roundtest values(4,398.43356578757);

insert into roundtest values(5,5.43356575757);

select * from roundtest;

select value, round(value,2) from roundtest;

select value, round(value,0) from roundtest;

--TRUNCATE

select value, trunc(value,2) from roundtest;

--ROW NUM(onlygives the data of the number of rows given

select * from hr.employees where rownum<=6;

select * from(select * from hr.employees order by salary desc) where rownum<10;

select * from hr.employees where rownum<20;

--REGEXP

select * from hr.employees where regexp_like(first_name,'^A(*)');

select * from hr.employees where regexp_like(first_name,'(*)r$');

--DATE

select hire_date from hr.employees;--will give all the dates of the table

select hire_date ,TO_CHAR(hire_date,'DD/MM/YY') from hr.employees;

select hire_date ,TO_CHAR(hire_date,'DD/Month/YY') from hr.employees;


select hire_date ,TO_CHAR(hire_date,'DD/MM/Year') from hr.employees;

select * from hr.employees where to_char(hire_date,'MM')='02';

select * from hr.employees where to_char(hire_date,'YYYY')='2005';

create table datex

( id number,

name varchar(40),

doj date);

desc datex;

insert into datex values(101,'Shivangi',TO_DATE('17-02-2025','DD-MM-YYYY'));

select doj, to_char(doj,'Dy') from datex;

insert into datex values(102,'Harshita',TO_DATE('17-June-2025','DD-Month-YYYY'));

select sysdate from dual; --gives the date of the system

select to_char(sysdate,'Dy')from dual; --gives the day according to system clock

create table datexx(

id number,

name varchar(50),

doj date);

insert into DATEXX (name) values('20-JAN-1993');

select to_date(name, 'DD-MM-YYYY') from datexx;

select to_date(name, 'DD-MM-YYYY','DY') from datexx;

select TRUNC((sysdate -to_date('06-02-2000','DD-MM-YYYY'))/365) from dual;

create table employee

(emp_id number,

emp_name varchar(50),
doj date);

insert into employee values(101,'Kashish',TO_DATE('17-02-2005','DD-MM-YYYY'));

insert into employee values(102,'Anuradha',TO_DATE('13-03-2002','DD-MM-YYYY'));

insert into employee values(104,'Kunal',TO_DATE('17-02-2014','DD-MM-YYYY'));

select * from employee;

--FIND THE EMPLOYEE ID, EMPLOYEE AND THEIR YEAR OF EXPERIENCE.

select emp_id,emp_name, TRUNC((sysdate -to_date(doj,'DD-MM-YYYY'))/365) as YEO from


employee;

--DATE FUNCTIONS

select * from hr.employees;

select TRUNC(MONTHs_BETWEEN(SYSDATE, hire_date)) from hr.employees; --will give the number of


months between current date and DOJ

select hire_date,ADD_MONTHS(hire_date,6) as SIX_Months from hr.employees; --adds tow months


extra to the given date.

select hire_date, NEXT_DAY(hire_date,'MONDAY')from hr.employees; --gives the next date occurence


of Monday from the current date.

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 TRUNC(SYSDATE,'MM') from dual;--gives the first day of the month

select TRUNC(hire_date,'Q'),hire_date from hr.employees;--gives the first day of the quarter(quarter


is one fourth 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

select extract (MONTH FROM SYSDATE) FROM DUAL;

select extract (hour from systimestamp) from dual;


--JOIN

--Cross Join-- artesian Join/Cartesian pRODUCT

-- M*N records will get in output

create table stud(

sid number,

sname varchar(40)

);

create table SPORTS

sport varchar(50)

);

INSERT INTO STUD VALUES(1,'Kashish');

insert into stud values(2,'Manish');

insert into stud values(3,'Manu');

insert into stud values(4,'Kartik');

select * from stud;

insert into sports values('Chess');

insert into sports values('Football');

select * from stud cross join sports;

truncate table sports;

--OUTER AND INNER JOIN

create table emp(

emp_id number,

ename varchar(40),
did number);

drop table dept;

create table dept(

did number,

dname varchar(20)

);

insert into emp values(1,'Rishi',10);

insert into emp values(2,'Deep',20);

insert into emp values(3,'Siddhartha',30);

insert into emp values(4,'Jhanvi',50);

delete from emp where did=40;

select * from emp;

insert into dept values(10,'IT');

insert into dept values(20,'MGMT');

insert into dept values(30,'FIN');

insert into dept values(40,'HR');

select * from dept;

-- JOIN QUERIES

select * from emp inner join dept on emp.did=dept.did;

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 right join dept d on e.did=d.did;--right 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

on m.manager_id=e.employee_id order by e.employee_id;

select * from hr.employees where upper(last_name)='ROY';

--GROUP FUNCTIONS

SELECT MAX(SALARY) FROM HR.EMPLOYEES;

SELECT COUNT(COMMISSION_PCT) FROM HR.EMPLOYEES;

SELECT (AVG(SALARY)) FROM HR.EMPLOYEES;

SELECT SUM(SALARY) FROM HR.EMPLOYEES;

SELECT MIN(SALARY) FROM HR.EMPLOYEES;

SELECT VARIANCE(SALARY) FROM HR.EMPLOYEES;

SELECT STDDEV(SALARY) FROM HR.EMPLOYEES;

--GROUP BY

SELECT JOB_ID,SUM(SALARY) FROM HR.EMPLOYEES GROUP BY JOB_ID;--WILL GIVE THE SUM OF


SALARIES FOR EACH DEPARTMENT.

SELECT JOB_ID, MAX(SALARY) FROM HR.EMPLOYEES GROUP BY JOB_ID;--WILL GIVE THE MAXIMUM
SALARY OF EVERY DEPT.

SELECT JOB_ID, MAX(SALARY) FROM HR.EMPLOYEES GROUP BY JOB_ID HAVING


MAX(SALARY)>8000;--WILL ONLY DISPLAY THE MAX SALARY OF DEPT WHERE MAX SAL>8000

SELECT JOB_ID, MAX(SALARY) FROM HR.EMPLOYEES GROUP BY JOB_ID HAVING


SUM(SALARY)>30000;

--ANALYTICAL FUNCTIONS

--ROW_NUMBER

--RANK

--DENSE RANK

select employee_id,salary,

ROW_NUMBER() OVER (ORDER BY SALARY) FROM HR.EMPLOYEES;

--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

-- does not assign corrrect rank in case of repetitive values.

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;

--does not skip any rank in case of similar values.

-- if two values are same at position 2, both will be assigned the value 2 and the following as3,4,5,etc

-- best suited for calculating ranks even with repetitive values.

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,

first_value(salary) over (partition by job_id order by salary ) dn

from hr.employees order by job_id;

-- 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,

first_value(salary) over(partition by job_id order by salary desc)


from hr.employees;

--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

select employee_id,job_id,salary, LAG(salary,1,0) over (order by salary) from hr.employees; --shifts


the values one row down.

--LEAD

select employee_id,job_id,salary, LEAD(salary,1,0) over (order by salary) from hr.employees; --shifts


the value one row up.

--LIST AGGREGATE FUNCTION-(LISTAGG)

--used for string aggregation.

--(USED TO CLUB ALL VALUES TOGETHER IN ONE ROW)

select listagg(first_name,':')

within group (order by job_id) from hr.employees;

select job_id,listagg(first_name,':')

within group (order by job_id) from hr.employees group by job_id;

--will order all the names, in a single row, department wise.

--employyes of each department will be written in a single row

--based on their dept column.

--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>

(select salary from hr.employees where first_name='Diana');

--Find the details of employees with highest salary

select * from hr.employees where salary=

(select max (salary) from hr.employees);

--fetch the details of the people working in AD_VP OR AD_PRES department.

select * from hr.employees where salary NOT IN

(select salary from hr.employees where job_id='AD_VP' or job_id='AD_PRES') order by job_id;

--WUTH CTE(Common Table Expression)

with sample_one as

(select employee_id, first_name ,job_id

from hr.employees where job_id='IT_PROG')

select * from sample_one;

--this sample_one will act as a sperate table which stores the particular values fetched from

--the hr.employee table;

--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

(select e.emp_id,e.ename,d.did,d.dname from emp e full join dept d on e.did=d.did)

select * from temporary_tb where dname='HR';

--the result of this full join will be stored in table temporary_tb and will only exist till the execution
of

--statements within the semi-colon

--SET OPERATORS

create table cityemp(

e_id number,
e_loc varchar(20)

);

insert into cityemp values(1,'HYD');

insert into cityemp values(2,'HYD');

insert into cityemp values(3,'BLR');

insert into cityemp values(4,'CHN');

create table city_dept(

d_id number,

d_loc_name varchar(40)

);

insert into city_dept values(101,'HYD');

insert into city_dept values(102,'GGN');

--UNION

select e_loc from cityemp

UNION

select d_loc_name from city_dept;

--UNION ALL

select e_loc from cityemp

UNION ALL

select d_loc_name from city_dept;

--INTERSECT

select e_loc from cityemp

INTERSECT

select d_loc_name from city_dept;

--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

select d_loc_name from city_dept;

--HIERARCHICAL RETRIEVAL QUERY

select first_name, employee_id,last_name, manager_id from hr.employees

start with manager_id is null

connect by manager_id=prior employee_id;

--SYS_CONNECT_BY_PATH

select first_name, employee_id,last_name, manager_id,LEVEL,

SYS_CONNECT_BY_PATH(first_name,',')

from hr.employees

start with manager_id is null

connect by manager_id=prior employee_id;

--used to concatenate the tables with hiearchies defined

You might also like