SQL File-1
SQL File-1
SQL is the standard language for Relational Database System. All the
Relational Database Management Systems (RDMS) like MySQL, MS Access,
Oracle, Sybase, Informix, Postgres and SQL Server use SQL as their standard
database language.
Command Description
CREATE Creates a new table, a view of a table, or other object in the
database.
Command Description
SELECT Retrieves certain records from one or more tables.
Command Description
GRANT Gives a privilege to user.
Command Description
COMMIT To commit any transaction (updates, insertions etc)
CREATE Statement
empname varchar2(30),
salary number(9,2),
emails varchar2(100)
);
Output
Table created.
INSERT Statement
Output
1 row created.
SELECT Statement
Output
DROP Statement
Output
Table dropped.
empfirstname varchar2(16),
emplastname varchar2(16),
salary number(9,2),
email1 varchar2(50),
email2 varchar2(50)
);
Output
Table created.
Output
1 row created.
In the above table, columns email1 and email2 are repeating groups.
We can use a separate table to store email addresses and map them
with our employee table using foreign key reference as below.
empfirstname varchar2(16),
emplastname varchar2(16),
salary number(9,2)
);
Output
Table created.
SQL> create table email_address
email varchar2(50),
empnumber number(5),
);
Output
Table created.
Output
1 row created.
Output
1 row created.
SQL> insert into email_address (id, email, empnumber)
Output
1 row created.
Output
Output
id email empnumber
1 [email protected] 1001
2 [email protected] 1001
2. Program to show DDL (create, alter, drop) command.
Output
Table created.
Output
Rno number(5)
Sname varchar2(15)
M1 number(3)
M2 number (3)
SQL> alter table student add (m3 number(3), total number (3), result
char(4));
Output
Table altered.
SQL> desc student;
Output
Rno number(5)
Sname varchar2(15)
M1 number(3)
M2 number (3)
Total number(3)
Result char(4)
Output
Table altered.
Rno number(5)
Sname varchar2(15)
M1 number(3)
M2 number (3)
Total number(3)
Result varchar2(4)
Output
Table dropped.
Output
INSERT Statement
Output
1 row created.
Output
1 row created.
Output
1 row created.
SELECT Statement
Output
Output
Rno Result
--------- ---------
1001 pass
1002 pass
1003 fail
SQL> select * from student;
Output
1001 Arora 60 70 80
1002 Jatin 65 86 92
1003 Neha 49 50 55
UPDATE Statement
Output
3 rows updated.
SQL> update student set result=’pass’ where m1>=50 and m2>=50 and
m3>=50;
Output
2 rows updated.
SQL> update student set result=’fail’ where m1<50 or m2<50 or m3>50;
Output
1 row updated.
Output
DELETE Statement
Output
1 row deleted.
SQL> select * from student;
Output
Example
Output
Table created.
1 row created.
Output
1 row created.
Output
1 row created.
Output
Rno result
------ ---------
1001 pass
1002 fail
1003 pass
SQL> commit;
Output
Commit complete.
Example
Output
Rno result
------ ---------
1001 pass
1002 fail
1003 pass
Output
1 row updated.
SQL> savepoint sp;
Output
Savepoint created.
Example
Output
1 row updated.
Output
1 row updated.
Rno result
------ ---------
1001 fail
1002 pass
1003 fail
Output
Rollback complete.
Output
Rno result
------ ---------
1001 pass
1002 pass
1003 pass
5. Program to show union command.
Output
Rno result
------ ---------
1001 pass
1002 fail
1003 pass
Output
Rno result
------ ---------
1001 pass
1004 fail
SQL> select * from stud1 union select * from stud2;
Output
Rno result
------ ---------
1001 pass
1002 fail
1003 pass
1004 fail
Output
Rno result
------ ---------
1001 pass
7. Write a PL/SQL code block to find sum and average of three
numbers.
declare
a number:=&a;
b number:=&b;
c number:=&c;
sm number;
av number;
begin
sm:=a+b+c;
av:=sm/3;
dbms_output.put_line('Sum = '||sm);
dbms_output.put_line('Average = '||av);
end;
Output
Enter value for a: 12
old 2: a number:=&a;
new 2: a number:=12;
Enter value for b: 4
old 3: b number:=&b;
new 3: b number:=4;
Enter value for c: 21
old 4: c number:=&c;
new 4: c number:=21;
Sum = 37
Average = 12.33333333333333333333333333333333333333
PL/SQL procedure successfully completed.
declare
p number(9,2);
r number(9,2);
t number(9,2);
si number(9,2);
begin
p:=&p;
r:=&r;
t:=&t;
si:=(p*r*t)/100;
dbms_output.put_line('Simple Interest = '||si);
end;
Output
Enter value for p: 150
old 7: p:=&p;
new 7: p:=150;
Enter value for r: 4
old 8: r:=&r;
new 8: r:=4;
Enter value for t: 3
old 9: t:=&t;
new 9: t:=3;
Simple Interest = 18
PL/SQL procedure successfully completed.
declare
area number(5,2);
radius number(1):=3;
pi constant number(3,2):=3.14;
begin
while radius<=7
loop
area:=pi*radius*radius;
insert into areas values (radius,area);
radius:=radius+1;
end loop;
end;
Output
PL/SQL procedure successfully completed.
SQL> select * from areas;
RADIUS AREA
---------- ----------
3 28.26
4 50.24
5 78.5
6 113.04
7 153.86
declare
n number;
i number;
f number:=1;
begin
n:=&n;
for i in 1..n
loop
f:=f*i;
end loop;
dbms_output.put_line(n||'! = '||f);
end;
Output
old 6: n:=&n;
new 6: n:=5;
5! = 120
PL/SQL procedure successfully completed.
11. Write a PL/SQL code block to find reverse of a number.
declare
N number;
S NUMBER := 0;
R NUMBER;
K number;
begin
N := &N;
K := N;
loop
exit WHEN N = 0;
S := S * 10;
R := MOD(N,10);
S := S + R;
N := TRUNC(N/10);
end loop;
dbms_output.put_line('THE REVERSED DIGITS OF '||K||' = '||S);
end;
Output
Output
declare
a number:= 0 ;
b number:= 1;
c number;
begin
dbms_output.put(a||' '||b||' ');
for i in 3..10 loop
c := a + b;
dbms_output.put(c||' ');
a := b;
b := c;
end loop;
dbms_output.put_line(' ');
end;
Output
0 1 1 2 3 5 8 13 21 34
PL/SQL procedure successfully completed.
14. Write a PL/SQL code block to find sum of digits of a
number.
declare
N number ;
S number:=0;
R number;
begin
N:=&N;
WHILE N<>0 LOOP
R := MOD(N,10);
S := S + R;
N := TRUNC(N/10);
end loop;
dbms_output.put_line('THE SUM OF THE DIGITS = '||S);
end;
Output
old 6: N:=&N;
new 6: N:=535;
THE SUM OF THE DIGITS = 13
PL/SQL procedure successfully completed.