본문 바로가기

카테고리 없음

프로그래머스

7 day SQL 챌린지 답

 

-- 코드를 입력하세요
SELECT ANIMAL_OUTS.ANIMAL_ID, ANIMAL_OUTS.NAME 
FROM ANIMAL_OUTS
LEFT OUTER JOIN ANIMAL_INS 
ON(ANIMAL_OUTS.ANIMAL_ID = ANIMAL_INS.ANIMAL_ID)
WHERE ANIMAL_INS.ANIMAL_ID IS NULL

 

 

 

 

-- 코드를 입력하세요
-- 보호 시작일보다 입양일이 더 빠른 동물의 아이디와 이름을 조회하는 SQL문을 작성해주세요.
SELECT ANIMAL_OUTS.ANIMAL_ID, ANIMAL_OUTS.NAME 
FROM ANIMAL_OUTS
LEFT OUTER JOIN ANIMAL_INS 
ON(ANIMAL_OUTS.ANIMAL_ID = ANIMAL_INS.ANIMAL_ID)
WHERE ANIMAL_INS.DATETIME > ANIMAL_OUTS.DATETIME

ORDER BY ANIMAL_INS.DATETIME --> 이걸 넣으니까 되네

 

 

 

 

-----------------

몰랐던 부분 

 

 

LEFT OUTER JOIN과 LEFT JOIN의 차이점

없다.

 

 

결론부터 말하면 LEFT OUTER JOIN과 LEFT JOIN의 결과는 같다.

 

LEFT OUTER JOIN을 줄여쓰면 LEFT JOIN이 된다.

 

 

또,

INNER JOIN을 줄여쓰면 그냥 JOIN이 된다.



출처: https://streetdeveloper.tistory.com/23 [return 0;]

 

 

https://stackoverflow.com/questions/406294/left-join-vs-left-outer-join-in-sql-server

 

LEFT JOIN vs. LEFT OUTER JOIN in SQL Server

What is the difference between LEFT JOIN and LEFT OUTER JOIN?

stackoverflow.com

 

JOIN 잘 몰라서 검색해봄. 학부때 얼마나 공부를 안했는지... 근데 이제는 밥먹고 살아야하니까 찾아서 하면 할수있다.

 

이렇게 하면 된다고 함.

 

 

어떻게 해도 풀지 못한 문제 

...더보기
  • 보호소에서 중성화한 동물
  • darklight

    sublimevimemacs

    MySQL 

문제 설명

ANIMAL_INS 테이블은 동물 보호소에 들어온 동물의 정보를 담은 테이블입니다. ANIMAL_INS 테이블 구조는 다음과 같으며, ANIMAL_ID, ANIMAL_TYPE, DATETIME, INTAKE_CONDITION, NAME, SEX_UPON_INTAKE는 각각 동물의 아이디, 생물 종, 보호 시작일, 보호 시작 시 상태, 이름, 성별 및 중성화 여부를 나타냅니다.

NAMETYPENULLABLE
ANIMAL_ID VARCHAR(N) FALSE
ANIMAL_TYPE VARCHAR(N) FALSE
DATETIME DATETIME FALSE
INTAKE_CONDITION VARCHAR(N) FALSE
NAME VARCHAR(N) TRUE
SEX_UPON_INTAKE VARCHAR(N) FALSE

ANIMAL_OUTS 테이블은 동물 보호소에서 입양 보낸 동물의 정보를 담은 테이블입니다. ANIMAL_OUTS 테이블 구조는 다음과 같으며, ANIMAL_ID, ANIMAL_TYPE, DATETIME, NAME, SEX_UPON_OUTCOME는 각각 동물의 아이디, 생물 종, 입양일, 이름, 성별 및 중성화 여부를 나타냅니다. ANIMAL_OUTS테이블의 ANIMAL_ID는 ANIMAL_INS의 ANIMAL_ID의 외래 키입니다.

NAMETYPENULLABLE
ANIMAL_ID VARCHAR(N) FALSE
ANIMAL_TYPE VARCHAR(N) FALSE
DATETIME DATETIME FALSE
NAME VARCHAR(N) TRUE
SEX_UPON_OUTCOME VARCHAR(N) FALSE

보호소에서 중성화 수술을 거친 동물 정보를 알아보려 합니다. 보호소에 들어올 당시에는 중성화1되지 않았지만, 보호소를 나갈 당시에는 중성화된 동물의 아이디와 생물 종, 이름을 조회하는 아이디 순으로 조회하는 SQL 문을 작성해주세요.

예시

예를 들어, ANIMAL_INS 테이블과 ANIMAL_OUTS 테이블이 다음과 같다면

ANIMAL_INS

ANIMAL_IDANIMAL_TYPEDATETIMEINTAKE_CONDITIONNAMESEX_UPON_INTAKE
A367438 Dog 2015-09-10 16:01:00 Normal Cookie Spayed Female
A382192 Dog 2015-03-13 13:14:00 Normal Maxwell 2 Intact Male
A405494 Dog 2014-05-16 14:17:00 Normal Kaila Spayed Female
A410330 Dog 2016-09-11 14:09:00 Sick Chewy Intact Female

ANIMAL_OUTS

ANIMAL_IDANIMAL_TYPEDATETIMENAMESEX_UPON_OUTCOME
A367438 Dog 2015-09-12 13:30:00 Cookie Spayed Female
A382192 Dog 2015-03-16 13:46:00 Maxwell 2 Neutered Male
A405494 Dog 2014-05-20 11:44:00 Kaila Spayed Female
A410330 Dog 2016-09-13 13:46:00 Chewy Spayed Female
  • Cookie는 보호소에 들어올 당시에 이미 중성화되어있었습니다.
  • Maxwell 2는 보호소에 들어온 후 중성화되었습니다.
  • Kaila는 보호소에 들어올 당시에 이미 중성화되어있었습니다.
  • Chewy는 보호소에 들어온 후 중성화되었습니다.

따라서 SQL문을 실행하면 다음과 같이 나와야 합니다.

ANIMAL_IDANIMAL_TYPENAME
A382192 Dog Maxwell 2
A410330 Dog Chewy

 

 

서브쿼리를 써야만 풀 수 있는 부분이었다.(기보다? 암튼 쓰면 더 쉬운것이 나도 느껴졌음. -> 모르고 쓰면 어렵고 짜증나고 불필요하게 느껴짐 -> 테이블 자체를 쓰는게 아니라 A테이블의 조건 & B테이블의 조건 이런경우에 쓰면 될듯)

 

 

SELECT B.ANIMAL_ID, B.ANIMAL_TYPE, B.NAME
FROM
    (
    SELECT *
    FROM ANIMAL_INS A
    WHERE SEX_UPON_INTAKE LIKE "%Intact%"
    ) SQ1, ANIMAL_OUTS B
WHERE SQ1.ANIMAL_ID = B.ANIMAL_ID
AND B.SEX_UPON_OUTCOME NOT LIKE "%Intact%"

 

난 서브쿼리를 쓸줄몰라서 

이 답안은 https://wakestand.tistory.com/32?category=728465 여기서 검색했다 

서브쿼리 종류 http://www.learncertification.com/study-material/types-of-subquery-in-sql

There are three types:

               1. Nested Subquery: The subquery appears in the WHERE clause of the SQL.

               2. Inline View: The subquery appears in the FROM clause of the SQL.

               3. Scalar Subquery: The subquery appears in the SELECT clause of the SQL.

          Single Row Sub Query: Return the outer query one row of results that consists of one column

          Multiple Row Sub Query: Return to the outer query more then one row of the results it Require use of IN, ANY, ALL, or EXISTS operators

          Multiple Column Sub Query: Return to the outer query more then one row of the results in this type of query Column list on the left side of operator must be in parentheses it use the IN operator for WHERE and HAVING clauses

In terms of the way the subquery is parsed, there are two categories of subqueries:

               1. Simple Subquery: This is the kind we saw above. A simple subquery is evaluated once only for each table.

               2. Correlated Subquery: This is a type of nested subquery that uses columns from the outer query in its WHERE clause. A correlated subquery is evaluated once for each row.

 

머.. 이렇게 있다구 한당. 필요해서 써보니 별로 어렵지 않은것같다. 

 

 

https://www.oracletutorial.com/oracle-view/inline-view-in-oracle/

불러오는 중입니다...