# Import create_engine function
from sqlalchemy import create_engine

# Create an engine to the census database
engine = create_engine("mysql+pymysql://student:datacamp@courses.csrrinzqubik.us-east-1.rds.amazonaws.com:3306/census")

# Print the table names
print(engine.table_names())


# Build query to return state names by population difference from 2008 to 2000: stmt
stmt = select([census.columns.state, (census.columns.pop2008-census.columns.pop2000).label('pop_change')])

# Append group by for the state: stmt_grouped
stmt_grouped = stmt.group_by(census.columns.state)

# Append order by for pop_change descendingly: stmt_ordered
stmt_ordered = stmt_grouped.order_by(desc('pop_change'))

# Return only 5 results: stmt_top5
stmt_top5 = stmt_ordered.limit(5)

# Use connection to execute stmt_top5 and fetch all results
results = connection.execute(stmt_top5).fetchall()

# Print the state and population change for each record
for result in results:
    print('{}:{}'.format(result.state, result.pop_change))



9d1625dfb36b7fc643b5f21f7f8c63be509bcee0
<mask token>

<mask token>
print(engine.table_names())
<mask token>
for result in results:
    print('{}:{}'.format(result.state, result.pop_change))

<mask token>
engine = create_engine(
    'mysql+pymysql://student:datacamp@courses.csrrinzqubik.us-east-1.rds.amazonaws.com:3306/census'
    )
print(engine.table_names())
stmt = select([census.columns.state, (census.columns.pop2008 - census.
    columns.pop2000).label('pop_change')])
stmt_grouped = stmt.group_by(census.columns.state)
stmt_ordered = stmt_grouped.order_by(desc('pop_change'))
stmt_top5 = stmt_ordered.limit(5)
results = connection.execute(stmt_top5).fetchall()
for result in results:
    print('{}:{}'.format(result.state, result.pop_change))

from sqlalchemy import create_engine
engine = create_engine(
    'mysql+pymysql://student:datacamp@courses.csrrinzqubik.us-east-1.rds.amazonaws.com:3306/census'
    )
print(engine.table_names())
stmt = select([census.columns.state, (census.columns.pop2008 - census.
    columns.pop2000).label('pop_change')])
stmt_grouped = stmt.group_by(census.columns.state)
stmt_ordered = stmt_grouped.order_by(desc('pop_change'))
stmt_top5 = stmt_ordered.limit(5)
results = connection.execute(stmt_top5).fetchall()
for result in results:
    print('{}:{}'.format(result.state, result.pop_change))

# Import create_engine function
from sqlalchemy import create_engine

# Create an engine to the census database
engine = create_engine("mysql+pymysql://student:datacamp@courses.csrrinzqubik.us-east-1.rds.amazonaws.com:3306/census")

# Print the table names
print(engine.table_names())


# Build query to return state names by population difference from 2008 to 2000: stmt
stmt = select([census.columns.state, (census.columns.pop2008-census.columns.pop2000).label('pop_change')])

# Append group by for the state: stmt_grouped
stmt_grouped = stmt.group_by(census.columns.state)

# Append order by for pop_change descendingly: stmt_ordered
stmt_ordered = stmt_grouped.order_by(desc('pop_change'))

# Return only 5 results: stmt_top5
stmt_top5 = stmt_ordered.limit(5)

# Use connection to execute stmt_top5 and fetch all results
results = connection.execute(stmt_top5).fetchall()

# Print the state and population change for each record
for result in results:
    print('{}:{}'.format(result.state, result.pop_change))


