Professional Documents
Culture Documents
02 Complete Day2 Queries
02 Complete Day2 Queries
50000000;
grant all on dbname to twm;
select database;
database DINESH;
/* STRING FUNCTIONS*/
/* 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;
/* 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;