0% found this document useful (0 votes)
98 views29 pages

Books Isbn - N o Title Publisher - I D Year

Uploaded by

23ce020
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)
98 views29 pages

Books Isbn - N o Title Publisher - I D Year

Uploaded by

23ce020
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/ 29

1)[18/01/2024]

Consider the following tables and answer the queries in SQL.


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_n title publisher_i year


o d
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_i author_name country city


d
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_nam city


e
1 Academic Press New York
2 McGaw Hill Chicago
3 Random House London
4 Tech Books San Francisco
5 Pearson Ahmedabad

4. WrittenBy

isbn_n author_id
o
123456 1
654321 2
112233 3
444555 4
678901 10
678901 1

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:
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:
2) [18/01/2024]
Consider the following tables and answer the queries in SQL.
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_i prod_name category price


d
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 Australia Sydney
Johnson

3. Orders

order_i cust_id prod_id order_date quantit


d y
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:
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:
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:
3)[24/02/2023]
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));
Consider above 3 tables ,assume appropriate data and solve
following SQL queries:

Worker Table

WORKER_I FIRST_NAME LAST_NAME SALAR JOINING_DATE DEPARTMENT


D Y

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 2020-01-01
Manager

4 Manager 2022-08-01

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:

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:

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:
4) [24/02/2023]

Write a PL/SQL function which takes 3 integer numbers as a parameters


and return an average of same.

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:
5)[28/07/2023]
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)
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:

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:
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:
6)[15/07/2022]
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)
1: List all Books whose Title contains word ‘DBMS’.
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.
4. Display the author name whose number of books is maximum in library.
5: Display all Books assigned to student with name “RAJESH”.

7)[04/03/2021]
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));
Consider above 3 tables ,assume appropriate data and solve following SQL queries
1. Find out unique values of DEPARTMENT from Worker table
2. Print details of the Workers whose SALARY lies between 100000 and 500000.
3. Print details of the Workers who have joined in Feb’2014.
4. Fetch worker names with salaries >= 50000 and <= 100000.

8) [04/03/2021]
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));
Consider above 3 tables ,assume appropriate data and solve following SQL queries
1. Print details of the Workers who are also Managers.
2. SQL query to clone a new table from another table.
3. Fetch the list of employees with the same salary.
4. Fetch “FIRST_NAME” from Worker table in upper case.
9)[11/09/2021]

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)

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- emp-name emp-salary dept-no


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- amount


name

201 Alice Smith Seattle 10000

202 Bob Johnson San Francisco 15000

deposit Table

acc- cust-name branch-name amount account-


no 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:

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:

10) [11/09/2021]
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.

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


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;
/
11)[29/10/2020]
Write a PL/SQL block to print the given number is odd or even.
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:
12)[29/10/2020]
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:
1. Specify the table definitions in SQL.
2. Find the names of all employees who work for first Bank Corporation.
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.
4. Change the city of First Bank Corporation to ‘New Delhi’.
13)[29/10/2020]
Write a PL/SQL block to print the sum of even numbers from 1 to 50.
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:
14)[29/10/2020]
Given the following relations
TRAIN (NAME, START, DEST)
TICKET (PNRNO., START, DEST, FARE)
PASSENGER (NAME, ADDRESS, PNRNO.)
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.
2. List the names of passengers who have a return journey ticket.
3. Insert a new Shatabti train from Delhi to Bangalore.
4. Cancel the ticket of Tintin.
15)[07/06/2019]
Write a PL/SQL block to print the sum of Numbers from 1 to 100.
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:
16)[07/06/2019]
Write a PL/SQL block to print the given number is prime or not.

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:

17)[30/11/2019]
Consider following schema and write SQL for given statements.
Student (RollNo, Name, DeptCode, City)
Department (DeptCode, DeptName) Result (RollNo, Semester, SPI)
1. Display the name of students with RollNo whose name ends with ‘sh’.
2. Display department wise total students whose total students are greater than 500.
3. List out the RollNo, Name along with CPI of Student.
4. Create RollNo field as primary key for existing Student table.
5. Display student name who got highest SPI in semester 1.
6. Display the list of students whose DeptCode is 5, 6,7,10.
7. Create table Student_New from student table without data.
18)[30/11/2019]
Consider the tables given below. Write the SQL queries for the questions given below:
T1 ( Empno, Ename , Salary, Designation,)
T2 (Empno, Deptno.)
1. 1.Display all the details of the employee whose salary is lesser than 10000.
2. 2.Display the Deptno in which Employees with name starting with letter ‘S’ is working.
3. 3.Add a new column Deptname in table T2.
4. 4.Change the designation of Geeta from ‘Manager’ to ‘Senior Manager’.
5. Find the total salary of all the employees department wise.
6. 6.Add Empno as primary key in existing table T1.
7. 7.Display the Deptno having highest number of employees.

You might also like