Spoolfiles 1

You might also like

Download as txt, pdf, or txt
Download as txt, pdf, or txt
You are on page 1of 18

> select deptno,sum(sal)

2 from emp
3 group by deptno
4 having deptno in(10,30)
5 order by sum(sal);

select deptno,count(*)
2 from emp
3 where deptno<>20
4 group by deptno
5 having count(*)> 4
6 order by deptno desc;

1 select deptno,sum(sal)
2 from emp
3 where deptno <=20
4 group by deptno
5 having sum(sal) > 10000
6* order by sum(sal)
SQL> /
DEPTNO

SUM(SAL)

---------- ---------20

10875

1 select empno,ename,sal,sal*5,sal*5+5,sal+5*5
2* from emp
SQL> /

1
2
3*
SQL>

select e.empno,e.ename,d.dname
from emp e,dept d
where e.deptno= d.deptno and d.dname like 'S%'
/
EMPNO ENAME

DNAME

---------- ---------- -------------7499 ALLEN

SALES

7521 WARD

SALES

7654 MARTIN

SALES

1 select e.empno,e.ename,d.dname
2* from emp e natural join dept d
1
2
3*
SQL>

select e.ename as employee, m.ename as mgr


from emp e join emp m
on e.mgr =m.empno
/

EMPLOYEE

MGR

---------- ---------FORD

JONES

SCOTT

JONES

select ename
2 from emp
3 where hiredate=(select min(hiredate) from emp);

1
2
3*
SQL>

select e.staff_name,mgr.staff_name as manager


from staff_master_720432 e join staff_master_720432 mgr
on e.mgr_code=mgr.staff_code
/

STAFF_NAME

MANAGER

-------------------------------------------------- ------------------------------------------------Arvind

Allen

Mohan

Allen

select ename
2 from emp
3* where sal >(select avg(sal) from emp)

select max(sal)
2 from emp
3* where sal <(select max(sal) from emp)
select max(sal)
2 from emp
3* where sal <(select max(sal) from emp where sal<(select max(sal) from emp))

select ename
2 from emp
3 where sal= (
4
select max(sal)
5
from emp
6
where sal < (select max(sal)
7
from emp where sal<(select max(sal) from emp)
8*
))
select rownum,empno,ename from emp;
select rownum,ename,sal
2 from(select ename,sal from emp order by sal desc)
3 where rownum < 5;
select empno,ename,mgr
2 from emp
3 start with empno =7839
4 connect by prior empno = mgr;
EMPNO ENAME

MGR

---------- ---------- ---------7839 KING


7566 JONES

7839

7788 SCOTT

7566

7876 ADAMS

7788

7902 FORD

7566

7369 SMITH

7902

7698 BLAKE

7839

7499 ALLEN

7698

7521 WARD

7698

7654 MARTIN

7698

7844 TURNER

7698

EMPNO ENAME

MGR

---------- ---------- ---------7900 JAMES

7698

7782 CLARK

7839

7934 MILLER

7782

select level,empno,ename,mgr
2 from emp
3 where level < 4
4 start with empno =7839
5* connect by prior empno = mgr
SQL> /
LEVEL

EMPNO ENAME

MGR

---------- ---------- ---------- ---------1

7839 KING

7566 JONES

7839

7788 SCOTT

7566

7902 FORD

7566

7698 BLAKE

7839

7499 ALLEN

7698

7521 WARD

7698

7654 MARTIN

7698

7844 TURNER

7698

7900 JAMES

7698

7782 CLARK

7839

LEVEL

EMPNO ENAME

MGR

---------- ---------- ---------- ---------3


7934 MILLER
1 select level,empno,ename,mgr
2 from emp
3 where level < 3
4 start with empno =7839
5* connect by prior empno = mgr
SQL> /
LEVEL

EMPNO ENAME

MGR

---------- ---------- ---------- ---------1

7839 KING

7566 JONES

7839

7698 BLAKE

7839

7782 CLARK

7839

1 select level,empno,ename,mgr
2 from emp
3 where level < 3
4 start with empno =7839
5* connect by prior empno = mgr

1
select e.staff_code,e.staff_name staff,b.staff_name as manager,e.mgr_code,d
.dept_name
2
from staff_master_720432 e join department_master_720432 d
3
on e.dept_code=d.dept_code
4
join staff_master_720432 b
5* on e.staff_code=b.mgr_code
SQL> /
STAFF_CODE STAFF

MANAGER
MGR_CODE DEPT_NAME

---------- -------------------------------------------------- ------------------------------------------------- ---------- ------------------------------------------------100006 Allen


Arvind
100005 Electronics
100006 Allen

Mohan
100005 Electronics

100006 Allen

Anil
100005 Electronics

100007 Smith

John
100005 Electricals

SQL> select replace('jack and jue', 'j','bl') from dual;


REPLACE('JACKA
-------------black and blue

SQL> select substr('helloworld',2,5) from dual;


SUBST
----ellow

SQL> ed
Wrote file afiedt.buf
1* select substr('helloworld',5,5) from dual

SQL> /
SUBST
----oworl

SQL> ed
Wrote file afiedt.buf
1* select substr('helloworld',3) from dual
SQL> /
SUBSTR('
-------lloworld

SQL> select instr('helloworld','W') from dual;


INSTR('HELLOWORLD','W')
----------------------0

SQL> ed
Wrote file afiedt.buf
1* select instr('helloworld','w') from dual
SQL> /
INSTR('HELLOWORLD','W')
----------------------6

SQL> select concat('gud','morning')from dual;


CONCAT('GU

---------gudmorning

SQL> select ltrim ('good morning','good')from dual;


LTRIM('G
-------morning

SQL> ed
Wrote file afiedt.buf
1* select ltrim ('good morning','go')from dual
SQL> /
LTRIM('GO
--------d morning

SQL> select trim (leading '0' from '000123')from dual;


TRI
--123

SQL> select lpad('aditya',12,'*')from dual;


LPAD('ADITYA
-----------******aditya

SQL> ed
Wrote file afiedt.buf
1* select rpad('adi',12,'*')from dual
SQL> /
RPAD('ADI',1
-----------adi*********

SQL> ed
Wrote file afiedt.buf
1* select lpad(ename,12,'*')from emp
SQL> /
LPAD(ENAME,1
-----------*******SMITH
*******ALLEN
********WARD
*******JONES
******MARTIN
*******BLAKE
*******CLARK
*******SCOTT
********KING
******TURNER
*******ADAMS

LPAD(ENAME,1
-----------*******JAMES
********FORD
******MILLER

14 rows selected.
SQL> select sysdate from dual;
SYSDATE
--------03-JAN-12

SQL> ed
Wrote file afiedt.buf
1* select last_day(sysdate) from dual
SQL> /
LAST_DAY(
--------31-JAN-12

select add_months(sysdate,6) from dual


SQL> /
ADD_MONTH
---------

03-JUL-12

SQL> ed
Wrote file afiedt.buf
1* select months_between(sysdate,'01-sep-10') from dual
SQL> /
MONTHS_BETWEEN(SYSDATE,'01-SEP-10')
----------------------------------16.087419

SQL> ed
Wrote file afiedt.buf
1* select extract(year from sysdate) from dual
SQL> /
EXTRACT(YEARFROMSYSDATE)
-----------------------2012

SQL> ed
Wrote file afiedt.buf
1* select extract(year from sysdate),extract(month from sysdate) from dual
SQL> /
EXTRACT(YEARFROMSYSDATE) EXTRACT(MONTHFROMSYSDATE)
------------------------ ------------------------2012

SQL> select extract(month from hiredate)from emp;

EXTRACT(MONTHFROMHIREDATE)
--------------------------

12
2
2
4
9
5
6
12
11
9
1

EXTRACT(MONTHFROMHIREDATE)
-------------------------12
12
1

14 rows selected.
SQL> select to_char(sysdate,'dd month,yyyy') from dual;
TO_CHAR(SYSDATE,'
-----------------

03 january ,2012

SQL> select to_char(17000,'$99,999.00') from dual;


TO_CHAR(170
----------$17,000.00

select to_char(sysdate,'ddth mon,yy') from dual;


TO_CHAR(SYS
----------03rd jan,12

select ename,sal,comm,nvl(comm,99) as modified_commission from emp


SQL> /
ENAME

SAL

COMM MODIFIED_COMMISSION

---------- ---------- ---------- ------------------SMITH

800

99

ALLEN

1600

300

300

WARD

1250

500

500

JONES

2975

MARTIN

1250

BLAKE

2850

99
1400

1400
99

CLARK

2450

99

SCOTT

3000

99

KING

5000

99

TURNER

1500

ADAMS

1100

99

ENAME

SAL

COMM MODIFIED_COMMISSION

---------- ---------- ---------- ------------------JAMES

950

99

FORD

3000

99

MILLER

1300

99

1 select student_code,student_name,dept_name,subject1,subject2,subject3
2 from student_master_720442 s
3 join department_master_720442 d
4 on s.dept_code=d.dept_code
5 join student_marks_720442 m
6 on s.student_code=m.student_code
7* where subject1>60 and subject2>60 and subject3>60 and d.dept_code in (10,20
)
SQL> /

insert into mynewemp_720432 select * from emp;


> rename mynwemp_720432 to mynewemp_720432;
alter table exp_720432 add location varchar2(14);

drop table exp_720432 cascade constraints;

sequence
-----------------create table empseq_720432
2 (
3 empo integer,
4 ename varchar2(15),
5 salary integer
6 )
7 ;
Table created.
SQL>
2
3
4
5

create sequence my_720432


increment by 2
start with 100
maxvalue 105
cycle;

Sequence created.
SQL> select * from empseq_720432;
no rows selected
SQL> insert into empseq_720432 values
2 (my_720432.nextval,'manoj',5000);
1 row created.
SQL> ed
Wrote file afiedt.buf
1 insert into empseq_720432 values
2* (my_720432.nextval,'mary',7000)
SQL> /
1 row created.
EMPO ENAME

SALARY

---------- --------------- ---------100 manoj

5000

102 mary

7000

104 kiran

2000

1 krishna

2000

3 ram

9000

5 shiva

8000

create sequence myseq_720432


2 increment by 2
3 start with 100
4 maxvalue 105
5 minvalue 95
6 cycle
7* nocache
SQL> /
Sequence created.
SQL> insert into empseq_720432 values
2 (empseq_720432.nextval,'krishna','9000');
(empseq_720432.nextval,'krishna','9000')
*
ERROR at line 2:
ORA-02289: sequence does not exist
SQL> ed
Wrote file afiedt.buf
1 insert into empseq_720432 values
2* (empseq_720432.nextval,'krishna','9000')
SQL> ed
Wrote file afiedt.buf
1 insert into empseq_720432 values
2* (myseq_720432.nextval,'krishna','9000')
SQL> /
1 row created.
SQL> ed
Wrote file afiedt.buf
1 insert into empseq_720432 values
2* (myseq_720432.nextval,'ram','8000')
SQL> /
1 row created.
SQL> ed
Wrote file afiedt.buf
1 insert into empseq_720432 values
2* (myseq_720432.nextval,'sam','7000')

SQL> /
1 row created.
SQL> ed
Wrote file afiedt.buf
1 insert into empseq_720432 values
2* (myseq_720432.nextval,'rahul','3000')
SQL> /
1 row created.
SQL> ed
Wrote file afiedt.buf
1 insert into empseq_720432 values
2* (myseq_720432.nextval,'rogue','2000')
SQL> /
1 row created.
SQL> ed
Wrote file afiedt.buf
1 insert into empseq_720432 values
2* (myseq_720432.nextval,'sahi','2000')
SQL> /
1 row created.
SQL> select * from empseq_720432;
EMPO ENAME

SALARY

---------- --------------- ---------100 krishna

9000

102 ram

8000

104 sam

7000

95 rahul

3000

97 rogue

2000

99 sahi

2000

select myseq_720432.currval from dual;

create index idx_720432 on empseq_720432(salary)

You might also like