XII Practical File (SQL)
XII Practical File (SQL)
SQL Command: -
2.Write an SQL command to create a STUDENT TABLE with the following attributes and
fields.
TABLE: STUDENT
mysql> Create table student (stud_id int primary key, stud_name varchar(20), class
char(2), sec char(2),stream varchar(10));
3.Write an SQL command to display the structure of the table named student.
mysql> Desc student; (or) Describe student;
OUTPUT:
mysql> insert into student values (1, “Ajay”, “12”, “A”,” Science”);
mysql> insert into student values (2, ”Siva”,”12”,”B”,”Science”);
1
OUTPUT:
OUTPUT:
2
MYSQL EXERCISE # 2
Aim: - To create an employee table and execute the following queries.
Table: Employee
Table: Department
1.Write an SQL query to fetch unique values of city from Employee table.
mysql> Select DISTINCT city from Employee;
OUTPUT:
2. Write an SQL query to print all Employee details from the Employee table order by
Employee Name Ascending.
mysql> Select * from Employee order by emp_name;
OUTPUT:
3
3. Write an SQL query to print details for department name with the dept_name as “HR”
and “Admin” from department table.
mysql> Select * from department where dept_name in (“HR”,”Admin”);
OUTPUT:
5. Write an SQL query to print details of the Employee whose Name ends with “a”.
mysql> Select emp_name from employee where emp_name like ‘%a’;
OUTPUT:
6. Write an SQL query to print details of workers whose Name contains four characters.
mysql> Select * from Employee where emp_name like ‘____’;
OUTPUT:
4
7. Write an SQL query to print details of Employee whose salary lies between 45000 and
50000.
mysql> Select * from employee where salary between 45000 and 50000;
OUTPUT:
8. Write an SQL query to print details of Employee name and their department name.
mysql> Select emp_name, dept_name from employee e, department d where e.emp_id =
d.emp_id;
OUTPUT:
5
MYSQL EXERCISE # 3
Aim: - To create a stock table and execute the following queries.
Table: STOCK
1.Compute the number of products with a price larger than or equal to 20.
mysql> Select count(*) from stock where price>=20;
OUTPUT:
2. Select the name and price of all products with a price is less than 50 and sort price by
descending order;
mysql> select stock_name, price from stock where price<50 order by price desc;
OUTPUT:
6
4. Apply a 5% discount to all products with a price larger than 25;
mysql> select stock_name, price, price-price*5/100 as 'Discount price' from stock where
price>25;
OUTPUT:
7. To display the details of all products whose stock is purchased January month.
mysql> select * from stock where dob>="2024-01-01" and dob<="2024-01-31";
OUTPUT:
8. Write an SQL query to delete the record whose stock price is 10.
mysql> delete from stock where price=10;
7
MYSQL EXERCISE # 4
Aim: - To create a book table and execute the following queries.
Table: BOOK
Table: AUTHOR
2. Write an SQL query to display the details of book name and author name of English
book.
mysql> select book_name, author_name from book b, author a where
b.book_id=a.book_id and book_name="eng";
OUTPUT:
8
3. Write an SQL query to add a primary key of book table.
mysql> alter table book add primary key (book_id);
OUTPUT:
4. Write an SQL query to display the book name and author name whose price is more
than 250 and quantity in not null;
mysql> select book_name, author_name from book b, author a where
b.book_id=a.book_id and price>250 and qty is not null;
OUTPUT:
5. Write an SQL query to display the sum of all price from book table.
mysql> select sum(price) from book;
OUTPUT:
6. Write an SQL query to display the details of book whose price is null;
mysql> select * from book where price is null;
OUTPUT:
9
7. Write an SQL query to display the total price without repetition and null.
mysql> select count(distinct price) from book;
10
PYTHON – MySQL Connectivity#1
connect_with_mysql()
OUTPUT: -
11
PYTHON – MySQL Connectivity#2
Aim: - Write a program to display all record from table‘student’ using MySQL
database connectivity in python.
Source Code: -
import mysql.connector
mydb=mysql.connector.connect(host="localhost",user="root",password="Vvsn@123"
,database="student")
mycursor=mydb.cursor()
mycursor.execute("Select * from classA")
mydata=mycursor.fetchall()
for i in mydata:
print(i)
print("The total number of rows are fetched:", mycursor.rowcount)
Output:-
(101,'Ajay', (2000, 12, 22),'Anna Nagar', 'Madurai')
12
PYTHON – MySQL Connectivity#3
Aim: - Write a program to display one record from table‘student’ using MySQL
database connectivity in python.
Source Code:-
import mysql.connector
mydb=mysql.connector.connect(host="localhost",user="root",password="Vvsn@123"
,database="student")
mycursor=mydb.cursor()
mycursor.execute("Select * from classA")
mydata=mycursor.fetchone()
print(mydata)
print("The total number of rows are fetched:", mycursor.rowcount)
Output:-
13
PYTHON – MySQL Connectivity#4
Aim: - Write a program to display number of record from table ‘student’ using
MySQL database connectivity in python.
Source Code: -
import mysql.connector
mydb=mysql.connector.connect(host="localhost",user="root",password="Vvsn@123"
,database="student") mycursor=mydb.cursor()
mycursor.execute("Select * from classA")
mydata=mycursor.fetchmany(3)
for i in mydata:
print(i)
print("The total number of rows are fetched:", mycursor.rowcount)
Output:-
(101,'Ajay', (2000, 12, 22),'Anna Nagar', 'Madurai')
(102,'Siva', (2001, 10, 20),'Rajaji Road', 'Chennai')
(103,'Sanjay', (2000, 9, 2),'JJ Colony', 'Covai')
The total numbers of rows are fetched:3
14
PYTHON - MySQL Connectivity#5
import mysql.connector
mydb=mysql.connector.connect(host="localhost",user="root",password="Vvsn
@123",database="school")
mycursor=mydb.cursor()
15