0% found this document useful (0 votes)
506 views42 pages

Assighnment 2

Uploaded by

dobariyapurva47
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
506 views42 pages

Assighnment 2

Uploaded by

dobariyapurva47
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 42

Assignment 2 DBMS(3130702)

Q – (1) Consider the following tables and answer the queries in SQL. [18/01/2024]
Books (isbn_no, title, publisher_id, year)
Authors (author_id, author_name, country, city)

Publishers (publisher_id, publisher_name, city)

WrittenBy (isbn_no, author_id)

1. Books
isbn_no title publisher_id year
123456 Learning SQL 1 2000
654321 Advanced Databases 2 2005
112233 Fundamentals of Math 3 2001
444555 Introduction to Python 4 1997
678901 Data Science Essentials 1 2020
2. Authors
author_id author_name country city
1 Alice Smith USA New York
2 Bob Brown UK London
3 Carol Jones India Mumbai
4 David White USA Chicago
10 Korth Canada Toronto
3. Publishers
publisher_id publisher_name city
1 Academic Press New York
2 McGaw Hill Chicago
3 Random House London
4 Tech Books San Francisco
5 Pearson Ahmedabad
4. WrittenBy
isbn_no author_id
123456 1
654321 2
112233 3
444555 4
678901 10
678901 1

Page No.1
Assignment 2 DBMS(3130702)

1. List all the books published after 1998.

SELECT * FROM Books WHERE year > 1998; Output:

2. Update the city of author to “Baroda” whose author id is 10.

UPDATE Authors SET city = 'Baroda' WHERE author_id = 10; Output:

3. List all the book titles written by author “korth”

SELECT b.title FROM Books b JOIN WrittenBy wb ON b.isbn_no = wb.isbn_no JOIN


Authors a ON wb.author_id = a.author_id WHERE a.author_name = 'Korth';
Output:

4. Add column “price” in the table Books.

ALTER TABLE Books ADD price DECIMAL(10, 2); Output:

Page No.2
Assignment 2 DBMS(3130702)

5. Display number of Publishers from the city “Ahmedabad”.

SELECT COUNT(*) AS num_publishers FROM Publishers WHERE city = 'Ahmedabad'; Output:

6. List all the books published by “McGaw Hill”

SELECT b.* FROM Books b


JOIN Publishers p ON b.publisher_id =
p.publisher_id WHERE p.publisher_name = 'McGaw
Hill'; Output:

7. Display all publishers in the ascending order of their name.

SELECT * FROM Publishers ORDER BY publisher_name ASC; Output:

Page No.3
Assignment 2 DBMS(3130702)

Q – (2) Consider the following tables and answer the queries in SQL. [18/01/2024]

Products(prod_id, prod_name, category, price)


Customers(cust_id, cust_name, country, city)

Orders(order_id, cust_id, prod_id, order_date, quantity)

1. Products
prod_id prod_name category price
1 Laptop Electronics 1500
2 Television Electronics 2500
3 Smartphone Electronics 1800
4 Refrigerator Appliances 3500
5 Microwave Appliances 1200
43 Washing Machine Appliances 3000
2. Customers
cust_id cust_name country city
1 John Doe USA New York
2 Jane Smith UK London
3 Alice Brown Canada Toronto
4 Bob Johnson Australia Sydney
3. Orders
order_id cust_id prod_id order_date quantity
1 1 1 2023-01-10 1
2 2 2 2023-02-15 1
3 1 3 2023-03-20 2
4 3 4 2023-04-25 1
5 1 5 2023-05-30 1

1. List all the products having price less than 2000.

SELECT * FROM Products WHERE price < 2000; Output:

Page No.4
Assignment 2 DBMS(3130702)

2. Update price of a product to 3000 whose product id is 43.

UPDATE Products SET price = 3000 WHERE prod_id = 43; Output:

3. Display names of customer who have placed atleast one order.

SELECT DISTINCT c.cust_name FROM Customers c JOIN Orders o ON c.cust_id = o.cust_id;


Output:

4. Display customer id and total number of orders placed by them.

SELECT cust_id, COUNT(order_id) AS total_orders FROM Orders GROUP BY cust_id; Output:

5. List all Customers in the descending order of their city.

SELECT * FROM Customers ORDER BY city DESC; Output:

Page No.5
Assignment 2 DBMS(3130702)

6. Display names of customers who have ordered “Television” (Product Name)

SELECT DISTINCT c.cust_name FROM Customers c


JOIN Orders o ON c.cust_id = o.cust_id JOIN Products p ON o.prod_id = p.prod_id
WHERE p.prod_name = 'Television'; Output:

7. Remove column “category” from the Products table.

ALTER TABLE Products DROP COLUMN category; Output:

Page No.6
Assignment 2 DBMS(3130702)

Q – (3) TABLE Worker(WORKER_ID INT NOT NULL PRIMARY

KEY,FIRST_NAME CHAR(25), LAST_NAME CHAR(25),SALARY


INT(15),JOINING_DATE DATETIME,DEPARTMENT CHAR(25));

TABLE Bonus(WORKER_REF_ID INT,BONUS_AMOUNT


INT(10),BONUS_DATE DATETIME,FOREIGN KEY
(WORKER_REF_ID),REFERENCES Worker(WORKER_ID));

TABLE Title(WORKER_REF_ID INT,WORKER_TITLE


CHAR(25),AFFECTED_FROM DATETIME,FOREIGN KEY
(WORKER_REF_ID)REFERENCES Worker(WORKER_ID)); [24/02/2023]

Worker Table
WORKER_ID FIRST_NAME LAST_NAME SALARY JOINING_DATE DEPARTMENT

1 Alice Smith 60000 2020-01-15 09:00:00 HR

2 Bob Johnson 75000 2019-03-22 10:00:00 IT

3 Charlie Brown 50000 2021-05-30 11:00:00 IT

4 Ethan White 70000 2022-08-15 12:00:00 Marketing

5 Faith Green 80000 2018-11-01 09:30:00 IT

6 Hannah Davis 40000 2023-04-12 08:45:00 HR

Bonus Table
WORKER_REF_ID BONUS_AMOUNT BONUS_DATE

1 5000 2021-06-01

2 7000 2023-01-01

3 3000 2022-09-01

Title Table
WORKER_REF_ID WORKER_TITLE AFFECTED_FROM

2 Senior Manager 2020-01-01

4 Manager 2022-08-01

Page No.7
Assignment 2 DBMS(3130702)

Consider above 3 tables ,assume appropriate data and solve following


SQL queries:

1. Write an SQL query to fetch “FIRST_NAME” from Worker table using the alias name as
<WORKER_NAME>

SELECT FIRST_NAME AS WORKER_NAME FROM Worker; Ouput:

2. Write an SQL query to fetch “FIRST_NAME” from Worker table in

uppercase.

SELECT UPPER(FIRST_NAME) AS WORKER_NAME FROM Worker; Output:

Page No.8
Assignment 2 DBMS(3130702)

3. Write an SQL query to print all Worker details from the Worker table order by
FIRST_NAME Ascending.

SELECT * FROM Worker ORDER BY FIRST_NAME ASC;

Output:

4. Write an SQL query to print details of the Workers whose FIRST_NAME ends
with ‘h’ and contains six alphabets.

SELECT * FROM Worker WHERE FIRST_NAME LIKE '%h' AND LENGTH(FIRST_NAME) = 6;


Output:

5. Write an SQL query to print details of the Workers who are also Managers.

SELECT w.* FROM Worker w JOIN Title t ON w.WORKER_ID = t.WORKER_REF_ID WHERE


t.WORKER_TITLE LIKE '%Manager%'; Output:

Page No.9
Assignment 2 DBMS(3130702)

6. Write an SQL query to fetch departments along with the total salaries paid for each of
them.

SELECT DEPARTMENT, SUM(SALARY) AS TOTAL_SALARY FROM Worker GROUP BY


DEPARTMENT; Output:

7. Write an SQL query to fetch the names of workers who earn the highest salary.

SELECT FIRST_NAME, LAST_NAME FROM Work WHERE SALARY = (SELECT MAX(SALARY)


FROM Worker); Output:

Page No.10
Assignment 2 DBMS(3130702)

Q – (4) Write a PL/SQL function which takes 3 integer numbers as a


parameters and return an average of same. [24/02/2023]

DECLARE
avg_value NUMBER;
BEGIN
-- Call the function with example values

avg_value := calculate_average(10, 20, 30);

-- Display the result


DBMS_OUTPUT.PUT_LINE('The average is: ' || avg_value);
END;

Output:

Page No.11
Assignment 2 DBMS(3130702)

Q – (5) Consider the following employee database, primary keys are


underlined :

employee (employee-name, street, city) works


(employee-name, company-name, salary) company
(company-name, city) manages (employee-name,
manager_name) [28/07/2023]
Give an expression in SQL for each of the following queries:

1. Find the names of all employees who work for First Bank
Corporation.

SELECT employee-name

FROM works
WHERE company-name = 'First Bank Corporation'; Output:

2. Give all employees of First Bank Corporation a 10 percent raise.

UPDATE works SET salary = salary * 1.10 WHERE company-name = 'First Bank Corporation';
Output:

Page No.12
Assignment 2 DBMS(3130702)

3. Find the names and cities of residence of all employees who work
for First Bank Corporation.

SELECT e.employee-name, e.city


FROM employee e
JOIN works w ON e.employee-name = w.employee-name
WHERE w.company-name = 'First Bank Corporation'; Output:

4. Find the names, street addresses, and cities of residence of all


employees who work for First Bank Corporation and earn more than
$10,000.

SELECT e.employee-name, e.street, e.city


FROM employee e
JOIN works w ON e.employee-name = w.employee-name

WHERE w.company-name = 'First Bank Corporation' AND w.salary > 10000; Ouput:

Page No.13
Assignment 2 DBMS(3130702)

5. Find all employees in the database who live in the same cities as the
companies for which they work.

SELECT DISTINCT e.employee-name


FROM employee e
JOIN works w ON e.employee-name = w.employee-name
JOIN company c ON w.company-name = c.company-name
WHERE e.city = c.city; Ouput:

6. Find all employees in the database who do not work for First Bank

Corporation.

SELECT e.employee-name
FROM employee e
WHERE e.employee-name NOT IN (

SELECT w.employee-name
FROM works w
WHERE w.company-name = 'First Bank Corporation'
);
Output:

Page No.14
Assignment 2 DBMS(3130702)

Q – (6) Consider Following 3 Tables for library database and Write SQL Queries.

1. Books ( BookID, BookTitle, Price, Author, Publisher )


2. Students (StudID, StudName, DOB, Gender, Branch, Sem, Address)

3. Issue_Books ( StudID, BookID, Issue_Date) [15/07/2022]

1. Books Table
BookID BookTitle Price Author Publisher
1 Introduction to DBMS 500 A. Author Tech Books
2 Advanced DBMS 600 A. Author Tech Books
3 Data Structures 450 B. Author Science Books
4 Operating Systems 700 C. Author Tech Books
5 Database Management Systems 550 A. Author Tech Books
6 Networking Basics 400 D. Author Tech Books
2. Students Table
StudID StudName DOB Gender Branch Sem Address
1 RAJESH 2000-01-01 M CSE 5 123 Main St
2 SIMRAN 2001-02-01 F CSE 5 456 Side St
3 AMIT 1999-03-01 M ECE 6 789 Another St
3. Issue_Books Table
StudID BookID Issue_Date
1 1 2023-09-01
1 3 2023-09-02
2 2 2023-09-03

1. List all Books whose Title contains word ‘DBMS’.

SELECT * FROM Books WHERE BookTitle LIKE '%DBMS%'; Output:

Page No.15
Assignment 2 DBMS(3130702)

2. Display all Publisher Name & Total Price of Books of that publisher.

SELECT Publisher, SUM(Price) AS TotalPrice FROM Books GROUP BY Publisher; Output:

3. Display list of all books which are not issued to any students.

SELECT * FROM Books WHERE BookID NOT IN (SELECT BookID FROM Issue_Books); Output:

4. Display the author name whose number of books is maximum in library.

SELECT Author FROM Books GROUP BY Author ORDER BY COUNT(BookID) DESC LIMIT 1;
Output:

5. Display all Books assigned to student with name “RAJESH”.

SELECT B.* FROM Books B JOIN Issue_Books IB ON B.BookID = IB.BookID JOIN


Students S ON IB.StudID = S.StudID WHERE S.StudName = 'RAJESH'; Output:

Page No.16
Assignment 2 DBMS(3130702)

Q – (7) TABLE Worker(WORKER_ID INT NOT NULL PRIMARY KEY,FIRST_NAME CHAR(25), LAST_NAME
CHAR(25),SALARY INT(15),JOINING_DATE DATETIME,DEPARTMENT CHAR(25));

TABLE Bonus(WORKER_REF_ID INT,BONUS_AMOUNT INT(10),BONUS_DATE DATETIME,FOREIGN KEY


(WORKER_REF_ID),REFERENCES Worker(WORKER_ID));

TABLE Title(WORKER_REF_ID INT,WORKER_TITLE CHAR(25),AFFECTED_FROM


DATETIME,FOREIGN KEY (WORKER_REF_ID)REFERENCES Worker(WORKER_ID)); [04/03/2021]

1. Worker Table

WORKER_ID FIRST_NAME LAST_NAME SALARY JOINING_DATE DEPARTMENT


1 John Doe 120000 2014-02-15 09:00:00 HR
2 Jane Smith 250000 2014-02-20 10:30:00 IT
3 Mike Johnson 80000 2014-01-10 11:00:00 IT
4 Emily Davis 150000 2015-03-05 09:00:00 Finance
5 Chris Wilson 95000 2014-02-25 08:00:00 HR
6 Alice Brown 60000 2014-04-15 07:30:00 IT
7 Bob Taylor 500000 2015-01-05 12:00:00 Marketing

2. Bonus Table
WORKER_REF_ID BONUS_AMOUNT BONUS_DATE
1 5000 2023-05-10 09:00:00
2 7000 2023-05-15 09:30:00
5 3000 2023-06-01 10:00:00
3. Title Table
WORKER_REF_ID WORKER_TITLE AFFECTED_FROM
1 Senior Manager 2014-02-15 09:00:00
2 Team Lead 2014-02-20 10:30:00
4 Financial Analyst 2015-03-05 09:00:00

Page No.17
Assignment 2 DBMS(3130702)

Consider above 3 tables ,assume appropriate data and solve following SQL queries

1. Find out unique values of DEPARTMENT from Worker table.

SELECT DISTINCT DEPARTMENT FROM Worker; Output:

2. Print details of the Workers whose SALARY lies between 100000 and 500000.

SELECT * FROM Worker WHERE SALARY BETWEEN 100000 AND 500000; Output:

3. Print details of the Workers who have joined in Feb’2014.

SELECT * FROM Worker WHERE JOINING_DATE BETWEEN '2014-02-01' AND '2014-02-28';


Output:

Page No.18
Assignment 2 DBMS(3130702)

4. Fetch worker names with salaries >= 50000 and <= 100000.

SELECT FIRST_NAME, LAST_NAME, SALARY FROM Worker WHERE SALARY >= 50000 AND
SALARY <= 100000; Output:

Page No.19
Assignment 2 DBMS(3130702)

Q – (8) TABLE Worker(WORKER_ID INT NOT NULL PRIMARY KEY,FIRST_NAME CHAR(25), LAST_NAME
CHAR(25),SALARY INT(15),JOINING_DATE DATETIME,DEPARTMENT CHAR(25));
TABLE Bonus(WORKER_REF_ID INT,BONUS_AMOUNT INT(10),BONUS_DATE DATETIME,FOREIGN
KEY (WORKER_REF_ID),REFERENCES Worker(WORKER_ID));

TABLE Title(WORKER_REF_ID INT,WORKER_TITLE CHAR(25), AFFECTED_FROM


DATETIME,FOREIGN KEY (WORKER_REF_ID)REFERENCES Worker(WORKER_ID)); [04/03/2021]

Worker Table
WORKER_ID FIRST_NAME LAST_NAME SALARY JOINING_DATE DEPARTMENT
1 John Doe 120000 2014-02-15 09:00:00 HR
2 Jane Smith 250000 2014-02-20 10:30:00 IT
3 Mike Johnson 80000 2014-01-10 11:00:00 IT
4 Emily Davis 150000 2015-03-05 09:00:00 Finance
5 Chris Wilson 95000 2014-02-25 08:00:00 HR
6 Alice Brown 60000 2014-04-15 07:30:00 IT
7 Bob Taylor 500000 2015-01-05 12:00:00 Marketing
8 David Green 95000 2023-09-29 08:00:00 HR

Bonus Table
WORKER_REF_ID BONUS_AMOUNT BONUS_DATE
1 5000 2023-05-10 09:00:00
2 7000 2023-05-15 09:30:00
5 3000 2023-06-01 10:00:00

Title Table
WORKER_REF_ID WORKER_TITLE AFFECTED_FROM
1 Manager 2014-02-15 09:00:00
2 Team Lead 2014-02-20 10:30:00
4 Manager 2015-03-05 09:00:00

Page No.20
Assignment 2 DBMS(3130702)

Consider above 3 tables ,assume appropriate data and solve following SQL queries

1. Print details of the Workers who are also Managers.


SELECT W.* FROM Worker W
JOIN Title T ON W.WORKER_ID = T.WORKER_REF_ID WHERE T.WORKER_TITLE = 'Manager';
Output:

2. SQL query to clone a new table from another table. CREATE TABLE Worker_Clone AS
SELECT * FROM Worker;

3. Fetch the list of employees with the same salary.


SELECT W1.FIRST_NAME, W1.LAST_NAME, W1.SALARY
FROM Worker W1
JOIN Worker W2 ON W1.SALARY = W2.SALARY AND W1.WORKER_ID <> W2.WORKER_ID;
Output:

4. Fetch “FIRST_NAME” from Worker table in upper case.


SELECT UPPER(FIRST_NAME) AS FIRST_NAME_UPPER
FROM Worker; Output:

Page No.21
Assignment 2 DBMS(3130702)

Q – (9) Consider the following relations and write SQL queries for given

statements. Assume suitable constraints.

job(job-id, job-title, minimum-salary, maximum-salary)


employee(emp-no, emp-name, emp-salary,dept-no) deposit(acc-
no, cust-name, branch-name, amount, account-date)
borrow(loan-no, cust-name, branch-name, amount) department
(dept-no, dept-name) [11/09/2021]

job Table
job-id job-title minimum-salary maximum-salary

1 Manager 50000 100000

2 Developer 30000 80000

3 Analyst 20000 60000

employee Table
emp-no emp-name emp-salary dept-no

001 Alice Smith 25000 1

002 Bob Johnson 15000 2

003 Charlie Brown 28000 1

004 Diana Prince 32000 3

005 Ethan Hunt 21000 1

borrow Table
loan-no cust-name branch-name amount

201 Alice Smith Seattle 10000

202 Bob Johnson San Francisco 15000

deposit Table

Page No.22
Assignment 2 DBMS(3130702)

acc-no cust-name branch-name amount account-date

101 Alice Smith Seattle 5000 2023-01-01

102 Bob Johnson San Francisco 3000 2022-12-15

103 Charlie Brown New York 7000 2023-02-20

104 Diana Prince Seattle 4000 2023-03-10

105 Ethan Hunt Boston 2000 2023-04-05

department Table
dept-no dept-name

1 Finance

2 HR

3 IT

1. Give name of employees whose employee number is '001'


SELECT emp-name
FROM employee
WHERE emp-no = '001';

Output:

2. Give name of depositors whose branch name starts from ‘S’.


SELECT cust-name
FROM deposit
WHERE branch-name LIKE 'S%';

Output:

Page No.23
Assignment 2 DBMS(3130702)

3. Give employee name(s) whose salary is between Rs. 20000 to 30000


and department name is Finance.

SELECT e.emp-name
FROM employee e
JOIN department d ON e.dept-no = d.dept-no
WHERE e.emp-salary BETWEEN 20000 AND 30000 AND d.dept-name = 'Finance'; Output:

4. Update the salary of employee by 10% of their salary who is working


in the Finance department.

UPDATE employee
SET emp-salary = emp-salary * 1.10
WHERE dept-no = (SELECT dept-no FROM department WHERE dept-name = 'Finance');
Output:

Page No.24
Assignment 2 DBMS(3130702)

Q – (10) Write a PL/SQL program that fetches records of all students and
insert record as students having CPI > 4 in ELIGIBLE table and students having

CPI <= 4 in NOT_ELIGIBLE table from student_master table. [11/09/2021]

DECLARE
CURSOR student_cursor IS

SELECT student_id, student_name, CPI


FROM student_master;

v_student_id student_master.student_id%TYPE;
v_student_name student_master.student_name%TYPE;
v_CPI student_master.CPI%TYPE;

BEGIN
FOR student_record IN student_cursor LOOP
v_student_id := student_record.student_id;
v_student_name := student_record.student_name;
v_CPI := student_record.CPI;

IF v_CPI > 4 THEN


INSERT INTO ELIGIBLE (student_id, student_name, CPI)

VALUES (v_student_id, v_student_name, v_CPI);


ELSE
INSERT INTO NOT_ELIGIBLE (student_id, student_name, CPI)
VALUES (v_student_id, v_student_name, v_CPI);
END IF;

END LOOP;

-- Commit the changes to make the inserts permanent


Page No.25
Assignment 2 DBMS(3130702)

COMMIT;

DBMS_OUTPUT.PUT_LINE('Records inserted successfully.');

EXCEPTION
WHEN OTHERS THEN

-- Handle exceptions and rollback


ROLLBACK;
DBMS_OUTPUT.PUT_LINE('Error occurred: ' || SQLERRM);
END;
/

Page No.26
Assignment 2 DBMS(3130702)

Q – (11) Write a PL/SQL block to print the given number is odd or even. [29/10/2020]
DECLARE

num INTEGER := 15; -- You can change this number to check different values
BEGIN
IF MOD(num, 2) = 0 THEN
DBMS_OUTPUT.PUT_LINE(num || ' is an even number.');
ELSE

DBMS_OUTPUT.PUT_LINE(num || ' is an odd number.');


END IF;
END;
/

Output:

Page No.27
Assignment 2 DBMS(3130702)

Q – (12) Consider the following relational schemas:

EMPLOYEE (EMPLOYEE_NAME, STREET, CITY)


WORKS (EMPLOYEE_NAME, COMPANYNAME, SALARY)

COMPANY (COMPANY_NAME, CITY) Give an expression in SQL for each of queries below:
[29/10/2020]

1. Specify the table definitions in SQL.

CREATE TABLE EMPLOYEE (


EMPLOYEE_NAME VARCHAR(255),
STREET VARCHAR(255),
CITY VARCHAR(255),
PRIMARY KEY (EMPLOYEE_NAME)
);

CREATE TABLE WORKS (


EMPLOYEE_NAME VARCHAR(255),
COMPANYNAME VARCHAR(255),
SALARY DECIMAL(10, 2),
PRIMARY KEY (EMPLOYEE_NAME, COMPANYNAME),
FOREIGN KEY (EMPLOYEE_NAME) REFERENCES EMPLOYEE(EMPLOYEE_NAME)
);

CREATE TABLE COMPANY (


COMPANY_NAME VARCHAR(255),
CITY VARCHAR(255),
PRIMARY KEY (COMPANY_NAME)
);
Output:

Page No.28
Assignment 2 DBMS(3130702)

EMPLOYEE Table
EMPLOYEE_NAME STREET CITY
Alice Johnson 123 Maple St New York
Bob Smith 456 Oak St Los Angeles
Charlie Brown 789 Pine St Chicago
David Williams 101 Birch St San Francisco

WORKS Table
EMPLOYEE_NAME COMPANYNAME SALARY
Alice Johnson First Bank Corporation 75000.00
Bob Smith Second Bank 60000.00
Charlie Brown First Bank Corporation 72000.00
David Williams Second Bank 58000.00

COMPANY Table
COMPANY_NAME CITY
First Bank Corporation Los Angeles
Second Bank San Francisco

2. Find the names of all employees who work for first Bank Corporation.

SELECT EMPLOYEE_NAME
FROM WORKS
WHERE COMPANYNAME = 'First Bank Corporation'; Output:

Page No.29
Assignment 2 DBMS(3130702)

3. Find the names and company names of all employees sorted in ascending order of
company name and descending order of employee names of that company.

SELECT W.EMPLOYEE_NAME, W.COMPANYNAME


FROM WORKS W
ORDER BY W.COMPANYNAME ASC, W.EMPLOYEE_NAME DESC; Output:

4. Change the city of First Bank Corporation to ‘New Delhi’.


UPDATE COMPANY SET
CITY = 'New Delhi'
WHERE COMPANY_NAME = 'First Bank Corporation’; Output:

Page No.30
Assignment 2 DBMS(3130702)

Q – (13) Write a PL/SQL block to print the sum of even numbers from 1 to 50. [29/10/2020]
DECLARE

sum_even NUMBER := 0; -- Variable to hold the sum of even numbers


BEGIN
FOR i IN 1 .. 50 LOOP
IF MOD(i, 2) = 0 THEN -- Check if the number is even
sum_even := sum_even + i; -- Add even number to the sum
END IF;
END LOOP;

-- Print the result


DBMS_OUTPUT.PUT_LINE('The sum of even numbers from 1 to 50 is: ' || sum_even);

END;

Output:

Page No.31
Assignment 2 DBMS(3130702)

Q – (14) Given the following relations

TRAIN (NAME, START, DEST)


TICKET (PNRNO., START, DEST, FARE)

PASSENGER (NAME, ADDRESS, PNRNO.) [29/10/2020]

TRAIN Table
NAME START DEST
Express Delhi Bangalore
Shatabdi Delhi Bangalore
Mail Mumbai Kolkata
Intercity Chennai Hyderabad

TICKET Table
PNRNO START DEST FARE
12345 Delhi Bangalore 1500
12346 Delhi Bangalore 1500
12347 Mumbai Kolkata 2000
12348 Chennai Hyderabad 1800
12349 Delhi Bangalore 1500

PASSENGER Table
NAME ADDRESS PNRNO
Tintin 123 Cartoon St 12345
Asterix 456 Gaul Ave 12346
Obelix 789 Gaul Blvd 12349
Sherlock 101 Baker St 12347

Page No.32
Assignment 2 DBMS(3130702)

Write SQL expressions for the following queries: Note: Assume NAME of Train is a column of Ticket.

1. List the names of passengers who are travelling from the start to the destination station of
the train.

SELECT P.NAME FROM PASSENGER P


JOIN TICKET T ON P.PNRNO = T.PNRNO
JOIN TRAIN R ON T.START = R.START AND T.DEST = R.DEST; Output:

2. List the names of passengers who have a return journey ticket.

SELECT P.NAME FROM PASSENGER P


JOIN TICKET T1 ON P.PNRNO = T1.PNRNO JOIN TICKET T2 ON P.PNRNO = T2.PNRNO
WHERE T1.START = T2.DEST AND T1.DEST = T2.START; Output:

3. Insert a new Shatabti train from Delhi to Bangalore.

INSERT INTO TRAIN (NAME, START, DEST) VALUES ('Shatabdi', 'Delhi', 'Bangalore'); Output:

Page No.33
Assignment 2 DBMS(3130702)

4. Cancel the ticket of Tintin.

DELETE FROM PASSENGER WHERE NAME = 'Tintin'; Output:

Page No.34
Assignment 2 DBMS(3130702)

Q – (15) Write a PL/SQL block to print the sum of Numbers from 1 to 100. [07/06/2019]
DECLARE

sum_total NUMBER := 0; -- Variable to hold the sum


BEGIN
FOR i IN 1 .. 100 LOOP sum_total := sum_total + i; -- Add the
current number to the sum END LOOP;

-- Print the result


DBMS_OUTPUT.PUT_LINE('The sum of numbers from 1 to 100 is: ' || sum_total); END;

Output:

Page No.35
Assignment 2 DBMS(3130702)

Q – (16) Write a PL/SQL block to print the given number is prime or not. [07/06/2019]

DECLARE
num INTEGER := 29; -- You can change this number to check different values
is_prime BOOLEAN := TRUE;
BEGIN
IF num <= 1 THEN

is_prime := FALSE;
ELSE
FOR i IN 2 .. TRUNC(SQRT(num)) LOOP
IF MOD(num, i) = 0 THEN
is_prime := FALSE;

EXIT; -- Exit loop if a divisor is found


END IF;
END LOOP;
END IF;

IF is_prime THEN
DBMS_OUTPUT.PUT_LINE(num || ' is a prime number.');
ELSE
DBMS_OUTPUT.PUT_LINE(num || ' is not a prime number.');
END IF;

END;
/

Output:

Page No.36
Assignment 2 DBMS(3130702)

Q – (17) Consider following schema and write SQL for given statements.

Student (RollNo, Name, DeptCode, City)

Department (DeptCode, DeptName) Result (RollNo, Semester, SPI) [30/11/2019]

Student Table
RollNo Name DeptCode City
101 Ashish 1 New York
102 Ramesh 2 Los Angeles
103 Suresh 1 Chicago
104 Nitesh 5 San Francisco
105 Piyush 6 Seattle
Department Table
DeptCode DeptName
1 Computer Science
2 Electrical Engineering
5 Mechanical Engineering
6 Civil Engineering
Result Table
RollNo Semester SPI
101 1 8.5
102 1 7.0
103 1 9.0
104 1 6.5
105 1 7.5

1. Display the name of students with RollNo whose name ends with ‘sh’.

SELECT RollNo, Name FROM Student WHERE Name LIKE '%sh'; Output:

Page No.37
Assignment 2 DBMS(3130702)

2. Display department wise total students whose total students are greater than 500. SELECT
D.DeptName, COUNT(S.RollNo) AS TotalStudents FROM Student S
JOIN Department D ON S.DeptCode = D.DeptCode GROUP
BY D.DeptName HAVING COUNT(S.RollNo) > 500; Output:

3. List out the RollNo, Name along with CPI of Student. SELECT S.RollNo, S.Name, AVG(R.SPI)
AS CPI FROM Student S JOIN Result R ON S.RollNo = R.RollNo GROUP BY S.RollNo, S.Name;
Output:

4. Create RollNo field as primary key for existing Student table.

ALTER TABLE Student ADD PRIMARY KEY (RollNo); Output:

5. Display student name who got highest SPI in semester 1.

SELECT S.Name FROM Student S


JOIN Result R ON S.RollNo = R.RollNo WHERE R.Semester = 1
ORDER BY R.SPI DESC LIMIT 1; Output:

Page No.38
Assignment 2 DBMS(3130702)

6. Display the list of students whose DeptCode is 5, 6,7,10.

SELECT * FROM Student WHERE DeptCode IN (5, 6, 7, 10); Output:

7. Create table Student_New from student table without data.

CREATE TABLE Student_New AS SELECT * FROM Student WHERE 1 = 0; Output:

Page No.39
Assignment 2 DBMS(3130702)

Q – (18) Consider the tables given below. Write the SQL queries for the questions given below:

T1 ( Empno, Ename , Salary, Designation,)

T2 (Empno, Deptno.) [30/11/2019]

T1 Table
Empno Ename Salary Designation
1 Geeta 9500 Manager
2 Ravi 12000 Developer
3 Sneha 8000 Analyst
4 Suresh 11000 Tester
5 Amit 5000 Intern
T2 Table
Empno Deptno
1 101
2 102
3 101
4 103
5 102

1. Display all the details of the employee whose salary is lesser than 10000.

SELECT * FROM T1 WHERE Salary < 10000; Output:

Page No.40
Assignment 2 DBMS(3130702)

2. Display the Deptno in which Employees with name starting with letter ‘S’ is working.

SELECT DISTINCT T2.Deptno FROM T1


JOIN T2 ON T1.Empno = T2.Empno WHERE T1.Ename LIKE 'S%'; Output:

3. Add a new column Deptname in table T2.

ALTER TABLE T2 ADD Deptname VARCHAR(255); Output:

4. Change the designation of Geeta from ‘Manager’ to ‘Senior Manager’.

UPDATE T1 SET Designation = 'Senior Manager' WHERE Ename = 'Geeta'; Output:

5. Find the total salary of all the employees department wise.

SELECT T2.Deptno, SUM(T1.Salary) AS TotalSalary FROM T1 JOIN


T2 ON T1.Empno = T2.Empno GROUP BY T2.Deptno; Output:

6. Add Empno as primary key in existing table T1.

ALTER TABLE T1 ADD PRIMARY KEY (Empno); Output:

Page No.41
Assignment 2 DBMS(3130702)

7. Display the Deptno having highest number of employees.

SELECT Deptno FROM T2


GROUP BY Deptno ORDER BY COUNT(Empno) DESC LIMIT 1; Output:

Page No.42

You might also like