You are on page 1of 15

PRACTICAL- 16

Stack operations
AIM- Write a program to create Lpush() and Lpop() function to do push
and pop operation on a stack using a list e.g. take a student information
and push and pop the details
Coding:

Output:
***STACK DEMONSTRATION***
1.PUSH
2.POP
3.DISPLAY
4.EXIT
Enter your choice:1
Enter the item to push:5
***STACK DEMONSTRATION***
1.PUSH
2.POP
3.DISPLAY
4.EXIT
Enter your choice:1
Enter the item to push:9
***STACK DEMONSTRATION***
1.PUSH
2.POP
3.DISPLAY
4.EXIT
Enter your choice:1
Enter the item to push:3
***STACK DEMONSTRATION***
1.PUSH
2.POP
3.DISPLAY
4.EXIT
Enter your choice:3
3 <-top
9
5
***STACK DEMONSTRATION***
1.PUSH
2.POP
3.DISPLAY
4.EXIT
Enter your choice:2
Deleted item is: 3
***STACK DEMONSTRATION***
1.PUSH
2.POP
3.DISPLAY
4.EXIT
Enter your choice:2
Deleted item is: 9
***STACK DEMONSTRATION***
1.PUSH
2.POP
3.DISPLAY
4.EXIT
Enter your choice:4
THANK YOU

Result:

PRACTICAL -17
MYSQL-1
Write and Execute the SQL command for the following
Aim: To Understand the use of DDL and DML commands.
1. Create and open Database named MYORG
mysql> create database MYORG;
mysql> use MYORG;
Database changed

2. Create table Emp as per following Table structure.


EmpId EmpName Designation DOJ Sal Comm
Int Varchar(20) Varchar(20) Date int Int
Primary key Not null Check>1000

mysql> create table Emp (EmpId int primary key,EmpName varchar(20) not null,
Designation varchar(20),DOJ date,Sal int check(Sal>1000),Comm int);

3. Insert 5 records with relevant information in the Emp table.

mysql> insert into Emp values(8369,'SMITH','CLERK','1990-12-18',800,null);


mysql> insert into Emp values(8499,'ANYA','SALESMAN','1991-02-20',1600,300);
mysql> insert into Emp values(8521,'SETH','SALESMAN','1991-02-22',1250,500);
mysql> insert into Emp values(8566,'MAHADEVAN','MANAGER','1991-04-
02',2985,null);
mysql> insert into Emp values(8654,'MOMIN','SALESMAN','1991-09-
28',1250,400);
mysql> insert into Emp values(8698,'BINA','MANAGER','1991-05-01',2850,NULL);
mysql> insert into Emp values(8882,'SHIVANSH','MANAGER','1991-06-
09',2450,NULL);
mysql> insert into Emp values(8888,'SCOTT','ANALYST','1992-12-09',3000,NULL);
mysql> insert into Emp values(8839,'AMIR','PRESIDENT','1991-11-
18',5000,NULL);
mysql> insert into Emp values(8844,'KULDEEP','SALESMAN','1991-09-08',1500,0);

mysql> SELECT * FROM EMP;


+-------+-----------+-------------+------------+------+------+
| EmpId | EmpName | Designation | DOJ | Sal | Comm |
+-------+-----------+-------------+------------+------+------+
| 8369 | SMITH | CLERK | 1990-12-18 | 800 | NULL |
| 8499 | ANYA | SALESMAN | 1991-02-20 | 1600 | 300 |
| 8521 | SETH | SALESMAN | 1991-02-22 | 1250 | 500 |
| 8566 | MAHADEVAN | MANAGER | 1991-04-02 | 2985 | NULL |
| 8654 | MOMIN | SALESMAN | 1991-09-28 | 1250 | 400 |
| 8698 | BINA | MANAGER | 1991-05-01 | 2850 | NULL |
| 8839 | AMIR | PRESIDENT | 1991-11-18 | 5000 | NULL |
| 8844 | KULDEEP | SALESMAN | 1991-09-08 | 1500 | 0 |
| 8882 | SHIVANSH | MANAGER | 1991-06-09 | 2450 | NULL |
| 8888 | SCOTT | ANALYST | 1992-12-09 | 3000 | NULL |
+-------+-----------+-------------+------------+------+------+
10 rows in set (0.00 sec)

4. Update all the records as add ‘Mr.’ with EmpName.


mysql> update Emp set EmpName=concat('MR.',EmpName);

5. Add one column Email of data type VARCHAR and size 30 to table Emp.
mysql> alter table Emp add Email varchar(20);

6. Drop the column Email from table Customer.


mysql> Alter table Emp drop Email;

7. Modify the column EmpName as change the size 40 characters long.


mysql> alter table Emp add Email varchar(20);
mysql> desc Emp;
+-------------+-------------+------+-----+---------+-------+
| Field | Type | Null | Key | Default | Extra |
+-------------+-------------+------+-----+---------+-------+
| EmpName | varchar(40) | YES | | NULL | |
+-------------+-------------+------+-----+---------+-------+
6 rows in set (0.03 sec)
8. Write a query to display all the records with all the columns.
mysql> select * from Emp;
+-------+--------------+-------------+------------+------+------+
| EmpId | EmpName | Designation | DOJ | Sal | Comm |
+-------+--------------+-------------+------------+------+------+
| 8369 | MR.SMITH | CLERK | 1990-12-18 | 800 | NULL |
| 8499 | MR.ANYA | SALESMAN | 1991-02-20 | 1600 | 300 |
| 8521 | MR.SETH | SALESMAN | 1991-02-22 | 1250 | 500 |
| 8566 | MR.MAHADEVAN | MANAGER | 1991-04-02 | 2985 | NULL |
| 8654 | MR.MOMIN | SALESMAN | 1991-09-28 | 1250 | 400 |
| 8698 | MR.BINA | MANAGER | 1991-05-01 | 2850 | NULL |
| 8839 | MR.AMIR | PRESIDENT | 1991-11-18 | 5000 | NULL |
| 8844 | MR.KULDEEP | SALESMAN | 1991-09-08 | 1500 | 0 |
| 8882 | MR.SHIVANSH | MANAGER | 1991-06-09 | 2450 | NULL |
| 8888 | MR.SCOTT | ANALYST | 1992-12-09 | 3000 | NULL |
+-------+--------------+-------------+------------+------+------+
10 rows in set (0.00 sec)

9. Write a query to display EmpName and Sal of employees whose salary are greater than
or equal to 2200
mysql> select EmpName,Sal from Emp where sal>=2200;
+------------------+---------------+
| EmpName | Sal |
+-----------------+----------------+
| MR.MAHADEVAN | 2985 |
| MR.BINA | 2850 |
| MR.AMIR | 5000 |
| MR.SHIVANSH | 2450 |
| MR.SCOTT | 3000 |
+-------------------------+---------+

10. Write a query to display details of employs who are not getting commission.
mysql> select * from Emp where Comm is NULL;
+-------+--------------+-------------+------------+------+------+
| EmpId | EmpName | Designation | DOJ | Sal | Comm |
+-------+--------------+-------------+------------+------+------+
| 8369 | MR.SMITH | CLERK | 1990-12-18 | 800 | NULL |
| 8566 | MR.MAHADEVAN | MANAGER | 1991-04-02 | 2985 | NULL |
| 8698 | MR.BINA | MANAGER | 1991-05-01 | 2850 | NULL |
| 8839 | MR.AMIR | PRESIDENT | 1991-11-18 | 5000 | NULL |
| 8882 | MR.SHIVANSH | MANAGER | 1991-06-09 | 2450 | NULL |
| 8888 | MR.SCOTT | ANALYST | 1992-12-09 | 3000 | NULL |
+-------+--------------+-------------+------------+------+------+

11. Write a query to display employeename and salary of those employees who
don’t have their salary in range of 2500 to 4000.
mysql> select Empname, Sal From Emp Where Sal not between 2500 and 4000;
+-------------+------+
| empname | sal |
+-------------+------+
| MR.SMITH | 800 |
| MR.ANYA | 1600 |
| MR.SETH | 1250 |
| MR.MOMIN | 1250 |
| MR.AMIR | 5000 |
| MR.KULDEEP | 1500 |
| MR.SHIVANSH | 2450 |
+-------------+------+

12. Write a query to display the name of employee whose name contains “A” as
third alphabet in Ascending order of employee names.
mysql> select EmpName from Emp where EmpName like " __A%" order by
Empname;

13. Write a query to display the sum of salary and commission of employees as “Total
Incentive” who are getting Commission.
mysql> select sal+comm As "Total Incentive" From Emp where comm is not NULL;
+-----------------+
| Total Incentive |
+-----------------+
| 1900 |
| 1750 |
| 1650 |
| 1500 |
+-----------------+

14. Write a query to display details of employs with the text “Not given”, if
commission is null.
mysql> SELECT EmpID,EmpName,Designation,DOJ,Sal,'Not Given' AS 'Comm'
FROM EMP WHERE Comm IS NULL;
+-------+--------------+-------------+------------+------+-----------+
| EmpID | EmpName | Designation | DOJ | Sal | Comm |
+-------+--------------+-------------+------------+------+-----------+
| 8369 | MR.SMITH | CLERK | 1990-12-18 | 800 | Not Given |
| 8566 | MR.MAHADEVAN | MANAGER | 1991-04-02 | 2985 | Not Given |
| 8698 | MR.BINA | MANAGER | 1991-05-01 | 2850 | Not Given |
| 8839 | MR.AMIR | PRESIDENT | 1991-11-18 | 5000 | Not Given |
| 8882 | MR.SHIVANSH | MANAGER | 1991-06-09 | 2450 | Not Given |
| 8888 | MR.SCOTT | ANALYST | 1992-12-09 | 3000 | Not Given |
+-------+--------------+-------------+------------+------+-----------+
6 rows in set (0.00 sec)

15. Display the distinct job titles offered by the Organization.


mysql> select distinct designation From emp;
+-------------+
| designation |
+-------------+
| CLERK |
| SALESMAN |
| MANAGER |
| PRESIDENT |
| ANALYST |
+-------------+
5 rows in set (0.02 sec)
16. Display the Names of employees who are working as Manager or Analyst.

mysql> select EmpName from Emp where Designation='MANAGER' or


Designation='ANALYST';
+--------------+
| EmpName |
+--------------+
| MR.MAHADEVAN |
| MR.BINA |
| MR.SHIVANSH |
| MR.SCOTT |
+--------------+
4 rows in set (0.09 sec)

17. Display the names of employees who joined on or after 01/05/1991.


mysql> select * From emp Where year(DOJ)=1991;
+-------+--------------+-------------+------------+------+------+
| EmpId | EmpName | Designation | DOJ | Sal | Comm |
+-------+--------------+-------------+------------+------+------+
| 8499 | MR.ANYA | SALESMAN | 1991-02-20 | 1600 | 300 |
| 8521 | MR.SETH | SALESMAN | 1991-02-22 | 1250 | 500 |
| 8566 | MR.MAHADEVAN | MANAGER | 1991-04-02 | 2985 | NULL |
| 8654 | MR.MOMIN | SALESMAN | 1991-09-28 | 1250 | 400 |
| 8698 | MR.BINA | MANAGER | 1991-05-01 | 2850 | NULL |
| 8839 | MR.AMIR | PRESIDENT | 1991-11-18 | 5000 | NULL |
| 8844 | MR.KULDEEP | SALESMAN | 1991-09-08 | 1500 | 0 |
| 8882 | MR.SHIVANSH | MANAGER | 1991-06-09 | 2450 | NULL |
+-------+--------------+-------------+------------+------+------+
8 rows in set (0.04 sec)
18. Display the employee records in order by DOJ
mysql> select * from Emp order by DOJ;
+-------+--------------+-------------+------------+------+------+
| EmpId | EmpName | Designation | DOJ | Sal | Comm |
+-------+--------------+-------------+------------+------+------+
| 8369 | MR.SMITH | CLERK | 1990-12-18 | 800 | NULL |
| 8499 | MR.ANYA | SALESMAN | 1991-02-20 | 1600 | 300 |
| 8521 | MR.SETH | SALESMAN | 1991-02-22 | 1250 | 500 |
| 8566 | MR.MAHADEVAN | MANAGER | 1991-04-02 | 2985 | NULL |
| 8698 | MR.BINA | MANAGER | 1991-05-01 | 2850 | NULL |
| 8882 | MR.SHIVANSH | MANAGER | 1991-06-09 | 2450 | NULL |
| 8844 | MR.KULDEEP | SALESMAN | 1991-09-08 | 1500 | 0 |
| 8654 | MR.MOMIN | SALESMAN | 1991-09-28 | 1250 | 400 |
| 8839 | MR.AMIR | PRESIDENT | 1991-11-18 | 5000 | NULL |
| 8888 | MR.SCOTT | ANALYST | 1992-12-09 | 3000 | NULL |
+-------+--------------+-------------+------------+------+------+
10 rows in set (0.06 sec)

19. Display the Distinct Designation in the Organisation


mysql> select distinct designation from Emp;
+-------------+
| designation |
+-------------+
| CLERK |
| SALESMAN |
| MANAGER |
| PRESIDENT |
| ANALYST |
+-------------+
5 rows in set (0.04 sec)

Ex No: 18 MYSQL-2 Date:

Write and Execute the SQL command for the following


Aim: To Understand the use of DDL and DML commands.

1.Create the following Table DEPT with DeptID as Primary Key.


DeptID DeptName MgrId Location
Int Varchar(20) Int Varchar(20)

mysql> create table DEPT (DeptId int,DeptName varchar(20),MgrId int,Location


varchar(20));
Query OK, 0 rows affected (0.09 sec)

2.Insert the following record in the DEPT Table.


mysql> insert into DEPT values(10,'SALES',8566,'MUMBAI');
mysql> insert into DEPT values(20,'PERSONEL',8698,'DELHI');
mysql> insert into DEPT values(30,'ACCOUNTS',8882,'DELHI');
mysql> insert into DEPT values(40,'RESEARCH',8839,'BANGALORE');
mysql> SELECT * FROM DEPT;
+--------+----------+-------+-----------+
| DeptId | DeptName | MgrId | Location |
+--------+----------+-------+-----------+
| 10 | SALES | 8566 | MUMBAI |
| 20 | PERSONEL | 8698 | DELHI |
| 30 | ACCOUNTS | 8882 | DELHI |
| 40 | RESEARCH | 8839 | BANGALORE |
+--------+----------+-------+-----------+
4 rows in set (0.00 sec)

3.Alter the table EMP as Add a column DeptID (Number)


mysql> ALTER TABLE EMP ADD DEPTID INT;

4.Show the minimum, maximum and average salary of Managers.


mysql> select min(sal), max(sal), avg(sal) From emp Where designation="Manager";
+----------+----------+-----------+
| min(sal) | max(sal) | avg(sal) |
+----------+----------+-----------+
| 2450 | 2985 | 2761.6667 |
+----------+----------+-----------+
1 row in set (0.09 sec)

5.Display the Designation wise list of employees with name, Sal and Date of Joining.

mysql> SELECT EmpName,Designation,Sal,DOJ as 'DateOfJoining' FROM EMP ORDER BY


Designation;
+--------------+-------------+------+---------------+
| EmpName | Designation | Sal | DateOfJoining |
+--------------+-------------+------+---------------+
| MR.SCOTT | ANALYST | 3000 | 1992-12-09 |
| MR.SMITH | CLERK | 800 | 1990-12-18 |
| MR.MAHADEVAN | MANAGER | 2985 | 1991-04-02 |
| MR.SHIVANSH | MANAGER | 2450 | 1991-06-09 |
| MR.BINA | MANAGER | 2850 | 1991-05-01 |
| MR.AMIR | PRESIDENT | 5000 | 1991-11-18 |
| MR.SETH | SALESMAN | 1250 | 1991-02-22 |
| MR.MOMIN | SALESMAN | 1250 | 1991-09-28 |
| MR.ANYA | SALESMAN | 1600 | 1991-02-20 |
| MR.KULDEEP | SALESMAN | 1500 | 1991-09-08 |
+--------------+-------------+------+---------------+
10 rows in set (0.03 sec)
6.Show the average salary for all departments with more than 5 working people.
mysql> select avg(sal) From emp Group by deptid Having count(*)>5;
Empty set (0.06 sec)

7.List the count of Employees grouped by DeptID.


mysql> select DeptId,count(*) from emp group by DeptId;
+--------+----------+
| DeptId | count(*) |
+--------+----------+
| 10 | 3|
| 20 | 4|
| 30 | 3|
+--------+----------+
3 rows in set (0.00 sec)

8.Set the commission as 100 who are not getting any commission.

mysql> update emp set comm=100 where comm is null;

9.Delete all the records who is working as “Salesman” and salary more than 1500.
mysql> delete from emp where Designation='SALESMAN' and sal>1500;
Query OK, 1 row affected (0.02 sec)

10.Drop the emp table.


mysql> drop table emp;

11.Write a command to return the position of the first occurrence of substring.


mysql> select instr('INFORMATICS','FOR');
+----------------------------+
| instr('INFORMATICS','FOR') |
+----------------------------+
| 3|
+----------------------------+
1 row in set (0.02 sec)

12.Write the command to round off value 15.93 to nearest ten’s i.e. 20.
mysql> SELECT ROUND(15.93,0);
+----------------+
| ROUND(15.93,0) |
+----------------+
| 16 |
+----------------+
1 row in set (0.00 sec)
13. Write the command to return the substring from the main string.

mysql> select substr('INFORMATICS',3,6);


+---------------------------+
| substr('INFORMATICS',3,6) |
+---------------------------+
| FORMAT |
+---------------------------+
1 row in set (0.00 sec)
14.Write a query to find out the result of 63.
mysql> select pow(6,3);
+----------+
| pow(6,3) |
+----------+
| 216 |
+----------+
1 row in set (0.07 sec)

15.Write command to print the day of the week of your birthday in the year 2019.
mysql> select dayname('2019-08-11');
+-----------------------+
| dayname('2019-08-11') |
+-----------------------+
| Sunday |
+-----------------------+
1 row in set (0.03 sec)

Ex No: 19 MYSQL JOINS Date:

Write and Execute the SQL command for the following


Aim: To Understand the use of joins in SQL.

1.Display the maximum salary of employees in each Department.

mysql> SELECT MAX(SAL),DESIGNATION FROM EMP GROUP BY DESIGNATION;


+----------+-------------+
| MAX(SAL) | DESIGNATION |
+----------+-------------+
| 3000 | ANALYST |
| 800 | CLERK |
| 2985 | MANAGER |
| 5000 | PRESIDENT |
| 1600 | SALESMAN |
+----------+-------------+
5 rows in set (0.04 sec)

2.Display the name of Employees along with their Designation and Department Name.
mysql> select EmpName,Designation,DeptName from Emp,Dept where
Emp.DeptId=Dept.DeptId;
+--------------+-------------+----------+
| EmpName | Designation | DeptName |
+--------------+-------------+----------+
| MR.SMITH | CLERK | SALES |
| MR.ANYA | SALESMAN | PERSONEL |
| MR.SETH | SALESMAN | PERSONEL |
| MR.MAHADEVAN | MANAGER | ACCOUNTS |
| MR.MOMIN | SALESMAN | PERSONEL |
| MR.BINA | MANAGER | ACCOUNTS |
| MR.AMIR | PRESIDENT | PERSONEL |
| MR.KULDEEP | SALESMAN | ACCOUNTS |
| MR.SHIVANSH | MANAGER | SALES |
| MR.SCOTT | ANALYST | SALES |
+--------------+-------------+----------+
10 rows in set (0.00 sec)

3.Count the number of Employees working in ACCOUNTS department.


mysql> select count(*) from emp,dept where deptname='accounts' and
emp.empid=dept.mgrid;
+----------+
| count(*) |
+----------+
| 1|
+----------+
1 row in set (0.02 sec)

4.Display the name of Employees who is managing SALES department.


mysql> select empname From emp, dept Where deptName="SALES" and
emp.DeptID=dept.DeptID;
+-------------+
| empname |
+-------------+
| MR.SMITH |
| MR.SHIVANSH |
| MR.SCOTT |
+-------------+
3 rows in set (0.00 sec)

5.Display the name of employees who are working in Delhi .


mysql> select empname From emp, dept Where location="DELHI" and
emp.DeptID=dept.DeptID;
+--------------+
| empname |
+--------------+
| MR.ANYA |
| MR.SETH |
| MR.MAHADEVAN |
| MR.MOMIN |
| MR.BINA |
| MR.AMIR |
| MR.KULDEEP |
+--------------+
7 rows in set (0.00 sec)
PRACTICAL 20
Mysql connectivity-creating

AIM- To Write a program to Integrate SQL with Python by creating the database and table.

Codings:

import mysql.connector
mydb=mysql.connector.connect(host="localhost",user="root",passwd="likis")
mycursor=mydb.cursor()
mycursor.execute("CREATE DATABASE School")

mycursor.execute("show databases")
for x in mycursor:
print(x)

mycursor.execute("use School")
mycursor.execute("create table student (RollNo int(5) Primary key, Name char(15))")
print("Table created")

PRACTICAL 21
Mysql connectivity-Inserting and Displaying

AIM- To Write a program to Integrate SQL with Python by inserting and displaying the
records.

Codings:
import mysql.connector
mydb=mysql.connector.connect(host="localhost",user="root",passwd="likis",database="School"
)
mycursor=mydb.cursor()

#TO INSERT RECORDS

mycursor.execute("Insert into Student values(101,'Bala')")


mycursor.execute("Insert into Student values(102,'Kaliz')")
mycursor.execute("Insert into Student values(103,'Roshan')")
mycursor.execute("Insert into Student values(104,'Banu')")
mycursor.execute("Insert into Student values(105,'Mahesh')")
mydb.commit()
print(mycursor.rowcount,"Record inserted")
#TO DISPLAY RECORDS

mycursor.execute("Select * from Student")


myresult=mycursor.fetchall()
for x in myresult:
print(x)

PRACTICAL 22
Mysql connectivity- Searching and Updating

AIM- To Write a program to Integrate SQL with Python by searching the record and updating it.
Codings:
import mysql.connector
mydb=mysql.connector.connect(host="localhost",user="root",passwd="likis",database="School
")
mycursor=mydb.cursor()

#To display records


print("Before updating record")
mycursor.execute("Select * from Student")
myresult=mycursor.fetchall()
for x in myresult:
print(x)

#To update records

mycursor.execute("Update Student set name='Mahesh' where RollNo=105")


mydb.commit()
print(mycursor.rowcount,"record(s) affected")

#To display records

print("After updating record")


mycursor.execute("Select * from Student")
myresult=mycursor.fetchall()
for x in myresult:
print(x)

PRACTICAL 23
Mysql connectivity-Searching and Deleting

AIM- To Write a program to Integrate SQL with Python by searching the record and deleting it.
Codings:

import mysql.connector
mydb=mysql.connector.connect(host="localhost",user="root",passwd="likis",database="School
")
mycursor=mydb.cursor()

#To display records


print("Before deleting record")
mycursor.execute("Select * from Student")
myresult=mycursor.fetchall()
for x in myresult:
print(x)

#To delete records


mycursor.execute("Delete from Student where RollNo=102")
mydb.commit()
print(mycursor.rowcount,"record(s) deleted")

#To display records


print("After deleting record")
mycursor.execute("Select * from Student")
myresult=mycursor.fetchall()
for x in myresult:
print(x)

PRACTICAL 24
Random number generator

Aim:
Write a random number generator that generates random numbers between 1 and 6
(simulates a dice).

Codings:

import random 
def roll_dice():
  print (random.randint(1, 6)) 
print("""Welcome to my python random dice program!
To start press enter! Whenever you are over, type quit.""")
flag = True
while flag:
   user_prompt = input(">")
   if user_prompt.lower() == "quit":
      flag = False
   else:
     print("Rolling dice...\nYour number is:") 
     roll_dice()

PRACTICAL- 25
Most commonly occurring words

AIM- Write a program to Take a sample of ten phishing e-mails (or any
text file) and find most commonly occurring word(s)

Codings:

file =open("Email.txt","r")
content=file.read()
max=0
max_occuring_word=""
occurances_dict={}
words=content.split()
for word in words:
count=content.count(word)
occurances_dict.update({word:count})
if (count>max):
max=count
max_occur_word= word
print("Most occuring word is:", max_occur_word)
print("No. of times it is occuring:", max)
print(occurances_dict)

You might also like