Question:

Arrange the steps required to read data from a MySQL database to a DataFrame:
A. pip install pymysql
B. pip install sqlalchemy
C. pandas.read_sql_query(query, sql_conn)
D. engine=create_engine('driver://username:password@host/name_of_database', index=false)

Show Hint

You cannot write code using libraries before installing them.
Therefore, "pip install" commands (A and B) must always come first in the workflow.
Updated On: Jun 11, 2026
  • A, B, D, C
  • B, C, A, D
  • B, A, C, D
  • C, B, D, A
Show Solution
collegedunia
Verified By Collegedunia

The Correct Option is A

Solution and Explanation




Step 1: Understanding the Question:

The question asks us to order the steps required to establish a connection with a MySQL database in Python, extract data using a query, and load it into a Pandas DataFrame.



Step 2: Key Formula or Approach:

Before writing code, we must ensure that the necessary libraries are installed.
The workflow for database connectivity in Python using Pandas is:
1. Install database driver (pymysql) and ORM/Connection engine (sqlalchemy).
2. Create the database connection engine using create_engine.
3. Execute the SQL query and load the result into a DataFrame using pd.read_sql_query().



Step 3: Detailed Explanation:

Let's sequence the steps:
- Step 1 (A & B): We must install the third-party dependencies. pymysql serves as the MySQL database driver for Python, and sqlalchemy is the SQL toolkit. Therefore, we run pip install pymysql (A) and pip install sqlalchemy (B).
- Step 2 (D): In Python code, we import create_engine from sqlalchemy and configure the database connection URI to instantiate an engine. Thus, engine = create_engine(...) is established (D).
- Step 3 (C): We pass our SQL query and connection engine object to pandas.read_sql_query() to fetch the table as a DataFrame (C).
Thus, the correct logical order is A, B, D, C.



Step 4: Final Answer:

The correct sequence is given by option (A).
Was this answer helpful?
0
0