Assighnment 2
Assighnment 2
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)
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)
Page No.2
Assignment 2 DBMS(3130702)
Page No.3
Assignment 2 DBMS(3130702)
Q – (2) Consider the following tables and answer the queries in SQL. [18/01/2024]
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
Page No.4
Assignment 2 DBMS(3130702)
Page No.5
Assignment 2 DBMS(3130702)
Page No.6
Assignment 2 DBMS(3130702)
Worker Table
WORKER_ID FIRST_NAME LAST_NAME SALARY JOINING_DATE DEPARTMENT
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
4 Manager 2022-08-01
Page No.7
Assignment 2 DBMS(3130702)
1. Write an SQL query to fetch “FIRST_NAME” from Worker table using the alias name as
<WORKER_NAME>
uppercase.
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.
Output:
4. Write an SQL query to print details of the Workers whose FIRST_NAME ends
with ‘h’ and contains six alphabets.
5. Write an SQL query to print details of the Workers who are also Managers.
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.
7. Write an SQL query to fetch the names of workers who earn the highest salary.
Page No.10
Assignment 2 DBMS(3130702)
DECLARE
avg_value NUMBER;
BEGIN
-- Call the function with example values
Output:
Page No.11
Assignment 2 DBMS(3130702)
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:
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.
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.
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 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
Page No.15
Assignment 2 DBMS(3130702)
2. Display all Publisher Name & Total Price of Books of that publisher.
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:
SELECT Author FROM Books GROUP BY Author ORDER BY COUNT(BookID) DESC LIMIT 1;
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));
1. Worker Table
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
2. Print details of the Workers whose SALARY lies between 100000 and 500000.
SELECT * FROM Worker WHERE SALARY BETWEEN 100000 AND 500000; 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));
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
2. SQL query to clone a new table from another table. CREATE TABLE Worker_Clone AS
SELECT * FROM Worker;
Page No.21
Assignment 2 DBMS(3130702)
Q – (9) Consider the following relations and write SQL queries for given
job Table
job-id job-title minimum-salary maximum-salary
employee Table
emp-no emp-name emp-salary dept-no
borrow Table
loan-no cust-name branch-name amount
deposit Table
Page No.22
Assignment 2 DBMS(3130702)
department Table
dept-no dept-name
1 Finance
2 HR
3 IT
Output:
Output:
Page No.23
Assignment 2 DBMS(3130702)
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:
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
DECLARE
CURSOR student_cursor IS
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;
END LOOP;
COMMIT;
EXCEPTION
WHEN OTHERS THEN
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
Output:
Page No.27
Assignment 2 DBMS(3130702)
COMPANY (COMPANY_NAME, CITY) Give an expression in SQL for each of queries below:
[29/10/2020]
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.
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
END;
Output:
Page No.31
Assignment 2 DBMS(3130702)
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.
INSERT INTO TRAIN (NAME, START, DEST) VALUES ('Shatabdi', 'Delhi', 'Bangalore'); Output:
Page No.33
Assignment 2 DBMS(3130702)
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
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;
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 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:
Page No.38
Assignment 2 DBMS(3130702)
Page No.39
Assignment 2 DBMS(3130702)
Q – (18) Consider the tables given below. Write the SQL queries for the questions given below:
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.
Page No.40
Assignment 2 DBMS(3130702)
2. Display the Deptno in which Employees with name starting with letter ‘S’ is working.
Page No.41
Assignment 2 DBMS(3130702)
Page No.42