Challenge - 5 Problems
Pandas SQL Mastery
Get all challenges correct to earn this badge!
Test your skills under time pressure!
❓ Predict Output
intermediate2: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)
Attempts:
2 left
💡 Hint
Look at the SQL WHERE clause filtering amounts greater than 150.
✗ Incorrect
The SQL query selects rows where amount is greater than 150, so only rows with amounts 200 and 300 are included.
❓ data_output
intermediate1: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))
Attempts:
2 left
💡 Hint
Check which employees have age 30 or more.
✗ Incorrect
Only Alice (30) and Charlie (35) meet the condition age >= 30, so the DataFrame has 2 rows.
🔧 Debug
advanced2: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')
Attempts:
2 left
💡 Hint
The second argument to read_sql_query must be a connection object, not a string.
✗ Incorrect
Passing a string instead of a connection causes pandas to try calling execute on a string, which fails with AttributeError.
🚀 Application
advanced2: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]})
Attempts:
2 left
💡 Hint
Check the pandas DataFrame method to_sql and its parameters.
✗ Incorrect
The correct method is df.to_sql with table name, connection, and optional parameters like if_exists and index.
🧠 Conceptual
expert3: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?
Attempts:
2 left
💡 Hint
Think about the strengths of SQL and pandas and how they complement each other.
✗ Incorrect
SQL is great for querying and filtering data efficiently in databases, while pandas offers powerful tools for data analysis once data is loaded.