HackerRank - Symmetric Pairs

Parker.Park·2022년 11월 24일
0

MySQL

목록 보기
6/6

UNION

두 가지의 select 문을 통합하는 것이다. 기본적으로 중복된 자료는 표시하지 않으며, 중복된 자료까지 가져오기 위해서는 "UNION ALL"을 사용해야 한다.
이번에 알게된 점은 order by를 사용 하기 위해서는 마지막 SELECT문에 적용 하면 된다는 것이다.
w3schools에 나오는 조건은 다음과 같다.

  • Every SELECT statement within UNION must have the same number of columns
  • The columns must also have similar data types
  • The columns in every SELECT statement must also be in the same order

문법은 다음과 같다.

SELECT column_name(s) FROM table1
UNION
SELECT column_name(s) FROM table2;

problem

solve

select *
from Functions
where x = y
group by x, y
having count(*) >= 2

UNION

select a.x
      ,a.y
from Functions as a
inner join Functions as b on a.x = b.y and a.y = b.x
where a.x < a.y
order by x
;

참조

[w3schools - The SQL UNION Operator]

[[hackerrank] Symmetric Pairs]

profile
개발자준비중

0개의 댓글