Python and SQL Programming Exercises
Python and SQL Programming Exercises
INFORMATICS
LABORATORY WORK
Name:Utkarsh Shah
Roll number:4125
1.12 Program 12 – 8
Multiplication table
3
n=int(input("Enter a number"))
if n%2==0:
print("Even")
6
else:
print("Odd")
[Link] a program to create a user defined function to create a right angled triangle
7
def print_right_triangle(rows):
if rows <= 0:
print("Number of rows must be a positive integer.")
return
[Link] a program to create a user defined function to find the area of rectangle.
[Link] a program to enter a numbers and check if it’s positive or negative or zero.
n=int(input("Enter a number"))
if n>0:
print("Positive")
elif n<0:
8
print("Negative")
else:
print("Zero")
n=int(input("Enter a number"))
print("Multiplication table")
for i in range(1,11):
9
print(i*n)
[Link] a program to enter a number and add all even and odd numbers including it.
n=int(input("Enter a number"))
i=n
e=o=0
while(n>0):
d=n%10
if d%2==0:
e=e+d
else:
o=o+d
n//=10
print("Sum of odd digits ",o)
print("Sum of even digits ",e)
[Link] a program to obtain a number and find out if it’s a perfect number or not.
n=int(input("Enter a number"))
s=0
for i in range(1,n):
if n%i==0:
s+=i
if s==n :
print("Perfect")
else:
print("Not Perfect")
15. Write a program to find the number of perfect numbers from 1 to 100.
10
print("")
s=0
c=0
for i in range(1,101):
n=i
s=0
for k in range(1,n):
if n%k==0:
s+=i
if s==n :
c=c+1
print("Frequency of perfect numbers between 0-101 is= ",c)
n=int(input("Enter a number"))
11
c=0
for i in range(1,n+1):
if n%i==0:
c+=1
if c==2 :
print("Prime")
else:
print("Not Prime")
[Link] a program to find out all prime numbers within a given range.
import random
def game():
n = [Link](1, 10)
c=0
while True:
x = int(input("Guess a number between 1 and 10: "))
c += 1
if x > n:
print("Too high!")
elif x < n:
print("Too low!")
else:
print("You Got It!!")
break
print("Attempts taken:", c)
game()
[Link] a program to enter a sentence and find the number of vowels in it.
for ch in s:
if ch in v:
c += 1
13
if year % 400 == 0:
print(year, "is a Leap Year and also a Century Year.")
elif year % 100 == 0:
print(year, "is a Century Year but not a Leap Year.")
elif year % 4 == 0:
print(year, "is a Leap Year.")
else:
print(year, "is neither a Leap Year nor a Century Year.")
import math
i=0
x=int(input("How many times do you want to use the AREA calculator??"))
while(i<x) :
print("\n===== Area Calculator =====")
print("1. Circle")
print("2. Rectangle")
print("3. Triangle")
print("4. Square")
print("5. Exit")
if choice == 1:
r = float(input("Enter radius of the circle: "))
area = [Link] * r * r
print("Area of Circle =", area)
elif choice == 2:
l = float(input("Enter length: "))
b = float(input("Enter breadth: "))
area = l * b
print("Area of Rectangle =", area)
elif choice == 3:
base = float(input("Enter base: "))
height = float(input("Enter height: "))
area = 0.5 * base * height
print("Area of Triangle =", area)
elif choice == 4:
side = float(input("Enter side length: "))
15
area = side * side
print("Area of Square =", area)
elif choice == 5:
print("Exiting the program. Goodbye!")
break
else:
print("Invalid choice! Please select between 1 and 5.")
i+=1
[Link] a program to find out if the given 3-digit number is an Armstrong number or
not.
25. Write a program to print fibonacci series till the given number of terms.
c=0
a=0
b=1
n=int(input("Enter the number of terms"))
print("Fibonacci Series")
print(a)
print(b)
i=3
while(i<=n):
c=a+b
print(c)
a=b
b=c
i+=1
26. Write a program to create stone , paper , scissor game using ascii.
import random
stone = "stone"
paper = "paper"
scissor = "scissor"
img2 = '''
.-. _
||/)
| |/ /
_|__ /_
/ __)-' )
\ `(.-')
> ._>-'
/ \/
'''
img3 = '''
___..__
__..--""" ._ __.'
"-..__
'"--..__";
___ '--...__"";
`-..__ '"---..._;"
""""----'
'''
# Game logic
if comp == player:
print("It's a draw!")
elif (player == stone and comp == scissor) or \
(player == scissor and comp == paper) or \
(player == paper and comp == stone):
print("Congrats, you win :)")
else:
print("Sorry, you lost :(")
28. Write a program to enter values in a list and add all its values.
for i in range(n):
val = int(input(f"Enter number {i+1}: "))
[Link](val)
total = sum(numbers)
print("The list is:", numbers)
print("The sum of all values is:", total)
[Link] a program to enter values in a list and segregate into odd and even.
[Link] a program to find those numbers which are divisible by both 5 and 7 between
500 and 700(both included)
print("Numbers divisible by 5 and 7 between 500 and 700 both included are:")
for i in range(500,701):
if i%5==0 and i%7==0:
print(i)
[Link] a program to enter numbers in a list and create and print a new list by
removing all even numbers from it.
l=[20,43,76,26,64,33,97,13]
print("List without even numbers")
o=[]
for i in range (len(l)):
if l[i]%2!=0:
[Link](l[i])
print(o)
21
[Link] a program to enter words in a list and print a new list with first letter of each
word .
l=["India","Australia","South Africa","New Zealand","England"]
n=[]
for i in range(len(l)):
x=l[i]
[Link](x[0])
print("List with first letter of each word")
print(n)
l1=[]
c=0
print("Enter words in a list")
for i in range(5):
s=input("")
[Link](s)
print("Words with 4 characters")
for k in range(5):
c=len(l1[k])
if c==4:
print(l1[k])
print("Enter 1. to append")
print("Enter 2. to extend")
print("Enter 3. to insert")
print("Enter 4. to remove")
22
print("Enter 5. to pop")
print("Enter 6. to delete (slice)")
print("Enter 7. to reverse")
print("Enter 8. to sort")
print("Enter 9. to split string")
print("Enter 10. to find the length")
print("Enter 11. to find index")
print("Enter 12. to find sum")
print("Enter 13. to find minimum value")
print("Enter 14. to find maximum value")
print("Enter 15. to count occurrences")
def append_item():
l1 = []
print("Enter numbers in a list")
for i in range(5):
n = int(input())
[Link](n)
x = int(input("Enter the value you want to append: "))
[Link](x)
print("Modified list =", l1)
def extend_list():
l1 = []
print("Enter numbers in first list")
for i in range(5):
n = int(input())
[Link](n)
l2 = []
print("Enter numbers in second list to extend")
for k in range(5):
m = int(input())
[Link](m)
[Link](l2)
print("Modified list =", l1)
def insert_item():
l1 = []
print("Enter numbers in a list")
for i in range(5):
n = int(input())
[Link](n)
v = int(input("Enter the value you want to insert: "))
ind = int(input("Enter the index: "))
[Link](ind, v)
print("Modified list =", l1)
23
def remove_item():
l1 = []
print("Enter numbers in a list")
for i in range(5):
n = int(input())
[Link](n)
v = int(input("Enter the value you want to remove: "))
if v in l1:
[Link](v)
else:
print("Value not found!")
print("Modified list =", l1)
def pop_item():
l1 = []
print("Enter numbers in a list")
for i in range(5):
n = int(input())
[Link](n)
v = int(input("Enter the index to pop: "))
if 0 <= v < len(l1):
[Link](v)
else:
print("Invalid index!")
print("Modified list =", l1)
def delete_slice():
l1 = []
print("Enter numbers in a list")
for i in range(5):
n = int(input())
[Link](n)
s = int(input("Enter the start index: "))
e = int(input("Enter the stop index: "))
del l1[s:e]
print("Modified list =", l1)
def reverse_list():
l1 = []
print("Enter numbers in a list")
for i in range(5):
n = int(input())
[Link](n)
[Link]()
print("Reversed list =", l1)
def sort_list():
24
l1 = []
print("Enter numbers in a list")
for i in range(5):
n = int(input())
[Link](n)
[Link]()
print("Ascending sorted list =", l1)
[Link](reverse=True)
print("Descending sorted list =", l1)
def split_string():
s = input("Enter a string: ")
words = [Link]()
print("Split string =", words)
def find_length():
l1 = []
print("Enter numbers in a list")
for i in range(5):
n = int(input())
[Link](n)
print("Length of the list =", len(l1))
def find_index():
l1 = []
print("Enter numbers in a list")
for i in range(5):
n = int(input())
[Link](n)
v = int(input("Enter the value whose index you want: "))
if v in l1:
print("Index =", [Link](v))
else:
print("Value not found!")
def find_sum():
l1 = []
print("Enter numbers in a list")
for i in range(5):
n = int(input())
[Link](n)
print("Sum of values in the list =", sum(l1))
def find_min():
l1 = []
print("Enter numbers in a list")
for i in range(5):
n = int(input())
25
[Link](n)
print("Minimum value in the list =", min(l1))
def find_max():
l1 = []
print("Enter numbers in a list")
for i in range(5):
n = int(input())
[Link](n)
print("Maximum value in the list =", max(l1))
def count_occurrences():
l1 = []
print("Enter numbers in a list")
for i in range(5):
n = int(input())
[Link](n)
v = int(input("Enter the value to count: "))
print("Frequency of value in the list =", [Link](v))
if ch == 1:
append_item()
elif ch == 2:
extend_list()
elif ch == 3:
insert_item()
elif ch == 4:
remove_item()
elif ch == 5:
pop_item()
elif ch == 6:
delete_slice()
elif ch == 7:
reverse_list()
elif ch == 8:
sort_list()
elif ch == 9:
split_string()
elif ch == 10:
find_length()
elif ch == 11:
find_index()
elif ch == 12:
find_sum()
elif ch == 13:
find_min()
elif ch == 14:
find_max()
26
elif ch == 15:
count_occurrences()
else:
print("Invalid choice!")
27
SQL
1. Consider the following tables EMPLOYEE and write the SQL query for the following
requirements.
mysql> INSERT INTO EMPLOYEE (Eid, Name, Deptid, Qualification, Gender, Points, Basic, DA, HRA, Bonus)
VALUES
-> (1, 'Deepali', 101, 'MCA', 'F', 20, 6000, 2000, 2300, 200),
-> (2, 'Rajat', 101, 'BCA', 'M', 15, 2000, 300, 300, 30),
-> (3, 'Hari', 102, 'B.A', 'M', 15, 1000, 300, 300, 40),
-> (4, 'Harry', 102, 'M.A', 'M', 40, 1500, 390, 490, 30),
-> (5, 'Sumit', 103, '[Link]', 'M', 32, 8000, 900, 900, 80),
-> (6, 'Jyoti', 101, '[Link]', 'F', 12, 10000, 300, 490, 89);
Query OK, 6 rows affected (0.00 sec)
Records: 6 Duplicates: 0 Warnings: 0
+-------+
| Name |
+-------+
| Hari |
| Harry |
+-------+
2 rows in set (0.00 sec)
+-------+---------------+
| Name | Qualification |
+-------+---------------+
| Sumit | [Link] |
+-------+---------------+
1 row in set (0.00 sec)
4. Display the record whose gender is ‘f’ and deptid is101 and arrange in ascending order of
name and descending of deptid.
mysql> SELECT * FROM EMPLOYEE WHERE Gender='F' AND Deptid=101
-> ORDER BY Name ASC, Deptid DESC;
+------+---------+--------+---------------+--------+--------+-------+------+------+-------+
| Eid | Name | Deptid | Qualification | Gender | Points | Basic | DA | HRA | Bonus |
+------+---------+--------+---------------+--------+--------+-------+------+------+-------+
29
| 1 | Deepali | 101 | MCA |F | 20 | 6000 | 2000 | 2300 | 200 |
| 6 | Jyoti | 101 | [Link] |F | 12 | 10000 | 300 | 490 | 89 |
+------+---------+--------+---------------+--------+--------+-------+------+------+-------+
2 rows in set (0.00 sec)
5. Add one more field for the employee table -DOJ (date of joining) next to Dept id.
mysql> ALTER TABLE EMPLOYEE ADD DOJ DATE AFTER Deptid;
+------+---------+--------+------+---------------+--------+--------+-------+------+------+-------+
| Eid | Name | Deptid | DOJ | Qualification | Gender | Points | Basic | DA | HRA | Bonus |
+------+---------+--------+------+---------------+--------+--------+-------+------+------+-------+
| 1 | Deepali | 101 | NULL | MCA |F | 20 | 6000 | 2000 | 2300 | 200 |
| 2 | Rajat | 101 | NULL | BCA |M | 15 | 2000 | 300 | 300 | 30 |
| 3 | Hari | 102 | NULL | B.A |M | 15 | 1000 | 300 | 300 | 40 |
| 5 | Sumit | 103 | NULL | [Link] |M | 32 | 8000 | 900 | 900 | 80 |
+------+---------+--------+------+---------------+--------+--------+-------+------+------+-------+
4 rows in set (0.00 sec)
7. Display Name, Deptid, Point, and NewScore. The NewScore is a product of Point and 12
8. Display the Name, deptid, Basic whose date of joining is after 23/09/2000.
+-----+---------+--------+------+---------------+--------+--------+-------+------+------+-------+
| Eid | Name | Deptid | DOJ | Qualification | Gender | Points | Basic | DA | HRA | Bonus |
+-----+---------+--------+------+---------------+--------+--------+-------+------+------+-------+
| 5 | Sumit | 103 | NULL | [Link] |M | 32 | 8000 | 900 | 900 | 80 |
| 2 | Rajat | 101 | NULL | BCA |M | 15 | 2000 | 300 | 400 | 30 |
| 4 | Harry | 102 | NULL | M.A |M | 40 | 1500 | 390 | 490 | 30 |
| 3 | Hari | 102 | NULL | B.A |M | 15 | 1000 | 300 | 400 | 40 |
| 1 | Deepali | 101 | NULL | MCA |F | 20 | 6000 | 2000 | 2300 | 200 |
+-----+---------+--------+------+---------------+--------+--------+-------+------+------+-------+
5 rows in set (0.00 sec)
+---------+
| Ename |
+---------+
| Deepali |
| Hari |
+---------+
2 rows in set (0.00 sec)
15.Change the points of Jyoti to 28.
mysql> SELECT *
-> FROM EMPLOYEE
-> WHERE Qualification IN ('B.A','M.A','MCA');
+-----+---------+--------+------+---------------+--------+--------+-------+------+------+-------+
| Eid | Ename | Deptid | DOJ | Qualification | Gender | Points | Basic | DA | HRA | Bonus |
+-----+---------+--------+------+---------------+--------+--------+-------+------+------+-------+
| 1 | Deepali | 101 | NULL | MCA |F | 20 | 6000 | 2000 | 2300 | 200 |
| 3 | Hari | 102 | NULL | B.A |M | 15 | 1000 | 300 | 400 | 40 |
| 4 | Harry | 102 | NULL | M.A |M | 40 | 1500 | 390 | 490 | 30 |
+-----+---------+--------+------+---------------+--------+--------+-------+------+------+-------+
3 rows in set (0.00 sec)
2. Consider the following table named “GYM” with details about Fitness
products being sold in the
store.
+-------------------+
| Prname |
+-------------------+
| Cross Trainer |
33
| TreadMill |
| Massage Chair |
| Vibration Trainer |
| Bike |
+-------------------+
5 rows in set (0.00 sec)
b) Display the names and unit price of all the products in the store.
mysql> SELECT Prname, UnitPrice FROM GYM;
+-------------------+-----------+
| Prname | UnitPrice |
+-------------------+-----------+
| Cross Trainer | 25000.00 |
| TreadMill | 32000.00 |
| Massage Chair | 20000.00 |
| Vibration Trainer | 22000.00 |
| Bike | 13000.00 |
+-------------------+-----------+
5 rows in set (0.00 sec)
c) Display the names of all the products with unit price less than Rs.20000.00
d) Display details of all the products with unit price in the range 20000 to
30000
mysql> SELECT * FROM GYM WHERE UnitPrice BETWEEN 20000 AND 30000;
+--------+-------------------+-----------+--------------+
| Prcode | Prname | UnitPrice | Manufacturer |
+--------+-------------------+-----------+--------------+
| P101 | Cross Trainer | 25000.00 | Avon Fitness |
| P103 | Massage Chair | 20000.00 | Fit Express |
| P104 | Vibration Trainer | 22000.00 | Avon Fitness |
+--------+-------------------+-----------+--------------+
3 rows in set (0.03 sec)
e) Display names of all products by the manufacturer "Fit Express"
g) Add a new row for product with the details: "P106","Vibro Exerciser",
23000, manufacturer : "Avon Fitness".
i) Display details of all products with manufacturer name starting with "A"
+--------+-------------------+-----------+--------------+
| Prcode | Prname | UnitPrice | Manufacturer |
+--------+-------------------+-----------+--------------+
| P101 | Cross Trainer | 22500.00 | Avon Fitness |
| P102 | TreadMill | 28800.00 | AG Fitline |
| P104 | Vibration Trainer | 19800.00 | Avon Fitness |
| P106 | Vibro Exerciser | 20700.00 | Avon Fitness |
+--------+-------------------+-----------+--------------+
4 rows in set (0.00 sec)
j) Change the unitprice of Bike to 15000.
-> );
Query OK, 0 rows affected (0.09 sec)
mysql>
mysql> -- Insert records
mysql> INSERT INTO Loan_Accounts (AccNo, Cust_Name, Loan_Amount, Instalments, Int_Rate,
Start_Date, Interest) VALUES
-> (1, 'R.K. Gupta', 300000, 36, 12.00, '2009-07-19', NULL),
-> (2, 'S.P. Sharma', 500000, 48, 10.00, '2008-03-22', NULL),
-> (3, 'K.P. Jain', 300000, 36, NULL, '2007-03-08', NULL),
37
-> (4, 'M.P. Yadav', 800000, 60, 10.00, '2008-12-06', NULL),
-> (5, 'S.P. Sinha', 200000, 36, 12.50, '2010-01-03', NULL),
-> (6, 'P. Sharma', 700000, 60, 12.50, '2008-06-05', NULL),
-> (7, 'K.S. Dhall', 500000, 48, NULL, '2008-03-05', NULL);
Query OK, 7 rows affected (0.08 sec)
Records: 7 Duplicates: 0 Warnings: 0
mysql>
mysql> -- Queries (Q4 - Q35)
mysql> SELECT * FROM Loan_Accounts; -- Q4
+-------+-------------+-------------+-------------+----------+------------+----------+----------+
| AccNo | Cust_Name | Loan_Amount | Instalments | Int_Rate | Start_Date | Interest | Category |
+-------+-------------+-------------+-------------+----------+------------+----------+----------+
| 1 | R.K. Gupta | 300000 | 36 | 12.00 | 2009-07-19 | NULL | NULL |
| 2 | S.P. Sharma | 500000 | 48 | 10.00 | 2008-03-22 | NULL | NULL |
| 3 | K.P. Jain | 300000 | 36 | NULL | 2007-03-08 | NULL | NULL |
| 4 | M.P. Yadav | 800000 | 60 | 10.00 | 2008-12-06 | NULL | NULL |
| 5 | S.P. Sinha | 200000 | 36 | 12.50 | 2010-01-03 | NULL | NULL |
| 6 | P. Sharma | 700000 | 60 | 12.50 | 2008-06-05 | NULL | NULL |
| 7 | K.S. Dhall | 500000 | 48 | NULL | 2008-03-05 | NULL | NULL |
+-------+-------------+-------------+-------------+----------+------------+----------+----------+
7 rows in set (0.00 sec)
6. Display the details of all the loans with less than 40 instalments.
mysql>
mysql> SELECT * FROM Loan_Accounts WHERE Instalments < 40; -- Q6
38
+-------+------------+-------------+-------------+----------+------------+----------+----------+
| AccNo | Cust_Name | Loan_Amount | Instalments | Int_Rate | Start_Date | Interest | Category |
+-------+------------+-------------+-------------+----------+------------+----------+----------+
| 1 | R.K. Gupta | 300000 | 36 | 12.00 | 2009-07-19 | NULL | NULL |
| 3 | K.P. Jain | 300000 | 36 | NULL | 2007-03-08 | NULL | NULL |
| 5 | S.P. Sinha | 200000 | 36 | 12.50 | 2010-01-03 | NULL | NULL |
+-------+------------+-------------+-------------+----------+------------+----------+----------+
3 rows in set (0.00 sec)
7. Display the AccNo and Loan_Amount of all the loans started before 01-04-2009.
mysql> SELECT AccNo, Loan_Amount FROM Loan_Accounts WHERE Start_Date < '2009-04-01';
-- Q7
+-------+-------------+
| AccNo | Loan_Amount |
+-------+-------------+
| 2 | 500000 |
| 3 | 300000 |
| 4 | 800000 |
| 6 | 700000 |
| 7 | 500000 |
+-------+-------------+
5 rows in set (0.00 sec)
9. Display the details of all the loans whose rate of interest is NULL
mysql>
mysql> SELECT * FROM Loan_Accounts WHERE Int_Rate IS NULL; -- Q9
+-------+------------+-------------+-------------+----------+------------+----------+----------+
| AccNo | Cust_Name | Loan_Amount | Instalments | Int_Rate | Start_Date | Interest | Category |
+-------+------------+-------------+-------------+----------+------------+----------+----------+
| 3 | K.P. Jain | 300000 | 36 | NULL | 2007-03-08 | NULL | NULL |
| 7 | K.S. Dhall | 500000 | 48 | NULL | 2008-03-05 | NULL | NULL |
+-------+------------+-------------+-------------+----------+------------+----------+----------+
2 rows in set (0.00 sec)
39
10. Display the details of all the loans whose rate of interest is not NULL.
11. Display the amounts of various loans from the table Loan_Accounts. A loan amount
should appear only once.
mysql>
mysql> SELECT DISTINCT Loan_Amount FROM Loan_Accounts; -- Q11
+-------------+
| Loan_Amount |
+-------------+
| 300000 |
| 500000 |
| 800000 |
| 200000 |
| 700000 |
+-------------+
5 rows in set (0.00 sec)
12. Display the number of instalments of various loans from the table Loan_Accounts. An
instalment should appear only once.
13. Display the details of all the loans started after 31-12-2008 for which the number of
instalments aremore than 36.
mysql>
mysql> SELECT * FROM Loan_Accounts WHERE Start_Date > '2008-12-31' AND Instalments >
36; -- Q13
Empty set (0.00 sec)
40
14. Display the Cust_Name and Loan_Amount for all the loans which do not have number
of instalments36.
mysql> SELECT Cust_Name, Loan_Amount FROM Loan_Accounts WHERE Instalments <> 36; --
Q14
+-------------+-------------+
| Cust_Name | Loan_Amount |
+-------------+-------------+
| S.P. Sharma | 500000 |
| M.P. Yadav | 800000 |
| P. Sharma | 700000 |
| K.S. Dhall | 500000 |
+-------------+-------------+
4 rows in set (0.00 sec)
15. Display the Cust_Name and Loan_Amount for all the loans for which the loan amount
is less than500000 or int_rate is more than 12.
16. Display the details of all the loans which started in the year 2009.
17. Display the details of all the loans whose Loan_Amount is in the range 400000 to
500000.
18. Display the details of all the loans whose rate of interest is in the range 11% to 12%.
mysql> SELECT * FROM Loan_Accounts WHERE Int_Rate BETWEEN 11 AND 12; -- Q18
+-------+------------+-------------+-------------+----------+------------+----------+----------+
| AccNo | Cust_Name | Loan_Amount | Instalments | Int_Rate | Start_Date | Interest | Category |
+-------+------------+-------------+-------------+----------+------------+----------+----------+
| 1 | R.K. Gupta | 300000 | 36 | 12.00 | 2009-07-19 | NULL | NULL |
+-------+------------+-------------+-------------+----------+------------+----------+----------+
1 row in set (0.00 sec)
19. Display the Cust_Name and Loan_Amount for all the loans for which the number of
instalments are24, 36, or 48.
20. Display the details of all the loans whose Loan_Amount is in the range 400000 to
500000.
mysql>
mysql> SELECT * FROM Loan_Accounts WHERE Loan_Amount BETWEEN 400000 AND
500000; -- Q20
+-------+-------------+-------------+-------------+----------+------------+----------+----------+
| AccNo | Cust_Name | Loan_Amount | Instalments | Int_Rate | Start_Date | Interest | Category |
+-------+-------------+-------------+-------------+----------+------------+----------+----------+
| 2 | S.P. Sharma | 500000 | 48 | 10.00 | 2008-03-22 | NULL | NULL |
| 7 | K.S. Dhall | 500000 | 48 | NULL | 2008-03-05 | NULL | NULL |
+-------+-------------+-------------+-------------+----------+------------+----------+----------+
2 rows in set (0.00 sec)
42
21. Display the details of all the loans whose rate of interest is in the range 11% to 12%.
mysql> SELECT * FROM Loan_Accounts WHERE Int_Rate BETWEEN 11 AND 12; -- Q21
+-------+------------+-------------+-------------+----------+------------+----------+----------+
| AccNo | Cust_Name | Loan_Amount | Instalments | Int_Rate | Start_Date | Interest | Category |
+-------+------------+-------------+-------------+----------+------------+----------+----------+
| 1 | R.K. Gupta | 300000 | 36 | 12.00 | 2009-07-19 | NULL | NULL |
+-------+------------+-------------+-------------+----------+------------+----------+----------+
1 row in set (0.00 sec)
22. Display the AccNo, Cust_Name, and Loan_Amount for all the loans for which the
Cust_Name ends with Sharma
mysql>
mysql> SELECT AccNo, Cust_Name, Loan_Amount FROM Loan_Accounts WHERE Cust_Name
LIKE '%Sharma'; -- Q22
+-------+-------------+-------------+
| AccNo | Cust_Name | Loan_Amount |
+-------+-------------+-------------+
| 2 | S.P. Sharma | 500000 |
| 6 | P. Sharma | 700000 |
+-------+-------------+-------------+
2 rows in set (0.00 sec)
23. Display the AccNo, Cust_Name, and Loan_Amount for all the loans for which the
Cust_Name ends with a.
24. Display the AccNo, Cust_Name, and Loan_Amount for all the loans for which the
Cust_Name contains 'a'
25. Display the AccNo, Cust_Name, and Loan_Amount for all the loans for which the
Cust_Name does not contain 'P'.
26. Display the AccNo, Cust_Name, and Loan_Amount for all the loans for which the
Cust_Name contains 'a' as the second last character.
27. Display the details of all the loans in the ascending order of their Loan_Amount.
mysql>
mysql> SELECT * FROM Loan_Accounts ORDER BY Loan_Amount ASC; -- Q27
44
+-------+-------------+-------------+-------------+----------+------------+----------+----------+
| AccNo | Cust_Name | Loan_Amount | Instalments | Int_Rate | Start_Date | Interest | Category |
+-------+-------------+-------------+-------------+----------+------------+----------+----------+
| 5 | S.P. Sinha | 200000 | 36 | 12.50 | 2010-01-03 | NULL | NULL |
| 1 | R.K. Gupta | 300000 | 36 | 12.00 | 2009-07-19 | NULL | NULL |
| 3 | K.P. Jain | 300000 | 36 | NULL | 2007-03-08 | NULL | NULL |
| 2 | S.P. Sharma | 500000 | 48 | 10.00 | 2008-03-22 | NULL | NULL |
| 7 | K.S. Dhall | 500000 | 48 | NULL | 2008-03-05 | NULL | NULL |
| 6 | P. Sharma | 700000 | 60 | 12.50 | 2008-06-05 | NULL | NULL |
| 4 | M.P. Yadav | 800000 | 60 | 10.00 | 2008-12-06 | NULL | NULL |
+-------+-------------+-------------+-------------+----------+------------+----------+----------+
7 rows in set (0.00 sec)
28. Display the details of all the loans in the descending order of their Start_Date.
29. 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.
mysql>
mysql> UPDATE Loan_Accounts SET Int_Rate = 11.50 WHERE Int_Rate IS NULL; -- Q30
Query OK, 2 rows affected (0.09 sec)
Rows matched: 2 Changed: 2 Warnings: 0
31. Increase the interest rate by 0.5% for all the loans for which the loan amount is more
than 400000.
mysql> UPDATE Loan_Accounts SET Int_Rate = Int_Rate + 0.5 WHERE Loan_Amount > 400000;
-- Q31
Query OK, 4 rows affected (0.11 sec)
Rows matched: 4 Changed: 4 Warnings: 0
33. Delete the records of all the loans whose start date is before 2007.
35. Add another column Category of type CHAR(1) in the Loan table.
mysql>
Database changed
-> );
mysql>
mysql> INSERT INTO Course (C_ID, C_Name, Duration_Months, Fees, Mode, Student_Name) VALUES
+-------------------------+
| C_Name |
+-------------------------+
| Artificial Intelligence |
| Python Programming |
| CCNA |
| Cyber Security |
| Cloud Computing |
+-------------------------+
+------+----------------+-----------------+--------+---------+--------------+
+------+----------------+-----------------+--------+---------+--------------+
+------+----------------+-----------------+--------+---------+--------------+
iii) Display the Course Name and Fees where duration is 6 months
mysql>
+--------------------+-------+
| C_Name | Fees |
+--------------------+-------+
+--------------------+-------+