Aggregate SQL Query Results with Pandas and Export to Excel
Beginner Mode
Start your terminal to use beginner mode.
Scenario
A SQLite database contains customer and order data across multiple tables. You need to analyze total order values per customer and export the results to Excel.
Task
Write a Python script at /home/interview/analyze_orders.py that:
- Connects to the SQLite database at
/home/interview/orders.db - Executes a SQL query to fetch customer and order data (join the tables)
- Loads the results into a pandas DataFrame
- Calculates total order value per customer using pandas
- Saves the results to
/home/interview/customer_totals.xlsxwith columns:customer_id,customer_name,total_order_value
Note: pandas and openpyxl are already installed. The database contains customers and orders tables.
Database Schema
customers table:
- id (INTEGER)
- first_name (TEXT)
- last_name (TEXT)
- email (TEXT)
orders table:
- id (INTEGER)
- customer_id (INTEGER)
- order_date (TEXT)
- order_amount (REAL)
Terminal requires a larger screen
Open this page on a desktop or tablet (≥ 768px) to launch the terminal and practice hands-on.
Linux Terminal Environment
Write and execute your solution in the terminal below.
Track
| Question | Difficulty | Company | Access |
|---|
Need more practice in this area? Explore more questions →
Meta