SQL(6)(union, joins with comparison operators, joins on multiple keys, self joins)

UNION

데이터를 다른 데이터 세트 위에 쌓을 수 있다. 

1
2
3
4
5
6
7
SELECT *
  FROM tutorial.crunchbase_investments_part1
 
 UNION
 
 SELECT *
   FROM tutorial.crunchbase_investments_part2
cs







union all은 중복행을 없애준다. 하지만 이 예는 중복행이 없으므로 union과 동일한 결과가 생성된다.

1
2
3
4
5
6
7
SELECT *
  FROM tutorial.crunchbase_investments_part1
 
 UNION ALL
 
 SELECT *
   FROM tutorial.crunchbase_investments_part2
cs








데이터 추가에 대한 규칙이 있다.

- 두 테이블 모두 동일한 수의 열을 가져야한다.

- 열은 첫 번째 테이블과 동일한 순서로 동일한 데이터 유형을 가져야한다.

연습문제

위 두 데이터 세트(중복값 포함)를 추가하는 쿼리를 작성. 첫 번째 데이터 세트를 이름이 "T"로 시작하는 회사로만 필터링하고 두 번째 데이터 세트를 "M"으로 시작하는 회사로 필터링

1
2
3
4
5
6
7
8
9
10
11
12
13
SELECT company_permalink,
       company_name,
       investor_name
  FROM tutorial.crunchbase_investments_part1
 WHERE company_name ILIKE 'T%'
 
 UNION ALL
 
SELECT company_permalink,
       company_name,
       investor_name
  FROM tutorial.crunchbase_investments_part2
 WHERE company_name ILIKE 'M%'
cs








3개의 열을 표시하는 쿼리 작성(데이터 출처, 회사 상태, 투자자 수)

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
SELECT 'investments_part1' AS dataset_name,
       companies.status,
       COUNT(DISTINCT investments.investor_permalink) AS investors
  FROM tutorial.crunchbase_companies companies
  LEFT JOIN tutorial.crunchbase_investments_part1 investments
    ON companies.permalink = investments.company_permalink
 GROUP BY dataset_name,companies.status
 
 UNION ALL
 
 SELECT 'investments_part2' AS dataset_name,
       companies.status,
       COUNT(DISTINCT investments.investor_permalink) AS investors
  FROM tutorial.crunchbase_companies companies
  LEFT JOIN tutorial.crunchbase_investments_part2 investments
    ON companies.permalink = investments.company_permalink
 GROUP BY dataset_name,companies.status
cs










JOINS WITH COMPARISON OPERATORS

조건문을 on절에 입력할 수 있다.

1
2
3
4
5
6
7
8
9
SELECT companies.permalink,
       companies.name,
       companies.status,
       COUNT(investments.investor_permalink) AS investors
  FROM tutorial.crunchbase_companies companies
  LEFT JOIN tutorial.crunchbase_investments_part1 investments
    ON companies.permalink = investments.company_permalink
   AND investments.funded_year > companies.founded_year + 5
 GROUP BY companies.permalink,companies.name, companies.status
cs








1
2
3
4
5
6
7
8
9
SELECT companies.permalink,
       companies.name,
       companies.status,
       COUNT(investments.investor_permalink) AS investors
  FROM tutorial.crunchbase_companies companies
  LEFT JOIN tutorial.crunchbase_investments_part1 investments
    ON companies.permalink = investments.company_permalink
 WHERE investments.funded_year > companies.founded_year + 5
 GROUP BY companies.permalink,companies.name, companies.status
cs

모든 행을 결합한 다음 필터링하는 대신 조건에 맞는 행만 결합하므로 위와 다른 결과가 생성된다.








JOINS ON MULTIPLE KEYS

여러 외래키에서 테이블을 조인하려는 이유는 정확성, 성능 관련이 있다.

1
2
3
4
5
6
7
8
SELECT companies.permalink,
       companies.name,
       investments.company_name,
       investments.company_permalink
  FROM tutorial.crunchbase_companies companies
  LEFT JOIN tutorial.crunchbase_investments_part1 investments
    ON companies.permalink = investments.company_permalink
   AND companies.name = investments.company_name
cs








SELF JOINS

아래 쿼리는 일본에서 투자한 후 영국으로부터 투자를 받은 회사를 식별하는 쿼리이다.

1
2
3
4
5
6
7
8
9
SELECT DISTINCT japan_investments.company_name,
       japan_investments.company_permalink
  FROM tutorial.crunchbase_investments_part1 japan_investments
  JOIN tutorial.crunchbase_investments_part1 gb_investments
    ON japan_investments.company_name = gb_investments.company_name
   AND gb_investments.investor_country_code = 'GBR'
   AND gb_investments.funded_at > japan_investments.funded_at
 WHERE japan_investments.investor_country_code = 'JPN'
 ORDER BY company_name
cs










댓글

이 블로그의 인기 게시물

SQL(9)(performance tuning queries, pivoting data)

데이터 크롤링(3주차)

SQL(8)(writing subqueries, window functions)