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

PL-SQL Questions and Answers

program
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
95 views

PL-SQL Questions and Answers

program
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 10

11/22/2020 Oracle Live SQL - SQL Worksheet

Live SQL  Feedback  Help  [email protected]

SQL Worksheet   Find   Save 


Clear Actions Run

1
2
3
4 /*1.Write a PL/SQL block to print "WELCOME ORACLE". */
5
6 DECLARE
7 message varchar2(20):= 'WELCOME ORACLE';
8 BEGIN
9 dbms_output.put_line(message);
10 END;

Statement processed.
WELCOME ORACLE

https://livesql.oracle.com/apex/f?p=590:1:12738837094827::NO:RP:: 1/1
11/22/2020 Oracle Live SQL - SQL Worksheet

Live SQL  Feedback  Help  [email protected]

SQL Worksheet   Find   Save 


Clear Actions Run

1
2
3 /* 2. Write a PL/SQL block to swap the values of two variables.Print the variables
4 before and after swapping.**/
5
6 DECLARE
7 NUM1 number;
8 NUM2 number;
9 TEMP number;
10 BEGIN
11 NUM1:= 600;
12 NUM2:= 200;
13
14 dbms_output.put_line('Before Swapping');
15 dbms_output.put_line('NUM1 = '||NUM1||' , '||'NUM2 = '||NUM2);
16
17 --swapping---
18
19 TEMP := NUM1;
20 NUM1 := NUM2;
21 NUM2 := TEMP;
22
23 dbms_output.put_line('After Swapping');
24 dbms_output.put_line('NUM1 = '||NUM1||' , '||'NUM2 = '||NUM2);
25 END;

Statement processed.
Before Swapping
NUM1 = 600 , NUM2 = 200
After Swapping
NUM1 = 200 , NUM2 = 600

© 2020 Oracle Corporation · Privacy · Terms of Use


Oracle Learning Library · Ask Tom · Dev Gym · Database Doc 19c , 18c , 12c · Follow on Twitter
Live SQL 20.4.1, running Oracle Database 19c Enterprise Edition - 19.8.0.0.0
Built with  using Oracle APEX running on Oracle Cloud Infrastructure and Oracle Kubernetes Engine

https://livesql.oracle.com/apex/f?p=590:1:12738837094827::NO:RP:: 1/1
11/22/2020 Oracle Live SQL - SQL Worksheet

Live SQL  Feedback  Help  [email protected]

SQL Worksheet   Find   Save 


Clear Actions Run

1
2
3 /* 3.Write a PL/SQL block to check whether a number is less than 20 or not. */
4
5 DECLARE
6 NUM1 number;
7
8 BEGIN
9 NUM1:= 600;
10 dbms_output.put_line('The Number is : '||NUM1);
11
12 -- checking the number is less than 20
13 if NUM1 < 20 then
14 dbms_output.put_line('The Number is less than 20');
15 else
16 dbms_output.put_line('The Number is larger than 20');
17 end if;
18 END;

Statement processed.
The Number is : 600
The Number is larger than 20

© 2020 Oracle Corporation · Privacy · Terms of Use


Oracle Learning Library · Ask Tom · Dev Gym · Database Doc 19c , 18c , 12c · Follow on Twitter
Live SQL 20.4.1, running Oracle Database 19c Enterprise Edition - 19.8.0.0.0

Built with  using Oracle APEX running on Oracle Cloud Infrastructure and Oracle Kubernetes Engine

https://livesql.oracle.com/apex/f?p=590:1:8803926172273::NO::: 1/1
11/22/2020 Oracle Live SQL - SQL Worksheet

Live SQL  Feedback  Help  [email protected]

SQL Worksheet   Find   Save 


Clear Actions Run

1
2
3 /*4. Write a PL/SQL block to read score and display the grade. */
4 DECLARE
5 num1 number(3) := 87;
6 BEGIN
7 IF ( num1>=90 ) THEN
8 dbms_output.put_line('A+' );
9 ELSIF ( num1<90 AND num1>=80 ) THEN
10 dbms_output.put_line('A' );
11 ELSIF ( num1>=70 AND num1<80) THEN
12 dbms_output.put_line('B+' );
13 ELSIF ( num1>=60 AND num1<70) THEN
14 dbms_output.put_line('B' );
15 ELSIF ( num1>=50 AND num1<60) THEN
16 dbms_output.put_line('C+' );
17 ELSIF ( num1>=40 AND num1<50) THEN
18 dbms_output.put_line('C' );
19 ELSIF ( num1>=30 AND num1<40) THEN
20 dbms_output.put_line('D' );
21 ELSE
22 dbms_output.put_line('failed');
23 END IF;
24
25 END;

Statement processed.
A

© 2020 Oracle Corporation · Privacy · Terms of Use


Oracle Learning Library · Ask Tom · Dev Gym · Database Doc 19c , 18c , 12c · Follow on Twitter
Live SQL 20.4.1, running Oracle Database 19c Enterprise Edition - 19.8.0.0.0

Built with  using Oracle APEX running on Oracle Cloud Infrastructure and Oracle Kubernetes Engine

https://livesql.oracle.com/apex/f?p=590:1:54318712603::NO::: 1/1
11/22/2020 Oracle Live SQL - SQL Worksheet

Live SQL  Feedback  Help  [email protected]

SQL Worksheet   Find   Save 


Clear Actions Run

1
2
3
4 /* 5. Write a PL/SQL block to read a number and check whether it is odd or even. */
5 declare
6 num1 number:= 7;
7
8 begin
9 if mod(num1,2)=0
10 then
11 dbms_output.put_line('number is even');
12 else
13 dbms_output.put_line('number is odd');
14 end if;
15 end;

Statement processed.
number is odd

© 2020 Oracle Corporation · Privacy · Terms of Use


Oracle Learning Library · Ask Tom · Dev Gym · Database Doc 19c , 18c , 12c · Follow on Twitter
Live SQL 20.4.1, running Oracle Database 19c Enterprise Edition - 19.8.0.0.0

Built with  using Oracle APEX running on Oracle Cloud Infrastructure and Oracle Kubernetes Engine

https://livesql.oracle.com/apex/f?p=590:1:54318712603::NO::: 1/1
11/22/2020 Oracle Live SQL - SQL Worksheet

Live SQL  Feedback  Help  [email protected]

SQL Worksheet   Find   Save 


Clear Actions Run

1
2
3
4
5 /*6. Write a PL/SQL block to find the average of first ten numbers*/
6 DECLARE
7 a number;
8 x number:=0;
9 average number;
10 BEGIN
11 -- sum :=0;
12 FOR a in 1 .. 10 LOOP
13 x := x+a;
14 END LOOP;
15 dbms_output.put_line('Sum : ' ||x );
16 average:=x/10;
17 dbms_output.put_line('Average : ' ||average );
18
19 END;

Statement processed.
Sum : 55
Average : 5.5

© 2020 Oracle Corporation · Privacy · Terms of Use


Oracle Learning Library · Ask Tom · Dev Gym · Database Doc 19c , 18c , 12c · Follow on Twitter
Live SQL 20.4.1, running Oracle Database 19c Enterprise Edition - 19.8.0.0.0

Built with  using Oracle APEX running on Oracle Cloud Infrastructure and Oracle Kubernetes Engine

https://livesql.oracle.com/apex/f?p=590:1:54318712603::NO::: 1/1
11/22/2020 Oracle Live SQL - SQL Worksheet

Live SQL  Feedback  Help  [email protected]

SQL Worksheet   Find   Save 


Clear Actions Run

1
2
3
4
5 /* 7. Write a PL/SQL block to read two numbers and print all the numbers
6 between them.*/
7 DECLARE
8 x number(2) := 15;
9 y number := 20;
10 BEGIN
11 dbms_output.put_line('Numbers between ' || x||' and '|| y );
12 x := x + 1;
13 WHILE x <y LOOP
14
15 dbms_output.put_line( x);
16 x := x + 1;
17 END LOOP;
18 END;
19

Statement processed.
Numbers between 15 and 20
16
17
18
19

© 2020 Oracle Corporation · Privacy · Terms of Use


Oracle Learning Library · Ask Tom · Dev Gym · Database Doc 19c , 18c , 12c · Follow on Twitter
Live SQL 20.4.1, running Oracle Database 19c Enterprise Edition - 19.8.0.0.0

Built with  using Oracle APEX running on Oracle Cloud Infrastructure and Oracle Kubernetes Engine

https://livesql.oracle.com/apex/f?p=590:1:54318712603::NO::: 1/1
11/22/2020 Oracle Live SQL - SQL Worksheet

Live SQL  Feedback  Help  [email protected]

SQL Worksheet   Find   Save 


Clear Actions Run

1
2
3
4
5 /* 8.Write a PL/SQL block to read a number and reverse the given number*/
6 DECLARE
7 x number(4) := 123;
8 y number := 0;
9 p number;
10
11 BEGIN
12
13 dbms_output.put_line('Number Before Reverse : ' || x);
14 WHILE x >0 LOOP
15
16 p := mod(x,10);
17 y:=(y*10)+p;
18 x:= x/10;
19 END LOOP;
20 dbms_output.put_line('Number After Reverse: ' || y);
21 END;

Statement processed.
Number Before Reverse : 123
Number After Reverse: 321

© 2020 Oracle Corporation · Privacy · Terms of Use


Oracle Learning Library · Ask Tom · Dev Gym · Database Doc 19c , 18c , 12c · Follow on Twitter
Live SQL 20.4.1, running Oracle Database 19c Enterprise Edition - 19.8.0.0.0

Built with  using Oracle APEX running on Oracle Cloud Infrastructure and Oracle Kubernetes Engine

https://livesql.oracle.com/apex/f?p=590:1:54318712603::NO::: 1/1
11/23/2020 Oracle Live SQL - SQL Worksheet

Live SQL  Feedback  Help  [email protected]

SQL Worksheet   Find   Save 


Clear Actions Run

3
4
5 /*9. Write a PL/SQL block to read a number and invert the given number by taking it as a string.*/
6 DECLARE
7 x number:=4567;
8 str varchar2(50);
9 str2 varchar2(50);
10 len number;
11 i number;
12 BEGIN
13 dbms_output.Put_line('before reversing'||x);
14 str:= to_char(x);
15 len := length(str);
16 for i in reverse 1..len
17 loop
18 str2:=str2 || substr(str,i,1);
19 end loop;
20
21 dbms_output.put_line('Reverse of String is:'||str2);
22 END;
23

Statement processed.
before reversing4567
Reverse of String is:7654

© 2020 Oracle Corporation · Privacy · Terms of Use


Oracle Learning Library · Ask Tom · Dev Gym · Database Doc 19c , 18c , 12c · Follow on Twitter
Live SQL 20.4.1, running Oracle Database 19c Enterprise Edition - 19.8.0.0.0

Built with  using Oracle APEX running on Oracle Cloud Infrastructure and Oracle Kubernetes Engine

https://livesql.oracle.com/apex/f?p=590:1:54318712603::NO::: 1/1
11/22/2020 Oracle Live SQL - SQL Worksheet

Live SQL  Feedback  Help  [email protected]

SQL Worksheet   Find   Save 


Clear Actions Run

3
4
5 /*10.Write a PL/SQL block to find the prime numbers between 2 and 50.*/
6 DECLARE
7 i NUMBER(3);
8 j NUMBER(3);
9 BEGIN
10 dbms_output.Put_line('The prime numbers are:');
11 dbms_output.new_line;
12 i := 3;
13 LOOP
14 j := 2;
15 LOOP
16 EXIT WHEN( ( MOD(i, j) = 0 )
17 OR ( j = i ) );
18 j := j + 1;
19 END LOOP;
20 IF( j = i )THEN
21 dbms_output.Put(i||' ');
22 END IF;
23 i := i + 1;
24 exit WHEN i = 50;
25 END LOOP;
26 dbms_output.new_line;
27 END;
28

Statement processed.
The prime numbers are:
3 5 7 11 13 17 19 23 29 31 37 41 43 47

© 2020 Oracle Corporation · Privacy · Terms of Use


Oracle Learning Library · Ask Tom · Dev Gym · Database Doc 19c , 18c , 12c · Follow on Twitter
Live SQL 20.4.1, running Oracle Database 19c Enterprise Edition - 19.8.0.0.0

Built with  using Oracle APEX running on Oracle Cloud Infrastructure and Oracle Kubernetes Engine

https://livesql.oracle.com/apex/f?p=590:1:54318712603::NO::: 1/1

You might also like