0
0
Pandasdata~20 mins

Pandas with SQL databases - Practice Problems & Coding Challenges

Choose your learning style9 modes available
Challenge - 5 Problems
🎖️
Pandas SQL Mastery
Get all challenges correct to earn this badge!
Test your skills under time pressure!
Predict Output
intermediate
2:00remaining
What is the output of this pandas SQL query?
Given a SQLite database connection, what will be the output of the following code snippet?
Pandas
import sqlite3
import pandas as pd

conn = sqlite3.connect(':memory:')
conn.execute('CREATE TABLE sales (id INTEGER, amount INTEGER)')
conn.execute('INSERT INTO sales VALUES (1, 100), (2, 200), (3, 300)')
df = pd.read_sql_query('SELECT * FROM sales WHERE amount > 150', conn)
print(df)
A
Empty DataFrame
Columns: [id, amount]
Index: []
B
   id  amount
0   1     100
1   2     200
2   3     300
C
   id  amount
0   2     200
1   3     300
D
   id  amount
0   1     100
Attempts:
2 left
💡 Hint
Look at the SQL WHERE clause filtering amounts greater than 150.
data_output
intermediate
1:30remaining
How many rows does this DataFrame have after reading from SQL?
Consider this code that reads data from a SQL table into a pandas DataFrame. How many rows will the DataFrame contain?
Pandas
import sqlite3
import pandas as pd

conn = sqlite3.connect(':memory:')
conn.execute('CREATE TABLE employees (name TEXT, age INTEGER)')
conn.execute("INSERT INTO employees VALUES ('Alice', 30), ('Bob', 25), ('Charlie', 35)")
df = pd.read_sql_query('SELECT * FROM employees WHERE age >= 30', conn)
print(len(df))
A2
B3
C1
D0
Attempts:
2 left
💡 Hint
Check which employees have age 30 or more.
🔧 Debug
advanced
2:00remaining
What error does this code raise when reading SQL into pandas?
This code tries to read data from a SQL database into a pandas DataFrame. What error will it raise?
Pandas
import pandas as pd

query = 'SELECT * FROM orders'
df = pd.read_sql_query(query, 'not_a_connection')
AAttributeError: 'str' object has no attribute 'execute'
BTypeError: argument 2 must be a sqlite3.Connection or SQLAlchemy connectable
CNameError: name 'orders' is not defined
DValueError: SQL query is empty
Attempts:
2 left
💡 Hint
The second argument to read_sql_query must be a connection object, not a string.
🚀 Application
advanced
2:30remaining
Which option correctly writes a DataFrame to a SQL table?
You have a pandas DataFrame df and a SQLite connection conn. Which code snippet correctly writes df to a SQL table named 'products'?
Pandas
import sqlite3
import pandas as pd

conn = sqlite3.connect(':memory:')
df = pd.DataFrame({'product': ['A', 'B'], 'price': [10, 20]})
Aconn.to_sql(df, 'products', if_exists='replace')
Bdf.to_sql('products', conn, if_exists='replace', index=False)
Cpd.to_sql(df, 'products', conn)
Ddf.write_sql('products', conn)
Attempts:
2 left
💡 Hint
Check the pandas DataFrame method to_sql and its parameters.
🧠 Conceptual
expert
3:00remaining
Why use pandas with SQL databases in data science?
Which of the following is the best reason to use pandas with SQL databases in data science workflows?
ATo perform fast in-memory computations on large datasets without any disk I/O
BTo avoid learning SQL by using pandas methods only
CTo replace SQL databases entirely with pandas DataFrames for all data storage needs
DTo combine the power of SQL for data retrieval with pandas for flexible data manipulation and analysis
Attempts:
2 left
💡 Hint
Think about the strengths of SQL and pandas and how they complement each other.