In [1]:
import MySQLdb


db = MySQLdb.connect(
    "db.fastcamp.us",
    "root",
    "dkstncks",
    "world",
    charset='utf8',
)

cursor = db.cursor()

In [2]:
import pandas as pd

In [3]:
# SQL Perspective

SQL_QUERY = """
    SELECT SUM(Population)
    FROM Country;
"""

pd.read_sql(SQL_QUERY, db)


Out[3]:
SUM(Population)
0 6078749450

In [4]:
# Pandas Perspective

SQL_QUERY = """
    SELECT *
    FROM Country;
"""

df = pd.read_sql(SQL_QUERY, db)

In [5]:
df["Population"].sum()


Out[5]:
6078749450

In [6]:
# New Example - MAX, MIN, MEAN, ...

SQL_QUERY = """
    SELECT AVG(Population)
    FROM Country;
"""

pd.read_sql(SQL_QUERY, db)


Out[6]:
AVG(Population)
0 25434098.1172

In [7]:
SQL_QUERY = """
    SELECT *
    FROM Country;
"""

df = pd.read_sql(SQL_QUERY, db)

In [9]:
df["Population"].mean()


Out[9]:
25434098.117154811

In [11]:
SQL_QUERY = """
    SELECT MAX(Population)
    FROM Country;
"""

pd.read_sql(SQL_QUERY, db)


Out[11]:
MAX(Population)
0 1277558000

In [12]:
df["Population"].max()


Out[12]:
1277558000