Professional Documents
Culture Documents
SQL
SQL
SQL
SQL
1
Table rakjay
Table created.
1 row created.
SQL> /
Enter value for ename: ashok
Enter value for job: salesman
Enter value for eno: 7499
2
Enter value for mgr: 7698
Enter value for hierdate: 20-feb-81
Enter value for sal: 3600
Enter value for comm: 300
Enter value for dept: 30
old 1: insert into rakjay values('&ename','&job','&eno','&mgr','&hierdate','&sal','&comm','&dept')
new 1: insert into rakjay values('ashok','salesman','7499','7698','20-feb-81','3600','300','30')
1 row created.
SQL> /
Enter value for ename: rohit
Enter value for job: salesman
Enter value for eno: 7521
Enter value for mgr: 7698
Enter value for hierdate: 22-feb-81
Enter value for sal: 5250
Enter value for comm: 500
Enter value for dept: 30
old 1: insert into rakjay values('&ename','&job','&eno','&mgr','&hierdate','&sal','&comm','&dept')
new 1: insert into rakjay values('rohit','salesman','7521','7698','22-feb-81','5250','500','30')
1 row created.
SQL> /
Enter value for ename: jyoti
Enter value for job: manager
Enter value for eno: 7566
Enter value for mgr: 7839
Enter value for hierdate: 02-apr-81
Enter value for sal: 4975
Enter value for comm:
Enter value for dept: 20
old 1: insert into rakjay values('&ename','&job','&eno','&mgr','&hierdate','&sal','&comm','&dept')
new 1: insert into rakjay values('jyoti','manager','7566','7839','02-apr-81','4975','','20')
1 row created.
SQL> /
Enter value for ename: martin
Enter value for job: salesman
Enter value for eno: 7654
Enter value for mgr: 7698
Enter value for hierdate: 28-sep-81
Enter value for sal: 6250
Enter value for comm: 1400
Enter value for dept: 30
old 1: insert into rakjay values('&ename','&job','&eno','&mgr','&hierdate','&sal','&comm','&dept')
new 1: insert into rakjay values('martin','salesman','7654','7698','28-sep-81','6250','1400','30')
1 row created.
SQL> /
Enter value for ename: binod
3
Enter value for job: manager
Enter value for eno: 7698
Enter value for mgr: 7839
Enter value for hierdate: 01-may-81
Enter value for sal: 5850
Enter value for comm:
Enter value for dept: 30
old 1: insert into rakjay values('&ename','&job','&eno','&mgr','&hierdate','&sal','&comm','&dept')
new 1: insert into rakjay values('binod','manager','7698','7839','01-may-81','5850','','30')
1 row created.
SQL> /
Enter value for ename: chetan
Enter value for job: manager
Enter value for eno: 7782
Enter value for mgr: 7839
Enter value for hierdate: 09-jun-81
Enter value for sal: 2450
Enter value for comm:
Enter value for dept: 10
old 1: insert into rakjay values('&ename','&job','&eno','&mgr','&hierdate','&sal','&comm','&dept')
new 1: insert into rakjay values('chetan','manager','7782','7839','09-jun-81','2450','','10')
1 row created.
SQL> /
Enter value for ename: sudhir
Enter value for job: analyst
Enter value for eno: 7781
Enter value for mgr: 7566
Enter value for hierdate: 19-apr-87
Enter value for sal: 5000
Enter value for comm:
Enter value for dept: 20
old 1: insert into rakjay values('&ename','&job','&eno','&mgr','&hierdate','&sal','&comm','&dept')
new 1: insert into rakjay values('sudhir','analyst','7782','7566','19-apr-87','5000','','20')
1 row created.
SQL> /
Enter value for ename: sumit vats
Enter value for job: analyst
Enter value for eno: 7902
Enter value for mgr: 7566
Enter value for hierdate: 03-dec-81
Enter value for sal: 3500
Enter value for comm: 3600
Enter value for dept: 20
old 1: insert into rakjay values('&ename','&job','&eno','&mgr','&hierdate','&sal','&comm','&dept')
new 1: insert into rakjay values('sumit vats','analyst','7902','7566','03-dec-81','3500','3600','2
1 row created.
4
2. Select all the columns of the above table.
SQL> select * from rakjay;
9 rows selected.
ENAME ENO
---------- ----------
Sunita 7369
ashok 7499
rohit 7521
jyoti 7566
martin 7654
binod 7698
chetan 7782
5
sudhir 7781
sumit vats 7902
E_NAME increment
--------------- ----------
sunita 3100
ashok 3900
rohit 5550
jyoti 5275
martin 6550
binod 6150
chetan 2750
sudhir 5300
9 rows selected
E_NAME SAL
--------------- ----------
sunita 2800
jyoti 4975
binod 5850
chetan 2450
sudhir 5000
DEPTNO
6
----------
10
20
30
JOB
--------------------
analyst
clerk
manager
salesman
SAL
----------
3500
E_NAME MGR
--------------- ----------
sunita 7902
sudhir 7566
7
10. List the ename starting with ‘s’
SQL> select e_name from rakjay where e_name like 's%';
E_NAME
---------------
sunita
sudhir
sumit vats
COMM DEPTNO
---------- ----------
500 30
20
1400 30
COMM DEPTNO
---------- ----------
30
8
12. List all the columns in the ascending order of hiredate.
SQL> select * from rakjay order by hiredate;
COMM DEPTNO
---------- ----------
20
300 30
500 30
COMM DEPTNO
---------- ----------
20
30
10
COMM DEPTNO
---------- ----------
1400 30
9
3600 20
20
9 rows selected.
13. List all the details in the ascending order of deptno & descending order of
salary.
SQL> select * from rakjay order by deptno,sal desc;
2450 10
5000 20
4975 20
3500 3600 20
2800 20
6250 1400 30
10
SAL COMM DEPTNO GRAD
5850 30
5250 500 30
3600 300 30
9 rows selected.
14. Display the ename & deptno of all employees in dept 20 &30 in alphabetical
order by name.
SQL> select e_name, deptno from rakjay where deptno=20 or deptno=30 order by e_name;
E_NAME DEPTNO
--------------- ----------
ashok 30
binod 30
jyoti 20
martin 30
rohit 30
sudhir 20
sumit vats 20
sunita 20
15. List the name & salary of all the employees who earn more than 1200
and are in dept 10 or 40.
SQL> select e_name, sal from rakjay where sal>1200 and (deptno=10 or deptno=40);
E_NAME SAL
--------------- ----------
chetan 2450
11
16. List the name of all employees where the second letter of their name is
an ‘a’.
SQL> select e_name from rakjay where e_name like '_a%';
E_NAME
---------------
Martin
difference
----------
2650
18. Display the name of employees whose name contains ‘M’as first alphabet ,’L’ as
third alphabet.
SQL> select e_name from rakjay where e_name like 'm_l%';
no rows selected
19. Display ename, sal of those employees who do not have their salary in the range
of 3000 to 6000.
SQL> select e_name, sal from rakjay where sal not between 3000 and 6000;
E_NAME SAL
--------------- ----------
sunita 2800
martin 6250
chetan 2450
20. Display the name of employee whose name contain ‘M’ as first
SQL> select e_name from rakjay where e_name like 'm%';
E_NAME
---------------
Martin
12
21. Display total salary of all employees listed in table emp.
SQL> select sum(sal)"total salary" from rakjay;
total salary
------------
39675
22. List the maximum salary of employee grouped by their department number.
SQL> select max(sal) from rakjay group by deptno;
MAX(SAL)
----------
2450
5000
6250
23. Display the sum, average, highest and lowest salary of the employees grouped by
department number.
SQL> select deptno, sum(sal), max(sal), min(sal), avg(sal) from rakjay group by deptno;
Table altered.
ENAME VARCHAR2(10)
JOB VARCHAR2(10)
ENO NUMBER(5)
13
MGR NUMBER(5)
HIREDATE DATE
SAL NUMBER(8)
COMM NUMBER(5)
DEPT NUMBER(5)
GRADE CHAR(4)
25. Calcualte the average gross & total gross for employees belonging to ‘E4’ grade.
SQL> Select avg(sal), sum(sal) from rakjay where grade= ‘E4’;
26. To display the jobs where the number of employees is less than 3.
SQL> select job from rakjay group by job having count(job)<3 ;
JOB
--------------------
analyst
clerk
SQL> create table rakjay(ID number(4) primary key, First_name varchar2(30) not null, last_name varchar2(30)
not null, user_id varchar2(10) unique, salary number(9,2), check (salary>5000));
Table created.
14
SQL> desc rakjay;
ID NUMBER(4)
USER_ID VARCHAR2(10)
SALARY NUMBER(9,2)
1 row deleted.
Table altered.
ERROR:
ENAME VARCHAR2(10)
JOB VARCHAR2(10)
ENO NUMBER(5)
MGR NUMBER(5)
HIREDATE DATE
SAL NUMBER(8)
COMM NUMBER(5)
DEPT NUMBER(5)
15
GRADE CHAR(4)
Table dropped.
31. Change the data type of column ename to char(20) in the Table employee.
SQL> alter table rakjay modify(ename varchar(20));
Table altered.
ENAME VARCHAR2(20)
JOB VARCHAR2(10)
ENO NUMBER(5)
MGR NUMBER(5)
HIREDATE DATE
SAL NUMBER(8)
COMM NUMBER(5)
DEPT NUMBER(5)
GRADE CHAR(4)
16
Q1. List the name and salary of the employees whose salary is more than 1000.
SQL> select ename, sal from emp where sal>1000;
ENAME SAL
---------- ----------
ALLEN 1600
WARD 1250
JONES 2975
MARTIN 1250
BLAKE 2850
CLARK 2450
SCOTT 5500
KING 5000
TURNER 1500
ADAMS 1100
FORD 5500
ENAME SAL
---------- ----------
MILLER 1300
12 Rows selected.
EMPNO ENAME
---------- ----------
7566 JONES
7698 BLAKE
17
7782 RK
ENAME
----------
SMITH
ADAMS
Q4. List the employee name and salary, whose salary is between 1000 and 2000.
SQL> select ename, sal from emp where sal between 1000 and 2000;
ENAME SAL
---------- ----------
ALLEN 1600
WARD 1250
MARTIN 1250
TURNER 1500
ADAMS 1100
MILLER 1300
6 rows selected.
ENAME JOB
---------- ---------
ALLEN SALESMAN
WARD SALESMAN
MARTIN SALESMAN
SCOTT ANALYST
TURNER SALESMAN
FORD ANALYST
6 rows selected.
18
Q6. List the details of Employees who have joined before end of September 81.
SQL> select ename from emp where hiredate<'30-sep-81'
ENAME
----------
SMITH
ALLEN
WARD
JONES
MARTIN
BLAKE
CLARK
TURNER
7 rows selected.
ENAME JOB
---------- ---------
SMITH CLERK
ALLEN SALESMAN
WARD SALESMAN
MARTIN SALESMAN
SCOTT ANALYST
KING PRESIDENT
TURNER SALESMAN
ADAMS CLERK
JAMES CLERK
FORD ANALYST
MILLER CLERK
11 rows selected
Q8. List the employees who are not eligible for commission.
SQL> select ename from emp where comm is NULL;
ENAME
----------
19
SMITH
JONES
BLAKE
CLARK
SCOTT
KING
ADAMS
JAMES
FORD
MILLER
no rows selected
ENAME
----------
ALLEN
WARD
MARTIN
TURNER
Q11. List the details of employees, whose salary is more than 2,500/- and
commission is null.
SQL> select * from emp where sal>2500 and comm is NULL;
DEPTNO
----------
20
30
20
7788 SCOTT ANALYST 7566 19-APR-87 5500
20
DEPTNO
----------
10
20
21
Q16. List the average salary of number of employees working in department 20.
SQL> select avg(sal), count(ename) from emp where deptno=20;
AVG(SAL) COUNT(ENAME)
---------- ------------
3175 2
Q17. List the department number and number of employees in each department.
SQL> select deptno, count(ename) from emp group by deptno;
DEPTNO COUNT(ENAME)
---------- ------------
10 3
20 5
30 6
Q18. List the department number and the total salary payable in each department.
SQL> select deptno, sum(sal) from emp group by deptno;
DEPTNO SUM(SAL)
---------- ----------
10 8750
20 15875
30 9400
Q20. List the employee names whose salary is greater than the lowest salary of an
employee belonging to department number 20.
SQL> select ename from emp where sal>(select min(sal) from emp where deptno=20);
ENAME
----------
ALLEN
WARD
JONES
MARTIN
BLAKE
22
CLARK
SCOTT
KING
TURNER
ADAMS
JAMES
ENAME
----------
FORD
MILLER
13 rows selected.
Q21. List the employee names whose salary is greater than the highest salary of an
employee belonging to department number 20.
SQL> select ename from emp where sal>(select max(sal) from emp where deptno=20);
ENAME
----------
KING
Q22. List the employees who joined in 1981.
SQL> select ename from emp where hiredate between '31-dec-80' and '01-jan-82';
ENAME
----------
ALLEN
WARD
JONES
MARTIN
BLAKE
CLARK
KING
TURNER
JAMES
FORD
10 rows selected.
Q23. Add number of month to a given date and returns a date after those many
months.
SQL> select hiredate, ADD_months(hiredate, 6)"probation" from emp where ename='SMITH';
HIREDATE probation
--------- ---------
23
17-DEC-80 17-JUN-81
TO_CHAR(HIREDATE,
-----------------
april ,19,1987
OUTPUT:
capitals
--------
The Soap
OUTPUT:
LOWER
-----------------
mr.scott mcmillan
Q27. Query to pad the left side of the string with the given set of characters.
SQL> select LPAD('AIMT',15,'*') "LPAD EXAMPLE" from DUAL;
OUTPUT:
24
LPAD EXAMPLE
---------------
***********AIMT
Q28. Query to remove all specified characters from the left side of the string.
SQL> SELECT LTRIM('ARMYINSTITUTE','ARMY') "LTRIM EXAMPLE" FROM DUAL;
OUTPUT:
LTRIM EXA
---------
INSTITUTE
Q29. Query to remove all specified characters from the right side of the string.
SQL> SELECT RTRIM('ARMYINSTITUTE', 'INSTITUTE') "RTRIM EXAMPLE" FROM
DUAL;
OUTPUT:
RTRI
----
ARMY
Q30. Query to write a string and grab a specified string from the given set of
characters.
SQL> SELECT SUBSTR('ARMYINSTITUTE',6,4)"SUBSTR" FROM DUAL;
OUTPUT:
25
SUBS
----
NSTI
OUTPUT:
CHANGES
--------------
Q32. Query to find out the starting location of the pattern of the string.
SQL> SELECT JOB,INSTR(JOB,'LE')FROM EMP;
OUTPUT:
JOB INSTR(JOB,'LE')
--------- ---------------
CLERK 2
SALESMAN 3
SALESMAN 3
MANAGER 0
SALESMAN 3
MANAGER 0
MANAGER 0
ANALYST 0
PRESIDENT 0
SALESMAN 3
CLERK 2
26
JOB INSTR(JOB,'LE')
--------- ---------------
CLERK 2
ANALYST 0
CLERK 2
14 rows selected.
PART II
PL SQL
27
Q1. Write a PL/SQL program to read two numbers and display their sum.
Ans. declare
num1 number(2);
num2 number(2);
num3 number(4);
begin
num1:=&number1;
num2:=&number2;
num3:=num1+num2;
dbms_output.put_line('the sum is:'||num3);
end;
/
Enter value for number1: 3
old 6: num1:=&number1;
new 6: num1:=3;
Enter value for number2: 4
old 7: num2:=&number2;
new 7: num2:=4;
the sum is:7
PL/SQL procedure successfully completed.
28
Q2. Write a program to read two numbers and display their square.
Ans. declare
num1 number(2);
sq number(6);
begin
num1:=&number1;
sq:=num1*num1;
dbms_output.put_line('the square is:'||sq);
end;
/
Enter value for number1: 3
old 5: num1:=&number1;
new 5: num1:=3;
the square is:9
PL/SQL procedure successfully completed.
29
new 6: num1:=4;
Enter value for number2: 6
old 7: num2:=&number2;
new 7: num2:=6;
the area of triangle is:12
PL/SQL procedure successfully completed.
30
B1 number(4);
C1 number(4);
D1 varchar(4);
begin
A1:=&number1;
B1:=&number2;
D1:=&varchar1;
if D1:='+'
then C1:=A1+B1;
dbms_output.put_line('The sum is:'||C1);
else if D1:='-'
then C1:=A1-B1;
dbms_output.put_line('The subtraction is:'||C1);
else if D1:='*'
then C1:=A1*B1;
dbms_output.put_line('The multiplication is:'||C1);
else D1:='/'
C1:=A1/B1;
dbms_output.put_line('The division is:'||C1);
end if;
end;
Q6. Program to display that if the number is greater than the other it swaps the two
number otherwise it doubles them.
Ans. Declare
X number(2);
Y number(2);
Z number(2);
S number(2);
D number(2);
D1 number(4);
Begin
31
X:=& number1;
Y:=& number2;
If X>Y
Then
Z:=X;
X:=Y;
Y:=Z;
Dbms_output.put_line(‘X||Y’);
Else
D=2XY;
D1=2XY;
Dbms_output.put_line(‘D||D1);
End if;
End;
32
Begin
N:=&num1;
For I IN 1.....10
Loop
T=N*I;
Q10. Write a PL/SQL code to increase the salary of employee whose salary is
3000.Get the employee number from the user.
Ans. Declare
Empno number(4);
Salary number(7,2);
Begin
Empno:=&empno;
Select sal into salary from emp
Where
33
Empno=eno;
If salary>3000
Update emp set sal=sal+.05*sal;
Where empno=eno;
End if;
End;
Q.11. Write a PL/SQL code that traps zero divide exceptions while calculating price
to earnings ratio with a company symbol xyz.
Ans. Declare
Pre ratio number(3,1);
Begin
Select price/earnings into price ratio from stock
Where symbol=’xyz’;
Exceptions
When zero divides then
Insert into stocks (symbol, ratio) values(‘xyz’,NULL);
Commit;
When other
Then rollback;
End;
Q.12. Write a PL/SQL code to obtain details of employee drawing maximum salary.
Add his/her details into a table new sal (empno, ename, salary) after incrementing
salary by Rs.700.
Ans. Declare
Eno emp.empno%type;
Name emp.ename%type;
34
Salary emp.sal%type;
Begin
Select empno,ename,sal into eno,ename,salary from emp where sal=(select max(sal) from emp);
Salary=salary+700;
Insert into new sal values (Eno,ename,salary);
End;
Begin
End if
Loop
End loop;
Close emp_cur;
End;
Rec number;
35
Begin
When sal>mysql
IF sql % found
Then
Dbms_output.put_line(mysql)
Then
Dbms_output.put_line(rec);
End if;
End;
36