0% found this document useful (0 votes)
98 views17 pages

Python SQL Data Science Lab Assignment

psds practical

Uploaded by

keyeh65547
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)
98 views17 pages

Python SQL Data Science Lab Assignment

psds practical

Uploaded by

keyeh65547
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

Subject code:BTEA19621 Subject Name: Python and Sql for Data Science

Enrollment No: ET21BTCO082 Name: Heta Vimalesh Patel Date:15 /02/24

Lab Assignment 5

● Students:
Columns: student_id (Primary Key), name, age, gender
● Courses:
Columns: course_id (Primary Key), course_name, instructor
● Enrollments:
Columns: enrollment_id (Primary Key), student_id (Foreign Key references Students table),
course_id (Foreign Key references Courses table), grade

SCET/CO/2023-24/EVEN/BTECH Div-II/Sem-VI Page No:


Subject code:BTEA19621 Subject Name: Python and Sql for Data Science
Enrollment No: ET21BTCO082 Name: Heta Vimalesh Patel Date:15 /02/24

● STUDENTS TABLE
import [Link]

mydb = [Link](
host="localhost",
user="root",
password="student",
database="CO_82"
)

mycursor = [Link]()

create_table_query = """CREATE TABLE IF NOT EXISTS STUDENTS(


student_id INT AUTO_INCREMENT PRIMARY KEY,
name VARCHAR(30),
age INT,
gender VARCHAR(5)
)"""

insert_data_query = """INSERT INTO Students(name, age, gender) VALUES (%s,


%s, %s)"""
data = [('Alice',20,'F'),
('Bob',22,'M'),
('Charlie',21,'M'),
('Diana',23,'F')]
display_data_query = """SELECT * FROM STUDENTS"""

try:
[Link](create_table_query)
[Link]()
print("Table created successfully.")
[Link](insert_data_query, data)
[Link]()
print("\n",[Link], "record(s) inserted.")
[Link](display_data_query)
result = [Link]()
print("\nData in table 'Students' :\n")
for row in result:
print(row)

SCET/CO/2023-24/EVEN/BTECH Div-II/Sem-VI Page No:


Subject code:BTEA19621 Subject Name: Python and Sql for Data Science
Enrollment No: ET21BTCO082 Name: Heta Vimalesh Patel Date:15 /02/24

except [Link] as err:


print("Error:", err)

finally:
if mydb.is_connected():
[Link]()
[Link]()
print("\nMySQL connection closed.")

Output:-

● COURSES TABLE
import [Link]

mydb = [Link](
host="[Link]",
user="root",
password="student",
database="CO_82"
)

create_table_query = """
CREATE TABLE IF NOT EXISTS courses (
course_id INT PRIMARY KEY,
couse_name VARCHAR(255),
instructor VARCHAR(255)
)
"""

insert_data_query = """
INSERT INTO courses (course_id, course_name, instructor ) VALUES (%s, %s, %s)
"""

SCET/CO/2023-24/EVEN/BTECH Div-II/Sem-VI Page No:


Subject code:BTEA19621 Subject Name: Python and Sql for Data Science
Enrollment No: ET21BTCO082 Name: Heta Vimalesh Patel Date:15 /02/24

data_to_insert = [
(101, 'Math', '[Link]'),
(102, 'Physics','[Link]'),
(103, 'Chemistry', '[Link]'),
]

display_data_query = """
SELECT * FROM courses
"""

try:
cursor = [Link]()
[Link](create_table_query)
[Link]()
print("Table created successfully.")

[Link](insert_data_query, data_to_insert)
[Link]()
print([Link], "record(s) inserted.")

[Link](display_data_query)
result = [Link]()
print("Data in example_table:")
for row in result:
print(row)

except [Link] as err:


print("Error:", err)

finally:
if mydb.is_connected():
[Link]()
[Link]()
print("MySQL connection closed.")

Output:-

SCET/CO/2023-24/EVEN/BTECH Div-II/Sem-VI Page No:


Subject code:BTEA19621 Subject Name: Python and Sql for Data Science
Enrollment No: ET21BTCO082 Name: Heta Vimalesh Patel Date:15 /02/24

● ENROLLMENTS TABLE
import [Link]

mydb = [Link](
host="[Link]",
user="root",
password="student",
database="CO_82"
)

create_table_query = """
CREATE TABLE IF NOT EXISTS enrollments (
enrollment_id INT PRIMARY KEY,
id Int,
course_id Int,
FOREIGN KEY(id) REFERENCES students(id),
FOREIGN KEY(course_id) REFERENCES courses(course_id),
grade VARCHAR(255)
)
"""

insert_data_query = """
INSERT INTO enrollments (enrollment_id, id, course_id, grade ) VALUES (%s, %s, %s,
%s)
"""
data_to_insert = [
( 1, 1,101, 'A'),
( 2, 2,101, 'B'),
( 3, 3,102, 'A'),
( 4, 4,103, 'B'),
( 5, 1,103, 'C')

SCET/CO/2023-24/EVEN/BTECH Div-II/Sem-VI Page No:


Subject code:BTEA19621 Subject Name: Python and Sql for Data Science
Enrollment No: ET21BTCO082 Name: Heta Vimalesh Patel Date:15 /02/24

]
display_data_query = """
SELECT * FROM enrollments
"""

try:
cursor = [Link]()
[Link](create_table_query)
[Link]()
print("Table created successfully.")
[Link](insert_data_query, data_to_insert)
[Link]()
print([Link], "record(s) inserted.")
[Link](display_data_query)
result = [Link]()
print("Data in example_table:")
for row in result:
print(row)

except [Link] as err:


print("Error:", err)

finally:
if mydb.is_connected():
[Link]()
[Link]()
print("MySQL connection closed.")

Output:-

SCET/CO/2023-24/EVEN/BTECH Div-II/Sem-VI Page No:


Subject code:BTEA19621 Subject Name: Python and Sql for Data Science
Enrollment No: ET21BTCO082 Name: Heta Vimalesh Patel Date:15 /02/24

1. Retrieve the names of all students who have enrolled in the Math course.

Code:-
import [Link]

mydb = [Link](
host="localhost",
user="root",
password="student",
database="CO_82"
)

mycursor = [Link]()

query = """SELECT [Link], name FROM STUDENTS AS S JOIN enrollments AS


E ON [Link]=[Link] JOIN COURSES AS C ON E.course_id=C.course_id
WHERE C.course_name='Math';"""

try:
[Link](query)
result = [Link]()
print("\nNames of all students who have enrolled in the Math course :\n")
for row in result:
print(row)

except [Link] as err:


print("Error:", err)

finally:
if mydb.is_connected():
[Link]()
[Link]()
print("\nMySQL connection closed.")

Output:-

SCET/CO/2023-24/EVEN/BTECH Div-II/Sem-VI Page No:


Subject code:BTEA19621 Subject Name: Python and Sql for Data Science
Enrollment No: ET21BTCO082 Name: Heta Vimalesh Patel Date:15 /02/24

2. Find the average age of students.

Code:-
import [Link]

mydb = [Link](
host="localhost",
user="root",
password="student",
database="CO_82"
)

mycursor = [Link]()

query = """SELECT AVG(age) FROM STUDENTS"""

try:
[Link](query)
result = [Link]()
print("\nAverage age of students :\n")
for row in result:
print(row)

except [Link] as err:


print("Error:", err)

finally:
if mydb.is_connected():
[Link]()
[Link]()
print("\nMySQL connection closed.")

Output:-

SCET/CO/2023-24/EVEN/BTECH Div-II/Sem-VI Page No:


Subject code:BTEA19621 Subject Name: Python and Sql for Data Science
Enrollment No: ET21BTCO082 Name: Heta Vimalesh Patel Date:15 /02/24

3. List all courses along with the number of students enrolled in each course.

Code:-
import [Link]

mydb = [Link](
host="localhost",
user="root",
password="student",
database="CO_82"
)

mycursor = [Link]()

query = """SELECT C.course_id, course_name, COUNT([Link]) AS count FROM Courses


AS C JOIN Enrollments AS E ON C.course_id=E.course_id GROUP BY E.course_id"""

try:
[Link](query)
result = [Link]()
print("\nList all courses along with the number of students enrolled in each course:\n")
for row in result:
print(row)

except [Link] as err:


print("Error:", err)

finally:
if mydb.is_connected():
[Link]()
[Link]()
print("\nMySQL connection closed.")

Output:-

SCET/CO/2023-24/EVEN/BTECH Div-II/Sem-VI Page No:


Subject code:BTEA19621 Subject Name: Python and Sql for Data Science
Enrollment No: ET21BTCO082 Name: Heta Vimalesh Patel Date:15 /02/24

4. Retrieve the names of students who have scored an A grade in any course.
Code:-
import [Link]

mydb = [Link](
host="localhost",
user="root",
password="student",
database="CO_82"
)

mycursor = [Link]()

query = """SELECT [Link],name FROM STUDENTS AS S JOIN Enrollments AS


E ON [Link]=[Link] WHERE [Link]='A'"""

try:
[Link](query)
result = [Link]()
print("\nNames of students who have scored an A grade in any course:\n")
for row in result:
print(row)

except [Link] as err:


print("Error:", err)

finally:
if mydb.is_connected():
[Link]()
[Link]()
print("\nMySQL connection closed.")

Output:-

SCET/CO/2023-24/EVEN/BTECH Div-II/Sem-VI Page No:


Subject code:BTEA19621 Subject Name: Python and Sql for Data Science
Enrollment No: ET21BTCO082 Name: Heta Vimalesh Patel Date:15 /02/24

5. Update Diana's age to 24.

Code:-
import [Link]

mydb = [Link](
host="localhost",
user="root",
password="student",
database="CO_82"
)

mycursor = [Link]()

update_table_query = """UPDATE STUDENTS SET age='24' WHERE name='Diana'"""


display_table_query = """SELECT * FROM STUDENTS"""

try:
[Link](update_table_query)
result = [Link]()
print("\nTable Updates Succesfully..!!!\n")
[Link](update_table_query)
[Link]()
print("Table updated successfully.")
[Link](display_table_query)
result = [Link]()
print("\nData in table 'Students' :\n")
for row in result:
print(row)

except [Link] as err:


print("Error:", err)

finally:
if mydb.is_connected():
[Link]()
[Link]()
print("\nMySQL connection closed.")

Output:-

SCET/CO/2023-24/EVEN/BTECH Div-II/Sem-VI Page No:


Subject code:BTEA19621 Subject Name: Python and Sql for Data Science
Enrollment No: ET21BTCO082 Name: Heta Vimalesh Patel Date:15 /02/24

6. Delete the enrollment of the student with ID 2 from the Physics course.

Code:-
import [Link]

mydb = [Link](
host="localhost",
user="root",
password="student",
database="CO_82"
)

mycursor = [Link]()

delete_table_query = """DELETE FROM Enrollments WHERE id=2 AND


course_id=(SELECT course_id FROM Courses WHERE course_name='Physics')"""
display_table_query = """SELECT * FROM Enrollments"""

try:
[Link](delete_table_query)
[Link]()
print("Data deleted successfully.")
[Link](display_table_query)
result = [Link]()
print("\nData in table 'Enrollments' :\n")
for row in result:
print(row)

SCET/CO/2023-24/EVEN/BTECH Div-II/Sem-VI Page No:


Subject code:BTEA19621 Subject Name: Python and Sql for Data Science
Enrollment No: ET21BTCO082 Name: Heta Vimalesh Patel Date:15 /02/24

except [Link] as err:


print("Error:", err)

finally:
if mydb.is_connected():
[Link]()
[Link]()
print("\nMySQL connection closed.")

Output:-

7. Find the course with the highest enrollment.

Code:-
import [Link]

mydb = [Link](
host="localhost",
user="root",
password="student",
database="CO_82"
)

mycursor = [Link]()

query = """SELECT course_id, course_name, enrollment_count FROM (SELECT


C.course_id, C.course_name, COUNT([Link]) AS enrollment_count, RANK()
OVER (ORDER BY COUNT([Link]) DESC) AS rnk FROM Courses C JOIN
Enrollments E ON C.course_id = E.course_id GROUP BY C.course_id,
C.course_name) ranked_courses WHERE rnk = 1;"""

SCET/CO/2023-24/EVEN/BTECH Div-II/Sem-VI Page No:


Subject code:BTEA19621 Subject Name: Python and Sql for Data Science
Enrollment No: ET21BTCO082 Name: Heta Vimalesh Patel Date:15 /02/24

try:
[Link](query)
result = [Link]()
print("\nThe course with the highest enrollment :\n")
for row in result:
print(row)

except [Link] as err:


print("Error:", err)

finally:
if mydb.is_connected():
[Link]()
[Link]()
print("\nMySQL connection closed.")

Output:-

8. Retrieve the names of instructors who have courses with no enrollments.

Code:-
import [Link]

mydb = [Link](
host="localhost",
user="root",
password="student",
database="CO_82"
)

mycursor = [Link]()

query = """SELECT C.course_id, instructor, COUNT([Link]) AS count FROM

SCET/CO/2023-24/EVEN/BTECH Div-II/Sem-VI Page No:


Subject code:BTEA19621 Subject Name: Python and Sql for Data Science
Enrollment No: ET21BTCO082 Name: Heta Vimalesh Patel Date:15 /02/24

Courses AS C JOIN Enrollments AS E ON C.course_id=E.course_id GROUP BY


E.course_id HAVING COUNT([Link])=0"""

try:
[Link](query)
result = [Link]()
print("\nNames of instructors who have courses with no enrollments :\n")
for row in result:
print(row)

except [Link] as err:


print("Error:", err)

finally:
if mydb.is_connected():
[Link]()
[Link]()
print("\nMySQL connection closed.")

Output:-

9. Calculate the average grade for each course.

Code:-
import [Link]

mydb = [Link](
host="localhost",
user="root",
password="student",
database="CO_82"
)

mycursor = [Link]()

SCET/CO/2023-24/EVEN/BTECH Div-II/Sem-VI Page No:


Subject code:BTEA19621 Subject Name: Python and Sql for Data Science
Enrollment No: ET21BTCO082 Name: Heta Vimalesh Patel Date:15 /02/24

query = """ SELECT C.course_id,instructor, avg([Link]) AS avgGrade FROM


Courses AS C JOIN Enrollments AS E ON C.course_id=E.course_id GROUP BY
E.course_id;"""

try:
[Link](query)
result = [Link]()
print("\nAverage grade for each course:\n")
for row in result:
print(row)

except [Link] as err:


print("Error:", err)

finally:
if mydb.is_connected():
[Link]()
[Link]()
print("\nMySQL connection closed.")

Output:-

10. Retrieve the students who have enrolled in more than one course.

Code:-
import [Link]

mydb = [Link](
host="localhost",
user="root",
password="student",

SCET/CO/2023-24/EVEN/BTECH Div-II/Sem-VI Page No:


Subject code:BTEA19621 Subject Name: Python and Sql for Data Science
Enrollment No: ET21BTCO082 Name: Heta Vimalesh Patel Date:15 /02/24

database="CO_82"
)

mycursor = [Link]()

query = """SELECT [Link], name, COUNT([Link]) AS count FROM


STUDENTS AS S JOIN Enrollments AS E ON [Link]=[Link] GROUP BY
[Link] HAVING COUNT([Link])>1"""

try:
[Link](query)
result = [Link]()
print("\nNames of students who have enrolled in more than one course :\n")
for row in result:
print(row)

except [Link] as err:


print("Error:", err)

finally:
if mydb.is_connected():
[Link]()
[Link]()
print("\nMySQL connection closed.")

Output:-

SCET/CO/2023-24/EVEN/BTECH Div-II/Sem-VI Page No:

Common questions

Powered by AI

The document effectively uses try-except blocks for error handling, which enhances the robustness of the application by catching and managing database errors, thus preventing application crashes and enabling graceful recovery. This structured error handling improves user experience by providing clear feedback if operations fail. However, over-reliance on generic error messages could obscure specific issues if not logged with sufficient detail, hindering troubleshooting and debugging processes .

Enhancing SQL script readability and maintainability could include adopting consistent naming conventions, using clear and concise comments to explain complex logic, employing indentation and spacing for better visual structure, and modularizing queries into stored procedures or functions. Versioning scripts with meaningful filenames and integrating automated testing for database changes also promote understanding and reduce errors. Additionally, documenting the schema and key relationships can aid comprehension for future developers .

The design uses primary and foreign keys to maintain referential integrity. The 'Students' table includes a primary key 'student_id', which serves as a foreign key in the 'Enrollments' table. Similarly, the 'Courses' table has 'course_id' as a primary key, also serving as a foreign key in 'Enrollments'. This setup ensures that enrollments are only valid if they correspond to existing students and courses, preventing orphan records and maintaining database integrity .

Separating 'Courses' and 'Enrollments' into two tables adheres to the principles of database normalization, eliminating data redundancy and ensuring data integrity. This separation allows each table to focus on specific entities and their attributes: 'Courses' handles course-specific data, while 'Enrollments' records each student's course registration. This design facilitates efficient data management and scalability, as it avoids repetitive course details in every enrollment record and simplifies updates to course attributes .

JOIN operations are beneficial as they enable the retrieval of related data across multiple tables in a single query, providing a comprehensive view and reducing the need for multiple database round-trips. This integration improves performance and user experience. However, joins can be complex to construct and might lead to performance issues if not optimized, especially with large datasets. They also increase the risk of introducing errors if not all required join conditions are correctly specified .

Omitting explicit foreign key constraints for 'student_id' and 'course_id' in the 'Enrollments' table can lead to broken referential integrity, where enrollments may reference non-existent students or courses, introducing data anomalies. This absence undermines relational database benefits, such as automatic cascading actions and referential integrity checks, necessitating manual data validation, potentially increasing maintenance efforts and error-proneness .

Allowing NULL values in the 'grade' column could result in ambiguity in data interpretation, as it may be unclear whether an empty value indicates a pending grade, a dropped course, or simply missing data. This could complicate analysis and reporting, as queries would need to account for NULL values explicitly. Moreover, it could lead to inconsistencies in data if not properly handled in the application layer or if business rules do not clearly define the meaning of NULL in this context .

Using auto-increment primary keys in the 'Students' table is effective for simplifying record creation by automatically generating unique identifiers for each entry. This approach minimizes user errors, as the user doesn’t have to manually input unique IDs. However, it assumes that sequential ordering is not crucial for student IDs and may not be ideal if collated data needs to be synchronized with an external system that requires predefined keys .

Implementing real-time updates in large-scale systems can introduce challenges such as increased load on the database, leading to performance bottlenecks if not optimized properly. Frequent updates may cause lock contentions, slowing down read operations. Implementing caching strategies or using database replication can mitigate these issues, though they introduce additional complexity. Ensuring data consistency across distributed systems may also require sophisticated conflict resolution strategies, especially in concurrent environments .

Using VARCHAR(5) for the 'gender' column allows more flexibility, accommodating various gender expressions beyond binary options, such as 'Male', 'Female', 'Non-binary'. However, it could lead to inconsistent data entries unless carefully validated, as users might input variations like 'M', 'Male', 'male'. Implementing boolean or ENUM would enforce stricter data integrity, with ENUM being preferable for capturing different gender options explicitly .

You might also like