You are on page 1of 18

1.

a) Create a database table BANK, add constraints (primary key, unique, check, Not null)
i) Insert 10 rows – insert the values in sql prompt;
ii) Update the column balance of ACCNO - 101 to 50000
iii) Delete the row where balance is greater the 1000000
iv). Create a view for the above data and display it.

--Creating the BANK table with constraints


CREATE TABLE BANK (
ACCNO INT PRIMARY KEY,
ACCOUNT_NAME VARCHAR(50) NOT NULL,
BALANCE DECIMAL(18, 2) NOT NULL,
UNIQUE(ACCOUNT_NAME),
CHECK (BALANCE >= 0));

-- Inserting 10 rows into the BANK table


INSERT INTO BANK (ACCNO, ACCOUNT_NAME, BALANCE)
VALUES
(101, 'John Doe', 20000),
(102, 'Alice Smith', 35000),
(103, 'Bob Johnson', 5000),
(104, 'Emily Brown', 75000),
(105, 'Michael Wilson', 100000),
(106, 'Sophia Garcia', 300000),
(107, 'William Martinez', 25000),
(108, 'Olivia Lopez', 125000),
(109, 'James Lee', 4000),
(110, 'Ava Gonzalez', 80000);

-- Update the balance of ACCNO 101 to 50000


UPDATE BANK
SET BALANCE = 50000
WHERE ACCNO = 101;

-- Delete rows where balance is greater than 1000000


DELETE FROM BANK
WHERE BALANCE > 1000000;

-- Creating a view for the BANK table


CREATE VIEW BankView AS
SELECT * FROM BANK;

-- Displaying data from the view


SELECT * FROM BankView;
2) a) Create a database table CUSTOMER ( ID, Name, Age, Address, Salary)
i) DELETE the rows which has age = 25
ii) Implement Count (* ) function and show the output;
iii) Display the customers who reside in Delhi
iv) Implement aggregate functions in salary

-- Creating the CUSTOMER table


CREATE TABLE CUSTOMER (
ID INT PRIMARY KEY,
Name VARCHAR(50) NOT NULL,
Age INT,
Address VARCHAR(100),
Salary DECIMAL(18, 2));

-- Inserting 6 rows into the CUSTOMER table


INSERT INTO CUSTOMER (ID, Name, Age, Address, Salary)
VALUES
(1, 'John Doe', 30, 'Delhi, India', 50000),
(2, 'Alice Smith', 25, 'Mumbai, India', 60000),
(3, 'Bob Johnson', 40, 'Delhi, India', 75000),
(4, 'Emily Brown', 25, 'Kolkata, India', 40000),
(5, 'Michael Wilson', 35, 'Delhi, India', 90000),
(6, 'Sophia Garcia', 28, 'Chennai, India', 80000);

-- Delete rows where age is 25


DELETE FROM CUSTOMER
WHERE Age = 25;

-- Implementing COUNT(*) function


SELECT COUNT(*) AS TotalCustomers
FROM CUSTOMER;

-- Displaying customers who reside in Delhi


SELECT *
FROM CUSTOMER
WHERE Address LIKE '%Delhi%';

-- Aggregate functions on salary


SELECT
SUM(Salary) AS TotalSalary,
AVG(Salary) AS AverageSalary,
MAX(Salary) AS MaxSalary,
MIN(Salary) AS MinSalary
FROM CUSTOMER;
3) a) Create a database Employee ( ID, Name, Age, Address, Salary)
i) Calculate average salary and display it.
ii) Display the records where the address starts with K,
iii) Rename column Name to Emp_Name

-- Creating the Employee table


CREATE TABLE Employee (
ID INT PRIMARY KEY,
Name VARCHAR(50) NOT NULL,
Age INT,
Address VARCHAR(100),
Salary DECIMAL(18, 2));

-- Inserting 5 rows into the Employee table


INSERT INTO Employee (ID, Name, Age, Address, Salary)
VALUES
(1, 'John Doe', 30, 'Delhi, India', 50000),
(2, 'Alice Smith', 25, 'Mumbai, India', 60000),
(3, 'Bob Johnson', 40, 'Kolkata, India', 75000),
(4, 'Emily Brown', 25, 'Kolkata, India', 40000),
(5, 'Michael Wilson', 35, 'Chennai, India', 90000);

-- Calculate average salary and display it


SELECT AVG(Salary) AS AverageSalary
FROM Employee;

-- Display records where the address starts with 'K'


SELECT *
FROM Employee
WHERE Address LIKE 'K%';

-- Rename column Name to Emp_Name


ALTER TABLE Employee
RENAME COLUMN Name TO Emp_Name;
4) a) Create an EMPLOYEE table with following schema:
(Emp_no, E_name, E_address, E_ph_no, Dept_no, Dept_name, Job_id , Salary)
i) Add a new column; HIREDATE to the existing relation.
ii) Change the datatype of JOB_ID from char to varchar2.
iii) Change the name of column/field Emp_no to E_no.
iv) Modify the column width of the job field of emp table

-- Creating the EMPLOYEE table


CREATE TABLE EMPLOYEE (
Emp_no INT PRIMARY KEY,
E_name VARCHAR(50) NOT NULL,
E_address VARCHAR(100),
E_ph_no VARCHAR(15),
Dept_no INT,
Dept_name VARCHAR(50),
Job_id CHAR(5),
Salary DECIMAL(18, 2));

-- Inserting 5 rows into the EMPLOYEE table


INSERT INTO EMPLOYEE (Emp_no, E_name, E_address, E_ph_no, Dept_no, Dept_name,
Job_id, Salary)
VALUES
(1, 'John Doe', '123 Main St', '123-456-7890', 101, 'HR', 'J1001', 50000),
(2, 'Alice Smith', '456 Park Ave', '987-654-3210', 102, 'Finance', 'J2001', 60000),
(3, 'Bob Johnson', '789 Broadway', '222-333-4444', 103, 'IT', 'J3001', 75000),
(4, 'Emily Brown', '101 Elm St', '555-777-8888', 104, 'Marketing', 'J4001', 40000),
(5, 'Michael Wilson', '202 Oak St', '333-222-1111', 105, 'Operations', 'J5001', 90000);

-- Add a new column HIREDATE to the existing relation


ALTER TABLE EMPLOYEE
ADD HIREDATE DATE;
Change the datatype of JOB_ID from CHAR to VARCHAR2
ALTER TABLE EMPLOYEE
MODIFY JOB_ID VARCHAR2(5);

-- Change the name of column Emp_no to E_no


ALTER TABLE EMPLOYEE
RENAME COLUMN Emp_no TO E_no;

-- Modify the column width of the Job_id field of EMPLOYEE table


ALTER TABLE EMPLOYEE
MODIFY JOB_ID VARCHAR2(10);
5) a) Create a STUDENT Database

i) Create Student table Course table and Registration table with the fields as shown above.
ii) Implement the Keys as shown in the diagram.
iii) Display the student's first name alone with the course name that they have registered.

Let's start by creating the tables:


CREATE TABLE Student (
StudentID INT PRIMARY KEY,
FirstName VARCHAR(50) NOT NULL,
LastName VARCHAR(50) NOT NULL,
Email VARCHAR(100) UNIQUE);

CREATE TABLE Course (


CourseID INT PRIMARY KEY,
CourseName VARCHAR(100) NOT NULL,
Instructor VARCHAR(50));

CREATE TABLE Registration (


RegID INT PRIMARY KEY,
StudentID INT,
CourseID INT,
RegistrationDate DATE,
FOREIGN KEY (StudentID) REFERENCES Student(StudentID),
FOREIGN KEY (CourseID) REFERENCES Course(CourseID));

-- Inserting data into Student table


INSERT INTO Student (StudentID, FirstName, LastName, Email)
VALUES
(1, 'John', 'Doe', 'john@example.com'),
(2, 'Alice', 'Smith', 'alice@example.com'),
(3, 'Bob', 'Johnson', 'bob@example.com');
-- Inserting data into Course table
INSERT INTO Course (CourseID, CourseName, Instructor)
VALUES
(101, 'Mathematics', 'Prof. Williams'),
(102, 'Science', 'Dr. Brown'),
(103, 'History', 'Prof. Davis');

-- Inserting data into Registration table


INSERT INTO Registration (RegID, StudentID, CourseID, RegistrationDate)
VALUES
(1, 1, 101, '2023-01-01'),
(2, 2, 102, '2023-01-15'),
(3, 3, 103, '2023-02-01');

-- Displaying student's first name with the course name they have registered for
SELECT s.FirstName, c.CourseName
FROM Student s
JOIN Registration r ON s.StudentID = r.StudentID
JOIN Course c ON r.CourseID = c.CourseID;

6. a) i) Create table person with following fields - person ID, Name age with primary key not null
constraint

CREATE TABLE person (


personID INT PRIMARY KEY NOT NULL,
Name VARCHAR(100) NOT NULL,
age INT NOT NULL);
ii) Create table orders with following fields - orderid, orderno, personid with referential key
constraint

CREATE TABLE orders (


orderid INT PRIMARY KEY,
orderno VARCHAR(50),
personid INT,
FOREIGN KEY (personid) REFERENCES person(personID));
7) a) Create a table EMPLOYEE with following schema:
(Emp_no, E_name, E_address, E_ph_no, Dept_no, Dept_name,Job_id , Salary)
CREATE TABLE EMPLOYEE (
Emp_no INT PRIMARY KEY,
E_name VARCHAR(50) NOT NULL,
E_address VARCHAR(100),
E_ph_no VARCHAR(15),
Dept_no VARCHAR(5),
Dept_name VARCHAR(50),
Job_id VARCHAR(10),
Salary DECIMAL(18, 2));

Write SQL queries for following question:


1.Insert aleast 5 rows in the table.
INSERT INTO EMPLOYEE (Emp_no, E_name, E_address, E_ph_no, Dept_no, Dept_name,
Job_id, Salary)
VALUES
(1, 'John Doe', '123 Main St', '123-456-7890', 'D10', 'IT', 'J1001', 50000),
(2, 'Alice Smith', '456 Park Ave', '987-654-3210', 'D20', 'HR', 'J2001', 60000),
(3, 'Bob Johnson', '789 Broadway', '222-333-4444', 'D10', 'IT', 'J3001', 75000),
(4, 'Emily Brown', '101 Elm St', '555-777-8888', 'D30', 'Finance', 'J4001', 40000),
(5, 'James Lee', '202 Oak St', '333-222-1111', 'D20', 'HR', 'J5001', 90000);

2.Display all the information of EMP table.


SELECT * FROM EMPLOYEE;
3.Display the record of each employee who works in department D10.
SELECT * FROM EMPLOYEE WHERE Dept_no = 'D10';
4.Display the details of Employee who works in department MECH.
SELECT * FROM EMPLOYEE WHERE Dept_name = 'MECH';
5.Delete the email_id of employee James.
UPDATE EMPLOYEE
SET E_ph_no = NULL
WHERE E_name = 'James';
8. a) Create a table EMPLOYEE with following schema:
(Emp_no, E_name, E_address, E_ph_no, Dept_no, Dept_name,Job_id, Designation , Salary)
Write SQL statements for the following query.

1. List the E_no, E_name, Salary of all employees working for MANAGER.

2. List the employees who are either ‘CLERK’ or ‘ANALYST’ .


3. List the employees who are working for the Deptno 10

4. List the Enames those are starting with ‘S’ .

5. List all the emps except ‘PRESIDENT’ & ‘MGR” in asc order of Salaries.

SELECT Emp_no, E_name, Salary

FROM EMPLOYEE

WHERE Designation = 'MANAGER';

SELECT *

FROM EMPLOYEE

WHERE Designation IN ('CLERK', 'ANALYST');

SELECT *

FROM EMPLOYEE

WHERE Dept_no = 10;

SELECT E_name

FROM EMPLOYEE

WHERE E_name LIKE 'S%';

SELECT *

FROM EMPLOYEE

WHERE Designation NOT IN ('PRESIDENT', 'MGR')

ORDER BY Salary ASC;


9. a) i) Create table product with the following fields productid, productname
-- Create table product
CREATE TABLE product (
productid INT PRIMARY KEY,
productname VARCHAR(100));
-- Insert 5 rows into the product table
INSERT INTO product (productid, productname)
VALUES
(1, 'Product A'),
(2, 'Product B'),
(3, 'Product C'),
(4, 'Product D'),
(5, 'Product E');

ii) Create table order with following attributes ordered, productid, and quantity unitprice.
-- Create table order
CREATE TABLE order_table (
ordered INT,
productid INT,
quantity INT,
unitprice DECIMAL(18, 2));
-- Insert 5 rows into the order table
INSERT INTO order_table (ordered, productid, quantity, unitprice)
VALUES
(1, 1, 100, 10.00),
(2, 2, 250, 15.00),
(3, 3, 180, 12.00),
(4, 4, 300, 20.00),
(5, 5, 150, 8.00);
iii) Write a sub query to display the records from product table whose product id matches with
product id in order table with quantity greater than 200.
SELECT *
FROM product
WHERE productid IN (
SELECT productid
FROM order
WHERE quantity > 200);

iv) Create savepoint s1 and roll back s1


SAVEPOINT s1;
-- Perform some operations
ROLLBACK TO s1;
10) Create an employee table and write PL SQL function to increment the employee salary

CREATE TABLE Employee (


EmployeeID INT PRIMARY KEY,
EmployeeName VARCHAR(100),
Salary DECIMAL(18, 2));

CREATE OR REPLACE FUNCTION IncrementSalary(


emp_id IN INT,
amount IN DECIMAL
) RETURN DECIMAL IS
new_salary DECIMAL(18, 2);
BEGIN
-- Retrieve current salary
SELECT Salary INTO new_salary FROM Employee WHERE EmployeeID = emp_id;

-- Increment salary
new_salary := new_salary + amount;

-- Update the employee's salary


UPDATE Employee
SET Salary = new_salary
WHERE EmployeeID = emp_id;

-- Return the new salary


RETURN new_salary;
END;
/

DECLARE
updated_salary DECIMAL(18, 2);
BEGIN
updated_salary := IncrementSalary(1, 1000); -- Increment salary of EmployeeID 1 by 1000
DBMS_OUTPUT.PUT_LINE('New Salary: ' || updated_salary);
END;
/
11) Create PL SQL function to calculate tax

CREATE TABLE Employee (


EmployeeID INT PRIMARY KEY,
EmployeeName VARCHAR(100),
Salary DECIMAL(18, 2),
Tax DECIMAL(18, 2)
);

INSERT INTO Employee (EmployeeID, EmployeeName, Salary)


VALUES
(1, 'John Doe', 50000),
(2, 'Alice Smith', 60000),
(3, 'Bob Johnson', 75000),
(4, 'Emily Brown', 40000),
(5, 'Michael Wilson', 90000);

CREATE OR REPLACE FUNCTION CalculateTax(


emp_id IN INT
) RETURN DECIMAL IS
emp_salary DECIMAL(18, 2);
emp_tax DECIMAL(18, 2);
BEGIN
-- Retrieve employee's salary
SELECT Salary INTO emp_salary FROM Employee WHERE EmployeeID = emp_id;

-- Calculate tax (assuming a simple fixed percentage tax rate)


emp_tax := CASE
WHEN emp_salary <= 50000 THEN emp_salary * 0.1 -- 10% tax
WHEN emp_salary > 50000 AND emp_salary <= 100000 THEN emp_salary * 0.2 -
- 20% tax
ELSE emp_salary * 0.3 -- 30% tax for salary above 100000
END;

-- Update the tax column in the Employee table


UPDATE Employee
SET Tax = emp_tax
WHERE EmployeeID = emp_id;

-- Return the calculated tax


RETURN emp_tax;
END;
/
DECLARE
calculated_tax DECIMAL(18, 2);
BEGIN
calculated_tax := CalculateTax(1); -- Calculate tax for EmployeeID 1
DBMS_OUTPUT.PUT_LINE('Calculated Tax: ' || calculated_tax);
END;
/

12) Create an employee table and write PL SQL function to calculate experience of the
employee.

CREATE TABLE Employee (


EmployeeID INT PRIMARY KEY,
EmployeeName VARCHAR(100),
StartDate DATE,
ExperienceInYears INT);

INSERT INTO Employee (EmployeeID, EmployeeName, StartDate)


VALUES
(1, 'John Doe', TO_DATE('2010-05-15', 'YYYY-MM-DD')),
(2, 'Alice Smith', TO_DATE('2015-08-20', 'YYYY-MM-DD')),
(3, 'Bob Johnson', TO_DATE('2012-03-10', 'YYYY-MM-DD')),
(4, 'Emily Brown', TO_DATE('2018-11-25', 'YYYY-MM-DD'));

CREATE OR REPLACE FUNCTION CalculateExperience(


emp_id IN INT
) RETURN INT IS
emp_start_date DATE;
emp_experience INT;
BEGIN
-- Retrieve employee's start date
SELECT StartDate INTO emp_start_date FROM Employee WHERE EmployeeID = emp_id;

-- Calculate experience in years (assuming the current date is used as the end date)
emp_experience := TRUNC(MONTHS_BETWEEN(SYSDATE, emp_start_date) / 12);

-- Update the ExperienceInYears column in the Employee table


UPDATE Employee
SET ExperienceInYears = emp_experience
WHERE EmployeeID = emp_id;

-- Return the calculated experience in years


RETURN emp_experience;
END;
/
13) Create a procedure to insert a record in to the department table which consists the following
fields- deptno number(10),name varchar2(20),location varchar2(20)

CREATE TABLE department (


deptno NUMBER(10),
name VARCHAR2(20),
location VARCHAR2(20)
);

CREATE OR REPLACE PROCEDURE InsertDepartment(


p_deptno IN NUMBER,
p_name IN VARCHAR2,
p_location IN VARCHAR2
) IS
BEGIN
-- Inserting a record into the department table
INSERT INTO department (deptno, name, location)
VALUES (p_deptno, p_name, p_location);

DBMS_OUTPUT.PUT_LINE('Record inserted successfully into department table.');


EXCEPTION
WHEN OTHERS THEN
DBMS_OUTPUT.PUT_LINE('Error: ' || SQLERRM);
END;
/
14) Create an employee table with following fields - empid number(10), ename
varchar2(20), sal number(7,2) and create PL SQL trigger for insert, delete, and update
operations in database table
CREATE TABLE Employee (
empid NUMBER(10),
ename VARCHAR2(20),
sal NUMBER(7, 2));

-- Create trigger for INSERT operation


CREATE OR REPLACE TRIGGER employee_insert_trigger
AFTER INSERT ON Employee
FOR EACH ROW
BEGIN
-- Perform actions after INSERT
-- You can add your own logic here
DBMS_OUTPUT.PUT_LINE('A new row was inserted into Employee table.');
END;
/

-- Create trigger for DELETE operation


CREATE OR REPLACE TRIGGER employee_delete_trigger
AFTER DELETE ON Employee
FOR EACH ROW
BEGIN
-- Perform actions after DELETE
-- You can add your own logic here
DBMS_OUTPUT.PUT_LINE('A row was deleted from Employee table.');
END;
/
-- Create trigger for UPDATE operation
CREATE OR REPLACE TRIGGER employee_update_trigger
AFTER UPDATE ON Employee
FOR EACH ROW
BEGIN
-- Perform actions after UPDATE
-- You can add your own logic here
DBMS_OUTPUT.PUT_LINE('A row was updated in Employee table.');
END;
/

15) To create a PL/SQL program to find the greatest of three numbers.

SET SERVEROUTPUT ON;

-- Create a PL/SQL block to find the greatest of three numbers


DECLARE
num1 NUMBER := 10; -- Assign your first number here
num2 NUMBER := 25; -- Assign your second number here
num3 NUMBER := 15; -- Assign your third number here
greatest_num NUMBER;
BEGIN
-- Logic to find the greatest number
IF (num1 >= num2) AND (num1 >= num3) THEN
greatest_num := num1;
ELSIF (num2 >= num1) AND (num2 >= num3) THEN
greatest_num := num2;
ELSE
greatest_num := num3;
END IF;

-- Display the greatest number


DBMS_OUTPUT.PUT_LINE('The greatest number among ' || num1 || ', ' || num2 || ', and ' ||
num3 || ' is: ' || greatest_num);
END;
/
16) To create a PL/SQL program to find the given number is odd or even.

SET SERVEROUTPUT ON;

-- Create a PL/SQL block to check if a number is odd or even


DECLARE
given_number NUMBER := 15; -- Replace this with the number you want to check
BEGIN
IF MOD(given_number, 2) = 0 THEN
DBMS_OUTPUT.PUT_LINE('The number ' || given_number || ' is even.');
ELSE
DBMS_OUTPUT.PUT_LINE('The number ' || given_number || ' is odd.');
END IF;
END;
/

You might also like