๐งท ์ค๋ช
- ํ๋์ SQL ๋ฌธ ์์ ํฌํจ๋์ด ์๋ ๋ ๋ค๋ฅธ SQL ๋ฌธ์ ๋งํ๋ค.
- ๋ฉ์ธ์ฟผ๋ฆฌ๊ฐ ์๋ธ์ฟผ๋ฆฌ๋ฅผ ํฌํจํ๋ ์ข ์์ ์ธ ๊ด๊ณ์ด๋ค.
- ์๋ธ์ฟผ๋ฆฌ๋ ๋ฉ์ธ์ฟผ๋ฆฌ์ ์นผ๋ผ ์ฌ์ฉ ๊ฐ๋ฅ
- ๋ฉ์ธ์ฟผ๋ฆฌ๋ ์๋ธ์ฟผ๋ฆฌ์ ์นผ๋ผ ์ฌ์ฉ ๋ถ๊ฐ
๐งท ์ฌ์ฉ์ ์ฃผ์
- Subquery ๋ ๊ดํธ๋ก ๋ฌถ์ด์ ์ฌ์ฉ
- ๋จ์ผ ํ ํน์ ๋ณต์ ํ ๋น๊ต ์ฐ์ฐ์์ ํจ๊ป ์ฌ์ฉ ๊ฐ๋ฅ
- subquery ์์๋ order by ๋ฅผ ์ฌ์ฉX
๐งท Subquery ์ข ๋ฅ
- ์ค์นด๋ผ ์๋ธ์ฟผ๋ฆฌ (Scalar Subquery) - SELECT ์ ์ ์ฌ์ฉ
- ์ธ๋ผ์ธ ๋ทฐ (Inline View) - FROM ์ ์ ์ฌ์ฉ
- ์ค์ฒฉ ์๋ธ์ฟผ๋ฆฌ (Nested Subquery) - WHERE ์ ์ ์ฌ์ฉ
๐งท ์ค์นผ๋ผ ์๋ธ์ฟผ๋ฆฌ
mysql> SELECT case_number,
-> ( SELECT avg(case_number)
-> FROM crime_status
-> WHERE crime_type LIKE '๊ฐ๋' AND status_type LIKE '๊ฒ๊ฑฐ' ) as '์์ธ์์ ์ฒด ๊ฐ๋ ๊ฒ๊ฑฐ ํ๊ท '
-> FROM crime_status
-> WHERE police_station LIKE '์ํ' AND crime_type LIKE '๊ฐ๋' AND status_type LIKE '๊ฒ๊ฑฐ';
+-------------+--------------------------------------+
| case_number | ์์ธ์์ ์ฒด ๊ฐ๋ ๊ฒ๊ฑฐ ํ๊ท |
+-------------+--------------------------------------+
| 1 | 4.1935 |
+-------------+--------------------------------------+
1 row in set (0.15 sec)
๐งท ์ธ๋ผ์ธ ๋ทฐ
mysql> SELECT c.police_station, c.crime_type, c.case_number
-> FROM crime_status c
-> ,(SELECT police_station, MAX(case_number) cnt
-> FROM crime_status
-> WHERE status_type LIKE '๋ฐ์'
-> GROUP BY police_station) m
-> WHERE c.police_station = m. police_station AND c.case_number = m.cnt;
+----------------+------------+-------------+
| police_station | crime_type | case_number |
+----------------+------------+-------------+
| ์ค๋ถ | ํญ๋ ฅ | 997 |
| ์ข
๋ก | ํญ๋ ฅ | 964 |
| ๋จ๋๋ฌธ | ์ ๋ | 699 |
| ์๋๋ฌธ | ํญ๋ ฅ | 1292 |
| ํํ | ํญ๋ ฅ | 747 |
| ์ฉ์ฐ | ํญ๋ ฅ | 1617 |
| ์ฑ๋ถ | ํญ๋ ฅ | 672 |
| ๋๋๋ฌธ | ํญ๋ ฅ | 1784 |
| ๋งํฌ | ํญ๋ ฅ | 1844 |
| ์๋ฑํฌ | ํญ๋ ฅ | 2701 |
| ์ฑ๋ | ํญ๋ ฅ | 1223 |
| ๋์ | ํญ๋ ฅ | 1631 |
| ๊ด์ง | ํญ๋ ฅ | 1676 |
| ์๋ถ | ํญ๋ ฅ | 748 |
| ๊ฐ๋ถ | ํญ๋ ฅ | 1817 |
| ๊ธ์ฒ | ํญ๋ ฅ | 1471 |
| ์ค๋ | ํญ๋ ฅ | 2022 |
| ๊ฐ๋จ | ํญ๋ ฅ | 2283 |
| ๊ด์
| ํญ๋ ฅ | 2614 |
| ๊ฐ์ | ํญ๋ ฅ | 2445 |
| ๊ฐ๋ | ํญ๋ ฅ | 1942 |
| ์ข
์ | ํญ๋ ฅ | 758 |
| ๊ตฌ๋ก | ํญ๋ ฅ | 2204 |
| ์์ด | ํญ๋ ฅ | 1750 |
| ์์ฒ | ํญ๋ ฅ | 1582 |
| ์กํ | ํญ๋ ฅ | 2675 |
| ๋
ธ์ | ํญ๋ ฅ | 2163 |
| ๋ฐฉ๋ฐฐ | ํญ๋ ฅ | 423 |
| ์ํ | ํญ๋ ฅ | 1092 |
| ๋๋ด | ํญ๋ ฅ | 1234 |
| ์์ | ํญ๋ ฅ | 1394 |
+----------------+------------+-------------+
31 rows in set (0.15 sec)
# ์ฐธ๊ณ
mysql> SELECT police_station, MAX(case_number) cnt
-> FROM crime_status
-> WHERE status_type LIKE '๋ฐ์'
-> GROUP BY police_station;
+----------------+------+
| police_station | cnt |
+----------------+------+
| ์ค๋ถ | 997 |
| ์ข
๋ก | 964 |
| ๋จ๋๋ฌธ | 699 |
| ์๋๋ฌธ | 1292 |
| ํํ | 747 |
| ์ฉ์ฐ | 1617 |
| ์ฑ๋ถ | 672 |
| ๋๋๋ฌธ | 1784 |
| ๋งํฌ | 1844 |
| ์๋ฑํฌ | 2701 |
| ์ฑ๋ | 1223 |
| ๋์ | 1631 |
| ๊ด์ง | 1676 |
| ์๋ถ | 748 |
| ๊ฐ๋ถ | 1817 |
| ๊ธ์ฒ | 1471 |
| ์ค๋ | 2022 |
| ๊ฐ๋จ | 2283 |
| ๊ด์
| 2614 |
| ๊ฐ์ | 2445 |
| ๊ฐ๋ | 1942 |
| ์ข
์ | 758 |
| ๊ตฌ๋ก | 2204 |
| ์์ด | 1750 |
| ์์ฒ | 1582 |
| ์กํ | 2675 |
| ๋
ธ์ | 2163 |
| ๋ฐฉ๋ฐฐ | 423 |
| ์ํ | 1092 |
| ๋๋ด | 1234 |
| ์์ | 1394 |
+----------------+------+
31 rows in set (0.15 sec)
๐งท ์ธ๋ผ์ธ ๋ทฐ
โข Single Row - ํ๋์ ๋ฐ์ดํฐ๋ฅผ ๊ฒ์ํ๋ ์๋ธ์ฟผ๋ฆฌ
โข Multiple Row - ํ๋ ์ด์์ ์ด์ ๊ฒ์ํ๋ ์๋ธ์ฟผ๋ฆฌ
โข Multiple Column - ํ๋ ์ด์์ ํ์ ๊ฒ์ํ๋ ์๋ธ์ฟผ๋ฆฌ
์๋ธ์ฟผ๋ฆฌ๊ฐ ๋น๊ต์ฐ์ฐ์( =, >, >=, <, <=, <>, !=)์ ์ฌ์ฉ๋๋ ๊ฒฝ์ฐ,
์๋ธ์ฟผ๋ฆฌ์ ๊ฒ์ ๊ฒฐ๊ณผ๋ ํ ๊ฐ์ ๊ฒฐ๊ณผ๊ฐ์ ๊ฐ์ ธ์ผ ํ๋ค. (๋๊ฐ ์ด์์ธ ๊ฒฝ์ฐ ์๋ฌ)
mysql> SELECT name FROM celeb
-> WHERE name = ( SELECT host FROM snl_show WHERE id = 1);
+-----------+
| name |
+-----------+
| ๊ฐ๋์ |
+-----------+
1 row in set (0.15 sec)
mysql> SELECT host
-> FROM snl_show
-> WHERE host IN ( SELECT name FROM celeb WHERE job_title LIKE '%์ํ๋ฐฐ์ฐ%' );
+-----------+
| host |
+-----------+
| ๊ฐ๋์ |
| ์ฐจ์น์ |
+-----------+
2 rows in set (0.15 sec)
์๋ธ์ฟผ๋ฆฌ ๊ฒฐ๊ณผ์ ๊ฐ์ด ์์ผ๋ฉด ๋ฐํ
mysql> Select * from crime_status limit 3;
+------+----------------+------------+-------------+-------------+-----------------------+
| year | police_station | crime_type | status_type | case_number | reference |
+------+----------------+------------+-------------+-------------+-----------------------+
| 2020 | ์ค๋ถ | ์ด์ธ | ๋ฐ์ | 1 | ์์ธ์ค๋ถ๊ฒฝ์ฐฐ์ |
| 2020 | ์ค๋ถ | ์ด์ธ | ๊ฒ๊ฑฐ | 1 | ์์ธ์ค๋ถ๊ฒฝ์ฐฐ์ |
| 2020 | ์ค๋ถ | ๊ฐ๋ | ๋ฐ์ | 3 | ์์ธ์ค๋ถ๊ฒฝ์ฐฐ์ |
+------+----------------+------------+-------------+-------------+-----------------------+
3 rows in set (0.15 sec)
mysql> Select * from police_station limit 3;
+-----------------------+--------------------------------------------------+
| name | address |
+-----------------------+--------------------------------------------------+
| ์์ธ๊ฐ๋จ๊ฒฝ์ฐฐ์ | ์์ธํน๋ณ์ ๊ฐ๋จ๊ตฌ ํ
ํค๋๋ก 114๊ธธ 11 |
| ์์ธ๊ฐ๋๊ฒฝ์ฐฐ์ | ์์ธํน๋ณ์ ๊ฐ๋๊ตฌ ์ฑ๋ด๋ก 33 |
| ์์ธ๊ฐ๋ถ๊ฒฝ์ฐฐ์ | ์์ธํน๋ณ์ ๊ฐ๋ถ๊ตฌ ์คํจ์ฐ๋ก 406 |
+-----------------------+--------------------------------------------------+
mysql> SELECT name
-> FROM police_station p
-> WHERE EXISTS ( SELECT police_station
-> FROM crime_status c
-> WHERE p.name = c.reference AND case_number > 2000);
+--------------------------+
| name |
+--------------------------+
| ์์ธ๊ฐ๋จ๊ฒฝ์ฐฐ์ |
| ์์ธ๊ฐ์๊ฒฝ์ฐฐ์ |
| ์์ธ๊ด์
๊ฒฝ์ฐฐ์ |
| ์์ธ๊ตฌ๋ก๊ฒฝ์ฐฐ์ |
| ์์ธ๋
ธ์๊ฒฝ์ฐฐ์ |
| ์์ธ์กํ๊ฒฝ์ฐฐ์ |
| ์์ธ์๋ฑํฌ๊ฒฝ์ฐฐ์ |
| ์์ธ์ค๋๊ฒฝ์ฐฐ์ |
+--------------------------+
8 rows in set (0.15 sec)
์๋ธ์ฟผ๋ฆฌ ๊ฒฐ๊ณผ ์ค์ ์ต์ํ ํ๋๋ผ๋ ๋ง์กฑํ๋ฉด (๋น๊ต์ฐ์ฐ์ ์ฌ์ฉ
mysql> SELECT * FROM celeb LIMIT 3;
+----+-----------+------------+------+------+-------------------+--------------------------+
| ID | NAME | BIRTHDAY | AGE | SEX | JOB_TITLE | AGENCY |
+----+-----------+------------+------+------+-------------------+--------------------------+
| 1 | ์์ด์ | 1993-05-16 | 29 | F | ๊ฐ์, ํค๋ฐํธ | EDAM์ํฐํ
์ด๋จผํธ |
| 2 | ์ด๋ฏธ์ฃผ | 1994-09-23 | 28 | F | ๊ฐ์ | ์ธ๋ฆผ์ํฐํ
์ด๋จผํธ |
| 3 | ์ก๊ฐ | 1994-04-23 | 28 | M | ํค๋ฐํธ | ๋๋ฌด์ํฐ์ค |
+----+-----------+------------+------+------+-------------------+--------------------------+
3 rows in set (0.15 sec)
mysql> SELECT * FROM snl_show LIMIT 3;
+----+--------+---------+----------------+-----------+
| ID | SEASON | EPISODE | BROADCAST_DATE | HOST |
+----+--------+---------+----------------+-----------+
| 1 | 8 | 7 | 2020-09-05 | ๊ฐ๋์ |
| 2 | 8 | 8 | 2020-09-12 | ์ ์ฌ์ |
| 3 | 8 | 9 | 2020-09-19 | ์ฐจ์น์ |
+----+--------+---------+----------------+-----------+
3 rows in set (0.15 sec)
mysql> SELECT name
-> FROM celeb
-> WHERE name = ANY ( SELECT host FROM snl_show ) ;
+-----------+
| name |
+-----------+
| ๊ฐ๋์ |
| ์ ์ฌ์ |
| ์ฐจ์น์ |
| ์ด์ํ |
+-----------+
4 rows in set (0.15 sec)
์๋ธ์ฟผ๋ฆฌ ๊ฒฐ๊ณผ๋ฅผ ๋ชจ๋ ๋ง์กฑํ๋ฉด (๋น๊ต ์ฐ์ฐ์ ์ฌ์ฉ)
mysql> SELECT name FROM celeb
-> WHERE name = ALL ( SELECT host FROM snl_show WHERE id = 1);
+-----------+
| name |
+-----------+
| ๊ฐ๋์ |
+-----------+
1 row in set (0.15 sec)
์๋ธ์ฟผ๋ฆฌ ๋ด์ ๋ฉ์ธ์ฟผ๋ฆฌ ์ปฌ๋ผ์ด ๊ฐ์ด ์ฌ์ฉ๋๋ ๊ฒฝ์ฐ
mysql> SELECT name, sex, agency
-> FROM celeb
-> WHERE (sex, agency) IN ( SELECT sex, agency FROM celeb WHERE name = '๊ฐ๋์') ;
+-----------+------+----------------------+
| name | sex | agency |
+-----------+------+----------------------+
| ๊ฐ๋์ | M | YG์ํฐํ
์ด๋จผํธ |
| ์ฐจ์น์ | M | YG์ํฐํ
์ด๋จผํธ |
+-----------+------+----------------------+
2 rows in set (0.15 sec)
mysql> SELECT * FROM oil_price LIMIT 3;
+------+----------------------+-------------------------------------------------+--------+--------+--------------------+--------------+
| ID | ์ํธ | ์ฃผ์ | ๊ฐ๊ฒฉ | ์
ํ | ์ํ | ๊ตฌ |
+------+----------------------+-------------------------------------------------+--------+--------+--------------------+--------------+
| 1 | ํ์ด๊ฑฐ์ฃผ์ ์ | ์์ธ ์ํ๊ตฌ ์์๋ก 188(์ค์ฐ๋) | 1484 | N | SK์๋์ง | ์ํ๊ตฌ |
| 2 | (์ฃผ)๋ช
์ฐ์๋์ง | ์์ธ ์ํ๊ตฌ ์์๋ก 236(์์๋) | 1485 | Y | ํ๋์ค์ผ๋ฑ
ํฌ | ์ํ๊ตฌ |
| 3 | ์ฑ๋ฝ์ฃผ์ ์ | ์์ธ ์๋ฑํฌ๊ตฌ ๊ฐ๋ง์ฐ๋ก 414(์ ๊ธธ๋) | 1498 | Y | S-OIL | ์๋ฑํฌ๊ตฌ |
+------+----------------------+-------------------------------------------------+--------+--------+--------------------+--------------+
3 rows in set (0.00 sec)
mysql> SELECT AVG(๊ฐ๊ฒฉ) AS '์
ํ์ฃผ์ ์ ํ๊ท ', (SELECT MAX(๊ฐ๊ฒฉ) FROM oil_price WHERE ์ํ LIKE 'SK์๋์ง') AS 'SK MAX ๊ฐ๊ฒฉ'
-> FROM oil_price
-> WHERE ์
ํ = 'Y';
+------------------------+---------------+
| ์
ํ์ฃผ์ ์ ํ๊ท | SK MAX ๊ฐ๊ฒฉ |
+------------------------+---------------+
| 1517.6000 | 2160 |
+------------------------+---------------+
1 row in set (0.02 sec)
mysql> SELECT a.์ํธ, a.์ํ, b.max_price
-> FROM oil_price a, ( SELECT ์ํ, MAX(๊ฐ๊ฒฉ) as max_price FROM oil_price GROUP BY ์ํ) b
-> WHERE a.์ํ = b.์ํ AND a.๊ฐ๊ฒฉ = b.max_price;
+-------------------+--------------------+-----------+
| ์ํธ | ์ํ | max_price |
+-------------------+--------------------+-----------+
| (์ฃผ)MS์ฃผ์ ์ | ํ๋์ค์ผ๋ฑ
ํฌ | 1498 |
| ์๋ฌธ์ฃผ์ ์ | S-OIL | 1509 |
| ๋ด์์ธ(๊ฐ๋จ) | SK์๋์ง | 2160 |
| ์ ๊ธธ์ฃผ์ ์ | GS์นผํ
์ค | 1498 |
+-------------------+--------------------+-----------+
4 rows in set (0.01 sec)
mysql> SELECT ์ํธ, ๊ฐ๊ฒฉ
-> FROM oil_price
-> WHERE ๊ฐ๊ฒฉ > (SELECT AVG(๊ฐ๊ฒฉ) FROM oil_price);
+--------------------+--------+
| ์ํธ | ๊ฐ๊ฒฉ |
+--------------------+--------+
| 21์ธ๊ธฐ์ฃผ์ ์ | 1598 |
| ์ดํผ์ฌ์ฃผ์ ์ | 1635 |
| ๋ด์์ธ(๊ฐ๋จ) | 2160 |
+--------------------+--------+
3 rows in set (0.00 sec)
mysql> SELECT ์ด๋ฆ, ์ฃผ์ ์, ์ฃผ์ ์ผ
-> FROM refueling
-> WHERE ์ฃผ์ ์ IN (SELECT ์ํธ FROM oil_price WHERE ๊ฐ๊ฒฉ > (SELECT AVG(๊ฐ๊ฒฉ) FROM oil_price));
+-----------+-------------------+------------+
| ์ด๋ฆ | ์ฃผ์ ์ | ์ฃผ์ ์ผ |
+-----------+-------------------+------------+
| ์ ์ฌ์ | ๋ด์์ธ(๊ฐ๋จ) | 2021-10-01 |
| ์ด๋ฏธ์ฃผ | ๋ด์์ธ(๊ฐ๋จ) | 2021-10-01 |
| ์ ์ฌ์ | ๋ด์์ธ(๊ฐ๋จ) | 2021-10-03 |
| ์์ด์ | ๋ด์์ธ(๊ฐ๋จ) | 2021-10-14 |
| ์์ด์ | ๋ด์์ธ(๊ฐ๋จ) | 2021-10-14 |
| ์ ์ฌ์ | ๋ด์์ธ(๊ฐ๋จ) | 2021-10-16 |
+-----------+-------------------+------------+
6 rows in set (0.00 sec)
mysql> SELECT r.์ด๋ฆ, o.์ํธ, o.์ํ, r.๊ธ์ก, o.๊ฐ๊ฒฉ
-> FROM refueling r ,
-> ( SELECT ์ํธ, ๊ฐ๊ฒฉ, ์ํ FROM oil_price ) o
-> WHERE r.๊ธ์ก >= 100000 AND r.์ฃผ์ ์ = o.์ํธ ;
+-----------+--------------------+-------------+--------+--------+
| ์ด๋ฆ | ์ํธ | ์ํ | ๊ธ์ก | ๊ฐ๊ฒฉ |
+-----------+--------------------+-------------+--------+--------+
| ์ด์์ | ํ์ด๊ฑฐ์ฃผ์ ์ | SK์๋์ง | 140000 | 1484 |
| ์ ์ฌ์ | ๋ด์์ธ(๊ฐ๋จ) | SK์๋์ง | 110000 | 2160 |
| ์์ด์ | ๋ด์์ธ(๊ฐ๋จ) | SK์๋์ง | 120000 | 2160 |
| ์์ด์ | ๋ด์์ธ(๊ฐ๋จ) | SK์๋์ง | 150000 | 2160 |
| ์ด๋ฏธ์ฃผ | ๋ด์์ธ(๊ฐ๋จ) | SK์๋์ง | 120000 | 2160 |
+-----------+--------------------+-------------+--------+--------+
5 rows in set (0.00 sec)