시작하기무료로 시작하기

조인 더 연습해 보기

이전 연습 문제에서 만든 select 문을 그대로 사용할 수 있지만, 이번에는 몇 개 열만 반환하고 다른 테이블을 group_by() 절에 사용해 보겠습니다.

이 연습은 강의의 일부입니다

Python으로 배우는 데이터베이스 입문

강의 보기

연습 안내

  • 다음을 선택하는 문을 만드세요:
    • census 테이블의 state
    • census 테이블의 pop2008 열 합계
    • state_fact 테이블의 census_division_name
  • stmt.select_from() 을 추가해 statename 열로 censusstate_fact 테이블을 조인하세요.
  • 문을 state_fact 테이블의 name 열로 그룹화하세요.
  • stmt_grouped 를 실행하여 모든 레코드를 가져오고 results 로 저장하세요.
  • results 객체를 순회하며 각 레코드를 출력한 뒤 Submit Answer 하세요.

실습형 인터랙티브 연습

이 예제를 이 샘플 코드를 완성하여 풀어보세요.

# Build a statement to select the state, sum of 2008 population and census
# division name: stmt
stmt = select([
    ____,
    func.sum(____),
    ____
])

# Append select_from to join the census and state_fact tables by the census state and state_fact name columns
stmt_joined = stmt.select_from(
    census.join(____, census.columns.____ == state_fact.columns.____)
)

# Append a group by for the state_fact name column
stmt_grouped = stmt_joined.group_by(____)

# Execute the statement and get the results: results
results = connection.execute(____).fetchall()

# Loop over the results object and print each record.
for record in results:
    print(record)
코드 편집 및 실행