In [1]:
from IPython.display import HTML
HTML("""
<style>
/* ocultar la primera celda */
.jp-CodeCell:first-child {
display:none;
}
/* tipografía general */
body {
font-family: 'Segoe UI', Arial, sans-serif;
line-height: 1.7;
background:#fafafc;
}
/* títulos */
h1 {
color:#6B8EEC;
border-bottom:3px solid #BFD3FF;
padding-bottom:6px;
}
h2 {
color:#5A5A7A;
margin-top:30px;
}
/* párrafos */
p {
font-size:16px;
color:#444;
}
/* bloques de código */
.jp-CodeCell pre {
background:#f4f6ff !important;
color:#333 !important;
border-left:4px solid #9FB8FF;
padding:12px;
border-radius:6px;
}
/* resultados */
.jp-OutputArea pre {
background:#fdf6ff;
border-left:4px solid #D7B8FF;
padding:10px;
border-radius:6px;
}
/* tablas de pandas */
.dataframe {
border-collapse: collapse !important;
margin-top:15px;
background:white;
border-radius:8px;
overflow:hidden;
}
.dataframe th {
background:#C9D8FF !important;
color:#333 !important;
padding:8px;
}
.dataframe td {
padding:8px;
border-bottom:1px solid #eee;
}
/* cajas informativas */
.note {
background:#EEF3FF;
padding:15px;
border-radius:8px;
border-left:6px solid #9FB8FF;
}
/* listas */
ul {
line-height:1.8;
}
</style>
""")
Out[1]:
In [1]:
!pip install -q mysql-connector-python pandas
In [2]:
import mysql.connector
from mysql.connector import Error
import pandas as pd
DB_NAME = "university_db"
def get_connection(database=None):
return mysql.connector.connect(
host="database-1.cd8w4cuu6a79.us-west-1.rds.amazonaws.com",
user="admin",
password="admin123", # Cambia esto si tienes contraseña
database=database
)
try:
conn = get_connection()
conn.autocommit = False
cursor = conn.cursor()
cursor.execute(f"CREATE DATABASE IF NOT EXISTS {DB_NAME}")
cursor.close()
conn.close()
conn = get_connection(DB_NAME)
cursor = conn.cursor()
print(f"Conectado correctamente a la base de datos '{DB_NAME}'.")
except Error as e:
print("Error al conectar a MySQL:", e)
raise
Conectado correctamente a la base de datos 'university_db'.
In [3]:
def run_query(query, params=None):
df = pd.read_sql(query, conn, params=params)
display(df)
return df
In [4]:
run_query("""SELECT * FROM students""")
/tmp/ipykernel_2609/657940896.py:2: UserWarning: pandas only supports SQLAlchemy connectable (engine/connection) or database string URI or sqlite3 DBAPI2 connection. Other DBAPI2 objects are not tested. Please consider using SQLAlchemy. df = pd.read_sql(query, conn, params=params)
| id | name | major | gpa | |
|---|---|---|---|---|
| 0 | 1 | Alice | CS | 3.8 |
| 1 | 2 | Bob | CS | 3.5 |
| 2 | 3 | Charlie | Math | 3.9 |
| 3 | 4 | Diana | Math | 3.7 |
| 4 | 5 | Eve | Physics | 4.0 |
| 5 | 6 | Frank | CS | 3.2 |
| 6 | 7 | Grace | Math | 3.6 |
| 7 | 8 | Henry | Physics | 3.4 |
| 8 | 9 | Ivy | CS | 3.9 |
| 9 | 10 | Jack | Math | 3.3 |
Out[4]:
| id | name | major | gpa | |
|---|---|---|---|---|
| 0 | 1 | Alice | CS | 3.8 |
| 1 | 2 | Bob | CS | 3.5 |
| 2 | 3 | Charlie | Math | 3.9 |
| 3 | 4 | Diana | Math | 3.7 |
| 4 | 5 | Eve | Physics | 4.0 |
| 5 | 6 | Frank | CS | 3.2 |
| 6 | 7 | Grace | Math | 3.6 |
| 7 | 8 | Henry | Physics | 3.4 |
| 8 | 9 | Ivy | CS | 3.9 |
| 9 | 10 | Jack | Math | 3.3 |
In [6]:
run_query("SELECT * FROM departments")
/tmp/ipykernel_2609/657940896.py:2: UserWarning: pandas only supports SQLAlchemy connectable (engine/connection) or database string URI or sqlite3 DBAPI2 connection. Other DBAPI2 objects are not tested. Please consider using SQLAlchemy. df = pd.read_sql(query, conn, params=params)
| id | name | |
|---|---|---|
| 0 | 1 | Computer Science |
| 1 | 2 | Mathematics |
| 2 | 3 | Physics |
Out[6]:
| id | name | |
|---|---|---|
| 0 | 1 | Computer Science |
| 1 | 2 | Mathematics |
| 2 | 3 | Physics |
In [7]:
run_query("SELECT * FROM students ORDER BY gpa DESC")
/tmp/ipykernel_2609/657940896.py:2: UserWarning: pandas only supports SQLAlchemy connectable (engine/connection) or database string URI or sqlite3 DBAPI2 connection. Other DBAPI2 objects are not tested. Please consider using SQLAlchemy. df = pd.read_sql(query, conn, params=params)
| id | name | major | gpa | |
|---|---|---|---|---|
| 0 | 5 | Eve | Physics | 4.0 |
| 1 | 3 | Charlie | Math | 3.9 |
| 2 | 9 | Ivy | CS | 3.9 |
| 3 | 1 | Alice | CS | 3.8 |
| 4 | 4 | Diana | Math | 3.7 |
| 5 | 7 | Grace | Math | 3.6 |
| 6 | 2 | Bob | CS | 3.5 |
| 7 | 8 | Henry | Physics | 3.4 |
| 8 | 10 | Jack | Math | 3.3 |
| 9 | 6 | Frank | CS | 3.2 |
Out[7]:
| id | name | major | gpa | |
|---|---|---|---|---|
| 0 | 5 | Eve | Physics | 4.0 |
| 1 | 3 | Charlie | Math | 3.9 |
| 2 | 9 | Ivy | CS | 3.9 |
| 3 | 1 | Alice | CS | 3.8 |
| 4 | 4 | Diana | Math | 3.7 |
| 5 | 7 | Grace | Math | 3.6 |
| 6 | 2 | Bob | CS | 3.5 |
| 7 | 8 | Henry | Physics | 3.4 |
| 8 | 10 | Jack | Math | 3.3 |
| 9 | 6 | Frank | CS | 3.2 |
In [8]:
run_query("""
SELECT s.id, s.name
FROM students s
WHERE NOT EXISTS (
SELECT d.id FROM departments d
WHERE NOT EXISTS (
SELECT e.id FROM enrollments e
JOIN courses c ON e.course_id = c.id
WHERE c.department_id = d.id
AND e.student_id = s.id
)
)
""")
/tmp/ipykernel_2609/657940896.py:2: UserWarning: pandas only supports SQLAlchemy connectable (engine/connection) or database string URI or sqlite3 DBAPI2 connection. Other DBAPI2 objects are not tested. Please consider using SQLAlchemy. df = pd.read_sql(query, conn, params=params)
| id | name |
|---|
Out[8]:
| id | name |
|---|
In [9]:
run_query("""
SELECT c.id, c.name, c.credits
FROM courses c
JOIN departments d ON c.department_id = d.id
WHERE d.name = 'Physics'
""")
/tmp/ipykernel_2609/657940896.py:2: UserWarning: pandas only supports SQLAlchemy connectable (engine/connection) or database string URI or sqlite3 DBAPI2 connection. Other DBAPI2 objects are not tested. Please consider using SQLAlchemy. df = pd.read_sql(query, conn, params=params)
| id | name | credits | |
|---|---|---|---|
| 0 | 5 | Mechanics | 4 |
| 1 | 6 | Electromagnetism | 4 |
Out[9]:
| id | name | credits | |
|---|---|---|---|
| 0 | 5 | Mechanics | 4 |
| 1 | 6 | Electromagnetism | 4 |
In [10]:
run_query("""
SELECT c.name, COUNT(e.id) AS total_inscripciones
FROM courses c
LEFT JOIN enrollments e ON c.id = e.course_id
GROUP BY c.id, c.name
""")
/tmp/ipykernel_2609/657940896.py:2: UserWarning: pandas only supports SQLAlchemy connectable (engine/connection) or database string URI or sqlite3 DBAPI2 connection. Other DBAPI2 objects are not tested. Please consider using SQLAlchemy. df = pd.read_sql(query, conn, params=params)
| name | total_inscripciones | |
|---|---|---|
| 0 | Intro to CS | 3 |
| 1 | Data Structures | 3 |
| 2 | Calculus I | 3 |
| 3 | Linear Algebra | 3 |
| 4 | Mechanics | 3 |
| 5 | Electromagnetism | 2 |
| 6 | Algorithms | 1 |
| 7 | Statistics | 2 |
Out[10]:
| name | total_inscripciones | |
|---|---|---|
| 0 | Intro to CS | 3 |
| 1 | Data Structures | 3 |
| 2 | Calculus I | 3 |
| 3 | Linear Algebra | 3 |
| 4 | Mechanics | 3 |
| 5 | Electromagnetism | 2 |
| 6 | Algorithms | 1 |
| 7 | Statistics | 2 |
In [11]:
run_query("""
SELECT DISTINCT p.id, p.name
FROM professors p
JOIN enrollments e ON p.id = e.professor_id
JOIN courses c ON e.course_id = c.id
WHERE c.department_id != p.department_id
""")
/tmp/ipykernel_2609/657940896.py:2: UserWarning: pandas only supports SQLAlchemy connectable (engine/connection) or database string URI or sqlite3 DBAPI2 connection. Other DBAPI2 objects are not tested. Please consider using SQLAlchemy. df = pd.read_sql(query, conn, params=params)
| id | name |
|---|
Out[11]:
| id | name |
|---|
In [14]:
run_query("SELECT COUNT(*) as total_departamentos FROM departments")
run_query("""
SELECT s.name, COUNT(DISTINCT c.department_id) as deptos_cursados
FROM students s
JOIN enrollments e ON s.id = e.student_id
JOIN courses c ON e.course_id = c.id
GROUP BY s.id, s.name
ORDER BY deptos_cursados DESC
LIMIT 5
""")
/tmp/ipykernel_2609/657940896.py:2: UserWarning: pandas only supports SQLAlchemy connectable (engine/connection) or database string URI or sqlite3 DBAPI2 connection. Other DBAPI2 objects are not tested. Please consider using SQLAlchemy. df = pd.read_sql(query, conn, params=params)
| total_departamentos | |
|---|---|
| 0 | 3 |
/tmp/ipykernel_2609/657940896.py:2: UserWarning: pandas only supports SQLAlchemy connectable (engine/connection) or database string URI or sqlite3 DBAPI2 connection. Other DBAPI2 objects are not tested. Please consider using SQLAlchemy. df = pd.read_sql(query, conn, params=params)
| name | deptos_cursados | |
|---|---|---|
| 0 | Bob | 2 |
| 1 | Diana | 2 |
| 2 | Alice | 1 |
| 3 | Charlie | 1 |
| 4 | Eve | 1 |
Out[14]:
| name | deptos_cursados | |
|---|---|---|
| 0 | Bob | 2 |
| 1 | Diana | 2 |
| 2 | Alice | 1 |
| 3 | Charlie | 1 |
| 4 | Eve | 1 |
In [ ]: