없어진 기록 찾기
-- 1.서브쿼리
SELECT animal_id,
name
FROM animal_outs
WHERE animal_id NOT IN (SELECT animal_id
FROM animal_ins);
-- 2.LEFT OUTER JOIN
SELECT A.animal_id,
A.name
FROM animal_outs A
LEFT OUTER JOIN animal_ins B
ON A.animal_id = B.animal_id
WHERE ins.animal_id IS NULL
ORDER BY outs.animal_id;
SELECT animal_id,
name
FROM animal_outs
WHERE animal_id NOT IN (SELECT animal_id
FROM animal_ins);
-- 2.LEFT OUTER JOIN
SELECT A.animal_id,
A.name
FROM animal_outs A
LEFT OUTER JOIN animal_ins B
ON A.animal_id = B.animal_id
WHERE ins.animal_id IS NULL
ORDER BY outs.animal_id;
있었는데요 없었습니다
SELECT A.animal_id,
A.name
FROM animal_ins A,
animal_outs B
WHERE A.animal_id = B.animal_id
AND A.datetime > B.datetime
ORDER BY A.datetime;
A.name
FROM animal_ins A,
animal_outs B
WHERE A.animal_id = B.animal_id
AND A.datetime > B.datetime
ORDER BY A.datetime;
우유와 요거트가 담긴 장바구니
-- 1.IN절
SELECT DISTINCT cart_id
FROM cart_products
WHERE cart_id IN (SELECT cart_id
FROM cart_products
WHERE name = "milk")
AND cart_id IN (SELECT cart_id
FROM cart_products
WHERE name = "yogurt");
-- 2.셀프조인
SELECT DISTINCT A.cart_id
FROM cart_products A,
cart_products B
WHERE A.cart_id = B.cart_id
AND A.name = "milk"
AND B.name = "yogurt";
-- 3.WHERE절 서브쿼리
SELECT DISTINCT cart_id
FROM cart_products
WHERE name = 'Milk'
AND cart_id IN (SELECT DISTINCT cart_id
FROM cart_products
WHERE name = 'Yogurt');
-- 4.FROM절 서브쿼리
SELECT DISTINCT A.cart_id
FROM (SELECT cart_id
FROM cart_products
WHERE name = 'Yogurt') AS A,
(SELECT cart_id
FROM cart_products
WHERE name = 'Milk') AS B
WHERE A.cart_id = B.cart_id;
SELECT DISTINCT cart_id
FROM cart_products
WHERE cart_id IN (SELECT cart_id
FROM cart_products
WHERE name = "milk")
AND cart_id IN (SELECT cart_id
FROM cart_products
WHERE name = "yogurt");
-- 2.셀프조인
SELECT DISTINCT A.cart_id
FROM cart_products A,
cart_products B
WHERE A.cart_id = B.cart_id
AND A.name = "milk"
AND B.name = "yogurt";
-- 3.WHERE절 서브쿼리
SELECT DISTINCT cart_id
FROM cart_products
WHERE name = 'Milk'
AND cart_id IN (SELECT DISTINCT cart_id
FROM cart_products
WHERE name = 'Yogurt');
-- 4.FROM절 서브쿼리
SELECT DISTINCT A.cart_id
FROM (SELECT cart_id
FROM cart_products
WHERE name = 'Yogurt') AS A,
(SELECT cart_id
FROM cart_products
WHERE name = 'Milk') AS B
WHERE A.cart_id = B.cart_id;
오랜 기간 보호한 동물(1)
SELECT A.name,
A.datetime
FROM animal_ins A
RIGHT OUTER JOIN animal_outs B
ON A.animal_id = B.animal_id
WHERE B.animal_id IS NULL
ORDER BY A.datetime
LIMIT 3;
A.datetime
FROM animal_ins A
RIGHT OUTER JOIN animal_outs B
ON A.animal_id = B.animal_id
WHERE B.animal_id IS NULL
ORDER BY A.datetime
LIMIT 3;
오랜 기간 보호한 동물(2)
SELECT A.animal_id,
A.name
FROM animal_ins A,
animal_outs B
WHERE A.animal_id = B.animal_id
ORDER BY B.datetime - A.datetime DESC
LIMIT 2;
A.name
FROM animal_ins A,
animal_outs B
WHERE A.animal_id = B.animal_id
ORDER BY B.datetime - A.datetime DESC
LIMIT 2;
헤비 유저가 소유한 장소
SELECT id,
name,
host_id
FROM places
WHERE host_id IN(SELECT host_id
FROM places
GROUP BY host_id
HAVING Count(id) > 1);
name,
host_id
FROM places
WHERE host_id IN(SELECT host_id
FROM places
GROUP BY host_id
HAVING Count(id) > 1);
입양 시각 구하기(2)
SET @hour = -1;
SELECT ( @hour := @hour + 1 ) AS "HOUR",
(SELECT Count(*)
FROM animal_outs
WHERE Hour(datetime) = @hour) AS "COUNT"
FROM animal_outs
WHERE @hour < 23;
SELECT ( @hour := @hour + 1 ) AS "HOUR",
(SELECT Count(*)
FROM animal_outs
WHERE Hour(datetime) = @hour) AS "COUNT"
FROM animal_outs
WHERE @hour < 23;
보호소에서 중성화한 동물
SELECT A.animal_id,
A.animal_type,
A.name
FROM animal_ins A,
animal_outs B
WHERE A.animal_id = B.animal_id
AND A.sex_upon_intake != B.sex_upon_outcome;
A.animal_type,
A.name
FROM animal_ins A,
animal_outs B
WHERE A.animal_id = B.animal_id
AND A.sex_upon_intake != B.sex_upon_outcome;
https://www.dpriver.com/pp/sqlformat.htm
'Programming > SQL' 카테고리의 다른 글
[Oracle] SQL 튜닝 - 실행계획 수립 (Execution Plan) (1) | 2019.12.27 |
---|---|
[Oracle] SELECT문 처리 과정 (0) | 2019.12.27 |
[Oracle] Delete, Truncate, Drop 비교 + HWM(High Water Mark) (0) | 2019.12.24 |
[Oracle] sqlplus 치환변수 single ampersand(&), double ampersand(&&), / (0) | 2019.12.24 |
[Oracle] view, inlineview 구조 상의 차이점 (0) | 2019.12.24 |