You are on page 1of 9

create database dbname as perm=50000000 spool =

50000000;
grant all on dbname to twm;

select database;

database DINESH;

select * from EMP;

/* STRING FUNCTIONS*/

select LOWER(ENAME) from emp;


select LENGTH(ENAME) from emp;
select SUBSTR('dineshkarthik',3,10);

/* DATE FUNCTIONS*/
select date;
select cast(date as varchar(20));
select date + interval '2' day;
select date + interval '2' month;
select date + interval '2' year;
select (date -2);
select extract (day from date);
select extract (month from date);
select extract (year from date);
/* JOIN*/
select * from emp;
select * from dept;
/* CARTESIAN JOIN */
select e.empno,e.ename,e.sal,d.deptno,d.dname,d.loc
from emp e ,dept d;
/* INNER JOIN*/
select e.empno,e.ename,e.sal,e.deptno,d.dname,d.loc
from EMP e INNER JOIN DEPT d
ON e.deptno = d.deptno;
/* LEFT OUTER JOIN */
select e.empno,e.ename,e.sal,e.deptno,d.dname,d.loc
from EMP e LEFT JOIN DEPT d
ON e.deptno = d.deptno;
/* RIGHT OUTER JOIN*/
select e.empno,e.ename,e.sal,d.deptno,d.dname,d.loc
from EMP e RIGHT JOIN DEPT d
ON e.deptno = d.deptno;
/*FULL OUTER JOIN*/
select e.empno,e.ename,e.sal,e.deptno,d.dname,d.loc
from EMP e FULL OUTER JOIN DEPT d
ON e.deptno = d.deptno;
/*SLEF JOIN*/
select * from emp;
select e.empno,e.ename,m.ename,m.mgr from emp
e,emp m where e.mgr = m.empno;
/* WORKING WITH SET OPERATORS*/

/*UNION*/
create table emp_10 as (select empno,ename,sal,deptno
from emp where deptno = 10) with data;
select * from emp_10;
create table emp_20 as (select empno,ename,sal,deptno
from emp where deptno = 20) with data;
select * from emp_20;
select * from emp_10 union select * from emp_20;

/*UNION ALL*/
create table emp_10_20 as (select
empno,ename,sal,deptno from emp where deptno
in(10,20)) with data;
select * from emp_10_20;
create table emp_20_30 as (select
empno,ename,sal,deptno from emp where deptno
in(20,30))with data;
select * from emp_20_30;
select * from emp_10_20 union all select * from
emp_20_30;
select * from emp_10_20 union select * from emp_20_30;

/*INTERSECT OPERATOR -- (AnB) - common value b/w


both tables*/
create table in1_dept as (select * from dept) with data;
create table in2_dept as (select * from dept) with data;
select * from in1_dept;
select * from in2_dept;
insert into in1_dept values(50,'IT','USA');
insert into in1_dept
values(60,'PROCUREMENT','NEWYORK');
select * from in1_dept intersect select * from in2_dept;

/*MINUS or EXCEPT -- subtraction*/


select * from in1_dept minus select * from in2_dept;
select * from in1_dept except select * from in2_dept;
select * from in2_dept except select * from in1_dept;

/* WORKING WITH MULTI ROW FUNCTIONS -


AGGREGATIONS*/
/* SUM,MIN,MAX,AVG,COUNT,VARIENCE,STDDEV...*/

/* BASIC SIMPLE AGG FUNCTIONS*/


select * from emp;
select sum(sal) from emp;
select max(sal) from emp;
select min(sal) from emp;
select avg(sal) from emp;
select count(*) from emp;

/*USING CLAUSES INAGGREGATE*/


create table emp_aggr(id int,name varchar(20),sal
int,gender varchar(10),city varchar(20));
insert into
emp_aggr(1,'ARAVIND',1000,'MALE','CHENNAI');
insert into
emp_aggr(2,'AZEEM',1400,'MALE','HDERABAD');
insert into emp_aggr(3,'ABHI',1450,'FEMALE','CHENNAI');
insert into
emp_aggr(4,'AISHU',2000,'FEMALE','HDERABAD');
insert into
emp_aggr(5,'DINESH',1600,'MALE','BANGALORE');
insert into
emp_aggr(6,'KISHORE',1700,'MALE','CHENNAI');
insert into
emp_aggr(7,'LAKSHMI',2400,'FEMALE','BANGALORE');
insert into
emp_aggr(8,'POOJA',3400,'FEMALE','BANGALORE');
insert into
emp_aggr(9,'KRISHNA',3600,'MALE','CHENNAI');
insert into
emp_aggr(10,'ARUN',5000,'MALE','HDERABAD');
select * from emp_aggr;

/* CITY WISE SUM(SAL) */


SELECT CITY,SUM(SAL) FROM EMP_AGGR GROUP
BY CITY;

/* CITY AND GENDER WISE SUM(SAL) */


SELECT CITY,GENDER, SUM(SAL) FROM EMP_AGGR
GROUP BY CITY,GENDER;
SELECT GENDER,CITY, SUM(SAL) FROM EMP_AGGR
GROUP BY GENDER,CITY;

/* MULTIPLE AGG FUNCTION ON A SAME QUERY*/


SELECT CITY,GENDER, SUM(SAL),COUNT(ID) AS
TOTAL_EMP FROM EMP_AGGR GROUP BY
CITY,GENDER;
/* FILTERING THE RESULT USING WHERE CLAUSE*/
SELECT CITY,GENDER, SUM(SAL),COUNT(ID) AS
TOTAL_EMP
FROM EMP_AGGR WHERE GENDER = 'MALE' GROUP
BY CITY,GENDER;

/* FILTERING THE RESULT USING HAVING CLAUSE*/


SELECT CITY,GENDER, SUM(SAL),COUNT(ID) AS
TOTAL_EMP
FROM EMP_AGGR GROUP BY CITY,GENDER HAVING
GENDER='MALE';

/* SORTING THE RESULT USING ORDER BY CLAUSE*/


SELECT CITY,GENDER, SUM(SAL),COUNT(ID) AS
TOTAL_EMP
FROM EMP_AGGR GROUP BY CITY,GENDER ORDER
BY CITY ;

/* FILETRING THE RECORD BASED ON AGG COLUMN


-- USING HAVING CLAUSE*/
SELECT CITY,GENDER, SUM(SAL),COUNT(ID) AS
TOTAL_EMP
FROM EMP_AGGR GROUP BY CITY,GENDER HAVING
SUM(SAL)>=1500;

/* FILTERING THE RESULT BASED ON AGG COLUMN -


- USING WHERE CLAUSE*/
SELECT CITY,GENDER, SUM(SAL),COUNT(ID) AS
TOTAL_EMP
FROM EMP_AGGR GROUP BY CITY,GENDER WHERE
SUM(SAL) >=1500;
/* CONSTRAINTS*/
-- NOT NULL:
create table nn(id int not null,name varchar(10));
insert into nn values(1,'RAJESH');
insert into nn values(NULL,'RAKESH');
--UNIQUE:
create table un(id int not null unique,name varchar(10));
insert into un(1,'RAJESH');
insert into un(2,'RAKESH');
insert into un(1,'RAJESH');
insert into un(,'RAKESH');
--CHECK:
create table emp_chk(id int,name varchar(10),sal int
check(sal>=1000));
insert into emp_chk(1,'RAJESH',1000);
insert into emp_chk(2,'RAKESH',2000);
insert into emp_chk(3,'SOMESH',999);
--DEFAULT:
create table emp_def(id int,name varchar(10),city
varchar(10) default 'USA');
insert into emp_def(id,name)values(1,'SURESH');
insert into emp_def(2,'SOMESH');
select * from emp_Def;
insert into emp_def(2,'SOMESH','NEWYORK');
--PRIMARY KEY AND FOREIGN KEY:
--PARENT TABLE:
create table parent_table(deptno int not null primary
key,dname varchar(10));
create table child_table(empno int not null primary
key,ename varchar(10),
deptno int references parent_table(deptno));

/* SUBQUERIES*/
select * from emp;
--SINGLE ROW SUB_QUERY:
select ename,sal from emp where sal>(select sal from
emp where ename = 'ADAMS');
select ename,sal from emp where sal<(select sal from
emp where ename = 'ADAMS');
--MULTIROW SUBQUERY:
select * from emp where deptno=20 and sal >ANY(select
sal from emp where deptno=10);
--1300,2450,5000
select empno,ename,sal,job from emp where
sal>ALL(select sal from emp where deptno=10);
select * from emp;
--1300,2450,5000
select empno,deptno,ename,sal,job from emp where
sal>ANY(select sal from emp where deptno=10);
select * from emp;
--1300,2450,5000
select empno,deptno,ename,sal,job from emp where
sal<ALL(select sal from emp where deptno=10);
select * from emp;
--1300,2450,5000
select empno,deptno,ename,sal,job from emp where
sal<ANY(select sal from emp where deptno=10);
select * from emp;

You might also like