INDIAN PUBLIC SCHOOL SAMBALPUR
AISSCE-2023-24
PRACTICAL FILE
COMPUTER SCIENCE (083)
Student Name :
Grade :
Roll No. :
SUBMITTED TO
PRAFULLA KUMAR GOUDA
PGT COMPUTER SCIENCE)
Practical No-18
SQL
Create a table EMPLOYEE with constraints
SOLUTION
Step-1 Create a database:
CREATE DATABASE Bank;
Step-2 Display the databases
SHOW DATABASES;
Step-3: Enter into database
Use Bank;
Step-4: Create the table EMPLOYEE
create table Employee(Ecode int primary key,Ename varchar(20) NOT NULL,
Dept varchar(15),City varchar(15), sex char(1), DOB date, Salary float(12,2));
Insert data into the table
SOLUTION
insert into Employee values(1001,"Atul","Production","Vadodara","M","1992-
10-23",23000.50);
Query OK, 1 row affected (0.11 sec)
Note: Insert more rows as per above insert command.
Add a new column in a table.
SOLUTION
ALTER TABLE EMPLOYEE ADD address varchar(50);
Page 40 of 45
Change the data-type and size of an existing column.
SOLUTION
ALTER TABLE EMPLOYEE MODIFY city char(30);
Write SQL queries using SELECT, FROM, WHERE clause based on
EMPLOYEE table.
1. List the name of female employees in EMPLOYEE table.
Solution:-
SELECT Ename FROM EMPLOYEE WHERE sex=’F’;
2. Display the name and department of those employees who work in surat
and salary is greater than 25000.
Solution:-
SELECT Ename, Dept FROM EMPLOYEE WHERE city=’surat’ and salary > 25000;
3. Display the name of those female employees who work in Mumbai.
Solution:-
SELECT Ename FROM EMPLOYEE WHERE sex=’F’ and city=’Mumbai’;
4. Display the name of those employees whose department is marketing or
RND.
Solution:-
SELECT Ename FROM EMPLOYEE WHERE Dept=’marketing’ OR Dept=’RND’
5. List the name of employees who are not males.
Solution:-
SELECT Ename, Sex FROM EMPLOYEE WHERE sex!=’M’;
Page 41 of 45
Queries using DISTINCT, BETWEEN, IN, LIKE, IS NULL, ORDER BY,
GROUP BY, HAVING
A. Display the name of departments. Each department should be displayed
once.
SOLUTION
SELECT DISTINCT(Dept) FROM EMPLOYEE;
B. Find the name and salary of those employees whose salary is between
35000 and 40000.
SOLUTION
SELECT Ename, salary FROM EMPLOYEE WHERE salary BETWEEN 35000 and
40000;
C. Find the name of those employees who live in guwahati, surat or jaipur city.
SOLUTION
SELECT Ename, city FROM EMPLOYEE WHERE city IN(‘Guwahati’,’Surat’,’Jaipur’);
D. Display the name of those employees whose name starts with ‘M’.
SOLUTION
SELECT Ename FROM EMPLOYEE WHERE Ename LIKE ‘M%’;
E. List the name of employees not assigned to any department.
SOLUTION
SELECT Ename FROM EMPLOYEE WHERE Dept IS NULL;
F. Display the list of employees in descending order of employee code.
SOLUTION
SELECT * FROM EMPLOYEE ORDER BY ecode DESC;
Page 42 of 45
G. Find the average salary at each department.
SOLUTION
SELECT Dept, avg(salary) FROM EMPLOYEE group by Dept;
H.Find maximum salary of each department and display the name of that
department which has maximum salary more than 39000.
SOLUTION
SELECT Dept, max(salary) FROM EMPLOYEE group by Dept HAVING
max(salary)>39000;
Queries for Aggregate functions- SUM( ), AVG( ), MIN( ), MAX( ), COUNT( )
a. Find the average salary of the employees in employee table.
Solution:-
SELECT avg(salary) FROM EMPLOYEE;
b. Find the minimum salary of a female employee in EMPLOYEE table.
Solution:-
SELECT Ename, min(salary) FROM EMPLOYEE WHERE sex=’F’;
c. Find the maximum salary of a male employee in EMPLOYEE table.
Solution:-
SELECT Ename, max(salary) FROM EMPLOYEE WHERE sex=’M’;
d. Find the total salary of those employees who work in Guwahati city.
Solution:-
SELECT sum(salary) FROM EMPLOYEE WHERE city=’Guwahati’;
e. Find the number of tuples in the EMPLOYEE relation.
Solution:- SELECT count(*) FROM EMPLOYEE
Page 43 of 45
Practical No-19
# Write a program to connect Python with MySQL using database connectivity
and perform the following operations on data in database: Fetch, Update and
delete the data.
A. CREATE A TABLE
SOLUTION
import mysql.connector
demodb = mysql.connector.connect(host="localhost", user="root",
passwd="computer", database="EDUCATION")
democursor=demodb.cursor( )
democursor.execute("CREATE TABLE STUDENT (admn_no int primary key,
sname varchar(30), gender char(1), DOB date, stream varchar(15), marks
float(4,2))")
B. INSERT THE DATA
SOLUTION
import mysql.connector
demodb = mysql.connector.connect(host="localhost", user="root",
passwd="computer", database="EDUCATION")
democursor=demodb.cursor( )
democursor.execute("insert into student values (%s, %s, %s, %s, %s, %s)",
(1245, 'Arush', 'M', '2003-10-04', 'science', 67.34))
demodb.commit( )
C. FETCH THE DATA
SOLUTION
import mysql.connector
Page 44 of 45
demodb = mysql.connector.connect(host="localhost", user="root",
passwd="computer", database="EDUCATION")
democursor=demodb.cursor( )
democursor.execute("select * from student")
for i in democursor:
print(i)
D. UPDATE THE RECORD
SOLUTION
import mysql.connector
demodb = mysql.connector.connect(host="localhost", user="root",
passwd="computer", database="EDUCATION")
democursor=demodb.cursor( )
democursor.execute("update student set marks=55.68 where admn_no=1356")
demodb.commit( )
E. DELETE THE DATA
SOLUTION
import mysql.connector
demodb = mysql.connector.connect(host="localhost", user="root",
passwd="computer", database="EDUCATION")
democursor=demodb.cursor( )
democursor.execute("delete from student where admn_no=1356")
demodb.commit()
Page 45 of 45