데이터베이스 쿼리하기

중급 SQL

Jasmin Ludolf

Data Science Content Developer, DataCamp

강의 로드맵

  • 데이터베이스 쿼리하기
  • 지정된 레코드 수 집계 및 조회하기
  • 쿼리 실행과 스타일 이해하기
  • 필터링하기
  • 정렬 및 그룹화

PostgreSQL Logo

중급 SQL

영화 데이터베이스

Schema showing the four tables in the films database including field names and data types

중급 SQL

COUNT()

  • COUNT()
  • 필드에 값이 있는 레코드 수를 계산함
  • 명확성을 위해 별칭 사용
SELECT COUNT(birthdate) AS count_birthdates
FROM people;
|count_birthdates|
|----------------|
|6152            |
중급 SQL

여러 필드에서의 COUNT()

SELECT COUNT(name) AS count_names, COUNT(birthdate) AS count_birthdates
FROM people;
|count_names|count_birthdates|
|-----------|----------------|
|6397       |6152            |
중급 SQL

COUNT()에서 * 사용하기

  • COUNT(field_name) 필드의 값을 셈
  • COUNT(*) 테이블의 레코드 수를 셈
  • *모든 필드를 의미
SELECT COUNT(*) AS total_records
FROM people;
|total_records|
|-------------|
|8397         |
중급 SQL

DISTINCT

  • DISTINCT 중복을 제거하여 고유한 값만 반환
SELECT language
FROM films;
|language |
|---------|
|Danish   |
|Danish   |
|Greek    |
|Greek    |
|Greek    |
  • 우리 films 테이블에는 어떤 언어가 있을까?
SELECT DISTINCT language
FROM films;
|language |
|---------|
|Danish   |
|Greek    |
중급 SQL

COUNT()와 DISTINCT

  • COUNT()DISTINCT를 결합해 고유한 값의 개수를 계산
SELECT COUNT(DISTINCT birthdate) AS count_distinct_birthdates
FROM people;
|count_distinct_birthdates|
|-------------------------|
|5398                     |
  • COUNT()에는 중복 항목이 포함됨
  • DISTINCT에는 중복 항목이 제외됨
중급 SQL

연습해봅시다!

중급 SQL

Preparing Video For Download...