Review Publically β€” Header (standalone)
How Python and SQL Work Together in Real-World Data Analysis
Python and SQL working together for real-world data analysis
Data Science

How Python and SQL Work Together in Real-World Data Analysis

A practical, code-first walkthrough of where SQL should do the heavy lifting, and exactly where Python takes over

Khalid Hussain & Mariam Jabbar Β· Published Aug 30, 2026 Β· 10 min read Β· Tutorial
A practical, code-first workflow guide
SQL and Python Aren't Competitors. They're Two Halves of One Workflow
Where SQL should do the heavy lifting, and exactly where Python takes over
SQL Python pandas Data Analysis

Modern data analysis rarely happens entirely inside one tool. In many real-world projects, data lives in a database, where SQL is used to retrieve and organize it. Python then helps analysts explore the results further, automate repeated tasks, perform more advanced calculations, and create visualizations.

The important point is that Python and SQL are not competitors. They solve different parts of the same workflow.

This article walks through a realistic example of how they can work together, from querying raw business data with SQL to analyzing the results with Python.

Quick Answer
Use SQL to filter, join, and aggregate data as close to the database as possible. Use Python, with pandas, for the analysis, visualization, automation, and custom logic SQL isn't built for. In a real workflow, you move back and forth between the two as new questions come up.
Note

The database structure, data, and examples in this article are simplified for demonstration. Real-world databases may contain additional tables, fields, relationships, and business rules.

A Realistic Scenario: An Online Store Wants to Understand Its Sales

Imagine an online store that keeps its data in a database. It has three tables:

  • customers
  • orders
  • order_items

The business team wants answers to questions such as:

  • Which customers generated the most revenue?
  • Which product categories performed best?
  • How did sales change over time?
  • Are there unusual changes that need investigation?

The database may contain thousands or millions of rows. Instead of exporting every row and processing everything in Python, it is usually more efficient to let SQL do the first stage of the work.

Step 1: Use SQL to Retrieve Only the Data You Need

Suppose the orders table contains order-level information and the order_items table contains individual products purchased in each order.

A SQL query can join the tables, filter the relevant date range, and calculate daily revenue:

SQL
SELECT
    o.order_date,
    SUM(oi.quantity * oi.unit_price) AS daily_revenue,
    COUNT(DISTINCT o.order_id) AS total_orders
FROM orders AS o
JOIN order_items AS oi
    ON o.order_id = oi.order_id
WHERE o.order_date >= '2026-01-01'
GROUP BY o.order_date
ORDER BY o.order_date;

This single query performs several important tasks.

First, the JOIN connects related information from two tables. The WHERE clause limits the analysis to the required period. The SUM() function calculates revenue, while COUNT(DISTINCT ...) counts unique orders. Finally, GROUP BY turns many individual transactions into a smaller daily summary.

This is where SQL is particularly useful: filtering, joining, grouping, and aggregating data close to where the data is stored.

Rather than moving an entire database into Python, the analyst receives a smaller and more relevant result set.

Step 2: Bring the SQL Results into Python

Python can connect to databases and work with query results. For a simple example, SQLite can be accessed through Python's built-in sqlite3 module.

The Python sqlite3 documentation describes the module's interface for working with SQLite databases. The pandas read_sql documentation explains how SQL query results can be loaded into a pandas DataFrame.

Here is a simplified example:

Python
import sqlite3
import pandas as pd

connection = sqlite3.connect("store_data.db")

query = """
SELECT
    o.order_date,
    SUM(oi.quantity * oi.unit_price) AS daily_revenue,
    COUNT(DISTINCT o.order_id) AS total_orders
FROM orders AS o
JOIN order_items AS oi
    ON o.order_id = oi.order_id
WHERE o.order_date >= '2026-01-01'
GROUP BY o.order_date
ORDER BY o.order_date;
"""

df = pd.read_sql_query(
    query,
    connection,
    parse_dates=["order_date"]
)

connection.close()

At this point, df contains the results of the SQL query in a pandas DataFrame.

The division of work is clear: SQL prepared the data. Python now provides a flexible environment for analyzing it further.

Step 3: Use Python for Deeper Analysis

Suppose the business team wants to understand whether revenue is generally increasing and whether there are days with unusual performance.

Python can calculate a rolling average:

Python
df["7_day_average"] = (
    df["daily_revenue"]
    .rolling(window=7)
    .mean()
)

This creates a smoother view of the data by calculating the average revenue over the previous seven days.

We can also calculate day-to-day percentage changes:

Python
df["daily_change_pct"] = (
    df["daily_revenue"]
    .pct_change() * 100
)

Now the analyst can identify days where revenue changed sharply. For example:

Python
unusual_days = df[
    df["daily_change_pct"].abs() > 30
]

print(unusual_days)

This does not automatically explain why revenue changed. Instead, it helps identify where further investigation may be useful. An analyst might then return to SQL and investigate those specific dates, products, customers, or regions.

The Iterative Loop

SQL β†’ Python β†’ new question β†’ SQL β†’ Python. The workflow is iterative rather than a one-time handoff.

Step 4: Create a Visualization

Python is also useful for turning query results into something easier to understand. For example:

Python
import matplotlib.pyplot as plt

plt.plot(
    df["order_date"],
    df["daily_revenue"]
)

plt.xlabel("Date")
plt.ylabel("Revenue")
plt.title("Daily Revenue Over Time")
plt.xticks(rotation=45)
plt.tight_layout()
plt.show()

A chart can reveal patterns that are difficult to notice in a table of numbers. Perhaps revenue rises steadily before a seasonal event. Perhaps there is a sudden drop on one particular day. Perhaps weekends consistently perform differently from weekdays.

The chart does not replace SQL. It builds on the data that SQL has already prepared.

When Should Processing Happen in SQL?

A useful rule is to ask whether the database can perform the operation efficiently before moving the data elsewhere. SQL is often a good choice for:

  • Filtering large datasets
  • Selecting only required columns
  • Joining related tables
  • Grouping and aggregating data
  • Calculating database-friendly summaries
  • Reducing the amount of data transferred to Python

For example, if a database contains 20 million transactions but the analysis only needs monthly revenue by country, it usually makes little sense to load all 20 million rows into a local DataFrame first. A query can summarize the data inside the database:

SQL
SELECT
    country,
    strftime('%Y-%m', order_date) AS month,
    SUM(amount) AS revenue
FROM orders
GROUP BY country, month;

Python can then work with the much smaller result. This approach can reduce memory usage and unnecessary data transfer.

When Does Python Become More Useful?

Python becomes particularly valuable when the analysis needs flexibility beyond a standard database query. Examples include:

  • Complex statistical analysis
  • Data cleaning involving multiple steps
  • Repeated analysis and automation
  • Custom business logic
  • Time-series analysis
  • Machine learning
  • Visualization
  • Combining database data with files or APIs
  • Generating reports automatically

For example, an analyst may retrieve monthly sales with SQL and then use Python to calculate growth rates, compare trends, create charts, and automatically export a report.

The pandas SQL I/O documentation provides additional guidance on working with SQL data in pandas, including database connections and reading SQL results.

If you want to go deeper on either end of this list, our descriptive statistics in data science guide and our machine learning portfolio projects guide both build directly on this kind of SQL-prepared dataset.

A Complete Example Workflow

A practical workflow might look like this.

Step01

Define the Business Question

For example: "Which customer segments are showing declining purchase activity?" Starting with a question helps prevent unnecessary data collection.

Step02

Use SQL to Prepare the Data

SQL can join customer and order tables, calculate recent purchase activity, and create a summary for each customer.

SQL
SELECT
    c.customer_id,
    c.customer_segment,
    MAX(o.order_date) AS last_order_date,
    COUNT(o.order_id) AS total_orders,
    SUM(o.order_total) AS lifetime_value
FROM customers AS c
LEFT JOIN orders AS o
    ON c.customer_id = o.customer_id
GROUP BY
    c.customer_id,
    c.customer_segment;
Step03

Load the Result into Python

Python
df = pd.read_sql_query(query, connection)
Step04

Clean and Explore the Data

Python can check for missing values, convert date columns, calculate time since the last purchase, and group customers into useful categories.

Python
df["last_order_date"] = pd.to_datetime(
    df["last_order_date"]
)

df["days_since_last_order"] = (
    pd.Timestamp.today()
    - df["last_order_date"]
).dt.days
Step05

Create Meaningful Categories

Python
def customer_status(days):
    if days <= 30:
        return "Active"
    elif days <= 90:
        return "At Risk"
    return "Inactive"

df["customer_status"] = (
    df["days_since_last_order"]
    .apply(customer_status)
)
Step06

Summarize and Communicate the Findings

Python can calculate how many customers belong to each category and create charts or reports for stakeholders.

The final result is more useful than either a raw SQL result or an isolated Python script. SQL retrieves and structures the information, while Python helps turn that information into analysis.

Connecting Python to Different Databases

SQLite is convenient for learning and small projects because it can be accessed through Python's standard sqlite3 module. In production environments, analysts may work with databases such as PostgreSQL, MySQL, or other systems.

The connection method may change, but the overall workflow remains similar:

  • Establish a database connection
  • Define a query
  • Retrieve the required data
  • Load it into a Python structure such as a DataFrame
  • Perform additional analysis
  • Communicate or store the results

For broader database workflows, pandas can work with database connections supported through appropriate database interfaces and drivers.

Common Mistakes When Moving Data Between SQL and Python

Using SQL and Python together is powerful, but several mistakes can make the workflow slower or harder to maintain.

MISTAKE 01

Loading Far More Data Than Necessary

A common mistake is writing SELECT * FROM large_table; and filtering everything in Python. Select only the columns and rows needed for the current analysis.

MISTAKE 02

Duplicating the Same Transformation in Both Places

If SQL already calculates a clean daily summary, Python should not repeat the same aggregation without a reason. Each part of the workflow should have a clear responsibility.

MISTAKE 03

Ignoring Data Types

Dates, numbers, missing data, and text may behave differently after moving between a database and Python. A date returned as text may need explicit conversion, e.g. pd.to_datetime(...).

MISTAKE 04

Building Queries Through Unsafe String Formatting

When a query includes values from users or external input, don't insert them directly into SQL strings. Python's sqlite3 documentation recommends placeholders for parameter binding instead.

MISTAKE 05

Losing Track of the Original Query

If a SQL query changes repeatedly inside a notebook without documentation, the analysis becomes hard to reproduce later. Save important queries and analysis steps.

Performance Considerations: SQL First or Python First?

There is no single answer for every project. The decision depends on the size of the data, the database, available computing resources, and the type of analysis. However, a practical pattern is:

Use SQL for data reduction. Use Python for deeper analysis.

For example, imagine a table with millions of events.

SQL CAN FIRST
  • Filter to the relevant date range
  • Select only required columns
  • Remove unnecessary rows
  • Join supporting tables
  • Aggregate detailed events into useful summaries
PYTHON CAN THEN
  • Explore trends
  • Calculate custom metrics
  • Test assumptions
  • Create visualizations
  • Automate reporting

For larger result sets, pandas also supports reading SQL results in chunks, which can be useful when loading everything into memory at once is not practical.

Best Practices for Reproducible and Maintainable Workflows

A strong analysis should be understandable not only today, but also weeks or months later. Here are several useful practices.

  • Keep SQL queries readable with meaningful table aliases and clear formatting.
  • Separate configuration, such as database credentials, file paths, and environment settings, from analysis code.
  • Use version control so it's easy to see what changed and why.
  • Document assumptions, such as excluded refunded orders or an active-customers-only filter.
  • Validate results by comparing totals and inspecting sample rows rather than trusting a query just because it ran.
  • Automate repeated reports instead of rebuilding them manually every week.
  • Keep the workflow modular: separate extraction, cleaning, analysis, visualization, and reporting into distinct stages.

The Real Skill Is Knowing Which Tool to Use When

Python and SQL work best together when each is used for the tasks it handles well.

SQL is effective for retrieving, filtering, joining, and aggregating data directly in a database. Python provides a flexible environment for deeper analysis, automation, visualization, and custom logic.

In a real-world workflow, the process may begin with SQL, continue in Python, and return to SQL when new questions emerge. The goal is not to choose one tool over the other. The goal is to create a workflow that is efficient, reproducible, and useful for answering a real question.

The Practical Takeaway

A good data analyst thinks beyond individual tools: what question needs to be answered, what data is required, where each transformation should happen, and how the final result can be checked and explained clearly.

Frequently Asked Questions

Should I use SQL or Python for data analysis?

Use both. SQL is the better tool for retrieving, filtering, joining, and aggregating data directly in the database. Python, with pandas, takes over for deeper analysis, statistics, automation, custom logic, and visualization. In real workflows you move back and forth between the two rather than picking one.

How do you load SQL query results into Python?

Connect to the database, for example with Python's built-in sqlite3 module for SQLite, then pass your query and the connection to pandas.read_sql_query(), which returns the results as a DataFrame you can analyze further.

Why is SELECT * a bad habit when using SQL with Python?

Running SELECT * and filtering everything in Python pulls far more data than necessary across the wire and into memory. It's more efficient to filter, select only the needed columns, and aggregate inside the database first, so Python receives a smaller, already-relevant result set.

How do you prevent SQL injection when building queries in Python?

Use parameter binding, meaning placeholders, instead of inserting user-supplied values directly into a SQL string. Python's sqlite3 documentation recommends this approach for any query that includes external input.

What's a good rule for deciding whether to do something in SQL or Python?

A practical pattern is to use SQL for data reduction (filtering, joining, aggregating) and Python for deeper analysis (statistics, visualization, automation, custom logic). If the database can do an operation efficiently, do it there first.

Khalid Hussain

Founder of Review Publically, an independent platform covering Data Science, Machine Learning, Deep Learning, and AI/LLM model reviews. Holds an MSc in Computer Science and the Google Advanced Data Analytics Professional Certificate, and edits the site's Data Science and Python/SQL tutorial coverage.

MSc Computer Science Google Advanced Data Analytics

Mariam Jabbar

Mariam Jabbar is an aspiring technology and data science content writer interested in AI, software, data analytics, and emerging technologies. She focuses on creating clear, well-researched, and practical content that helps readers understand complex technical topics and their real-world applications

Content Writer Aspiring Technology