์์ด์ง ๊ธฐ๋ก ์ฐพ๊ธฐ
-- join ๋ฏธ์ฌ์ฉ
SELECT ANIMAL_ID, NAME
FROM ANIMAL_OUTS
WHERE ANIMAL_ID NOT IN (
SELECT ANIMAL_ID
FROM ANIMAL_INS)
-- join ์ฌ์ฉ
SELECT OUTS.ANIMAL_ID, OUTS.NAME
FROM ANIMAL_OUTS AS OUTS
LEFT OUTER JOIN ANIMAL_INS AS INS
ON OUTS.ANIMAL_ID = INS.ANIMAL_ID
WHERE INS.ANIMAL_ID IS NULL
ORDER BY OUTS.ANIMAL_ID
๐ JOIN ์ฌ์ฉํ ๋๋ ๊ฐ ํ ์ด๋ธ์ ์ด๋ฆ ๋ถ์ฌ์ ์จ์ฃผ๊ณ , ON์ผ๋ก ์ธ๋ํค ๋ง์ถฐ์ฃผ๊ธฐ
๐ OUTER JOIN ์ฌ์ฉํ๋ฉด ์๋ก ์ด๋ค ์ฐจ์ด๊ฐ ์๋์ง ์ ์ ์์
(https://seolhee2750.tistory.com/175)
์ค๋ ๊ธฐ๊ฐ ๋ณดํธํ ๋๋ฌผ(1)
-- join ๋ฏธ์ฌ์ฉ
SELECT NAME, DATETIME
FROM ANIMAL_INS
WHERE ANIMAL_ID NOT IN (
SELECT ANIMAL_ID
FROM ANIMAL_OUTS)
ORDER BY DATETIME
LIMIT 3
-- join ์ฌ์ฉ
SELECT INS.NAME, INS.DATETIME
FROM ANIMAL_INS AS INS
LEFT OUTER JOIN ANIMAL_OUTS AS OUTS
ON INS.ANIMAL_ID = OUTS.ANIMAL_ID
WHERE OUTS.ANIMAL_ID IS NULL
ORDER BY DATETIME
LIMIT 3
์ค๋ ๊ธฐ๊ฐ ๋ณดํธํ ๋๋ฌผ(2)
SELECT OUTS.ANIMAL_ID, OUTS.NAME
FROM ANIMAL_INS AS INS
JOIN ANIMAL_OUTS AS OUTS
ON INS.ANIMAL_ID = OUTS.ANIMAL_ID
ORDER BY OUTS.DATETIME - INS.DATETIME DESC
LIMIT 2
์์๋๋ฐ์ ์์์ต๋๋ค
SELECT INS.ANIMAL_ID, INS.NAME
FROM ANIMAL_INS AS INS
JOIN ANIMAL_OUTS AS OUTS
ON INS.ANIMAL_ID = OUTS.ANIMAL_ID
WHERE INS.DATETIME > OUTS.DATETIME
ORDER BY INS.DATETIME
ํค๋น ์ ์ ๊ฐ ์์ ํ ์ฅ์
SELECT ID, NAME, HOST_ID
FROM PLACES
WHERE HOST_ID IN (
SELECT HOST_ID
FROM PLACES
GROUP BY HOST_ID
HAVING COUNT(HOST_ID) >= 2)
ORDER BY ID
'6๏ธโฃ SQL > Problem Solving' ์นดํ ๊ณ ๋ฆฌ์ ๋ค๋ฅธ ๊ธ
[MySQL] Programmers Lv.2 ์ ์ฒด ๋ฌธ์ ํ์ด (2023 ์ ๋ฐ์ดํธ!) (1) | 2023.06.27 |
---|---|
[MySQL] Programmers Lv.1 ์ ์ฒด ๋ฌธ์ ํ์ด (2023 ์ ๋ฐ์ดํธ!) (2) | 2023.04.26 |
[MySQL] Programmers Lv.4 ์ ์ฒด ๋ฌธ์ ํ์ด (0) | 2022.02.02 |
[MySQL] Programmers Lv.2 ์ ์ฒด ๋ฌธ์ ํ์ด (0) | 2022.02.02 |
[MySQL] Programmers Lv.1 ์ ์ฒด ๋ฌธ์ ํ์ด (0) | 2022.02.02 |
๋๊ธ