CLASS–XII :: INFORMATICS PRACTICES
Question 1: Consider a database LOANS with the following table. Create a table Loan_Accounts with the
given attributes with relevant data types. Insert the given data into the table and answer the queries.
Table: Loan_Accounts
AccNo Cust_Name Loan_Amount Instalments Int_Rate Start_Date Interest
1 R.K. Gupta 300000 36 12.00 19–07–2009
2 S.P. Sharma 500000 48 10.00 22–03–2008
3 K.P. Jain 300000 36 NULL 08–03–2007
4 M.P. Yadav 800000 60 10.00 06–12–2008
5 S.P. Sinha 200000 36 12.50 03–01–2010
6 P. Sharma 700000 60 12.50 05–06–2008
7 K.S. Dhall 500000 48 NULL 05–03–2008
Queries:
CREATE DATABASE AND USE IT
1. Write a query to create the database Loans
CREATE DATABASE Loans;
2. Write a query to use the database Loans
USE Loans;
CREATE TABLE / INSERT INTO
3. Write a query to create Table Loan_Accounts and Insert Tuples into it:
CREATE TABLE Loan_Accounts (AccNo INT(1) PRIMARY KEY, Cust_Name VARCHAR(20),
Loan_Amount INT(6), Instalments INT(2), Int_Rate DECIMAL(4, 2), Start_Date DATE, Interest
DECIMAL(10, 2));
INSERT INTO Loan_Accounts(AccNo, Cust_Name, Loan_Amount, Instalments, Int_Rate, Start_Date)
VALUES (1, ‘R.K. Gupta’, 300000, 36, 12.00, ‘2009–07–19’);
INSERT INTO Loan_Accounts(AccNo, Cust_Name, Loan_Amount, Instalments, Int_Rate, Start_Date)
VALUES (2, ‘S.P. Sharma’, 500000, 48, 10.00, ‘2008–03–22’);
INSERT INTO Loan_Accounts(AccNo, Cust_Name, Loan_Amount, Instalments, Int_Rate, Start_Date)
VALUES (3, ‘K.P. Jain’, 300000, 36, NULL, ‘2007–03–08’);
INSERT INTO Loan_Accounts VALUES (4, ‘M.P. Yadav’, 800000, 60, 10.00, ‘2008–12–06’, NULL);
INSERT INTO Loan_Accounts VALUES (5, ‘S.P. Sinha’, 200000, 36, 12.50, ‘2010–01–03’, NULL);
INSERT INTO Loan_Accounts(AccNo, Cust_Name, Loan_Amount, Instalments, Int_Rate, Start_Date)
VALUES (6, ‘P. Sharma’, 700000, 60, 12.50, ‘2008–06–05’);
INSERT INTO Loan_Accounts VALUES (7, ‘K.S. Dhall’, 500000, 48, NULL, ‘2008–03–05’, NULL);
SIMPLE SELECT
4. Write a query to display the details of all the loans
SELECT * FROM Loan_Accounts;
5. Write a query to display the Account Number, Customer Name, and Loan Amount of all the loans
SELECT AccNo, Cust_Name, Loan_Amount FROM Loan_Accounts;
CONDITIONAL SELECT USING WHERE CLAUSE
6. Write a query to display the loans with less than 40 instalments
SELECT * FROM Loan_Accounts WHERE Instalments < 40;
Sree Vidyanikethan International School 2
.
7. Write a query to display the details of Account Number, Loan Amount of all loans started before
01–04–2009
SELECT AccNo, Loan_Amount FROM Loan_Accounts WHERE Start_Date < ‘2009–04–01’;
8. Write a query to display the Int_Rate of all the loans started after 01–04–2009
SELECT Int_Rate FROM Loan_Accounts WHERE Start_Date> ‘2009–04–01’;
USING NULL
9. Write a query to display the details of all the loans whose rate of interest is NULL
SELECT * FROM Loan_Accounts WHERE Int_Rate IS NULL;
10. Write a query to display the details of all the loans whose rate of interest is not NULL
SELECT * FROM Loan_Accounts WHERE Int_Rate IS NOT NULL;
USING DISTINCT CLAUSE
11. Write a query to display the amounts of various loans from the table Loan_Accounts. A loan amount
should appear only once
SELECT DISTINCT Loan_Amount FROM Loan_Accounts;
12. Write a query to display the number of instalments of various loans from the table Loan_Accounts.
An Instalment should appear only once.
SELECT DISTINCT Instalments FROM Loan_Accounts;
USING LOGICAL OPERATORS (NOT, AND, OR)
13. Write a query to display the details of all the loans started after 31–12–2008 for which the number of
instalments are more than 36
SELECT * FROM Loan_Accounts WHERE Start_Date>‘2008–12–31’ AND Instalments>36;
14. Write a query to display the Cust_Name and Loan_Amount for all the loans which do not have
number of instalments 36
SELECT Cust_Name, Loan_Amount FROM Loan_Accounts WHERE NOT Instalments=36;
15. Write a query to display the Cust_Name and Loan_Amount for all the loans for which the loan
amount is less than 500000 or Int_Rate is more than 12
SELECT Cust_name, Loan_Amount FROM Loan_Accounts WHERE Loan_Amount < 500000 OR
Int_Rate > 12;
16. Write a query to display the details of all the loans which started in the year 2009
SELECT * FROM Loan_Accounts WHERE Start_Date >= ‘2009–01–01’ AND
Start_Date >= ‘2009–12–31’;
17. Write a query to display the details of all the loans whose Loan_Amount is in the range 400000 to
500000
SELECT * FROM Loan_Accounts WHERE Loan_Amount>=400000 AND Loan_Amount<=500000;
18. Write a query to display the details of all the loans whose rate of interest is in the range 11% to 12%
SELECT * FROM Loan_Accounts WHERE Int_Rate>=11 AND Int_Rate<=12;
USING IN OPERATOR
19. Write a query to display the Cust_name and Loan_Amount for all the loans for which the number of
instalments are 24, 36 or 48 using IN operator
SELECT Cust_name, Loan_Amount FROM Loan_Accounts WHERE Instalments IN (24, 36, 48);
Sree Vidyanikethan International School 3
USING BETWEEN OPERATOR
20. Write a query to display the details of all the loans whose Loan_Amount is in the range 400000 to
500000 (USING BETWEEN OPERATOR)
SELECT * FROM Loan_Accounts WHERE Loan_Amount BETWEEN 400000 AND 500000;
21. Write a query to display the details of all the loans whose rate of interest is in the range 11% to 12%
(USING BETWEEN OPERATOR)
SELECT * FROM Loan_Accounts WHERE Int_Rate BETWEEN 11 AND 12;
USING LIKE OPERATOR
22. Write a query to display AccNo, Cust_Name and Loan_Amount for all the loans for which the
Cust_Name ends with ‘Sharma’
SELECT AccNo, Cust_Name, Loan_Amount FROM Loan_Accounts WHERE Cust_Name LIKE
‘%Sharma’;
23. Write a query to display AccNo, Cust_Name and Loan_Amount for all the loans for which the
Cust_Name ends with ‘a’
SELECT AccNo, Cust_Name, Loan_Amount FROM Loan_Accounts WHERE Cust_Name LIKE ‘%a’;
24. Write a query to display AccNo, Cust_Name and Loan_Amount for all the loans for which the
Cust_Name contains ‘a’
SELECT AccNo, Cust_Name, Loan_Amount FROM Loan_Accounts WHERE Cust_Name LIKE
‘%a%’;
25. Write a query to display AccNo, Cust_Name and Loan_Amount for all the loans for which the
Cust_Name does not contain ‘P’
SELECT AccNo, Cust_Name, Loan_Amount FROM Loan_Accounts WHERE Cust_Name NOT LIKE
‘%P%’;
26. Write a query to display AccNo, Cust_Name, and Loan_Amount for all the loans for which the
Cust_Name contains ‘a’ as the second last character
SELECT AccNo, Cust_Name, Loan_Amount FROM Loan_Accounts WHERE Cust_Name LIKE ‘%a_’;
USING ORDER BY CLAUSE
27. Write a query to display the details of all the loans in the ascending order of their Loan_Amount
SELECT * FROM Loan_Accounts ORDER BY Loan_Amount;
28. Write a query to display the details of all the loans in the descending order of their Start_Date
SELECT * FROM Loan_Accounts ORDER BY Start_Date DESC;
29. Write a query to display the details of all the loans in the ascending order of their Loan_Amount and
within Loan_Amount in the descending order of their Start_Date
SELECT * FROM Loan_Accounts ORDER BY Loan_Amount, Start_Date DESC;
USING UPDATE, DELETE, ALTER TABLE
30. Write a query to put the interest rate 11.50% for all the loans for which interest rate is NULL
UPDATE Loan_Accounts SET Int_Rate = 11.50 WHERE Int_Rate IS NULL;
31. Write a query to increase the interest rate by 0.5% for all the loans for which the loan amount is more
than 400000
UPDATE Loan_Accounts SET Int_Rate = Int_Rate + 0.5 WHERE Loan_Amount > 400000;
32. Write a query to replace Interest with Loan_Amount*Int_Rate*Instalments*12/100
UPDATE Loan_Accounts SET Interest = (Loan_Amount * Int_Rate * Instalments ) * 12 / 100;
Sree Vidyanikethan International School 4
33. Write a query to delete the records of all the loans whose start date is before 2007
DELETE FROM Loan_Accounts WHERE Start_Date < ‘ 2007–01–01’;
34. Write a query to delete the records of all the loans of ‘K.P. Jain’
DELETE FROM Loan_Accounts WHERE Cust_Name = ‘K.P. Jain’;
35. Write a query to add another column Category of type CHAR(1) in the table\
ALTER TABLE Loan_Accounts ADD Category CHAR(1);
Question 2: Consider a table Loan_Accounts with the given data below and answer the queries that follow
Table: Loan_Accounts
AccNo Cust_Name Loan_Amount Instalments Int_Rate Start_Date Interest
1 R.K. Gupta 300000 36 12.00 19–07–2009
2 S.P. Sharma 500000 48 10.00 22–03–2008
3 K.P. Jain 300000 36 NULL 08–03–2007
4 M.P. Yadav 800000 60 10.00 06–12–2008
5 S.P. Sinha 200000 36 12.50 03–01–2010
6 P. Sharma 700000 60 12.50 05–06–2008
7 K.S. Dhall 500000 48 NULL 05–03–2008
Queries:
WRITE THE OUTPUT PRODUCED BY THE FOLLOWING QUERIES
1. SELECT cust_name, LENGTH(Cust_Name), LCASE(Cust_Name), UCASE(Cust_Name)
FROM Loan_Accounts WHERE Int_Rate < 11.00;
Cust_Name LENGTH(Cust_Name) LCASE(Cust_Name) UCASE(Cust_Name)
S.P. Sharma 10 s.p. sharma S.P. SHARMA
M.P. Yadav 9 m.p. yadav M.P. YADAV
2. SELECT LEFT(Cust_Name, 3), RIGHT(Cust_Name, 3), SUBSTR(Cust_Name, 1, 3) FROM
Loan_Accounts WHERE Int_Rate > 10.00;
LEFT(Cust_Name, 3) RIGHT(Cust_Name, 3) SUBSTR(Cust_Name, 1, 3)
R.K pta R.K
S.P nha S.P
P.S rma P.S
3. SELECT RIGHT(Cust_Name, 3), SUBSTR(Cust_Name, 5) FROM Loan_Accounts;
RIGHT(Cust_Name, 3) SUBSTR(Cust_Name, 5)
pta Gupta
rma Sharma
ain Jain
dav Yadav
nha Sinha
rma Arma
all Dhall
4. SELECT DAYNAME(Start_Date) FROM Loan_Accounts;
DAYNAME(Start_Date)
Sunday
Saturday
Thursday
Saturday
Sunday
Thursday
Sree Vidyanikethan International School 5
Wednesday
5. SELECT ROUND(Int_Rate*110/100, 2) FROM Loan_Accounts WHERE Int_Rate > 10;
ROUND(Int_Rate*110/100, 2)
13.20
13.75
13.75
6. SELECT POW(4,3), POW(3,4);
POW(4,3) POW(3,4)
64 81
7. SELECT ROUND(543.5694,2), ROUND(543.5694), ROUND(543.5694,-1);
ROUND(543.5694,2) ROUND(543.5694) ROUND(543.5694,-1)
543.57 544 540
8. SELECT TRUNCATE(543.5694,2), TRUNCATE(543.5694,-1);
TRUNCATE(543.5694,2) TRUNCATE(543.5694,-1)
543.56 540
9. SELECT LENGTH("Prof. M. L. Sharma");
LENGTH("Prof. M. L. Sharma")
18
10. SELECT CONCAT("SHEIKH", " HAROON") "FULL NAME";
FULL NAME
SHEIKHHAROON
11. SELECT YEAR(CURDATE()), MONTH(CURDATE()), DAY(CURDATE());
YEAR(CURDATE()) MONTH(CURDATE()) DAY(CURDATE())
2022 1 2
12. SELECT DAYOFYEAR(CURDATE()), DAYOFMONTH(CURDATE()), DAYNAME(CURDATE());
DAYOFYEAR(CURDATE()) DAYOFMONTH(CURDATE()) DAYNAME(CURDATE())
2 2 Sunday
13. SELECT LEFT("Unicode",3), RIGHT("Unicode",4);
LEFT("Unicode",3) RIGHT("Unicode",4)
Uni code
14. SELECT INSTR("UNICODE","CO"), INSTR("UNICODE","CD");
INSTR("UNICODE","CO") INSTR("UNICODE","CD")
4 0
15. SELECT MID("Informatics",3,4), SUBSTR("Practices",3);
MID("Informatics",3,4) SUBSTR("Practices",3)
form actices
Sree Vidyanikethan International School 6
Question 3: In MySQL create the following table with given data and answer the queries that follow
Table : Student
Student_ID Name Marks
1001 Kalyan 98
1002 Chakravarthy 46
1003 Swamy NULL
1004 Narasimha 25
1005 SreeDevi 94
Queries:
1. Creating Table ‘Student’:
CREATE TABLE Student (Student_ID INT(4) PRIMARY KEY, Name VARCHAR(20),
Marks INT(3));
2. Inserting Tuples into Table ‘Student’:
INSERT INTO Student VALUES (1001, 'Kalyan', 98);
INSERT INTO Student VALUES (1002, 'Chakravarthy', 46);
INSERT INTO Student VALUES (1003, 'Swamy', NULL);
INSERT INTO Student VALUES (1004, 'Narasimha', 25);
INSERT INTO Student VALUES (1005, 'SreeDevi', 94);
3. Write a query to display the details of the students whose Marks are greater than 80
SELECT * FROM Student WHERE Marks > 80;
4. Write a query to display Student ID, Name of those students whose names starts with 'S'
SELECT Student_ID, Name FROM Student WHERE Name LIKE S%;
5. Write a query to display all the details of Students whose Marks are greater than or equal to 50 and
less than or equal to 90, using BETWEEN
SELECT * FROM Student WHERE Marks BETWEEN 50 and 90;
6. Write a query to replace the Marks NULL with 68 for the Student with ID 1003.
UPDATE Student SET Marks = 68 WHERE Marks IS NULL;
7. Write a query to delete the student information with the Student ID 1004;
DELETE FROM Student WHERE Student_ID = 1004;
8. Write a query to display the Minimum, Maximum, Sum and Average of the Marks
SELECT MIN(Marks), MAX(Marks), SUM(Marks), AVG(Marks) FROM Student;
9. Write a query to display all the details of students in the ascending order of Student_ID
SELECT * FROM Student ORDER BY Student_ID;
10. Write a query to display all the details of students in the descending order of Marks, and if there is a tie
with Marks then in ascending order of Names of Students
SELECT * FROM Student ORDER BY Marks DESC, Name;
Sree Vidyanikethan International School 7
Question 4: In MySQL create the following table with given data and answer the queries that follow
Table : ORDERS
Order_ID Cust_ID Cust_Name Country Order_Date
O101 11101 Ramesh India 2020–11–03
O102 11102 Samuel USA 2020–11–14
O103 11103 Pragya India 2020–10–30
O104 11104 John USA 2020–08–15
O105 11105 Sowmya Nepal 2020–11–16
Queries:
1. Creating Table ‘Orders’:
CREATE TABLE Orders (Order_ID VARCHAR(5), Cust_ID INT(5), Cust_Name VARCHAR(10),
Country VARCHAR(10), Order_Date DATE);
2. Inserting Tuples into Table ‘Orders’:
INSERT INTO Orders VALUES (O101, 11101, Ramesh, India, 2020–11–03);
INSERT INTO Orders VALUES (O102, 11102, Samuel, USA, 2020–11–14);
INSERT INTO Orders VALUES (O103, 11103, Pragya, India, 2020–10–30);
INSERT INTO Orders VALUES (O104, 11104, John, USA, 2020–08–15);
INSERT INTO Orders VALUES (O105, 11105, Sowmya, Nepal, 2020–11–16);
3. Write a query to display the DayName of the Ordered Date like Monday, Tuesday, … of all the orders
SELECT DAYNAME(Order_Date) FROM Orders;
4. Write a query to display the MonthName of the Ordered Date like January, February, … of all the
orders
SELECT MONTHNAME(Order_Date) FROM Orders;
5. Write a query to display Minimum and Maximum of Date of Orders
SELECT MIN(Order_Date), MAX(Order_Date) FROM Orders;
.
6. Write a query to display the details of USA orders
SELECT * FROM Orders WHERE Country = USA;
7. Write a query to display the count of orders Country–wise
SELECT Country, COUNT(Country) FROM Orders GROUP BY Country;
8. Write a query to display the Day, Month and Year of Orders of India
SELECT DAY(Order_Date), MONTH(Order_Date), YEAR(Order_Date) FROM Orders WHERE
Country = India;
9. Write a query to display the details of orders in the descending order of Order_Date
SELECT * FROM Orders ORDER BY Order_Date DESC;
10. Write a query to display Customer ID and Customer Name of those orders whose Customer Name ends
with a
SELECT Cust_ID, Cust_Name FROM Orders WHERE Cust_Name LIKE %a;
Sree Vidyanikethan International School 8
Question 5: In MySQL create the following table with given data and answer the queries that follow
Table: SHOES
Code Name Type Size Cost Margin Qty
1001 School Canvas School 6 132.50 2.00 1200
1002 School Canvas School 7 135.50 2.00 800
1003 School Canvas School 8 140.75 2.00 600
1011 School Leather School 6 232.50 2.00 2200
1012 School Leather School 7 270.00 2.00 1280
1013 School Leather School 8 320.75 NULL 1100
1101 Galaxy Office 7 640.00 3.00 200
1102 Galaxy Office 8 712.00 3.00 500
1103 Galaxy Office 9 720.00 3.00 400
1201 Tracker Sports 6 700.00 NULL 280
1202 Tracker Sports 7 745.25 3.50 NULL
1203 Tracker Sports 8 800.50 3.50 600
1204 Tracker Sports 9 843.00 NULL 860
Queries:
1. Creating Table ‘Shoes’:
CREATE TABLE Shoes (Code INT(4), Name VARCHAR(25), Type VARCHAR(10), Size INT(1),
Cost DECIMAL(5,2), Margin DECIMAL(3,2), Qty INT(5));
2. Inserting Tuples into Table ‘Shoes’:
INSERT INTO Shoes VALUES (1001, School Canvas, School, 6, 132.50, 2.00, 1200);
INSERT INTO Shoes VALUES (1002, School Canvas, School, 7, 135.50, 2.00, 800);
INSERT INTO Shoes VALUES (1003, School Canvas, School, 8, 140.75, 2.00, 600);
INSERT INTO Shoes VALUES (1011, School Leather, School, 6, 232.50, 2.00, 2200);
INSERT INTO Shoes VALUES (1012, School Leather, School, 7, 270.00, 2.00, 1280);
INSERT INTO Shoes VALUES (1013, School Leather, School, 8, 320.75, NULL, 1100);
INSERT INTO Shoes VALUES (1101, Galaxy, Office, 7, 640.00, 3.00, 200);
INSERT INTO Shoes VALUES (1102, Galaxy, Office, 8, 712.00, 3.00, 500);
INSERT INTO Shoes VALUES (1103, Galaxy, Office, 9, 720.00, 3.00, 400);
INSERT INTO Shoes VALUES (1201, Tracker, Sports, 6, 700.00, NULL, 280);
INSERT INTO Shoes VALUES (1202, Tracker, Sports, 7, 745.25, 3.50, NULL);
INSERT INTO Shoes VALUES (1203, Tracker, Sports, 8, 800.50, 3.50, 600);
INSERT INTO Shoes VALUES (1204, Tracker, Sports, 9, 843.00, NULL, 860);
3. Write a query to display Minimum Cost of all the items
SELECT MIN(Cost) FROM Shoes;
4. Write a query to display the Minimum Cost of the School Shoes
SELECT MIN(Cost) FROM Shoes WHERE Type='School';
5. Write a query to display the Minimum of Cost*Qty of Shoes
SELECT MIN(Cost*Qty) FROM Shoes;
.
6. Write a query to display Maximum Cost of all the items
SELECT MAX(Cost) FROM Shoes;
Sree Vidyanikethan International School 9
7. Write a query to display the Maximum Cost of the Sports Shoes
SELECT MAX(Cost) FROM Shoes WHERE Type='Sports';
8. Write a query to display the Maximum of Cost*Qty of Shoes
SELECT MAX(Cost*Qty) FROM Shoes;
9. Write a query to display the Average of Margin of Shoes
SELECT AVG(Margin) FROM Shoes;
10. Write a query to display the Average of Margins of Office Shoes
SELECT AVG(Margin) FROM Shoes WHERE Type='Office';
11. Write a query to display the Sum of Quantity of Shoes
SELECT SUM(Qty) FROM Shoes;
12. Write a query to display the Sum of Cost*Quantity of School Shoes
SELECT SUM(Qty*Cost) FROM Shoes WHERE Type=School';
13. Write a query to Count the number of rows in the table
SELECT COUNT(*) FROM Shoes;
14. Write a query to count the distinct number of rows of each Type of the table Shoes
SELECT COUNT(Distinct Type) FROM Shoes;
15. Write a query to Count the number of Non–Null values in the column Margin in the table
SELECT COUNT(Margin) FROM Shoes;
16. Write a query to display the details of Shoes in the ascending order of Cost
SELECT * FROM Shoes ORDER BY Cost;
17. Write a query to display the details of Shoes in the descending order of Margin
SELECT * FROM Shoes ORDER BY Margin DESC;
18. Write a query to display the Sum of Quantities of each type of Shoes
SELECT Type, SUM(Qty) FROM Shoes GROUP BY Type;
.
19. Write a query to display the Minimum, Maximum, Average of Margin of each type of Shoes
SELECT Type, MIN(Margin), MAX(Margin), AVG(Margin) FROM Shoes GROUP BY Type;
20. Write a query to display the Count of each Type of Shoes
SELECT Type, COUNT(Type) FROM Shoes GROUP BY Type;
21. Write a query to display the Sum of Quantities of each type of Shoes whose Sum of Quantities is
greater than 1500
SELECT Type, SUM(Qty) FROM Shoes GROUP BY Type HAVING SUM(Qty) > 1500;
22. Write a query to display the Count of each Type of Shoes whose Count is greater than or equal to 3
SELECT Type, COUNT(Type) FROM Shoes GROUP BY Type HAVING COUNT(Type) >= 3;