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

SQL Term 2 Comp Practical

The document contains examples of SQL queries performed on a students database table using MySQL. It demonstrates various SQL operations like selecting, filtering, sorting, grouping, joining multiple tables, and modifying the table structure. Python code examples are also provided to connect to the database and perform CRUD (create, read, update, delete) operations on the students table programmatically. The key operations shown include selecting all records, filtering by criteria, adding/deleting/updating a record, and retrieving data from joined tables.

Uploaded by

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

SQL Term 2 Comp Practical

The document contains examples of SQL queries performed on a students database table using MySQL. It demonstrates various SQL operations like selecting, filtering, sorting, grouping, joining multiple tables, and modifying the table structure. Python code examples are also provided to connect to the database and perform CRUD (create, read, update, delete) operations on the students table programmatically. The key operations shown include selecting all records, filtering by criteria, adding/deleting/updating a record, and retrieving data from joined tables.

Uploaded by

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

Q.

9
Display maximum marks
mysql> select max(marks) from students;
+------------+
| max(marks) |
+------------+
| 110 |
+------------+

Q.10
Display minimum marks
mysql> select min(marks) from students;
+------------+
| min(marks) |
+------------+
| 80 |
+------------+

Q.11
Count number of records
mysql> select count(rno) from students;
+------------+
| count(rno) |
+------------+
| 6|
+------------+

Q.12
Display sum of marks
mysql> select sum(marks) from students;
+------------+
| sum(marks) |
+------------+
| 560 |
+------------+

Q.13
Display average of marks
mysql> select avg(marks) from students;
+------------+
| avg(marks) |
+------------+
| 93.3333 |
+------------+

Q.14
Add a column for admission number
mysql> alter table students add admno varchar(10);
Query OK, 0 rows affected (0.03 sec)
Records: 0 Duplicates: 0 Warnings: 0

mysql> select * from students;


+-----+-----------+-------+------+-------+
| rno | name | marks | cl | admno |
+-----+-----------+-------+------+-------+
| 1 | Shreyas | 100 | 12c | NULL |
| 2 | Zaid | 100 | 12c | NULL |
| 3 | Tashu | 100 | 12c | NULL |
| 4 | Taksh | 90 | 12c | NULL |
| 5 | Sheeru | 90 | 12c | NULL |
| 6 | Parth | 80 | 12c | NULL |
+-----+-----------+-------+------+-------+

Q.15
Update the value in the admno column
mysql> update students set admno= 10000;
Query OK, 6 rows affected (0.00 sec)
Rows matched: 6 Changed: 6 Warnings: 0

mysql> select * from students;


+-----+-----------+-------+------+-------+
| rno | name | marks | cl | admno |
+-----+-----------+-------+------+-------+
| 1 | Shreyas | 100 | 12c | 10000 |
| 2 | Zaid | 100 | 12c | 10000 |
| 3 | Taksh | 100 | 12c | 10000 |
| 4 | Tashu | 90 | 12c | 10000 |
| 5 | Sheeru | 90 | 12c | 10000 |
| 6 | Parth | 80 | 12c | 10000 |
+-----+-----------+-------+------+-------+

Q.16
Modify admno column datatype to int
mysql> alter table students modify column admno int(10);
Query OK, 6 rows affected, 1 warning (0.05 sec)
Records: 6 Duplicates: 0 Warnings: 1

mysql> select * from students;


+-----+-----------+-------+------+-------+
| rno | name | marks | cl | admno |
+-----+-----------+-------+------+-------+
| 1 | Shreyas | 100 | 12c | 10000 |
| 2 | Zaid | 100 | 12c | 10000 |
| 3 | Taksh | 100 | 12c | 10000 |
| 4 | Tashu | 90 | 12c | 10000 |
| 5 | Sheeru | 90 | 12c | 10000 |
| 6 | Parth | 80 | 12c | 10000 |
+-----+-----------+-------+------+-------+

Q.17
Drop column admno
mysql> alter table students drop column admno;
Query OK, 0 rows affected (0.01 sec)
Records: 0 Duplicates: 0 Warnings: 0

mysql> select * from students;


+-----+-----------+-------+------+
| rno | name | marks | cl |
+-----+-----------+-------+------+
| 1 | Shreyas | 100 | 12c |
| 2 | Zaid | 100 | 12c |
| 3 | Taksh | 100 | 12c |
| 4 | Tashu | 90 | 12c |
| 5 | Sheeru | 90 | 12c |
| 6 | Parth | 80 | 12c |
+-----+-----------+-------+------+

Q.18
Display all contents in increasing order of marks
mysql> select * from students order by marks;
+-----+-----------+-------+------+
| rno | name | marks | cl |
+-----+-----------+-------+------+
| 6 | Parth | 80 | 12c |
| 4 | Tashu | 90 | 12c |
| 5 | Sheeru | 90 | 12c |
| 1 | Shreyas | 100 | 12c |
| 2 | Zaid | 100 | 12c |
| 3 | Taksh | 100 | 12c |

Q.19
Display all contents in decreasing order of marks
mysql> select * from students order by marks desc;
+-----+-----------+-------+------+
| rno | name | marks | cl |
+-----+-----------+-------+------+
| 1 | Shreyas | 100 | 12c |
| 2 | Zaid | 100 | 12c |
| 3 | Taksh | 100 | 12c |
| 4 | Tashu | 90 | 12c |
| 5 | Sheeru | 90 | 12c |
| 6 | Parth | 80 | 12c |
+-----+-----------+-------+------+

Q.20
Display all contents grouped by marks
mysql> select marks, count(marks) from students group by marks;
+-------+--------------+
| marks | count(marks) |
+-------+--------------+
| 100 | 3|
| 90 | 2|
| 80 | 1|
+-------+--------------+

Q.21
Display all contents in increasing order of marks
mysql> select * from students order by marks;
+-----+-----------+-------+------+
| rno | name | marks | cl |
+-----+-----------+-------+------+
| 6 | Parth | 80 | 12c |
| 4 | Tashu | 90 | 12c |
| 5 | Sheeru | 90 | 12c |
| 1 | Shreyas | 100 | 12c |
| 2 | Zaid | 100 | 12c |
| 3 | Taksh | 100 | 12c |
+-----+-----------+-------+------+

Q.22
Natural Join two tables
mysql> select * from students natural join g3;
+-----+----------+-------+------+-----------+
| rno | name | marks | cl | post |
+-----+----------+-------+------+-----------+
| 5 | Sheeru | 90 | 12c | secretary |
| 4 | Tashu | 90 | 12c | Prezz |

Q.23
Equi join two tables
mysql> select * from students, g3 where students.rno=g3.rno;
+-----+----------+-------+------+-----------+----------+------+
| rno | name | marks | cl | post | name | rno |
+-----+----------+-------+------+-----------+----------+------+
| 5 | Sheeru | 90 | 12c | secretary | Sheeru | 5 |
| 4 | Tashu | 90 | 12c | Prezz | Tashu | 4 |
+-----+----------+-------+------+-----------+----------+------+

Q.24
Retrieval of data from multiple tables(2 tables)
mysql> select students.rno,students.name,students.marks,students.cl, g3.post from students, g3
where students.rno=g3.rno;
+-----+----------+-------+------+-----------+
| rno | name | marks | cl | post |
+-----+----------+-------+------+-----------+
| 5 | Sheeru | 90 | 12c | secretary |
| 4 | Tashu | 90 | 12c | Prezz |
+-----+----------+-------+------+-----------+

Q.25
Cartesian Product of two tables
mysql> select * from students, g3;
+-----+-----------+-------+------+-----------+----------+------+
| rno | name | marks | cl | post | name | rno |
+-----+-----------+-------+------+-----------+----------+------+
| 1 | Shreyas | 100 | 12c | Prezz | Tashu | 4 |
| 1 | Shreyas | 100 | 12c | secretary | Sheeru | 5 |
| 2 | Zaid | 100 | 12c | Prezz | Tashu | 4 |
| 2 | Zaid | 100 | 12c | secretary | Sheeru | 5 |
| 3 | Taksh | 100 | 12c | Prezz | Tashu | 4 |
| 3 | Taksh | 100 | 12c | secretary | Sheeru | 5 |
| 4 | Tashu | 90 | 12c | Prezz | Tashu | 4 |
| 4 | Tashu | 90 | 12c | secretary | Sheeru | 5 |
| 5 | Sheeru | 90 | 12c | Prezz | Tashu | 4 |
| 5 | Sheeru | 90 | 12c | secretary | Sheeru | 5 |
| 6 | Parth | 80 | 12c | Prezz | Tashu | 4 |
| 6 | Parth | 80 | 12c | secretary | Sheeru | 5 |
+-----+-----------+-------+------+-----------+----------+------+

Q Display all records in sql table using python


INPUT
import mysql.connector as sqlcon
mycon=sqlcon.connect(host='localhost',user='Naksh',password='Naksh',database='n
aksh')
if mycon.is_connected():
print("Connection Established")
else:
print("Connection Errors! Kindly check!!!")
cursor=mycon.cursor()
cursor.execute('select * from students;')
data= cursor.fetchall()
print(data)
mycon.close()

OUTPUT
Connection Established
[(1, 'Shreyas', 100, '12c'), (2, 'Zaid', 100, '12c'), (3, 'Taksh', 100, '12c'), (4, 'Tashu', 90, '12c'),
(5, 'Sheeru', 90, '12c'), (6, 'Parth', 80, '12c')]

Q Display all records of students with marks greater than 90 using python
INPUT
import mysql.connector as sqlcon
mycon=sqlcon.connect(host='localhost',user='Naksh',password='Naksh',database='n
aksh')
if mycon.is_connected():
print("Connection Established")
else:
print("Connection Errors! Kindly check!!!")
cursor=mycon.cursor()
cursor.execute('select * from students where marks>90;')
data= cursor.fetchall()
print(data)
mycon.close()

OUTPUT
Connection Established
[(1, 'Shreyas', 100, '12c'), (2, 'Zaid', 100, '12c'), (3, 'Taksh', 100, '12c')]

Q Add a record in sql table using python


INPUT
import mysql.connector as sqlcon
mycon=sqlcon.connect(host='localhost',user='Naksh',password='Naksh',database='n
aksh')
if mycon.is_connected():
print("Connection Established")
else:
print("Connection Errors! Kindly check!!!")
cursor=mycon.cursor()
rn=7
nm='advaith'
m=100
cl='12c'
values=(rn,nm,m,cl)
query='insert into students values (%s,%s,%s,%s)'
cursor.execute(query,values)
mycon.commit()
cursor.execute('select * from students;')
data= cursor.fetchall()
print(data)
mycon.close()

OUTPUT
Connection Established
[(1, 'Shreyas', 100, '12c'), (2, 'Zaid', 100, '12c'), (3, 'Taksh', 100, '12c'), (4, 'Tashu', 90, '12c'),
(5, 'Sheeru', 90, '12c'), (6, 'Parth', 80, '12c'), (7, 'advaith', 100, '12c')]

Q Delete a record in sql table using python


INPUT
import mysql.connector as sqlcon
mycon=sqlcon.connect(host='localhost',user='Naksh',password='Naksh',database='naksh')
if mycon.is_connected():
print("Connection Established")
else:
print("Connection Errors! Kindly check!!!")
cursor=mycon.cursor()
query="DELETE FROM students WHERE name like 'a%'"
cursor.execute(query)
mycon.commit()
cursor.execute('select * from students;')
data= cursor.fetchall()
print(data)
mycon.close()

OUTPUT
Connection Established
[(1, 'Shreyas', 100, '12c'), (2, 'Zaid', 100, '12c'), (3, 'Taksh', 100, '12c'), (4, 'Tashu', 90, '12c'),
(5, 'Sheeru', 90, '12c'), (6, 'Parth', 80, '12c')]

Q Update a record in sql table using python


INPUT
import mysql.connector as sqlcon
mycon=sqlcon.connect(host='localhost',user='Naksh',password='Naksh',database='n
aksh')
if mycon.is_connected():
print("Connection Established")
else:
print("Connection Errors! Kindly check!!!")
cursor=mycon.cursor()
query="update students set name='huzayfa' where name like 'l%'"
cursor.execute(query)
mycon.commit()
cursor.execute('select * from students;')
data= cursor.fetchall()
print(data)
mycon.close()

OUTPUT
Connection Established
[(1, 'Shreyas', 100, '12c'), (2, 'Zaid', 100, '12c'), (3, 'Taksh', 100, '12c'), (4, 'Tashu', 90, '12c'),
(5, 'huzayfa', 90, '12c'), (6, 'Parth', 80, '12c')]

You might also like