What is the main purpose of using pandas with SQL databases?
To easily read data from SQL databases into pandas DataFrames and write DataFrames back to SQL tables for analysis and manipulation.
Click to reveal answer
beginner
Which pandas function is used to read data from a SQL database into a DataFrame?
The function pandas.read_sql() or pandas.read_sql_query() is used to read SQL query results into a DataFrame.
Click to reveal answer
beginner
How do you write a pandas DataFrame to a SQL table?
Use the DataFrame.to_sql() method, specifying the table name and a database connection.
Click to reveal answer
intermediate
What is needed to connect pandas to a SQL database?
A database connection object, often created using libraries like sqlite3, SQLAlchemy, or database-specific connectors.
Click to reveal answer
intermediate
Why might you prefer using SQL queries inside pandas instead of loading entire tables?
Using SQL queries lets you select only the needed data, reducing memory use and speeding up data loading.
Click to reveal answer
Which pandas function reads SQL query results into a DataFrame?
Aread_sql_query()
Bread_csv()
Cto_sql()
Dread_excel()
✗ Incorrect
read_sql_query() reads SQL query results into a DataFrame. to_sql() writes DataFrames to SQL tables.
What argument is required to write a DataFrame to a SQL table using to_sql()?
AFile path
BTable name
CSQL query
DColumn names
✗ Incorrect
You must provide the table name where the DataFrame will be saved.
Which library is commonly used to create a database connection for pandas to use?
ARequests
BNumPy
CMatplotlib
DSQLAlchemy
✗ Incorrect
SQLAlchemy is a popular library to create database connections for pandas.
Why use SQL queries inside pandas instead of loading whole tables?
ATo avoid using DataFrames
BTo make data bigger
CTo select only needed data and save memory
DTo skip data cleaning
✗ Incorrect
SQL queries help select only the data you need, which saves memory and speeds up processing.
What does read_sql() require as input?
ASQL query or table name and a database connection
BFile path only
CDataFrame object
DList of columns
✗ Incorrect
read_sql() needs a SQL query or table name and a database connection to fetch data.
Explain how you would load data from a SQL database into a pandas DataFrame.
Think about the connection and the function to run SQL inside pandas.
You got /4 concepts.
Describe the steps to save a pandas DataFrame back into a SQL database table.
Focus on the method used on the DataFrame and what information it needs.
You got /4 concepts.
Practice
(1/5)
1. What is the main purpose of using pandas.read_sql() when working with SQL databases?
easy
A. To read data from a SQL database into a pandas DataFrame
B. To write a pandas DataFrame into a SQL database
C. To create a new SQL database
D. To delete tables from a SQL database
Solution
Step 1: Understand the function purpose
pandas.read_sql() is designed to read data from a SQL query or table.
Step 2: Match function to action
Reading data means loading it into a pandas DataFrame for analysis.
Final Answer:
To read data from a SQL database into a pandas DataFrame -> Option A
Quick Check:
read_sql() = read data [OK]
Hint: read_sql reads data into DataFrame fast [OK]
Common Mistakes:
Confusing read_sql with to_sql
Thinking it creates or deletes databases
Assuming it writes data instead of reading
2. Which of the following is the correct way to create a connection engine to a SQLite database named data.db using SQLAlchemy for pandas?
easy
A. engine = create_engine('sqlite://data.db')
B. engine = create_engine('sqlite:///data.db')
C. engine = create_engine('sqlite:data.db')
D. engine = create_engine('sqlite:/data.db')
Solution
Step 1: Recall SQLite connection string format
SQLite uses the format 'sqlite:///' followed by the database file path.
Step 2: Match correct syntax
engine = create_engine('sqlite:///data.db') uses 'sqlite:///data.db' which is the correct format for a local file.
Final Answer:
engine = create_engine('sqlite:///data.db') -> Option B
Quick Check:
SQLite engine needs three slashes [OK]
Hint: SQLite needs three slashes in connection string [OK]
Common Mistakes:
Using two slashes instead of three
Missing slashes or colon
Confusing with other database connection formats
3. Given the following code, what will be the output of print(df.shape)?
import pandas as pd
from sqlalchemy import create_engine
engine = create_engine('sqlite:///test.db')
df = pd.read_sql('SELECT * FROM users WHERE age > 30', engine)
medium
A. (0, 0) because no data is returned
B. (total_rows_in_users_table, number_of_columns_in_users_table)
C. An error because of incorrect SQL syntax
D. (number_of_rows_with_age_greater_than_30, number_of_columns_in_users_table)
Solution
Step 1: Understand the SQL query
The query selects all columns from 'users' where age is greater than 30, so only matching rows are returned.
Step 2: Understand pandas DataFrame shape
The DataFrame shape is (rows, columns). Rows = filtered rows, columns = all columns from 'users'.
Final Answer:
(number_of_rows_with_age_greater_than_30, number_of_columns_in_users_table) -> Option D
Quick Check:
Filtered rows and all columns = shape [OK]
Hint: DataFrame shape = filtered rows, all columns [OK]
Common Mistakes:
Assuming all rows are returned
Thinking shape returns data content
Confusing SQL syntax with pandas syntax
4. What is wrong with this code snippet that tries to write a DataFrame df to a SQL table named sales?
from sqlalchemy import create_engine
engine = create_engine('sqlite:///sales.db')
df.to_sql('sales', engine)
medium
A. Missing the argument if_exists to handle existing tables
B. The engine connection string is incorrect
C. to_sql cannot write to SQLite databases
D. DataFrame df must be converted to a list first
Solution
Step 1: Check to_sql default behavior
By default, to_sql will raise an error if the table exists unless if_exists is set.
Step 2: Identify missing argument
The code lacks if_exists='replace' or if_exists='append' to avoid errors on existing tables.
Final Answer:
Missing the argument if_exists to handle existing tables -> Option A
Quick Check:
to_sql needs if_exists for existing tables [OK]
Hint: Add if_exists='replace' or 'append' to avoid errors [OK]
Common Mistakes:
Assuming to_sql always overwrites
Thinking engine string is wrong
Believing DataFrame must convert before writing
5. You want to load data from a SQL database into pandas, filter rows where status = 'active', and then write only these filtered rows back to a new SQL table named active_users. Which sequence of commands correctly achieves this?
hard
A. df = pd.read_sql('SELECT * FROM users WHERE status = "active"', engine)
df.to_sql('active_users', engine, if_exists='append')
B. df = pd.read_sql('SELECT * FROM users WHERE status = active', engine)
df.to_sql('active_users', engine)
C. df = pd.read_sql('SELECT * FROM users', engine)
df_filtered = df[df['status'] == 'active']
df_filtered.to_sql('active_users', engine, if_exists='replace')
D. df = pd.read_sql('SELECT * FROM users', engine)
df_filtered = df[df.status = 'active']
df_filtered.to_sql('active_users', engine)
Solution
Step 1: Read all data and filter in pandas
df = pd.read_sql('SELECT * FROM users', engine)
df_filtered = df[df['status'] == 'active']
df_filtered.to_sql('active_users', engine, if_exists='replace') reads all users, then filters rows where status equals 'active' using correct pandas syntax.
Step 2: Write filtered data with proper if_exists argument
df = pd.read_sql('SELECT * FROM users', engine)
df_filtered = df[df['status'] == 'active']
df_filtered.to_sql('active_users', engine, if_exists='replace') writes filtered data to 'active_users' table, replacing if it exists, avoiding errors.
Final Answer:
df = pd.read_sql('SELECT * FROM users', engine)
df_filtered = df[df['status'] == 'active']
df_filtered.to_sql('active_users', engine, if_exists='replace') -> Option C
Quick Check:
Read all, filter in pandas, write with if_exists [OK]
Hint: Filter in pandas, use if_exists='replace' when writing [OK]
Common Mistakes:
Incorrect SQL syntax in WHERE clause
Using single equals (=) in pandas filter
Missing if_exists argument causing errors
Recommended
Ready to prove it?
Pass the test and this pattern moves unknownstrongwhile earning +1 Grip toward your certificate.