0% found this document useful (0 votes)
38 views

Cbse - Comp SC

Uploaded by

swagboigamer32
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
38 views

Cbse - Comp SC

Uploaded by

swagboigamer32
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 42

COMPUTER SCIENCE

PRACTICAL PROGRAMS
FOR GRADE – XII
[2024-2025]
TABLE OF CONTENTS

S.No Name of the Exercises Page.No


Python Programs
1. Creating a menu driven program to perform arithmetic operations. 3
2. Creating a python program to display Fibonacci series 5
3. 7
To write the program to find the given string is palindrome or not
4. Read a text file and count the number of occurrence of the particular word in the file 9
content.
5. Creating a python program to implement mathematical functions. 10
6. Creating a python program to generate random number between 1 to 6 11
7. Creating a python program to read a text file line by line and display each word 12
separated by '#'.
8. Creating a python program to read a text file and display the number of 13
vowels/consonants/lower case/ upper case characters.
9. Creating python program to display short words from a text file 15
10. Creating a python program to copy particular lines of a text file into another text 16
file.
11. Creating a python program to create and search records in binary file. 17
12. Creating a python program to create and update/modify records in binary file. 19
13. Creating a python program to create and search employee’s record in csv file. 21
14. Creating a python program to create and update employee’s record in csv file. 23
15. Creating a python program to implement stack operations (list). 26
Python – SQL connectivity programs
16. Creating a python program to integrate MYSQL with Python (Creating database 28
and table)
17. Creating a python program to integrate MYSQL with Python (Inserting records 30
and displaying records)
18. Creating a python program to integrate MYSQL with Python (Searching and 32
displaying records)
19. Creating a python program to integrate MYSQL with Python (Updating records) 33
SQL Queries
20. SQL COMMANDS EXERCISE – 1 35
21. SQL COMMANDS EXERCISE – 2 36
22. SQL COMMANDS EXERCISE – 3 38
23. SQL COMMANDS EXERCISE – 4 40
24. SQL COMMANDS EXERCISE – 5 42

2
EX.NO: 1
DATE:
CREATING A MENU DRIVEN PROGRAM TO PERFORM ARITHMETIC
OPERATIONS
AIM:

To write a menu driven Python Program to perform Arithmetic operations (+,-*,/)


based on the user’s choice.

SOURCE CODE:

Result:
Thus, the above Python program has been executed and the output is verified
successfully.

3
SAMPLE OUTPUT:

Python Program Executed Output:

***************************************************************************************

4
EX.NO: 2
DATE:
CREATING A PYTHON PROGRAM TO DISPLAY FIBONACCI SERIES

AIM:
To write a Python Program to display Fibonacci Series up to ‘n’ numbers.

SOURCE CODE:

Result:
Thus, the above Python program has been executed and the output is verified
successfully.

5
SAMPLE OUTPUT:
Python Executed Program Output:

*****************************************************************************************

6
EX.NO: 3

DATE:
Write the program to find the given string is palindrome or not.

AIM:

To write the program to find the given string is palindrome or not.


SOURCE CODE:

print("Palindrome checking program")


ans = ‘yes’
while ans.lower()==’yes’:
s=input("Enter the string :")
s1=s[::-1]
if s==s1:
print("The given string ",s,"is a palindrome")
else:
print("The given string ",s,"is not a palindrome")
ans = input(‘do you want to check for more strings(yes/no):’)

Result:
Thus, the above Python program has been executed and the output is verified
successfully.

7
SAMPLE OUTPUT:
Python Executed Program Output:

Palindrome checking program


Enter the string :malayalam
The given string malayalam is a palindrome
do you want to check for more strings(yes/no):yes
Enter the string :level
The given string level is a palindrome
do you want to check for more strings(yes/no):no
>>>

************************************************************************************************

8
EX.NO: 4

DATE:
Read a text file and count the number of occurrence of the particular
word in the file content.

AIM:

To Write a Python program that reads a text file and count the number of occurrence of
the particular word in the file content.

Source Code:
a=open("sample.txt",'r')
b=a.read()
c=b.split()
d=0
w=input("Enter the word which you want to count:")
for i in c:
if i.lower()==w:
d+=1
if d==0:
print("The given word",w,"not found in the file content")
else:
print("The given word",w,"found",d,"time(s) in the file content")

Result:
Thus, the above Python program has been executed and the output is verified
successfully.

sample.txt
Welcome to python programming
Introduced by guido van rossum
Named after Monte Carlo Python

Sample Output1:

Enter the word which you want to count:python


The given word python found 2 time(s) in the file content

**********************************************************************************

9
EX.NO: 5

DATE:

CREATING A PYTHON PROGRAM TO IMPLEMENT MATHEMATICAL FUNCTIONS

AIM:

To write a Python program to implement python mathematical functions to find:

(i) To find Square of a Number.


(ii) To find Log of a Number(i.e. Log10)
(iii) To find square root of a Number

SOURCE CODE:
import math

def square(num):
s = math.pow(num,2)
return s

def log(num):
s = math.log10(num)
return s

def square_root(num):
s = math.sqrt(num)
return s

num = int(input("enter a number to implement math functions on:"))


print('The square of the number is',square(num))
print('The log of the number is',log(num))
print('The square root of the number is',square_root(num))

Result:
Thus, the above Python program has been executed and the output is verified
successfully.

SAMPLE OUTPUT:
Python Executed Program Output:
enter a number to implement math functions on:4
The square of the number is 16.0
The log of the number is 0.6020599913279624
The square root of the number is 2.0
***************************************************************************

10
EX.NO: 6

DATE:

CREATING A PYTHON PROGRAM TO GENERATE RANDOM NUMBER


BETWEEN 1 TO 6
AIM:

To write a Python program to generate random number between 1 to 6 to simulate


the dice.

SOURCE CODE:

import random
print("Dice game ")
print("Game starts...")
ans='y'
while ans=='y':
print("Dice rolling")
s=random.randint(1,6)
print("You got:",s)
ans=input("Do you want to roll again the dice (y/n):")
print("See you again.. Bye")

Result:
Thus, the above Python program has been executed and the output is verified
successfully.

SAMPLE OUTPUT:
Python Executed Output Program:
Dice game
Game starts...
Dice rolling
You got: 2
Do you want to roll again the dice (y/n):y
Dice rolling
You got: 1
Do you want to roll again the dice (y/n):y
Dice rolling
You got: 6
Do you want to roll again the dice (y/n):n
See you again.. Bye

**********************************************************************************

11
EX.NO: 7
DATE:

CREATING A PYTHON PROGRAM TO READ A TEXT FILE LINE BY LINE AND


DISPLAY EACH WORD SEPARATED BY '#'

AIM:

To write a Python Program to Read a text file "Story.txt" line by line and display
each word separated by '#'.

SOURCE CODE:
a=open('Story.txt','r')
b=a.readline()
while b:
c=b.split()
for i in range(len(c)-1):
print(c[i],'#',end='')
print(c[-1])
print()
b=a.readline()
a.close()

Result:
Thus, the above Python program has been executed and the output is verified
successfully.

SAMPLE OUTPUT:

Story.txt:

Python Program output after execution:

Like#a#Joy#on#the#heart#of#a#sorrow.
The#sunset#hangs#on#a#cloud.

12
EX.NO: 8

DATE:

CREATING A PYTHON PROGRAM TO READ A TEXT FILE AND DISPLAY THE


NUMBER OF VOWELS/CONSONANTS/LOWER CASE/ UPPER CASE CHARACTERS.
AIM:

To write a Python Program to read a text file "Story.txt" and displays the number of
Vowels/ Consonants/ Lowercase / Uppercase/characters in the file.

SOURCE CODE:
a=open('sample.txt','r')
b=a.read()
v=c=uc=lc=0
for i in b:
if i.isalpha():
if i in 'aeiouAEIOU':
v+=1
else:
c+=1
if i.isupper():
uc+=1
elif i.islower():
lc+=1
print("The total number of vowels in the file:",v)
print("The total number of consonants in the file:",c)
print("The total number of uppercase in the file:",uc)
print("The total number of lowercase in the file:",lc)
a.close()

Result:
Thus, the above Python program has been executed and the output is verified
successfully.

13
SAMPLE OUTPUT:
Story.txt:

***************************************************************************************

14
EX.NO: 9
DATE:

CREATING PYTHON PROGRAM TO DISPLAY SHORT WORDS FROM A TEXT FILE

AIM:
To Write a method Disp() in Python, to read the lines from poem.txt and display those
words which are less than 5 characters.

Source Code:

Result:
Thus, the above Python program has been executed and the output is verified
successfully.

Sample Output:

Poem.txt:

Python Executed Program Output:

**********************************************************************************

15
EX.NO: 10
DATE:

CREATING A PYTHON PROGRAM TO COPY PARTICULAR LINES OF A TEXT FILE


INTO AN ANOTHER TEXT FILE
AIM:

To write a python program to read lines from a text file "Sample1.txt" and copy those
lines into another file which are starting with an alphabet 'a' or 'A'.
SOURCE CODE:

f1= open('sample1.txt','r')
a = f1.readlines()
f1.close()
f1= open('sample1.txt','w')
f2=open('sample2.txt','w')
for i in a:
if ('a' in i) or ("A" in i):
f2.write(i)
else:
f1.write(i)
a.close()
b.close()
print("File Content written into Sample2.txt which lines not contains the letter 'a'")
Result:
Thus, the above Python program has been executed and the output is verified
successfully.
SAMPLE OUTPUT:
Python Executed Program output:

sample1.txt:

Python Executed Program Output:

sample1.txt: sample2.txt
My favorite color is skyblue. Aeroplane was invented by the Right Brothers.
An apple a day keeps the doctor away.

16
EX.NO: 11
DATE:
CREATING A PYTHON PROGRAM TO CREATE AND SEARCH RECORDS IN
BINARY FILE
AIM:

To write a Python Program to Create a binary file with roll number and name.
Search for a given roll number and display the name, if not found display appropriate
message.

SOURCE CODE:
import pickle
def addrec():
a=open('student.bin','wb')
print('Add student details')
l=[]
ans='y'
while ans=='y':
r=int(input("Enter the roll no:"))
n=input("Enter name :")
m=float(input("Enter mark:"))
l.append([r,n,m])
print(l)
ans=input("Do you want to add another record(y/n):")
pickle.dump(l,a)
print("Records stored successfully!.")
a.close()
def searchrec():
a=open("student.bin",'rb')
r=int(input("Enter the roll no which you want to search:"))
b=pickle.load(a)
for i in b:
if r==i[0]:
print("The searched roll number details are:",i)
break
else:
print("Entered Roll no not found in the file")
a.close()
print("Student record search program")
addrec()
searchrec()

Result:
Thus, the above Python program has been executed and the output is verified
successfully.

17
SAMPLE OUPUT:

PYTHON PROGRAM EXECUTED OUTPUT:

Student record search program


Add student details
Enter the roll no:1
Enter name :abhay
Enter mark:20
[[1, 'abhay', 20.0]]
Do you want to add another record(y/n):y
Enter the roll no:2
Enter name :akash
Enter mark:30
[[1, 'abhay', 20.0], [2, 'akash', 30.0]]
Do you want to add another record(y/n):y
Enter the roll no:3
Enter name :sneha
Enter mark:42
[[1, 'abhay', 20.0], [2, 'akash', 30.0], [3, 'sneha', 42.0]]
Do you want to add another record(y/n):n
Records stored successfully!.
Enter the roll no which you want to search:2
The searched roll number details are: [2, 'akash', 30.0]

*******************************************************************************************

18
EX.NO: 12
DATE:
CREATING A PYTHON PROGRAM TO CREATE AND UPDATE/MODIFY RECORDS INBINARY FILE

AIM:

To write a Python Program to Create a binary file with roll number, name, mark
and update/modify the mark for a given roll number.

SOURCE CODE:
import pickle
def addrec():
a=open('student.bin','wb')
print('Add student details')
l=[]
ans='y'
while ans=='y':
r=int(input("Enter the roll no:"))
n=input("Enter name :")
m=float(input("Enter mark:"))
l.append([r,n,m])
print(l)
ans=input("Do you want to add another record(y/n):")
pickle.dump(l,a)
print("Records stored successfully!.")
a.close()
def updaterec():
a=open("student.bin",'rb+')
r=int(input("Enter the roll no which you want to update:"))
b=pickle.load(a)
l=[]
for i in b:
l.append(i)
for i in l:
if r==i[0]:
nm=float(input("Enter new mark:"))
i[2]=nm
break
else:
print("Entered Roll no not found in the file")
a.seek(0)
pickle.dump(l,a)
a.seek(0)
print(‘The updated data is:’,pickle.load(a))
a.close()
print("Record updated successfully")
addrec()
updaterec()

19
Result:
Thus, the above Python program has been executed and the output is verified
successfully.

SAMPLE OUTPUT:
PYTHON PROGRAM EXECUTED OUTPUT:

Record updated successfully


Student record update program
Add student details
Enter the roll no:1
Enter name :bhavana
Enter mark:35
[[1, 'bhavana', 35.0]]
Do you want to add another record(y/n):y
Enter the roll no:2
Enter name :hima
Enter mark:40
[[1, 'bhavana', 35.0], [2, 'hima', 40.0]]
Do you want to add another record(y/n):y
Enter the roll no:3
Enter name :rayan
Enter mark:80
[[1, 'bhavana', 35.0], [2, 'hima', 40.0], [3, 'rayan', 80.0]]
Do you want to add another record(y/n):n
Records stored successfully!.
Enter the roll no which you want to update:2
Enter new mark:79
The updated data is: [[1, 'bhavana', 35.0], [2, 'hima', 79.0], [3, 'rayan', 80.0]]

20
EXP:13

DATE:

CREATING A PYTHON PROGRAM TO CREATE AND SEARCH EMPLOYEE’S RECORD


IN CSV FILE.

AIM:

To write a Python program Create a CSV file to store Empno, Name, Salary and search any Empno
and display Name, Salary and if not found display appropriate message.

SOURCE CODE:
import csv

def addrec():
a = open('emp.csv','w',newline='')
b=csv.writer(a)
l=[]
ans='y'
while ans=='y':
eno=int(input("Enter the emp no:"))
ename=input("Enter emp name :")
s=int(input("Enter emp salary:"))
l.append([eno,ename,s])
ans=input("Do you want to add another record(y/n):")
b.writerows(l)
print("record stored")
a.close()

def searchrec():
a = open('emp.csv','r',newline='')
b = csv.reader(a)
s=int(input('Enter the emp no which you want to search:'))
for i in b:
if i[0]==str(s):
print("The details found are:",i)
break
else:
print('no record found')
a.close()
print("Employee details - search record program")
addrec()
searchrec()

Result:
Thus, the above Python program has been executed and the output is verified
successfully.

21
SAMPLE OUTPUT:

PYTHON PROGRAM EXECUTED OUTPUT:

Employee details - search record program


Enter the emp no:101
Enter emp name :ajay
Enter emp salary:20000
Do you want to add another record(y/n):y
Enter the emp no:102
Enter emp name :jhansi
Enter emp salary:20000
Do you want to add another record(y/n):y
Enter the emp no:103
Enter emp name :kavya
Enter emp salary:30000
Do you want to add another record(y/n):n
record stored
Enter the emp no which you want to search:102
The details found are: ['102', 'jhansi', '20000']

******************************************************************************

22
EXP:14

DATE:

CREATING A PYTHON PROGRAM TO CREATE AND UPDATE EMPLOYEE’S RECORD


IN CSV FILE.

AIM:

To create the csv file which should contains the employee details and to update their salary based on emp no.

SOURCE CODE:
import csv
def addrec():
a = open('emp.csv','w',newline='')
b=csv.writer(a)
l=[]
ans='y'
while ans=='y':
eno=int(input("Enter the emp no:"))
ename=input("Enter emp name :")
s=int(input("Enter emp salary:"))
l.append([eno,ename,s])
ans=input("Do you want to add another record(y/n):")
b.writerows(l)
print("record stored")
a.close()
def updaterec():
a=open("emp.csv",'r+',newline='')
r=int(input("Enter the employee no which you want to update salary:"))
b=csv.reader(a)
l=[]
for i in b:
l.append(i)
for i in l:
if str(r)==i[0]:
ns=float(input("Enter new salary:"))
i[2]=ns
break
else:
print("Entered empolyee id not found in the file")
a.seek(0)
w = csv.writer(a)
w.writerows(l)
a.seek(0)
print('The updated data is:',list(csv.reader(a)))
a.close()
print("Record updated successfully")
print("Student record update program")
addrec()
updaterec()

23
Result:
Thus, the above Python program has been executed and the output is verified
successfully.

SAMPLE OUTPUT:
Python Program Executed Output:
Record updated successfully
Student record update program
Enter the emp no:101
Enter emp name :ganu
Enter emp salary:20000
Do you want to add another record(y/n):y
Enter the emp no:102
Enter emp name :tina
Enter emp salary:29000
Do you want to add another record(y/n):y
Enter the emp no:103
Enter emp name :gayan
Enter emp salary:89000
Do you want to add another record(y/n):n
record stored
Enter the employee no which you want to update salary:103
Enter new salary:39000
The updated data is: [['101', 'ganu', '20000'], ['102', 'tina', '29000'], ['103', 'gayan', '39000.0']]

24
EXP:15

DATE:

Write a program to implement all stack operations using list as stack.


AIM:
To write a program to implement all stack operations using list as stack.

SOURCE CODE:

def PUSH(stk):
a=input('Enter a city:')
stk.append(a)

def POP(stk):
if len(stk)==0:
print('STACK UNDERFLOW')
else:
c=stk.pop()
print('The popped city is:',c)

def DISPLAY(stk):
if len(stk)==0:
print('STACK EMPTY')
else:
print("The stack elements are:")
print(stk[-1],'<--TOP')
for i in reversed(stk[:-1]):
print(i)

print('STACK IMPLEMENTATION PROGRAM')


stk=[]
ans='y'
while ans.lower()=='y':
print('1-PUSH\n2-POP\n3-DISPLAY\n4-EXIT')
ch=int(input('Enter your choice:'))
if ch==1:
PUSH(stk)
elif ch==2:
POP(stk)
elif ch==3:
DISPLAY(stk)
elif ch==4:
break
ans=input('Do you want to do any operation(y/n):')

25
SAMPLE OUTPUT:
Python Program Executed Output:
STACK IMPLEMENTATION PROGRAM
1-PUSH
2-POP
3-DISPLAY
4-EXIT
Enter your choice:1
Enter a city:vizag
Do you want to do any operation(y/n):y
1-PUSH
2-POP
3-DISPLAY
4-EXIT
Enter your choice:1
Enter a city:hyderabad
Do you want to do any operation(y/n):y
1-PUSH
2-POP
3-DISPLAY
4-EXIT
Enter your choice:1
Enter a city:banglore
Do you want to do any operation(y/n):y
1-PUSH
2-POP
3-DISPLAY
4-EXIT
Enter your choice:1
Enter a city:hosur
Do you want to do any operation(y/n):y
1-PUSH
2-POP
3-DISPLAY
4-EXIT
Enter your choice:2
The popped city is: hosur
Do you want to do any operation(y/n):y
1-PUSH
2-POP
3-DISPLAY
4-EXIT
Enter your choice:3
The stack elements are:
banglore <--TOP
hyderabad
vizag
Do you want to do any operation(y/n):y
1-PUSH
2-POP
3-DISPLAY
4-EXIT
Enter your choice:4

26
EX.NO: 16

DATE:
CREATING A PYTHON PROGRAM TO INTEGRATE MYSQL WITH PYTHON
(CREATING DATABASE AND TABLE)
AIM:
To write a Python Program to integrate MYSQL with Python to create Database and Table
to store the details of employees.

Source Code:

Result:
Thus, the above Python program has been executed and the output is verified
successfully.

27
Sample Output:

*******************************************************************************************************

28
EX.NO: 17
DATE:

CREATING A PYTHON PROGRAM TO INTEGRATE MYSQL WITH PYTHON


(INSERTING RECORDS AND DISPLAYING RECORDS)

AIM:

To write a Python Program to integrate MYSQL with Python by inserting records to


Emp table and display the records.

SOURCE CODE:

Result:
Thus, the above Python program has been executed and the output is verified
successfully.

29
SAMPLE OUTPUT:

Python Executed Program Output:

******************************************************************************************************************************************************************************

30
EX.NO: 18
DATE:

CREATING A PYTHON PROGRAM TO INTEGRATE MYSQL WITH PYTHON


(SEARCHING AND DISPLAYING RECORDS)
AIM:

To write a Python Program to integrate MYSQL with Python to search an Employee using
EMPID and display the record if present in already existing table EMP, if not display the
appropriate message.

SOURCE CODE:

Result:
Thus, the above Python program has been executed and the output is verified
successfully.

SAMPLE OUTPUT:

Python Executed Program Output:

31
EX.NO: 19

DATE:

CREATING A PYTHON PROGRAM TO INTEGRATE MYSQL WITH PYTHON


(UPDATING RECORDS)
AIM:

To write a Python Program to integrate MYSQL with Python to search an Employee using
EMPID and update the Salary of an employee if present in already existing table
EMP, if not display the appropriate message.
SOURCE CODE:

Result:
Thus, the above Python program has been executed and the output is verified
successfully.

32
SAMPLE OUTPUT:

Python Executed Program Output:

**************************************************************************************************************************************************

33
SQL COMMANDS EXERCISE - 1
Ex.No: 20
DATE:

AIM:
To write Queries for the following Questions based on the given table:

Rollno Name Gender Age Dept DOA Fees


1 Arun M 24 COMPUTER 1997-01-10 120
2 Ankit M 21 HISTORY 1998-03-24 200
3 Anu F 20 HINDI 1996-12-12 300
4 Bala M 19 NULL 1999-07-01 400
5 Charan M 18 HINDI 1997-09-05 250
6 Deepa F 19 HISTORY 1997-06-27 300
7 Dinesh M 22 COMPUTER 1997-02-25 210
8 Usha F 23 NULL 1997-07-31 200

(a) Write a Query to Create a new database in the name of "STUDENTS".

CREATE DATABASE STUDENTS;

(b) Write a Query to Open the database "STUDENTS".

USE STUDENTS;

(c) Write a Query to create the above table called: "STU"

CREATE TABLE STU(ROLLNO INT PRIMARY KEY,NAME VARCHAR(10),


GENDER VARCHAR(3), AGE INT,DEPT VARCHAR(15),
DOA DATE,FEES INT);

(d) Write a Query to list all the existing database names.

SHOW DATABASES;

(e) Write a Query to List all the tables that exists in the current database.

SHOW TABLES;
Output:

34
SQL COMMANDS EXERCISE - 2
Ex.No: 21
DATE:

AIM:
To write Queries for the following Questions based on the given table:
STU:
Rollno Name Gender Age Dept DOA Fees
1 Arun M 24 COMPUTER 1997-01-10 120
2 Ankit M 21 HISTORY 1998-03-24 200
3 Anu F 20 HINDI 1996-12-12 300
4 Bala M 19 NULL 1999-07-01 400
5 Charan M 18 HINDI 1997-09-05 250
6 Deepa F 19 HISTORY 1997-06-27 300
7 Dinesh M 22 COMPUTER 1997-02-25 210
8 Usha F 23 NULL 1997-07-31 200

(a) Write a Query to insert all the rows above.

INSERT INTO STU VALUES (1,'Arun','M', 24,'COMPUTER','1997-01-10', 120);

INSERT INTO STU VALUES (2,'Ankit','M', 21,'HISTORY','1998-03-24', 200);

INSERT INTO STU VALUES (3,'Anu','F', 20,'HINDI','1996-12-12', 300);

INSERT INTO STU VALUES (4,'Bala','M', 19, NULL,'1999-07-01', 400);

INSERT INTO STU VALUES (5,'Charan','M', 18,'HINDI','1997-06-27', 250);

INSERT INTO STU VALUES (6,'Deepa','F', 19,'HISTORY','1997-06-27', 300);

INSERT INTO STU VALUES (7,'Dinesh','M', 22,'COMPUTER','1997-02-25', 210);

INSERT INTO STU VALUES (8,'Usha','F', 23, NULL,'1997-07-31', 200);

(b) Write a Query to display all the details of the Employees from the above table 'STU'.

SELECT * FROM STU;

Output:

35
(c) Write a query to display Rollno, Name and Department from STU table.

SELECT ROLLNO,NAME,DEPT FROM STU;

(d) Write a Query to display distinct Department from STU table.

SELECT DISTICT(DEPT) FROM STU;

Output:

(e) To show all information about students of History department.

SELECT * FROM STU WHERE DEPT='HISTORY';

Output:

********************************************************************************************

36
Ex.No: 22 SQL COMMANDS EXERCISE - 3

DATE:

AIM:
To write Queries for the following Questions based on the given table:

Rollno Name Gender Age Dept DOA Fees


1 Arun M 24 COMPUTER 1997-01-10 120
2 Ankit M 21 HISTORY 1998-03-24 200
3 Anu F 20 HINDI 1996-12-12 300
4 Bala M 19 NULL 1999-07-01 400
5 Charan M 18 HINDI 1997-09-05 250
6 Deepa F 19 HISTORY 1997-06-27 300
7 Dinesh M 22 COMPUTER 1997-02-25 210
8 Usha F 23 NULL 1997-07-31 200

(a) Write a Query to list name of female students in Hindi Department.

SELECT NAME FROM STU WHERE DEPT='HINDI' AND GENDER='F';

Output:

(b) Write a Query to list name of the students whose age’s are between 18 to 20.

SELECT NAME FROM STU WHERE AGE BETWEEN 18 AND 20;

Output:

37
(c) Write a Query to display the names of the students whose name is starting with 'A'.

SELECT NAME FROM STU WHERE NAME LIKE 'A%';

Output:

(d) Write a query to list the names of those students whose name have second alphabet 'n' in their
names.

SELECT NAME FROM STU WHERE NAME LIKE '_N%';

Output:

**********************************************************************************************************

38
Ex.No: 23 SQL COMMANDS EXERCISE - 4

DATE:

AIM:
To write Queries for the following Questions based on the given table:

Rollno Name Gender Age Dept DOA Fees


1 Arun M 24 COMPUTER 1997-01-10 120
2 Ankit M 21 HISTORY 1998-03-24 200
3 Anu F 20 HINDI 1996-12-12 300
4 Bala M 19 NULL 1999-07-01 400
5 Charan M 18 HINDI 1997-09-05 250
6 Deepa F 19 HISTORY 1997-06-27 300
7 Dinesh M 22 COMPUTER 1997-02-25 210
8 Usha F 23 NULL 1997-07-31 200

(a) Write a Query to delete the details of Roll number 8.

DELETE FROM STU WHERE ROLLNO=8;

Output (After Deletion):

(b) Write a Query to change the fess of Student to 170 whose Roll number is 1, if the existing fess
is less than 130.

UPDATE STU SET FEES=170 WHERE ROLLNO=1 AND FEES<130;

Output(After Update):

39
(c) Write a Query to add a new column named Area of type varchar in table STU.

ALTER TABLE STU ADD AREA VARCHAR(20);

Output:

(d) Write a Query to Display Name of all students whose Area Contains NULL.

SELECT NAME FROM STU WHERE AREA IS NULL;

Output:

(e) Write a Query to delete Area Column from the table STU.

ALTER TABLE STU DROP AREA;

Output:

(f) Write a Query to delete table from Database.

DROP TABLE STU;

Output:
*******************************************************************************************

40
Ex.No: 24 SQL COMMANDS EXERCISE - 5
DATE:

AIM:
To write Queries for the following Questions based on the given table:
TABLE: UNIFORM

Ucode Uname Ucolor StockDate


1 Shirt White 2021-03-31
2 Pant Black 2020-01-01
3 Skirt Grey 2021-02-18
4 Tie Blue 2019-01-01
5 Socks Blue 2019-03-19
6 Belt Black 2017-12-09

TABLE: COST

Ucode Size Price Company


1 M 500 Raymond
1 L 580 Mattex
2 XL 620 Mattex
2 M 810 Yasin
2 L 940 Raymond
3 M 770 Yasin
3 L 830 Galin
4 S 150 Mattex

(a) To Display the average price of all the Uniform of Raymond Company from table COST.

SELECT AVG(PRICE) FROM COST WHERE COMPANY='RAYMOND';

Output:

(b) To display details of all the Uniform in the Uniform table in descending order of Stock date.

SELECT * FROM UNIFORM ORDER BY STOCKDATE DESC;

Output:

41
(c) To Display max price and min price of each company.

SELECT COMPANY,MAX(PRICE),MIN(PRICE) FROM COST GROUP BY COMPANY;

Output:

(d) To display the company where the number of uniforms size is more than 2.

SELECT COMPANY, COUNT(*) FROM COST GROUP BY COMPANY HAVING COUNT(*)>2;

Output:

(e) To display the Ucode, Uname, Ucolor, Size and Company of tables uniform and cost.

SELECT U.UCODE,UNAME,UCOLOR,SIZE,COMPANY FROM UNIFORM U,COST C WHERE


U.UCODE=C.UCODE;

Output:

*****************************************************************************************

42

You might also like