0% found this document useful (0 votes)
40 views

PLSQL

The document contains SQL commands for creating tables and queries on the EMPLOYEE and DEPARTMENT tables. It also contains PL/SQL programs demonstrating various functions like calculating the sum, average, swapping values, checking even/odd numbers, calculating grade, finding factors etc. All the PL/SQL programs take input from the user and display the output using dbms_output.put_line.

Uploaded by

Akash Akm
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)
40 views

PLSQL

The document contains SQL commands for creating tables and queries on the EMPLOYEE and DEPARTMENT tables. It also contains PL/SQL programs demonstrating various functions like calculating the sum, average, swapping values, checking even/odd numbers, calculating grade, finding factors etc. All the PL/SQL programs take input from the user and display the output using dbms_output.put_line.

Uploaded by

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

EXP-1: SQL

Commands for creating table:-

*CREATE THE TABLE EMPLOYEE:-

CREATE TABLE EMPLOYEE(ENO CHAR(3) PRIMARY


KEY,ENAME VARCHAR(30) NOT NULL,JOB_TYPE VARCHAR(30)
NOT NULL,MANAGER CHAR(3),HIRE_DATE DATE NOT
NULL,DNO INTEGER,COMMISSION DECIMAL(10,2),SALARY
DECIMAL(7,2) NOT NULL);

*CREATE TABLE DEPARTMENT:-

CREATE TABLE DEPARTMENT(DNO INTEGER NOT NULL


PRIMARY KEY,DNAME VARCHAR(50),LOCATION VARCHAR(50)
DEFAULT 'NEW DELHI');

* TO ADD FOREIGN KEY

ALTER TABLE EMPLOYEE ADD FOREIGN KEY(DNO)

REFERENCES DEPARTMENT(DNO);

QUERY:-

1. SELECT ENO,ENAME,JOB_TYPE,HIRE_DATE

FROM EMPLOYEE;

2. SELECT DISTINCT JOB_TYPE

FROM EMPLOYEE;

3. SELECT ENAME || ',' || JOB_TYPE

FROM EMPLOYEE;

4. SELECT ENAME || ',' || ENO || ',' || JOB_TYPE || ',' ||


MANAGER || ',' || HIRE_DATE || ',' || DNO || ',' ||
COMMISSION || ',' || SALARY AS "THE_OUTPUT" FROM
EMPLOYEE;

5. SELECT ENAME,SALARY
FROM EMPLOYEE

WHERE SALARY>2850;

6. SELECT ENAME,DNO

FROM EMPLOYEE

WHERE ENO=7900;

7. SELECT ENAME,SALARY FROM EMPLOYEE

WHERE SALARY<1500 OR SALARY>2850;

8. SELECT ENAME,DNO FROM EMPLOYEE

WHERE DNO=10 AND DNO=30

ORDER BY ENAME;

9. SELECT ENAME,HIRE_DATE FROM EMPLOYEE

WHERE HIRE_DATE BETWEEN '1-1-1981' AND '12-31-1981';

10. SELECT ENAME,SALARY,COMMISSION FROM EMPLOYEE

WHERE COMMISSION>0;

11. SELECT ENAME,JOB_TYPE

FROM EMPLOYEE

WHERE MANAGER='NO';

12. SELECT * FROM EMPLOYEE

ORDER BY SALARY DESC,COMMISSION DESC;

13. SELECT ENAME FROM EMPLOYEE

WHERE ENAME='__A';

14.

15.

PL/ SQL
Exp 2- 15

*ADD TWO NUMBERS

declare
a integer;
b integer;
s integer;
begin
a:=:a;
b:=:b;
s:=a+b;
dbms_output.put_line('Sum of two numbers='||s);
end;

OUTPUT:-

*AVERAGE OF THREE NUMBERS

declare
a integer;
b integer;
c integer;
t decimal(5,2);
begin
a:=:a;
b:=:b;
c:=:c;
t:=(a+b+c)/3;
dbms_output.put_line('Average of three numbers='||t);
end;

OUTPUT:-
*SWAP VALUES OF TWO VARIABLE

declare
a integer;
b integer;
t integer;
begin
a:=:a;
b:=:b;
dbms_output.put_line('Before Swapping');
dbms_output.put_line('a='||a);
dbms_output.put_line('b='||b);
t:=a;
a:=b;
b:=t;
dbms_output.put_line('After Swapping');
dbms_output.put_line('a='||a);
dbms_output.put_line('b='||b);
end;

OUTPUT:-

*CALCULATE THE AREA AND PERIMETER OF CIRCLE


declare
r integer;
p decimal(10,2);
a decimal(10,2);
begin
r:=:r;
p:=2*3.14*r;
a:=3.14*r*r;
dbms_output.put_line('Perimeter of the circle='||p);
dbms_output.put_line('Area of the circle='||a);
end;

OUTPUT:-

*Calculate the net salary of the employee


Net salary=da+hra-pf
Da=30 % of salary
Hra=20 % of salary
Pf =10 % of salary

declare
s integer;
da decimal(10,2);
hra decimal(10,2);
pf decimal(10,2);
net decimal(10,2);
begin
s:=:s;
da:=(((s/100)*30));
hra:=(((s/100)*20));
pf:=(((s/100)*10));
net:=(s+da+hra)-pf;
dbms_output.put_line('Net salary of the employee='||net);
end;

OUTPUT:-

*To check a number is even or odd


declare
n integer;
begin
n:=:n;
if n mod 2 = 0 then
dbms_output.put_line('Even number');
else
dbms_output.put_line('Odd number');
end if;
end;

OUTPUT:-

*to check pass or fail

declare
m1 integer;
m2 integer;
m3 integer;
per decimal(5,2);
begin
m1:=:m1;
m2:=:m2;
m3:=:m3;
per:=((m1+m2+m3)/300)*100;
if per > 50 then
dbms_output.put_line('PASS');
else
dbms_output.put_line('FAIL');
end if;
end;

OUTPUT:-

*CALCULATE GRADE
declare
m1 integer;
m2 integer;
m3 integer;
per decimal(5,2);
ch char;
begin
m1:=:m1;
m2:=:m2;
m3:=:m3;
per:=((m1+m2+m3)/300)*100;
if per > 75 then
ch:='A';
elsif per > 60 then
ch:='B';
elsif per > 50 then
ch:='C';
elsif per > 40 then
ch:='D';
else
ch:='F';
end if;
dbms_output.put_line('Grade='||ch);
end;

OUTPUT:-

*Sum of 1-n

declare
n integer;
s integer;
i integer;
begin
n:=:n;
s:=0;
i:=1;
while i<=n loop
s:=s+i;
i:=i+1;
end loop;
dbms_output.put_line('Sum='||s);
end;

OUTPUT:-

*Sum of digits

declare
n integer;
s integer;
begin
n:=:n;
s:=0;
while n > 0 loop
s:=s+n mod 10;
n:=n/10;
end loop;
dbms_output.put_line('Sum of digits='||s);
end;

OUTPUT:-
*Factorial of a number

declare
n integer;
fact integer;
begin
n:=:n;
fact:=1;
while n > 0 loop
fact:=fact*n;
n:=n-1;
end loop;
dbms_output.put_line('Factorial of number='||fact);
end;

OUTPUT:-

*Prime number or not

declare
n integer;
c integer;
i integer;
begin
n:=:n;
c:=0;
i:=1;
while i<=n loop
if n mod i=0 then
c:=c+1;
end if;
i:=i+1;
end loop;
if c=2 then
dbms_output.put_line('Prime number');
else
dbms_output.put_line('Not Prime number');
end if;
end;

OUTPUT:-

*reverse a number

declare
n integer;
rev integer;
t integer;
begin
n:=:n;
rev:=0;
while n>0 loop
t:=n mod 10;
rev:=rev*10+t;
n:=n/10;
end loop;
dbms_output.put_line('Reverse of a number='||rev);
end;

OUTPUT:-

* print factors of a number

declare
n integer;
i integer;
begin
n:=:n;
i:=1;
dbms_output.put_line('Factors of the number:');
while i<=n loop
if n mod i=0 then
dbms_output.put_line(i);
end if;
i:=i+1;
end loop;
end;

OUTPUT:-

You might also like