Location via proxy:   [ UP ]  
[Report a bug]   [Manage cookies]                
Download as pdf or txt
Download as pdf or txt
You are on page 1of 38

KENDRIYA VIDYALAYA

PAINAVU, IDUKKI

CLASS XII – TERM 2


LAB RECORD

COMPUTER SCIENCE (083)


2021-22

1
Index

SNo Program Name Page No Date Sign of Teacher


1 Stack using a list data-structure - I 22/01/2022

2 Stack using a list data-structure - II 22/01/2022

3 Stack using a list data-structure - III 22/01/2022

4 DBMS 1: Create & Populate Table 22/01/2022

5 DBMS 2: Alter Table 22/01/2022

6 DBMS 3: ORDER BY Clause 22/01/2022

7 DBMS 4: Inserting Records 22/01/2022

8 DBMS 5: DELETE to remove tuple(s) 22/01/2022

9 DBMS 6: GROUP BY CLAUSE 22/01/2022

10 DBMS 7: JOINING OF 2 TABLES 22/01/2022

11 DBMS 8: INTEGRATE SQL with Python - I 22/01/2022

12 DBMS 9: INTEGRATE SQL with Python - II 22/01/2022

2
Expt No:1 Date: 22/01/2022

Stack using a list data-structure-I


Aim:

Write a Python program to implement a stack using a list data-structure.

Program:

def isempty(stk):
if stk==[]:
return True
else:
return False

def push(stk,item):
stk.append(item)
top=len(stk)-1

def peek(stk):
if isempty(stk):
return "UnderFlow"
else:
top=len(stk)-1
return stk[top]

def pop(stk):
if isempty(stk):
return "Underflow"
else:
item=stk.pop()
if len(stk)==0:
top=None
else:
top=len(stk)-1
return item

3
def Display(stk):
if isempty(stk):
print("Underflow! Stack is empty")
else:
top=len(stk)-1
print(stk[top],"<-- top")
for a in range(top-1,-1,-1):
print(stk[a])

Stack=[]
top=None
while True:
print("Stack Operations")
print("1. Push")
print("2. Pop")
print("3. Display")
print("4. Peek")
print("5. Exit")
ch=int(input("Enter your choice(1-5): "))
if ch==1:
item=input("enter the item to pushed: ")
push(Stack,item)
print("Satck after Push",Stack)
elif ch==2:
item=pop(Stack)
if item=="Underflow":
print("Underflow! Stack is empty")
else:
print("Popped item is",item)
print("Stack after Pop",Stack)
elif ch==3:
Display(Stack)
elif ch==4:
print(peek(Stack))
elif ch==5:
break

else:
print("Wrong Choice")

4
Output:

Stack Operations
1. Push
2. Pop
3. Display
4. Peek
5. Exit

Enter your choice(1-5): 1

enter the item to pushed: 23


Satck after Push ['23']
Stack Operations
1. Push
2. Pop
3. Display
4. Peek
5. Exit

Enter your choice(1-5): 1

enter the item to pushed: 56


Satck after Push ['23', '56']
Stack Operations
1. Push
2. Pop
3. Display
4. Peek
5. Exit

Enter your choice(1-5): 1

enter the item to pushed: 87


Satck after Push ['23', '56', '87']
Stack Operations
1. Push
2. Pop
3. Display
4. Peek
5. Exit

Enter your choice(1-5): 1

enter the item to pushed: 65

5
Satck after Push ['23', '56', '87', '65']
Stack Operations
1. Push
2. Pop
3. Display
4. Peek
5. Exit

Enter your choice(1-5): 1

enter the item to pushed: 98


Satck after Push ['23', '56', '87', '65', '98']
Stack Operations
1. Push
2. Pop
3. Display
4. Peek
5. Exit

Enter your choice(1-5): 3


98 <-- top
65
87
56
23

Enter your choice(1-5): 2


Popped item is 98
Stack after Pop ['23', '56', '87', '65']
Stack Operations
1. Push
2. Pop
3. Display
4. Peek
5. Exit

Enter your choice(1-5): 2


Popped item is 65
Stack after Pop ['23', '56', '87']
Stack Operations
1. Push
2. Pop
3. Display
4. Peek
5. Exit

6
Enter your choice(1-5): 3
87 <-- top
56
23
Stack Operations
1. Push
2. Pop
3. Display
4. Peek
5. Exit

Enter your choice(1-5): 4


87
Stack Operations
1. Push
2. Pop
3. Display
4. Peek
5. Exit

Enter your choice(1-5): 3


87 <-- top
56
23
Stack Operations
1. Push
2. Pop
3. Display
4. Peek
5. Exit

Result:

Program executed successfully.

7
Expt No:2 Date: 22/01/2022

Stack using a list data-structure - II


Aim:

Julie has created a dictionary containing names and marks as key value pairs of 6 students. Write a
program, with separate user defined functions to perform the following operations:

 Push the keys (name of the student) of the dictionary into a stack, where the corresponding value
(marks) is greater than 75.
 Pop and display the content of the stack.

For example: If the sample content of the dictionary is as follows:


R={"OM":76, "JAI":45, "BOB":89, "ALI":65, "ANU":90, "TOM":82}
The output from the program should be:
TOM ANU BOB OM

Program:

R={"OM":76, "JAI":45, "BOB":89, "ALI":65, "ANU":90, "TOM":82}


stk=[]
def spush(stk,inp):
for k in inp:
if inp[k]>75:
stk.append(k)

def display(stk):
print("current stack contents")
for i in range(-1,-len(stk)-1,-1):
if i==-1:
print(stk[i],"<---top",sep="")
else:
print(stk[i])
print()

def spop(stk):
while len(stk)!=0:
item=stk.pop()
print("popped item is",item)
display(stk)
else:
print("stack empty")

spush(stk,R)
display(stk)
spop(stk)

8
Output:

current stack contents

TOM<---top

ANU

BOB

OM

popped item is TOM

current stack contents

ANU<---top

BOB

OM

popped item is ANU

current stack contents

BOB<---top

OM

popped item is BOB

current stack contents

OM<---top

popped item is OM

current stack contents

stack empty

Result: Program executed successfully.

9
Expt No:3 Date: 22/01/2022

Stack using a list data-structure - III


Aim:

Alam has a list containing 10 integers. You need to help him create a program with separate user
defined functions to perform the following operations based on this list.

 Traverse the content of the list and push the even numbers into a stack.
 Pop and display the content of the stack.

For Example:

If the sample Content of the list is as follows: N = [12, 13, 34, 56, 21, 79, 98, 22, 35, 38]

Sample Output of the code should be: 38 22 98 56 34 12

Program:

N = [12, 13, 34, 56, 21, 79, 98, 22, 35, 38]
stk=[]
def spush(stk,inp):
for k in inp:
if k%2==0:
stk.append(k)

def display(stk):
print("current stack contents")
for i in range(-1,-len(stk)-1,-1):
if i==-1:
print(stk[i],"<---top",sep="")
else:
print(stk[i])
print()

def spop(stk):
while len(stk)!=0:
item=stk.pop()
print("popped item is",item)
display(stk)
else:
print("stack empty")

spush(stk,N)
display(stk)
spop(stk)

10
Output:

current stack contents


38<---top
22
98
56
34
12

popped item is 38
current stack contents
22<---top
98
56
34
12

popped item is 22
current stack contents
98<---top
56
34
12

popped item is 98
current stack contents
56<---top
34
12

popped item is 56
current stack contents
34<---top
12

popped item is 34
current stack contents
12<---top

popped item is 12
current stack contents
stack empty

Result: Program executed successfully.


11
Expt No:4 Date: 22/01/2022

DBMS 1: Create & Populate Table


Aim:

Create a student table with following details and populate the table with data given below.

Table Description: student


+--------+----------+------+-----+---------+-------+
| Field | Type | Null | Key | Default | Extra |
+--------+----------+------+-----+---------+-------+
| adno | int | YES | | NULL | |
| sname | char(20) | YES | | NULL | |
| gender | char(1) | YES | | NULL | |
| class | char(5) | YES | | NULL | |
| mark | int | YES | | NULL | |
| height | int | YES | | NULL | |
+--------+----------+------+-----+---------+-------+

Table Contents: student


+------+----------+--------+-------+------+--------+
| adno | sname | gender | class | mark | height |
+------+----------+--------+-------+------+--------+
| 1001 | abhishek | M | XII | 95 | 170 |
| 1002 | alen | M | XI | 90 | 172 |
| 1003 | althaf | M | X | 91 | 168 |
| 1004 | amita | F | XII | 92 | 165 |
| 1005 | devanand | M | XI | 85 | 160 |
| 1006 | margret | F | IX | 96 | 163 |
| 1007 | nayana | F | XI | 95 | 165 |
| 1008 | sreehari | M | XII | 90 | 160 |
+------+----------+--------+-------+------+--------+

Program:

mysql> use kvidk;


Database changed

mysql> CREATE TABLE STUDENT(adno int,sname char(20), gender


-> char(1),class char(5),mark int,height int);
Query OK, 0 rows affected (1.11 sec)

12
mysql> desc student;
+--------+----------+------+-----+---------+-------+
| Field | Type | Null | Key | Default | Extra |
+--------+----------+------+-----+---------+-------+
| adno | int | YES | | NULL | |
| sname | char(20) | YES | | NULL | |
| gender | char(1) | YES | | NULL | |
| class | char(5) | YES | | NULL | |
| mark | int | YES | | NULL | |
| height | int | YES | | NULL | |
+--------+----------+------+-----+---------+-------+
6 rows in set (0.05 sec)

mysql> insert into student values(1001,"abhishek","M","XII",95,170);


Query OK, 1 row affected (0.07 sec)

mysql> insert into student values(1002,"alen","M","XI",90,172);


Query OK, 1 row affected (0.16 sec)

mysql> insert into student values(1003,"althaf","M","X",91,168);


Query OK, 1 row affected (0.08 sec)

mysql> insert into student values(1004,"amita","F","XII",92,165);


Query OK, 1 row affected (0.10 sec)

mysql> insert into student values(1005,"devanand","M","XI",85,160);


Query OK, 1 row affected (0.06 sec)

mysql> insert into student values(1006,"margret","F","IX",96,163);


Query OK, 1 row affected (0.12 sec)

mysql> insert into student values(1007,"nayana","F","XI",95,165);


Query OK, 1 row affected (0.08 sec)

mysql> insert into student values(1008,"sreehari","M","XII",90,160);


Query OK, 1 row affected (0.11 sec)

13
mysql> select * from student;
+------+----------+--------+-------+------+--------+
| adno | sname | gender | class | mark | height |
+------+----------+--------+-------+------+--------+
| 1001 | abhishek | M | XII | 95 | 170 |
| 1002 | alen | M | XI | 90 | 172 |
| 1003 | althaf | M | X | 91 | 168 |
| 1004 | amita | F | XII | 92 | 165 |
| 1005 | devanand | M | XI | 85 | 160 |
| 1006 | margret | F | IX | 96 | 163 |
| 1007 | nayana | F | XI | 95 | 165 |
| 1008 | sreehari | M | XII | 90 | 160 |
+------+----------+--------+-------+------+--------+
8 rows in set (0.00 sec)

14
Expt No:5 Date: 22/01/2022

DBMS 2: Alter Table


Aim:

In student table given below, do the following operations.


1) Alter student table to add a new field with name DOB.
2) Modify the data type of attribute sname to VARCHAR(20).
3) Remove the attribute height.
4) To add primary key constraint to adno field.

Table Description: student


+--------+----------+------+-----+---------+-------+
| Field | Type | Null | Key | Default | Extra |
+--------+----------+------+-----+---------+-------+
| adno | int | YES | | NULL | |
| sname | char(20) | YES | | NULL | |
| gender | char(1) | YES | | NULL | |
| class | char(5) | YES | | NULL | |
| mark | int | YES | | NULL | |
| height | int | YES | | NULL | |
+--------+----------+------+-----+---------+-------+

Table Contents: student


+------+----------+--------+-------+------+--------+
| adno | sname | gender | class | mark | height |
+------+----------+--------+-------+------+--------+
| 1001 | abhishek | M | XII | 95 | 170 |
| 1002 | alen | M | XI | 90 | 172 |
| 1003 | althaf | M | X | 91 | 168 |
| 1004 | amita | F | XII | 92 | 165 |
| 1005 | devanand | M | XI | 85 | 160 |
| 1006 | margret | F | IX | 96 | 163 |
| 1007 | nayana | F | XI | 95 | 165 |
| 1008 | sreehari | M | XII | 90 | 160 |
+------+----------+--------+-------+------+--------+

Program:

mysql> use kvidk;


Database changed

mysql> ALTER TABLE STUDENT ADD DOB DATE;


Query OK, 0 rows affected (1.31 sec)
Records: 0 Duplicates: 0 Warnings: 0

15
mysql> SELECT * FROM STUDENT;
+------+----------+--------+-------+------+--------+------+
| adno | sname | gender | class | mark | height | DOB |
+------+----------+--------+-------+------+--------+------+
| 1001 | abhishek | M | XII | 95 | 170 | NULL |
| 1002 | alen | M | XI | 90 | 172 | NULL |
| 1003 | althaf | M | X | 91 | 168 | NULL |
| 1004 | amita | F | XII | 92 | 165 | NULL |
| 1005 | devanand | M | XI | 85 | 160 | NULL |
| 1006 | margret | F | IX | 96 | 163 | NULL |
| 1007 | nayana | F | XI | 95 | 165 | NULL |
| 1008 | sreehari | M | XII | 90 | 160 | NULL |
+------+----------+--------+-------+------+--------+------+
8 rows in set (0.01 sec)

mysql> UPDATE STUDENT SET DOB="2003-01-01";


Query OK, 8 rows affected (0.06 sec)
Rows matched: 8 Changed: 8 Warnings: 0

mysql> SELECT * FROM STUDENT;


+------+----------+--------+-------+------+--------+------------+
| adno | sname | gender | class | mark | height | DOB |
+------+----------+--------+-------+------+--------+------------+
| 1001 | abhishek | M | XII | 95 | 170 | 2003-01-01 |
| 1002 | alen | M | XI | 90 | 172 | 2003-01-01 |
| 1003 | althaf | M | X | 91 | 168 | 2003-01-01 |
| 1004 | amita | F | XII | 92 | 165 | 2003-01-01 |
| 1005 | devanand | M | XI | 85 | 160 | 2003-01-01 |
| 1006 | margret | F | IX | 96 | 163 | 2003-01-01 |
| 1007 | nayana | F | XI | 95 | 165 | 2003-01-01 |
| 1008 | sreehari | M | XII | 90 | 160 | 2003-01-01 |
+------+----------+--------+-------+------+--------+------------+
8 rows in set (0.00 sec)

16
mysql> DESC STUDENT;
+--------+----------+------+-----+---------+-------+
| Field | Type | Null | Key | Default | Extra |
+--------+----------+------+-----+---------+-------+
| adno | int | YES | | NULL | |
| sname | char(20) | YES | | NULL | |
| gender | char(1) | YES | | NULL | |
| class | char(5) | YES | | NULL | |
| mark | int | YES | | NULL | |
| height | int | YES | | NULL | |
| DOB | date | YES | | NULL | |
+--------+----------+------+-----+---------+-------+
7 rows in set (0.02 sec)

mysql> ALTER TABLE STUDENT MODIFY sname varchar(20);


Query OK, 8 rows affected (1.07 sec)
Records: 8 Duplicates: 0 Warnings: 0

mysql> DESC STUDENT;


+--------+-------------+------+-----+---------+-------+
| Field | Type | Null | Key | Default | Extra |
+--------+-------------+------+-----+---------+-------+
| adno | int | YES | | NULL | |
| sname | varchar(20) | YES | | NULL | |
| gender | char(1) | YES | | NULL | |
| class | char(5) | YES | | NULL | |
| mark | int | YES | | NULL | |
| height | int | YES | | NULL | |
| DOB | date | YES | | NULL | |
+--------+-------------+------+-----+---------+-------+
7 rows in set (0.00 sec)

17
mysql> SELECT * FROM STUDENT;
+------+----------+--------+-------+------+--------+------------+
| adno | sname | gender | class | mark | height | DOB |
+------+----------+--------+-------+------+--------+------------+
| 1001 | abhishek | M | XII | 95 | 170 | 2003-01-01 |
| 1002 | alen | M | XI | 90 | 172 | 2003-01-01 |
| 1003 | althaf | M | X | 91 | 168 | 2003-01-01 |
| 1004 | amita | F | XII | 92 | 165 | 2003-01-01 |
| 1005 | devanand | M | XI | 85 | 160 | 2003-01-01 |
| 1006 | margret | F | IX | 96 | 163 | 2003-01-01 |
| 1007 | nayana | F | XI | 95 | 165 | 2003-01-01 |
| 1008 | sreehari | M | XII | 90 | 160 | 2003-01-01 |
+------+----------+--------+-------+------+--------+------------+
8 rows in set (0.00 sec)

mysql> ALTER TABLE STUDENT DROP height;


Query OK, 0 rows affected (1.63 sec)
Records: 0 Duplicates: 0 Warnings: 0

mysql> SELECT * FROM STUDENT;


+------+----------+--------+-------+------+------------+
| adno | sname | gender | class | mark | DOB |
+------+----------+--------+-------+------+------------+
| 1001 | abhishek | M | XII | 95 | 2003-01-01 |
| 1002 | alen | M | XI | 90 | 2003-01-01 |
| 1003 | althaf | M | X | 91 | 2003-01-01 |
| 1004 | amita | F | XII | 92 | 2003-01-01 |
| 1005 | devanand | M | XI | 85 | 2003-01-01 |
| 1006 | margret | F | IX | 96 | 2003-01-01 |
| 1007 | nayana | F | XI | 95 | 2003-01-01 |
| 1008 | sreehari | M | XII | 90 | 2003-01-01 |
+------+----------+--------+-------+------+------------+
8 rows in set (0.01 sec)

18
mysql> DESC STUDENT;
+--------+-------------+------+-----+---------+-------+
| Field | Type | Null | Key | Default | Extra |
+--------+-------------+------+-----+---------+-------+
| adno | int | YES | | NULL | |
| sname | varchar(20) | YES | | NULL | |
| gender | char(1) | YES | | NULL | |
| class | char(5) | YES | | NULL | |
| mark | int | YES | | NULL | |
| DOB | date | YES | | NULL | |
+--------+-------------+------+-----+---------+-------+
6 rows in set (0.00 sec)

mysql> ALTER TABLE STUDENT ADD PRIMARY KEY(adno);


Query OK, 0 rows affected (1.41 sec)
Records: 0 Duplicates: 0 Warnings: 0

mysql> desc student;


+--------+-------------+------+-----+---------+-------+
| Field | Type | Null | Key | Default | Extra |
+--------+-------------+------+-----+---------+-------+
| adno | int | NO | PRI | NULL | |
| sname | varchar(20) | YES | | NULL | |
| gender | char(1) | YES | | NULL | |
| class | char(5) | YES | | NULL | |
| mark | int | YES | | NULL | |
| DOB | date | YES | | NULL | |
+--------+-------------+------+-----+---------+-------+
6 rows in set (0.00 sec)

19
Expt No:6 Date: 22/01/2022

DBMS 3: ORDER BY Clause


Aim:

Write an SQL query to display the student records based on their names in in ascending / descending
order.

Table Contents: student


+------+----------+--------+-------+------+------------+
| adno | sname | gender | class | mark | DOB |
+------+----------+--------+-------+------+------------+
| 1001 | abhishek | M | XII | 95 | 2003-01-01 |
| 1002 | alen | M | XI | 90 | 2003-01-01 |
| 1003 | althaf | M | X | 91 | 2003-01-01 |
| 1004 | amita | F | XII | 92 | 2003-01-01 |
| 1005 | devanand | M | XI | 85 | 2003-01-01 |
| 1006 | margret | F | IX | 96 | 2003-01-01 |
| 1007 | nayana | F | XI | 95 | 2003-01-01 |
| 1008 | sreehari | M | XII | 90 | 2003-01-01 |
+------+----------+--------+-------+------+------------+

Program:

mysql> use kvidk;


Database changed

mysql> SELECT * FROM STUDENT ORDER BY SNAME ASC;


+------+----------+--------+-------+------+------------+
| adno | sname | gender | class | mark | DOB |
+------+----------+--------+-------+------+------------+
| 1001 | abhishek | M | XII | 95 | 2003-01-01 |
| 1002 | alen | M | XI | 90 | 2003-01-01 |
| 1003 | althaf | M | X | 91 | 2003-01-01 |
| 1004 | amita | F | XII | 92 | 2003-01-01 |
| 1005 | devanand | M | XI | 85 | 2003-01-01 |
| 1006 | margret | F | IX | 96 | 2003-01-01 |
| 1007 | nayana | F | XI | 95 | 2003-01-01 |
| 1008 | sreehari | M | XII | 90 | 2003-01-01 |
+------+----------+--------+-------+------+------------+
8 rows in set (0.01 sec)

20
mysql> SELECT * FROM STUDENT ORDER BY SNAME DESC;
+------+----------+--------+-------+------+------------+
| adno | sname | gender | class | mark | DOB |
+------+----------+--------+-------+------+------------+
| 1008 | sreehari | M | XII | 90 | 2003-01-01 |
| 1007 | nayana | F | XI | 95 | 2003-01-01 |
| 1006 | margret | F | IX | 96 | 2003-01-01 |
| 1005 | devanand | M | XI | 85 | 2003-01-01 |
| 1004 | amita | F | XII | 92 | 2003-01-01 |
| 1003 | althaf | M | X | 91 | 2003-01-01 |
| 1002 | alen | M | XI | 90 | 2003-01-01 |
| 1001 | abhishek | M | XII | 95 | 2003-01-01 |
+------+----------+--------+-------+------+------------+
8 rows in set (0.00 sec)

21
Expt No:7 Date: 22/01/2022

DBMS 4: Inserting Records


Aim:

Write an SQL query to insert the following records into the student table.

+------+----------+--------+-------+------+------------+
| adno | sname | gender | class | mark | DOB |
+------+----------+--------+-------+------+------------+
| 1009 | cebil | M | XI | 99 | 2003-09-12 |
| 1010 | helen | F | IX | 98 | 2002-10-21 |
+------+----------+--------+-------+------+------------+

Program:

mysql> use kvidk;


Database changed

mysql> insert into student values(1009,"cebil","M","XI",99,


-> "2003-09-12");
Query OK, 1 row affected (0.04 sec)

mysql> insert into student values(1010,"helen","F","IX",98,


-> "2002-10-21");
Query OK, 1 row affected (0.04 sec)

mysql> SELECT * FROM STUDENT;


+------+----------+--------+-------+------+------------+
| adno | sname | gender | class | mark | DOB |
+------+----------+--------+-------+------+------------+
| 1001 | abhishek | M | XII | 95 | 2003-01-01 |
| 1002 | alen | M | XI | 90 | 2003-01-01 |
| 1003 | althaf | M | X | 91 | 2003-01-01 |
| 1004 | amita | F | XII | 92 | 2003-01-01 |
| 1005 | devanand | M | XI | 85 | 2003-01-01 |
| 1006 | margret | F | IX | 96 | 2003-01-01 |
| 1007 | nayana | F | XI | 95 | 2003-01-01 |
| 1008 | sreehari | M | XII | 90 | 2003-01-01 |
| 1009 | cebil | M | XI | 99 | 2003-09-12 |
| 1010 | helen | F | IX | 98 | 2002-10-21 |
+------+----------+--------+-------+------+------------+
10 rows in set (0.00 sec)

22
Expt No:8 Date: 22/01/2022

DBMS 5: DELETE to remove tuple(s)


Aim:

Write an SQL query to delete the students whose mark is 98 or above from the student table given
below.
+------+----------+--------+-------+------+------------+
| adno | sname | gender | class | mark | DOB |
+------+----------+--------+-------+------+------------+
| 1001 | abhishek | M | XII | 95 | 2003-01-01 |
| 1002 | alen | M | XI | 90 | 2003-01-01 |
| 1003 | althaf | M | X | 91 | 2003-01-01 |
| 1004 | amita | F | XII | 92 | 2003-01-01 |
| 1005 | devanand | M | XI | 85 | 2003-01-01 |
| 1006 | margret | F | IX | 96 | 2003-01-01 |
| 1007 | nayana | F | XI | 95 | 2003-01-01 |
| 1008 | sreehari | M | XII | 90 | 2003-01-01 |
| 1009 | cebil | M | XI | 99 | 2003-09-12 |
| 1010 | helen | F | IX | 98 | 2002-10-21 |
+------+----------+--------+-------+------+------------+
Program:

mysql> use kvidk;


Database changed

mysql> DELETE FROM STUDENT WHERE MARK>=98;


Query OK, 2 rows affected (0.07 sec)

mysql> SELECT * FROM STUDENT;


+------+----------+--------+-------+------+------------+
| adno | sname | gender | class | mark | DOB |
+------+----------+--------+-------+------+------------+
| 1001 | abhishek | M | XII | 95 | 2003-01-01 |
| 1002 | alen | M | XI | 90 | 2003-01-01 |
| 1003 | althaf | M | X | 91 | 2003-01-01 |
| 1004 | amita | F | XII | 92 | 2003-01-01 |
| 1005 | devanand | M | XI | 85 | 2003-01-01 |
| 1006 | margret | F | IX | 96 | 2003-01-01 |
| 1007 | nayana | F | XI | 95 | 2003-01-01 |
| 1008 | sreehari | M | XII | 90 | 2003-01-01 |
+------+----------+--------+-------+------+------------+
8 rows in set (0.01 sec)

23
Expt No:9 Date: 22/01/2022

DBMS 6: GROUP BY CLAUSE


Aim:

Write an SQL query to find the following details from the student table given below.
 To find the highest scorer in each class.
 To find the low performer in each class.
 To find the total marks in each class.
 To find the no of students in each class.
 To find the average marks in each class.

+------+----------+--------+-------+------+------------+
| adno | sname | gender | class | mark | DOB |
+------+----------+--------+-------+------+------------+
| 1001 | abhishek | M | XII | 95 | 2003-01-01 |
| 1002 | alen | M | XI | 90 | 2003-01-01 |
| 1003 | althaf | M | X | 91 | 2003-01-01 |
| 1004 | amita | F | XII | 92 | 2003-01-01 |
| 1005 | devanand | M | XI | 85 | 2003-01-01 |
| 1006 | margret | F | IX | 96 | 2003-01-01 |
| 1007 | nayana | F | XI | 95 | 2003-01-01 |
| 1008 | sreehari | M | XII | 90 | 2003-01-01 |
| 1009 | cebil | M | XI | 99 | 2003-09-12 |
| 1010 | helen | F | IX | 98 | 2002-10-21 |
+------+----------+--------+-------+------+------------+

Program:

mysql> use kvidk;


Database changed

mysql> SELECT adno,sname,class,max(mark) as topmark,dob


-> FROM STUDENT GROUP BY class;
+------+----------+-------+---------+------------+
| adno | sname | class | topmark | dob |
+------+----------+-------+---------+------------+
| 1001 | abhishek | XII | 95 | 2003-01-01 |
| 1002 | alen | XI | 99 | 2003-01-01 |
| 1003 | althaf | X | 91 | 2003-01-01 |
| 1006 | margret | IX | 98 | 2003-01-01 |
+------+----------+-------+---------+------------+
4 rows in set (0.00 sec)

mysql> SELECT adno,sname,class,min(mark) as lowestmark,dob

24
-> FROM STUDENT GROUP BY class;
+------+----------+-------+------------+------------+
| adno | sname | class | lowestmark | dob |
+------+----------+-------+------------+------------+
| 1001 | abhishek | XII | 90 | 2003-01-01 |
| 1002 | alen | XI | 85 | 2003-01-01 |
| 1003 | althaf | X | 91 | 2003-01-01 |
| 1006 | margret | IX | 96 | 2003-01-01 |
+------+----------+-------+------------+------------+
4 rows in set (0.01 sec)

mysql> SELECT class,sum(mark) as totalmark


-> FROM student GROUP BY class;
+-------+-----------+
| class | totalmark |
+-------+-----------+
| XII | 277 |
| XI | 369 |
| X | 91 |
| IX | 194 |
+-------+-----------+
4 rows in set (0.00 sec)

mysql> SELECT class,count(*) as no_o_students


-> FROM student GROUP BY class;
+-------+---------------+
| class | no_o_students |
+-------+---------------+
| XII | 3 |
| XI | 4 |
| X | 1 |
| IX | 2 |
+-------+---------------+
4 rows in set (0.00 sec)

25
mysql> SELECT class,avg(mark) as class_average
-> FROM student GROUP BY class;
+-------+---------------+
| class | class_average |
+-------+---------------+
| XII | 92.3333 |
| XI | 92.2500 |
| X | 91.0000 |
| IX | 97.0000 |
+-------+---------------+
4 rows in set (0.00 sec)

26
Expt No:10 Date: 22/01/2022

DBMS 7: JOINING OF 2 TABLES


Aim:

Write an SQL query to find the following details from the student table given below.
a) To display eno, ename, salary and corresponding dept_name of all the employees.
b) To display eno, ename, salary and corresponding dept_name of all the employees whose age is between
25 and 35 (both values inclusive).
c) To display dept_name and corresponding ename from the tables DEPARTMENT and EMPLOYEE,
(Hint’ HOD of the DEPARTMENT table should be matched with eno of the EMPLOYEE table for
getting the desired result).
d) To display ename, salary, zone and income tax (Note income tax to be calculated as 30% of salary) of
all the employees with appropriate column headings.
Table Description: employee
+--------+-------------+------+-----+---------+-------+
| Field | Type | Null | Key | Default | Extra |
+--------+-------------+------+-----+---------+-------+
| eno | int | NO | PRI | NULL | |
| ename | varchar(20) | YES | | NULL | |
| salary | int | YES | | NULL | |
| zone | char(5) | YES | | NULL | |
| age | int | YES | | NULL | |
| grade | char(1) | YES | | NULL | |
| dept | int | YES | | NULL | |
+--------+-------------+------+-----+---------+-------+
Table Description: department
+-----------+----------+------+-----+---------+-------+
| Field | Type | Null | Key | Default | Extra |
+-----------+----------+------+-----+---------+-------+
| deptno | int | NO | PRI | NULL | |
| dept_name | char(15) | YES | | NULL | |
| hod | int | YES | MUL | NULL | |
+-----------+----------+------+-----+---------+-------+
*MUL- Multiple occurrences of a given value are permitted
within the column. (i.e. Foreign Key)- here hod field refers
primary key of employee table, i.e. enono;

Table: employee
+-----+--------+--------+-------+------+-------+------+
| eno | ename | salary | zone | age | grade | dept |
+-----+--------+--------+-------+------+-------+------+
| 1 | MONA | 70000 | EAST | 40 | A | 10 |
| 2 | MUKTAR | 71000 | WEST | 45 | B | 20 |
| 3 | NALINI | 60000 | EAST | 26 | A | 10 |
| 4 | SANAJ | 65000 | SOUTH | 36 | A | 20 |
| 5 | SURYA | 58000 | NORTH | 30 | B | 30 |
+-----+--------+--------+-------+------+-------+------+

27
Table : department
+--------+-------------+------+
| deptno | dept_name | hod |
+--------+-------------+------+
| 10 | IT | 1 |
| 20 | LOAN | 2 |
| 30 | AGRICULTURE | 5 |
| 40 | LANGUAGE | 3 |
+--------+-------------+------+

Program:

mysql> use kvidk;


Database changed

mysql> create table employee(eno int,ename varchar(20),salary int,


-> zone char(5),age int,grade char(1),dept int,
-> primary key(eno));
Query OK, 0 rows affected (0.37 sec)

mysql> create table department(deptno int,dept_name char(15),hod int,


-> primary key(deptno),
-> foreign key(hod) references employee(eno));
Query OK, 0 rows affected (1.44 sec)

a) To display eno, ename, salary and corresponding dept_name of all the employees.

mysql> select e.eno,e.ename,e.salary, d.dept_name


-> from employee e inner join department d
-> on e.dept=d.deptno;
+-----+--------+--------+-------------+
| eno | ename | salary | dept_name |
+-----+--------+--------+-------------+
| 1 | MONA | 70000 | IT |
| 2 | MUKTAR | 71000 | LOAN |
| 3 | NALINI | 60000 | IT |
| 4 | SANAJ | 65000 | LOAN |
| 5 | SURYA | 58000 | AGRICULTURE |
+-----+--------+--------+-------------+
5 rows in set (0.00 sec)

OR

28
mysql> select employee.eno,employee.ename,employee.salary,
-> department.dept_name from employee inner join department
-> on employee.dept=department.deptno;
+-----+--------+--------+-------------+
| eno | ename | salary | dept_name |
+-----+--------+--------+-------------+
| 1 | MONA | 70000 | IT |
| 2 | MUKTAR | 71000 | LOAN |
| 3 | NALINI | 60000 | IT |
| 4 | SANAJ | 65000 | LOAN |
| 5 | SURYA | 58000 | AGRICULTURE |
+-----+--------+--------+-------------+
5 rows in set (0.00 sec)

OR

mysql> select employee.eno,employee.ename,employee.salary,


-> department.dept_name from employee,department
-> where employee.dept=department.deptno;
+-----+--------+--------+-------------+
| eno | ename | salary | dept_name |
+-----+--------+--------+-------------+
| 1 | MONA | 70000 | IT |
| 2 | MUKTAR | 71000 | LOAN |
| 3 | NALINI | 60000 | IT |
| 4 | SANAJ | 65000 | LOAN |
| 5 | SURYA | 58000 | AGRICULTURE |
+-----+--------+--------+-------------+
5 rows in set (0.00 sec)

b) To display eno, ename, salary and corresponding dept_name of all the employees whose age is between
25 and 35 (both values inclusive).

mysql> select e.eno,e.ename,e.salary, d.dept_name


-> from employee e inner join department d
-> on e.dept=d.deptno
-> WHERE e.age BETWEEN 25 AND 35;
+-----+--------+--------+-------------+
| eno | ename | salary | dept_name |
+-----+--------+--------+-------------+
| 3 | NALINI | 60000 | IT |
| 5 | SURYA | 58000 | AGRICULTURE |
+-----+--------+--------+-------------+
2 rows in set (0.00 sec)

29
c) To display dept_name and corresponding ename from the tables DEPARTMENT and EMPLOYEE.

mysql> select d.dept_name, e.ename


-> from department d inner join employee e
-> on d.deptno=e.dept;
+-------------+--------+
| dept_name | ename |
+-------------+--------+
| IT | MONA |
| LOAN | MUKTAR |
| IT | NALINI |
| LOAN | SANAJ |
| AGRICULTURE | SURYA |
+-------------+--------+
5 rows in set (0.00 sec)

d) To display ename, salary, zone and income tax (Note income tax to be calculated as 30% of salary) of
all the employees with appropriate column headings.

mysql> select e.ename,e.salary,e.zone, e.salary*.30 as income_tax


-> from employee e;
+--------+--------+-------+------------+
| ename | salary | zone | income_tax |
+--------+--------+-------+------------+
| MONA | 70000 | EAST | 21000.00 |
| MUKTAR | 71000 | WEST | 21300.00 |
| NALINI | 60000 | EAST | 18000.00 |
| SANAJ | 65000 | SOUTH | 19500.00 |
| SURYA | 58000 | NORTH | 17400.00 |
+--------+--------+-------+------------+
5 rows in set (0.00 sec)

30
Expt No:11 Date: 22/01/2022

DBMS 8: INTEGRATE SQL with Python - I


Aim:

Write a python program to do the following operations on the student table (database: kvidk) given below.
a) To reduce 5 marks from mark of every student.
b) To change the date of birth of cebil to “1996-05-25”.
c) To display the class’s having 3 or more students.

Table:student
+------+----------+--------+-------+------+------------+
| adno | sname | gender | class | mark | DOB |
+------+----------+--------+-------+------+------------+
| 1001 | abhishek | M | XII | 95 | 2003-01-01 |
| 1002 | alen | M | XI | 90 | 2003-01-01 |
| 1003 | althaf | M | X | 91 | 2003-01-01 |
| 1004 | amita | F | XII | 92 | 2003-01-01 |
| 1005 | devanand | M | XI | 85 | 2003-01-01 |
| 1006 | margret | F | IX | 96 | 2003-01-01 |
| 1007 | nayana | F | XI | 95 | 2003-01-01 |
| 1008 | sreehari | M | XII | 90 | 2003-01-01 |
| 1009 | cebil | M | XI | 99 | 2003-09-12 |
| 1010 | helen | F | IX | 98 | 2002-10-21 |
+------+----------+--------+-------+------+------------+

31
Program:

a) To reduce 5 marks from mark of every student.

import mysql.connector as sqltor


coobj=sqltor.connect(host='localhost',user='root',passwd='1234',database='kvidk')

if coobj.is_connected():
print('Connected')
else:
print('Failed')

cursor=coobj.cursor()
cursor.execute("UPDATE STUDENT SET MARK=MARK-5")
cursor.execute("SELECT * FROM STUDENT")
data=cursor.fetchall()
for i in data:
print(i)
coobj.commit()

Output(Python):
Connected
(1001, 'abhishek', 'M', 'XII', 90, datetime.date(2003, 1, 1))
(1002, 'alen', 'M', 'XI', 85, datetime.date(2003, 1, 1))
(1003, 'althaf', 'M', 'X', 86, datetime.date(2003, 1, 1))
(1004, 'amita', 'F', 'XII', 87, datetime.date(2003, 1, 1))
(1005, 'devanand', 'M', 'XI', 80, datetime.date(2003, 1, 1))
(1006, 'margret', 'F', 'IX', 91, datetime.date(2003, 1, 1))
(1007, 'nayana', 'F', 'XI', 90, datetime.date(2003, 1, 1))
(1008, 'sreehari', 'M', 'XII', 85, datetime.date(2003, 1, 1))
(1009, 'cebil', 'M', 'XI', 94, datetime.date(2003, 9, 12))
(1010, 'helen', 'F', 'IX', 93, datetime.date(2002, 10, 21))

Output(MySQL):
+------+----------+--------+-------+------+------------+
| adno | sname | gender | class | mark | DOB |
+------+----------+--------+-------+------+------------+
| 1001 | abhishek | M | XII | 90 | 2003-01-01 |
| 1002 | alen | M | XI | 85 | 2003-01-01 |
| 1003 | althaf | M | X | 86 | 2003-01-01 |
| 1004 | amita | F | XII | 87 | 2003-01-01 |
| 1005 | devanand | M | XI | 80 | 2003-01-01 |
| 1006 | margret | F | IX | 91 | 2003-01-01 |
| 1007 | nayana | F | XI | 90 | 2003-01-01 |
| 1008 | sreehari | M | XII | 85 | 2003-01-01 |
| 1009 | cebil | M | XI | 94 | 2003-09-12 |
| 1010 | helen | F | IX | 93 | 2002-10-21 |
+------+----------+--------+-------+------+------------+

32
Program:

b) To change the date of birth of cebil to “1996-05-25”.

import mysql.connector as sqltor


coobj=sqltor.connect(host='localhost',user='root',passwd='1234',database='kvidk')

if coobj.is_connected():
print('Connected')
else:
print('Failed')

cursor=coobj.cursor()
cursor.execute("UPDATE STUDENT SET DOB=%s WHERE SNAME=%s",("1996-05-25","CEBIL"))
cursor.execute("select * from student")
data=cursor.fetchall()
for i in data:
print(i)
coobj.commit()

Output(Python):
Connected
(1001, 'abhishek', 'M', 'XII', 90, datetime.date(2003, 1, 1))
(1002, 'alen', 'M', 'XI', 85, datetime.date(2003, 1, 1))
(1003, 'althaf', 'M', 'X', 86, datetime.date(2003, 1, 1))
(1004, 'amita', 'F', 'XII', 87, datetime.date(2003, 1, 1))
(1005, 'devanand', 'M', 'XI', 80, datetime.date(2003, 1, 1))
(1006, 'margret', 'F', 'IX', 91, datetime.date(2003, 1, 1))
(1007, 'nayana', 'F', 'XI', 90, datetime.date(2003, 1, 1))
(1008, 'sreehari', 'M', 'XII', 85, datetime.date(2003, 1, 1))
(1009, 'cebil', 'M', 'XI', 94, datetime.date(1996, 5, 25))
(1010, 'helen', 'F', 'IX', 93, datetime.date(2002, 10, 21))

Output(MySQL):
+------+----------+--------+-------+------+------------+
| adno | sname | gender | class | mark | DOB |
+------+----------+--------+-------+------+------------+
| 1001 | abhishek | M | XII | 90 | 2003-01-01 |
| 1002 | alen | M | XI | 85 | 2003-01-01 |
| 1003 | althaf | M | X | 86 | 2003-01-01 |
| 1004 | amita | F | XII | 87 | 2003-01-01 |
| 1005 | devanand | M | XI | 80 | 2003-01-01 |
| 1006 | margret | F | IX | 91 | 2003-01-01 |
| 1007 | nayana | F | XI | 90 | 2003-01-01 |
| 1008 | sreehari | M | XII | 85 | 2003-01-01 |
| 1009 | cebil | M | XI | 94 | 1996-05-25 |
| 1010 | helen | F | IX | 93 | 2002-10-21 |
+------+----------+--------+-------+------+------------+

33
Program:

c) To display the class’s having 3 or more students.

import mysql.connector as sqltor


coobj=sqltor.connect(host='localhost',user='root',passwd='1234',database='kvidk')

if coobj.is_connected():
print('Connected')
else:
print('Failed')

cursor=coobj.cursor()
cursor.execute("select class, count(*) as strength from student group by class
having count(*)>=3")
data=cursor.fetchall()
for i in data:
print(i)
coobj.commit()

Output(Python):
Connected
('XII', 3)
('XI', 4)

Output(MySQL):

mysql> select class, count(*) as strength from student


-> group by class having count(*)>=3;
+-------+----------+
| class | strength |
+-------+----------+
| XII | 3 |
| XI | 4 |
+-------+----------+
2 rows in set (0.00 sec)

34
Expt No:12 Date: 22/01/2022

DBMS 9: INTEGRATE SQL with Python - II


Aim:

Write a python program to do the following operations on the student table (database: kvidk) given below.
a) To display the student records where student name ends with character ‘a’.
b) To delete all the student records with mark less than 90.
c) To display the average mark of class XI students.

Table:student
+------+----------+--------+-------+------+------------+
| adno | sname | gender | class | mark | DOB |
+------+----------+--------+-------+------+------------+
| 1001 | abhishek | M | XII | 95 | 2003-01-01 |
| 1002 | alen | M | XI | 90 | 2003-01-01 |
| 1003 | althaf | M | X | 91 | 2003-01-01 |
| 1004 | amita | F | XII | 92 | 2003-01-01 |
| 1005 | devanand | M | XI | 85 | 2003-01-01 |
| 1006 | margret | F | IX | 96 | 2003-01-01 |
| 1007 | nayana | F | XI | 95 | 2003-01-01 |
| 1008 | sreehari | M | XII | 90 | 2003-01-01 |
| 1009 | cebil | M | XI | 99 | 2003-09-12 |
| 1010 | helen | F | IX | 98 | 2002-10-21 |
+------+----------+--------+-------+------+------------+

35
Program:

a) To display the student records where student name ends with character ‘a’.

import mysql.connector as sqltor


coobj=sqltor.connect(host='localhost',user='root',passwd='1234',database='kvidk')

if coobj.is_connected():
print('Connected')
else:
print('Failed')

cursor=coobj.cursor()
cursor.execute("select * from student where sname like %s",("%a",))
data=cursor.fetchall()
for i in data:
print(i)
coobj.commit()

Output(Python):
Connected
(1004, 'amita', 'F', 'XII', 87, datetime.date(2003, 1, 1))
(1007, 'nayana', 'F', 'XI', 90, datetime.date(2003, 1, 1))

Output(MySQL):
mysql> select * from student where sname like "%a";
+------+--------+--------+-------+------+------------+
| adno | sname | gender | class | mark | DOB |
+------+--------+--------+-------+------+------------+
| 1004 | amita | F | XII | 87 | 2003-01-01 |
| 1007 | nayana | F | XI | 90 | 2003-01-01 |
+------+--------+--------+-------+------+------------+
2 rows in set (0.00 sec)

36
Program:
b) To delete all the student records with mark less than 90.

import mysql.connector as sqltor


coobj=sqltor.connect(host='localhost',user='root',passwd='1234',database='kvidk')

if coobj.is_connected():
print('Connected')
else:
print('Failed')

cursor=coobj.cursor()
cursor.execute("delete from student where mark<90")
cursor.execute("select * from student")
data=cursor.fetchall()
for i in data:
print(i)
coobj.commit()

Output(Python):
Connected
(1001, 'abhishek', 'M', 'XII', 90, datetime.date(2003, 1, 1))
(1006, 'margret', 'F', 'IX', 91, datetime.date(2003, 1, 1))
(1007, 'nayana', 'F', 'XI', 90, datetime.date(2003, 1, 1))
(1009, 'cebil', 'M', 'XI', 94, datetime.date(1996, 5, 25))
(1010, 'helen', 'F', 'IX', 93, datetime.date(2002, 10, 21))

Output(MySQL):
mysql> select * from student;
+------+----------+--------+-------+------+------------+
| adno | sname | gender | class | mark | DOB |
+------+----------+--------+-------+------+------------+
| 1001 | abhishek | M | XII | 90 | 2003-01-01 |
| 1002 | alen | M | XI | 85 | 2003-01-01 |
| 1003 | althaf | M | X | 86 | 2003-01-01 |
| 1004 | amita | F | XII | 87 | 2003-01-01 |
| 1005 | devanand | M | XI | 80 | 2003-01-01 |
| 1006 | margret | F | IX | 91 | 2003-01-01 |
| 1007 | nayana | F | XI | 90 | 2003-01-01 |
| 1008 | sreehari | M | XII | 85 | 2003-01-01 |
| 1009 | cebil | M | XI | 94 | 1996-05-25 |
| 1010 | helen | F | IX | 93 | 2002-10-21 |
+------+----------+--------+-------+------+------------+
10 rows in set (0.00 sec)
mysql> delete from student where mark<90
mysql> select * from student;
+------+----------+--------+-------+------+------------+
| adno | sname | gender | class | mark | DOB |
+------+----------+--------+-------+------+------------+
| 1001 | abhishek | M | XII | 90 | 2003-01-01 |
| 1006 | margret | F | IX | 91 | 2003-01-01 |
| 1007 | nayana | F | XI | 90 | 2003-01-01 |
| 1009 | cebil | M | XI | 94 | 1996-05-25 |
| 1010 | helen | F | IX | 93 | 2002-10-21 |
+------+----------+--------+-------+------+------------+
5 rows in set (0.00 sec)

37
Program:

c) To display the average mark of class XI students.

import mysql.connector as sqltor


coobj=sqltor.connect(host='localhost',user='root',passwd='1234',database='kvidk')

if coobj.is_connected():
print('Connected')
else:
print('Failed')

cursor=coobj.cursor()
cursor.execute("select class, avg(mark) from student group by class having
class=%s",("XI",))
data=cursor.fetchall()
for i in data:
print(i)
coobj.commit()

Output(Python):
Connected
('XI', Decimal('92.0000'))

Output(MySQL):

mysql> select class, avg(mark) from student


-> group by class having class="XI";
+-------+-----------+
| class | avg(mark) |
+-------+-----------+
| XI | 92.0000 |
+-------+-----------+
1 row in set (0.00 sec)

****** THE END ******

38

You might also like