0% found this document useful (0 votes)
31 views22 pages

DBMS (Ankit) (Last)

dbms project

Uploaded by

Abhinandan Dutta
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)
31 views22 pages

DBMS (Ankit) (Last)

dbms project

Uploaded by

Abhinandan Dutta
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/ 22

Assignment No 1

Design an ER diagram for a real-world system, identify entities, attributes, keys, relationships, and cardinalities for
the following problem:

Problem:
Consider a Library Management System. The library has multiple books, students, and employees. The students borrow
books from the library. Each book is written by one or more authors. Employees work in the library and manage the books
and transactions. A student can borrow many books, but each book can only be borrowed by one student at a time.
The system also keeps track of the due dates for borrowed books and fines for overdue books. Each book can have multiple
copies available in the library.

Assignment No 2

Convert an ER model to a relational model and apply normalization techniques for the
following problem:

Problem:
Consider the following University Management System with the following entities and relationships:
Entities:
Student: Attributes - StudentID, Name, DOB, Email
Course: Attributes - CourseID, CourseName, Credits
Professor: Attributes - ProfessorID, Name, Department
Relationships:
A Student enrolls in many Courses, and each Course can have many Students (Many-to-Many).
A Professor teaches many Courses, and each Course is taught by one Professor (One-to-Many).
Assignment No 3

Instructor

Id Name Dept_name Salary Phone_no


CS001 Amit CSE 35000 0231602851
ME002 Rohit ME 25000 8389378320
CE003 Abhishek CE 30000 1462930172
EE004 Raja EE 32000 5738920739
EC005 Bishal ECE 20000 7828186338

Queries:

Query 1: Create a table instructor.


Ans: CREATE TABLE Instructor (
InstructorID VARCHAR(100) PRIMARY KEY,
Name VARCHAR(50) NOT NULL,
Department VARCHAR(100),
Salary DECIMAL(10, 2),
Phone_no VARCHAR(15) UNIQUE
);

Query 2: To view the structure of the table.


Ans: DESCRIBE Instructor
TABLE INSTRUCTOR
Result Set 2
Column Null? Type
INSTRUCTORID NOT NULL VARCHAR2(100)
NAME NOT NULL VARCHAR2(50)
DEPARTMENT - VARCHAR2(100)
SALARY - NUMBER(10,2)
PHONE_NO - VARCHAR2(15)

Query 3: Insert values into the table.


Ans: INSERT INTO Instructor (InstructorID, Name, Department, Salary, Phone_no)
VALUES ('CS001', 'Amit', 'CSE', 35000.00, '0231602851');
INSERT INTO Instructor (InstructorID, Name, Department, Salary, Phone_no)
VALUES ('ME002', 'Rohit', 'ME', 25000.00, '8389378320');
INSERT INTO Instructor (InstructorID, Name, Department, Salary, Phone_no)
VALUES ('CE003', 'Abhishek', 'CE', 30000.00, '1462930172');
INSERT INTO Instructor (InstructorID, Name, Department, Salary, Phone_no)
VALUES ('EE004', 'Raja', 'EE', 32000.00, '5738920739');
INSERT INTO Instructor (InstructorID, Name, Department, Salary, Phone_no)
VALUES ('EC005', 'Bishal', 'ECE', 20000.00, '7828186338');

Query 4: To display all the fields & values in the table.


Ans: SELECT * FROM Instructor;
INSTRUCTORID NAME DEPARTMENT SALARY PHONE_NO
CS001 Amit CSE 35000 0231602851
ME002 Rohit ME 25000 8389378320
CE003 Abhishek CE 30000 1462930172
EE004 Raja EE 32000 5738920739
EC005 Bishal ECE 20000 7828186338

Query 5: To rename the table.


Ans: RENAME Instructor TO Faculty;

Query 6: Add a new column location to the table.


Ans: ALTER TABLE Faculty
ADD email VARCHAR(100) DEFAULT 'Unknown';

Query 7: Show only the id fields from the table.


Ans: SELECT InstructorID FROM Faculty;
INSTRUCTORID
CE003
CS001
EC005
EE004
ME002

Query 8: How to change the data type of an attribute.


Ans: ALTER TABLE table_name
MODIFY COLUMN column_name new_data_type;

Query 9: Create a table (copy) from an existing table.


Ans: CREATE TABLE Faculty_Copy AS
SELECT * FROM Faculty;

Query 10: How to draw a column from a table.


Ans: SELECT column_name FROM table_name;
Query 11: How to draw a table.
Ans: DESCRIBE table_name;

Query 12: How to rename attribute of a table.


Ans: ALTER TABLE table_name
CHANGE COLUMN old_column_name new_column_name new_data_type;

Assignment No 4

Employee

Id Name Dept_name Salary


01 Amit CSE 35000
02 Rohit ME 25000
03 Abhishek CE 25000
04 Raja EE 32000
05 Bishal ECE 20000

Queries:

Query 1: Create an employee table (Attributes: id, name,


department, salary).
Ans: CREATE TABLE Employee (
Id INT PRIMARY KEY,
Name VARCHAR(50),
Dept_name VARCHAR(50),
Salary DECIMAL(10, 2)
);

Query 2: Enter 5 rows in the table.


Ans: INSERT INTO Employee (Id, Name, Dept_name, Salary)
VALUES (1, 'Amit', 'CSE', 35000);
INSERT INTO Employee (Id, Name, Dept_name, Salary)
VALUES (2, 'Rohit', 'ME', 25000);
INSERT INTO Employee (Id, Name, Dept_name, Salary)
VALUES (3, 'Abhishek', 'CE', 25000);
INSERT INTO Employee (Id, Name, Dept_name, Salary)
VALUES (4, 'Raja', 'EE', 32000);
INSERT INTO Employee (Id, Name, Dept_name, Salary)
VALUES (5, 'Bishal', 'ECE', 20000);

Query 3: To Display all the fields and the values of the table.
Ans: SELECT * FROM Employee
ID NAME DEPT_NAME SALARY
1 Amit CSE 35000
2 Rohit ME 25000
3 Abhishek CE 25000
4 Raja EE 32000
5 Bishal ECE 20000

Query 4: Update the salary field against id = 01.


Ans: UPDATE Employee
SET Salary = 38000
WHERE Id = 1;

Query 5: Display the table according to the salary in ascending order.


Ans: SELECT * FROM Employee
ORDER BY Salary ASC;
ID NAME DEPT_NAME SALARY
5 Bishal ECE 20000
2 Rohit ME 25000
3 Abhishek CE 25000
4 Raja EE 32000
1 Amit CSE 38000

Query 6: Display the table according to the salary in descending order.


Ans: SELECT * FROM Employee
ORDER BY Salary ASC;
ID NAME DEPT_NAME SALARY
5 Bishal ECE 20000
2 Rohit ME 25000
3 Abhishek CE 25000
4 Raja EE 32000
1 Amit CSE 38000

Query 7: Display the id, name of all the employees from the employee table.
Ans: SELECT Id, Name FROM Employee;
ID NAME
1 Amit
2 Rohit
3 Abhishek
4 Raja
5 Bishal

Query 8: Display only the salary field from the table eliminating the duplicate values.
Ans: SELECT DISTINCT Salary FROM Employee;
SALARY
38000
32000
20000
25000

Query 9: Display the names of all the employees whose salary is > 30000 and belongs to hr
department.
Ans: SELECT Name
FROM Employee
WHERE Salary > 30000
AND Dept_name = 'HR';
no data found
Query 10: After giving 10% increment to the salary display the name & salary from employee table.
Ans: UPDATE Employee
SET Salary = Salary * 1.10;
NAME SALARY
Amit 41800
Rohit 27500
Abhishek 27500
Raja 35200
Bishal 22000

Query 11: To delete one tuple from the table.


Ans: DELETE FROM Employee
WHERE Id = 1;
Assignment No 5

Employee

Emp_id Emp_name Emp_address Age Salary


001 Rohan Kolkata 22 5000
002 Suman Durgapur 23 10500
003 Sukanya Kharagpur 27 5005
004 Arpan Durgapur 30 9000
005 Arpita Delhi 35 8000

Queries:

Q.1) Create table Employee.


Ans: CREATE TABLE Employee (
Emp_id VARCHAR(10) PRIMARY KEY,
Emp_name VARCHAR(50),
Emp_address VARCHAR(100),
Age INT,
Salary DECIMAL(10, 2)
);

Q.2) Insert five rows into the table.


Ans: INSERT INTO Employee (Emp_id, Emp_name, Emp_address, Age, Salary)
VALUES ('001', 'Rohan', 'Kolkata', 22, 5000);
INSERT INTO Employee (Emp_id, Emp_name, Emp_address, Age, Salary)
VALUES ('002', 'Suman', 'Durgapur', 23, 10500);
INSERT INTO Employee (Emp_id, Emp_name, Emp_address, Age, Salary)
VALUES ('003', 'Sukanya', 'Kharagpur', 27, 5005);
INSERT INTO Employee (Emp_id, Emp_name, Emp_address, Age, Salary)
VALUES ('004', 'Arpan', 'Durgapur', 30, 9000);
INSERT INTO Employee (Emp_id, Emp_name, Emp_address, Age, Salary)
VALUES ('005', 'Arpita', 'Delhi', 35, 8000);
Q.3) Find all the records whose name ends with the letter "n".
Ans: SELECT *
FROM Employee
WHERE Emp_name LIKE '%n';
EMP_ID EMP_NAME EMP_ADDRESS AGE SALARY
001 Rohan Kolkata 22 5000
002 Suman Durgapur 23 10500
004 Arpan Durgapur 30 9000

Q.4) Find all the records whose name contains the letter "s".
Ans: SELECT *
FROM Employee
WHERE LOWER(Emp_name) LIKE '%s%';
EMP_ID EMP_NAME EMP_ADDRESS AGE SALARY
002 Suman Durgapur 23 10500
003 Sukanya Kharagpur 27 5005

Q.5) Find name of Employees whose salary has 4 digits & starts with 500.
Ans: SELECT Emp_name
FROM Employee
WHERE Salary LIKE '500%';
EMP_NAME
Rohan
Sukanya

Q.6) Find the name of all employees whose age is b/w 20 & 30.
Ans: SELECT Emp_name
FROM Employee
WHERE Age BETWEEN 20 AND 30;
EMP_NAME
Rohan
Suman
Sukanya
Arpan

Q.7) Find name of the Employee who are staying either in Kolkata or Durgapur.
Ans: SELECT Emp_name
FROM Employee
WHERE Emp_address = 'Kolkata' OR Emp_address = 'Durgapur';
EMP_NAME
Rohan
Suman
Arpan

Q.8) Find name of all the Employees whose city is not in Delhi.
Ans: SELECT Emp_name
FROM Employee
WHERE Emp_address NOT IN ('Delhi');
EMP_NAME
Rohan
Suman
Sukanya
Arpan

Q.9) Display the data in descending order based on the salary field.
Ans: SELECT *
FROM Employee
ORDER BY Salary DESC;
EMP_I
EMP_NAME EMP_ADDRESS AGE SALARY
D
002 Suman Durgapur 23 10500
004 Arpan Durgapur 30 9000
005 Arpita Delhi 35 8000
003 Sukanya Kharagpur 27 5005
001 Rohan Kolkata 22 5000
Assignment No 6

Employee

E_id E_name Address Age Salary


CS001 Rohan Kolkata 22 35000
EE002 Suman Durgapur 24 30000
ECE003 Sukanya Kharagpur 22 25000
ME004 Arpan Durgapur 25 27000
CST005 Arpita Delhi 26 20000

Queries:

Q.1) Create table Employee.


Ans: CREATE TABLE Employee (
E_id VARCHAR(10) PRIMARY KEY,
E_name VARCHAR(50),
Address VARCHAR(100),
Age INT,
Salary DECIMAL(10, 2)
);

Q.2) Insert the values into the table Employee.


Ans: INSERT INTO Employee (E_id, E_name, Address, Age, Salary)
VALUES ('CS001', 'Rohan', 'Kolkata', 22, 35000);
INSERT INTO Employee (E_id, E_name, Address, Age, Salary)
VALUES ('EE002', 'Suman', 'Durgapur', 24, 30000);
INSERT INTO Employee (E_id, E_name, Address, Age, Salary)
VALUES ('ECE003', 'Sukanya', 'Kharagpur', 22, 25000);
INSERT INTO Employee (E_id, E_name, Address, Age, Salary)
VALUES ('ME004', 'Arpan', 'Durgapur', 25, 27000);
INSERT INTO Employee (E_id, E_name, Address, Age, Salary)
VALUES ('CST005', 'Arpita', 'Delhi', 26, 20000);

Q.3) Find the total salary of all the Employees.


Ans: SELECT SUM(Salary) AS Total_Salary
FROM Employee;
TOTAL_SALARY
137000

Q.4) Find the average salary of all the Employees.


Ans: SELECT AVG(Salary) AS Average_Salary
FROM Employee;
AVERAGE_SALARY
27400
Q.5) Find the minimum salary from the Employee table.
Ans: SELECT MIN(Salary) AS Minimum_Salary
FROM Employee;
MINIMUM_SALARY
20000

Q.6) Find the maximum age from the Employee table.


Ans: SELECT MAX(Age) AS Maximum_Age
FROM Employee;
MAXIMUM_AGE
26

Q.7) Count the number of rows in the table.


Ans: SELECT COUNT(*) AS Number_of_Employees
FROM Employee;

NUMBER_OF_EMPLOYEES
5
Assignment No 7

Project_592

Pno Pname Location


P01 VB Kolkata
P02 DBMS Bangalore
P03 MATH DGP
P04 ORACLE Delhi
P05 NET Noida

Employee_592

eno ename city salary Pno


e02 AD Delhi 35000 P03
e01 OB DGP 28000 P01
e03 MB Kolkata 20000 P05
e04 SG Malda 30000 P02
e05 RM Bankura 18000 P04

Queries:

Query 1: Create table Project_592.


Ans: CREATE TABLE Project_592 (
Pno VARCHAR(10) PRIMARY KEY,
Pname VARCHAR(50),
Location VARCHAR(100)
);

Query 2: Create table Employee_592.


Ans: CREATE TABLE Employee_592 (
eno VARCHAR(10) PRIMARY KEY,
ename VARCHAR(50),
city VARCHAR(50),
salary DECIMAL(10, 2),
Pno VARCHAR(10),
FOREIGN KEY (Pno) REFERENCES Project_592(Pno)
);

Query 3: Insert 5 rows in each table.


Ans: INSERT INTO Project_592 (Pno, Pname, Location)
VALUES ('P01', 'VB', 'Kolkata');
INSERT INTO Project_592 (Pno, Pname, Location)
VALUES ('P02', 'DBMS', 'Bangalore');
INSERT INTO Project_592 (Pno, Pname, Location)
VALUES ('P03', 'MATH', 'DGP');
INSERT INTO Project_592 (Pno, Pname, Location)
VALUES ('P04', 'ORACLE', 'Delhi');
INSERT INTO Project_592 (Pno, Pname, Location)
VALUES ('P05', 'NET', 'Noida');
INSERT INTO Employee_592 (eno, ename, city, salary, Pno)
VALUES ('e02', 'AD', 'Delhi', 35000, 'P03');
INSERT INTO Employee_592 (eno, ename, city, salary, Pno)
VALUES ('e01', 'OB', 'DGP', 28000, 'P01');
INSERT INTO Employee_592 (eno, ename, city, salary, Pno)
VALUES ('e03', 'MB', 'Kolkata', 20000, 'P05');
INSERT INTO Employee_592 (eno, ename, city, salary, Pno)
VALUES ('e04', 'SG', 'Malda', 30000, 'P02');
INSERT INTO Employee_592 (eno, ename, city, salary, Pno)
VALUES ('e05', 'RM', 'Bankura', 18000, 'P04');

Query 4: Find Pno, Pname, ename who are working on a project located at Bangalore.
Ans: SELECT P.Pno, P.Pname, E.ename
FROM Project_592 P
JOIN Employee_592 E ON P.Pno = E.Pno
WHERE P.Location = 'Bangalore';
PNO PNAME ENAME
P02 DBMS SG

Query 5: Find the salary of employee whose Pname is ORACLE.


Ans: SELECT E.salary
FROM Employee_592 E
JOIN Project_592 P ON E.Pno = P.Pno
WHERE P.Pname = 'ORACLE';
SALAR
Y
18000

Query 6: Find Pname whose city is not in Kolkata.


Ans: SELECT P.Pname
FROM Project_592 P
JOIN Employee_592 E ON P.Pno = E.Pno
WHERE E.city != 'Kolkata';
PNAME
VB
DBMS
MATH
ORACLE

Query 7: Find Pname whose salary is in between 20000 and 30000.


Ans: SELECT P.Pname PNAME
FROM Project_592 P VB
JOIN Employee_592 E ON P.Pno = E.Pno NET
WHERE E.salary BETWEEN 20000 AND 30000; DBMS
Assignment No 8

Department_592

dname Building Budget


CSE CS block 60000
ME ME block 40000
CE CE block 30000

Instructor_592

ID Name dname Salary


101 OB CSE 60000
102 AD CSE 58000
103 MB ME 55000
104 RM CE 50000
105 MG ME 50000

Course_592

ID C_id Sec_id Sem Year


101 CS201 A 2 2021
102 CS101 B 2 2021
103 ME301 A 3 2022
104 CE203 A 2 2024

Queries:

Query 1: Create table department-592 (Primary key-dname).


Ans: CREATE TABLE Department_592 (
dname VARCHAR(50) PRIMARY KEY,
Building VARCHAR(50),
Budget DECIMAL(10, 2)
);

Query 2: Create table instructor-592 (Primary key-ID, Foreign key-dname).


Ans: CREATE TABLE Instructor_592 (
ID INT PRIMARY KEY,
Name VARCHAR(50),
dname VARCHAR(50),
Salary DECIMAL(10, 2),
FOREIGN KEY (dname) REFERENCES Department_592(dname)
);
Query 3: Create table course-592 (Primary key-ID, C_id, Sec_id, Foreign key-ID).
Ans: CREATE TABLE Course_592 (
ID INT,
C_id VARCHAR(10),
Sec_id CHAR(1),
Sem INT,
Year INT,
PRIMARY KEY (ID, C_id, Sec_id),
FOREIGN KEY (ID) REFERENCES Instructor_592(ID)
);

Query 4: Retrieve the name of all instructors along with their dname, and building.
Ans: SELECT I.Name, I.dname, D.Building
FROM Instructor_592 I
JOIN Department_592 D ON I.dname = D.dname;
NAME DNAME BUILDING
OB CSE CS block
AD CSE CS block
MB ME ME block
RM CE CE block
MG ME ME block

Query 5: Find those instructors (name, C_id) who have taught courses.
Ans: SELECT I.Name, C.C_id
FROM Instructor_592 I
JOIN Course_592 C ON I.ID = C.ID;
NAME C_ID
OB CS201
AD CS101
MB ME301
RM CE203

Query 6: Find those instructors (name, C_id) who belong to the CSE department.
Ans: SELECT I.Name, C.C_id
FROM Instructor_592 I
JOIN Course_592 C ON I.ID = C.ID
WHERE I.dname = 'CSE';
NAME C_ID
OB CS201
AD CS101
Assignment No 9

emp_592

eno ename job dept_no salary


e01 AB AP 1 10000
e02 SD Lecturer 2 20000
e03 RS AP 1 15000
e04 TP Lecturer 2 12000
e05 TD AP 1 18000

Queries:

Query 1: Display all employee names and their salary whose salary is greater than the minimum salary
of the employee and job title is 'AP'.
SELECT ename, salary
FROM emp_592
WHERE salary > (SELECT MIN(salary) FROM emp_592 WHERE job = 'AP')
AND job = 'AP';
ENAME SALARY
RS 15000
TD 18000

Query 2: Write a query to find all employees who work in the same job as 'AB'.
Ans: SELECT ename, job
FROM emp_592
WHERE job = (SELECT job FROM emp_592 WHERE ename = 'AB');
ENAME JOB
AB AP
RS AP
TD AP

Query 3: Write a query to display information about employees who earn more than any employee in
dept_no 1.
Ans: SELECT *
FROM emp_592
WHERE salary > ALL (SELECT salary FROM emp_592 WHERE dept_no = 1);
ENO ENAME JOB DEPT_NO SALARY
e02 SD Lecturer 2 20000

Query 4: Write a query to find the name of employees who work in the same department as
'TD'.
Ans: SELECT ename
FROM emp_592
WHERE dept_no = (SELECT dept_no FROM emp_592 WHERE ename = 'TD');
ENAME
AB
RS
TD

Query 5: Write a query to find the eno whose salary is less than the maximum salary of an employee in
dept_no 2.
Ans: SELECT eno
FROM emp_592
WHERE salary < (SELECT MAX(salary) FROM emp_592 WHERE dept_no = 2);
ENO
e01
e03
e04
e05
Assignment No 10

Dual Table: Dual is a special 1 row and 1 column table present by default in Oracle. It contains 1
column named "dummy" and contains one row with a value X. It is useful for constant expression with
select statement.

Queries:

Query 1: To display the system date.


Ans: SELECT SYSDATE FROM DUAL;
SYSDATE
21-NOV-24

Query 2: To display the absolute value of a number.


Ans: SELECT ABS(-25) FROM DUAL;
ABS(-25)
25

Query 3: To display the power of a number.


Ans: SELECT POWER(2, 3) FROM DUAL;
POWER(2,3)
8

Query 4: To display the square root of a number.


Ans: SELECT SQRT(16) FROM DUAL;
SQRT(16)
4

Query 5: To display a floating point number in a round figure (ceil and floor value option).
Ans: SELECT CEIL(25.3) AS Ceil_Value, FLOOR(25.7) AS Floor_Value FROM DUAL;
CEIL_VALUE FLOOR_VALUE
26 25

Query 6: To display the modulus of a number.


Ans: SELECT MOD(10, 3) FROM DUAL;
MOD(10,3)
1

Query 7: To display the string in lower case.


Ans: SELECT LOWER('HELLO WORLD') FROM DUAL;
LOWER('HELLOWORLD')
hello world

Query 8: Display the string in uppercase.


Ans: SELECT UPPER('hello world') FROM DUAL;
UPPER('HELLOWORLD')
HELLO WORLD

Query 9: To display initial letter in capital.


Ans: SELECT INITCAP('hello world') FROM DUAL;
INITCAP('HELLOWORLD')
Hello World

Query 10: Find out the length of a string.


Ans: SELECT LENGTH('hello world') FROM DUAL;
LENGTH('HELLOWORLD')
11

Query 11: To find out the substring of a string.


Ans: SELECT SUBSTR('hello world', 7, 5) FROM DUAL;
SUBSTR('HELLOWORLD',7,5)
world

Query 12: To trim a letter from left side.


Ans: SELECT LTRIM(' hello') FROM DUAL;
LTRIM('HELLO')
hello

Query 13: To trim a character from right side.


Ans: SELECT RTRIM('hello ') FROM DUAL;
RTRIM('HELLO')
hello

Query 14: To add character from left side.


Ans: SELECT LPAD('hello', 10, '*') FROM DUAL;
LPAD('HELLO',10,'*')
*****hello

Query 15: To add character from right side.


Ans; SELECT RPAD('hello', 10, '*') FROM DUAL;
RPAD('HELLO',10,'*')
hello*****
Assignment No 11

Program1: Using PL/SQL write a program to find the sum of even and odd numbers in a given range.
Ans: DECLARE
start_num NUMBER := 1; -- starting number of the range
end_num NUMBER := 10; -- ending number of the range
even_sum NUMBER := 0; -- variable to store the sum of even numbers
odd_sum NUMBER := 0; -- variable to store the sum of odd numbers
BEGIN
FOR i IN start_num..end_num LOOP
IF MOD(i, 2) = 0 THEN
even_sum := even_sum + i; -- add to even sum
ELSE
odd_sum := odd_sum + i; -- add to odd sum
END IF;
END LOOP;
DBMS_OUTPUT.PUT_LINE('Sum of even numbers: ' || even_sum);
DBMS_OUTPUT.PUT_LINE('Sum of odd numbers: ' || odd_sum);
END;

Output:
Statement processed.
Sum of even numbers: 30
Sum of odd numbers: 25

Program2: Using PL/SQL write a program to find the summation of first and last digit of a n-digit
number.
Ans: DECLARE
num NUMBER := 12345; -- input number
first_digit NUMBER; -- variable to store the first digit
last_digit NUMBER; -- variable to store the last digit
sum_digits NUMBER; -- variable to store the sum of first and last digit
BEGIN
last_digit := MOD(num, 10); -- get the last digit

-- find the first digit by dividing the number until it's a single digit
first_digit := num;
WHILE first_digit >= 10 LOOP
first_digit := FLOOR(first_digit / 10);
END LOOP;

sum_digits := first_digit + last_digit; -- summation of first and last digits


DBMS_OUTPUT.PUT_LINE('Sum of first and last digit: ' || sum_digits);
END;

Output:
Statement processed.
Sum of first and last digit: 6
Program3: Using PL/SQL write a program to reverse a number.
Ans: DECLARE
num NUMBER := 12345; -- input number
reversed_num NUMBER := 0; -- variable to store the reversed number
BEGIN
WHILE num > 0 LOOP
reversed_num := (reversed_num * 10) + MOD(num, 10); -- reverse the number
num := FLOOR(num / 10); -- remove the last digit from the number
END LOOP;

DBMS_OUTPUT.PUT_LINE('Reversed number: ' || reversed_num);


END;

Output:
Statement processed.
Reversed number: 54321

Program4: Write a PL/SQL program to print the following pattern:

*
**
***
****
*****
Ans: DECLARE
i NUMBER;
j NUMBER;
BEGIN
FOR i IN 1..5 LOOP -- 5 rows for the pattern
FOR j IN 1..i LOOP -- increasing number of stars in each row
DBMS_OUTPUT.PUT('*');
END LOOP;
DBMS_OUTPUT.PUT_LINE(''); -- move to the next line after each row
END LOOP;
END;

Output:
Statement processed.
*
**
***
****
*****
Program5: Write a PL/SQL program to generate the Fibonacci series up to nth term.
Ans: DECLARE
n NUMBER := 10; -- number of terms in the Fibonacci series
a NUMBER := 0; -- first term
b NUMBER := 1; -- second term
c NUMBER; -- variable to store the next term in the series
BEGIN
DBMS_OUTPUT.PUT_LINE('Fibonacci series:');
DBMS_OUTPUT.PUT_LINE(a); -- print the first term
DBMS_OUTPUT.PUT_LINE(b); -- print the second term

FOR i IN 3..n LOOP -- start from the 3rd term


c := a + b; -- next term is the sum of the previous two terms
DBMS_OUTPUT.PUT_LINE(c); -- print the next term
a := b; -- update a to be the previous b
b := c; -- update b to be the current term
END LOOP;
END;

Output:
Statement processed.
Fibonacci series:
0
1
1
2
3
5
8
13
21
34

You might also like