Search
Duplicate

4. SQL ํ™œ์šฉ(์—ฐ์‚ฐ์ž,ํ•จ์ˆ˜)

Created
2022/03/02 16:05
Updated
2022/03/02 16:30
Category
Stack
Creator

์—ฐ์‚ฐ์ž ๋ฐ ํ•จ์ˆ˜

์—ฐ์‚ฐ์ž

UNION : ์šฐ๋ฆฌ๊ฐ€ ์•„๋Š” ํ•ฉ์ง‘ํ•ฉ(์ค‘๋ณต ์ œ๊ฑฐ)
UNION ALL : ๋ชจ๋“  ํ–‰์„ ๋”ํ•จ (์ค‘๋ณต์ด ์žˆ๋”๋ผ๋„)
/***************๋น„๊ต ์—ฐ์‚ฐ์ž***************/ /* = : ๊ฐ™์Œ */ SELECT * FROM CUSTOMER WHERE GENDER = 'MAN'; /* <> : ๊ฐ™์ง€ ์•Š์Œ */ SELECT * FROM CUSTOMER WHERE GENDER <> 'MAN'; /* >= : ~๋ณด๋‹ค ํฌ๊ฑฐ๋‚˜ ๊ฐ™์Œ */ SELECT * FROM CUSTOMER WHERE YEAR(JOIN_DATE) >= 2020; /* <= : ~๋ณด๋‹ค ์ž‘๊ฑฐ๋‚˜ ๊ฐ™์Œ */ SELECT * FROM CUSTOMER WHERE YEAR(JOIN_DATE) <= 2019; /* > : ~๋ณด๋‹ค ํผ */ SELECT * FROM CUSTOMER WHERE YEAR(JOIN_DATE) > 2019; /* < : ~๋ณด๋‹ค ์ž‘์Œ */ SELECT * FROM CUSTOMER WHERE YEAR(JOIN_DATE) < 2020; /***************๋…ผ๋ฆฌ ์—ฐ์‚ฐ์ž***************/ /* AND : ์•ž, ๋’ค ์กฐ๊ฑด ๋ชจ๋‘ ๋งŒ์กฑ */ SELECT * FROM CUSTOMER WHERE GENDER = 'MAN' AND ADDR = 'Gyeonggi'; /* NOT : ๋’ค์— ์˜ค๋Š” ์กฐ๊ฑด๊ณผ ๋ฐ˜๋Œ€ */ SELECT * FROM CUSTOMER WHERE NOT GENDER = 'MAN' AND ADDR = 'Gyeonggi'; /* OR : ํ•˜๋‚˜๋ผ๋„ ๋งŒ์กฑ */ SELECT * FROM CUSTOMER WHERE GENDER = 'MAN' OR ADDR = 'Gyeonggi'; /***************ํŠน์ˆ˜ ์—ฐ์‚ฐ์ž***************/ /* BETWEEN a AND b : a์™€ b์˜ ๊ฐ’ ์‚ฌ์ด */ SELECT * FROM CUSTOMER WHERE YEAR(BIRTHDAY) BETWEEN 2010 AND 2011; /* NOT BETWEEN a AND b : a์™€ b์˜ ๊ฐ’ ์‚ฌ์ด๊ฐ€ ์•„๋‹˜ */ SELECT * FROM CUSTOMER WHERE YEAR(BIRTHDAY) NOT BETWEEN 1950 AND 2020; /* IN (List) : ๋ฆฌ์ŠคํŠธ ๊ฐ’ */ SELECT * FROM CUSTOMER WHERE YEAR(BIRTHDAY) IN (2010,2011); /* NOT IN (List) : ๋ฆฌ์ŠคํŠธ ๊ฐ’์ด ์•„๋‹˜ */ SELECT * FROM CUSTOMER WHERE YEAR(BIRTHDAY) NOT IN (2010, 2011); /* LIKE โ€˜๋น„๊ต๋ฌธ์ž์—ดโ€™ */ SELECT * FROM CUSTOMER WHERE ADDR LIKE 'D%'; /* ~๋กœ ์‹œ์ž‘ํ•˜๋Š” */ SELECT * FROM CUSTOMER WHERE ADDR LIKE '%N'; /* ~๋กœ ๋๋‚˜๋Š” */ SELECT * FROM CUSTOMER WHERE ADDR LIKE '%EO%'; /* ~๋ฅผ ํฌํ•จํ•˜๋Š” */ /* NOT LIKE โ€˜๋น„๊ต๋ฌธ์ž์—ดโ€™ */ SELECT * FROM CUSTOMER WHERE ADDR NOT LIKE '%EO%'; /* ~๋ฅผ ์ œ์™ธํ•˜๋Š” */ /* IS NULL : NULL */ SELECT * FROM CUSTOMER AS A LEFT JOIN SALES AS B ON A.MEM_NO = B.MEM_NO WHERE B.MEM_NO IS NULL; /* ํ™•์ธ */ SELECT * FROM SALES WHERE MEM_NO = '1001736'; /* IS NOT NULL : NOT NULL */ SELECT * FROM CUSTOMER AS A LEFT JOIN SALES AS B ON A.MEM_NO = B.MEM_NO WHERE B.MEM_NO IS NOT NULL; /***************์‚ฐ์ˆ  ์—ฐ์‚ฐ์ž***************/ SELECT * ,A.SALES_QTY * PRICE AS ๊ฒฐ์ œ๊ธˆ์•ก FROM SALES AS A LEFT JOIN PRODUCT AS B ON A.PRODUCT_CODE = B.PRODUCT_CODE; /***************์ง‘ํ•ฉ ์—ฐ์‚ฐ์ž***************/ CREATE TEMPORARY TABLE SALES_2019 SELECT * FROM SALES WHERE YEAR(ORDER_DATE) = '2019'; /* 1235ํ–‰ */ SELECT * FROM SALES_2019; /* 3115ํ–‰ */ SELECT * FROM SALES; /* UNION : 2๊ฐœ ์ด์ƒ ํ…Œ์ด๋ธ” ์ค‘๋ณต๋œ ํ–‰ ์ œ๊ฑฐ ํ•˜์—ฌ ์ง‘ํ•ฉ(* ์—ด ๊ฐœ์ˆ˜์™€ ๋ฐ์ดํ„ฐ ํƒ€์ž… ์ผ์น˜) */ SELECT * FROM SALES_2019 UNION SELECT * FROM SALES; /* UNION ALL: 2๊ฐœ ์ด์ƒ ํ…Œ์ด๋ธ” ์ค‘๋ณต๋œ ํ–‰ ์ œ๊ฑฐ ์—†์ด ์ง‘ํ•ฉ(* ์—ด ๊ฐœ์ˆ˜์™€ ๋ฐ์ดํ„ฐ ํƒ€์ž… ์ผ์น˜) */ SELECT 3115 + 1235; SELECT * FROM SALES_2019 UNION ALL SELECT * FROM SALES;
SQL
๋ณต์‚ฌ

ํ•จ์ˆ˜

๋‹จ์ผํ–‰ ํ•จ์ˆ˜

ํ–‰ ํ•˜๋‚˜์— ์ ์šฉ๋˜๋Š” ํ•จ์ˆ˜
/***************์ˆซ์žํ˜• ํ•จ์ˆ˜***************/ /* ABS(์ˆซ์ž) : ์ ˆ๋Œ€๊ฐ’ ๋ฐ˜ํ™˜ */ SELECT ABS(- 200); /* ROUND(์ˆซ์ž, N) : N ๊ธฐ์ค€์œผ๋กœ ๋ฐ˜์˜ฌ๋ฆผ ๊ฐ’ ๋ฐ˜ํ™˜ */ SELECT ROUND(2.18, 1); /* SQRT(์ˆซ์ž) : ์ œ๊ณฑ๊ทผ ๊ฐ’ ๋ฐ˜ํ™˜ */ SELECT SQRT(9); /***************๋ฌธ์žํ˜• ํ•จ์ˆ˜***************/ /* LOWER(๋ฌธ์ž) / UPPER(๋ฌธ์ž) : ์†Œ๋ฌธ์ž / ๋Œ€๋ฌธ์ž ๋ฐ˜ํ™˜ */ SELECT LOWER('AB'); SELECT UPPER('ab'); /* LEFT(๋ฌธ์ž, N) / RIGHT(๋ฌธ์ž, N) : ์™ผ์ชฝ / ์˜ค๋ฅธ์ชฝ๋ถ€ํ„ฐ N๋งŒํผ ๋ฐ˜ํ™˜ */ SELECT LEFT('AB', 1); SELECT RIGHT('AB', 1); /* LENGTH(๋ฌธ์ž) : ๋ฌธ์ž์ˆ˜ ๋ฐ˜ํ™˜ */ SELECT LENGTH('AB'); /***************๋‚ ์งœํ˜• ํ•จ์ˆ˜***************/ /* YEAR / MONTH / DAY(๋‚ ์งœ) : ์—ฐ / ์›” / ์ผ ๋ฐ˜ํ™˜ */ SELECT YEAR('2022-12-31'); SELECT MONTH('2022-12-31'); SELECT DAY('2022-12-31'); /* DATE_ADD(๋‚ ์งœ, INTERVAL) : INTERVAL๋งŒํผ ๋”ํ•œ ๊ฐ’ ๋ฐ˜ํ™˜ */ SELECT DATE_ADD('2022-12-31', INTERVAL -1 MONTH); /* DATEDIFF(๋‚ ์งœa, ๋‚ ์งœb) : ๋‚ ์งœa โ€“ ๋‚ ์งœb ์ผ์ˆ˜ ๋ฐ˜ํ™˜ */ SELECT DATEDIFF('2022-12-31', '2022-12-1'); /***************ํ˜•๋ณ€ํ™˜ ํ•จ์ˆ˜***************/ /* DATE_FORMAT(๋‚ ์งœ, ํ˜•์‹) : ๋‚ ์งœํ˜•์‹์œผ๋กœ ๋ณ€ํ™˜ */ SELECT DATE_FORMAT('2022-12-31', '%m-%d-%y'); SELECT DATE_FORMAT('2022-12-31', '%M-%D-%Y'); /* CAST(ํ˜•์‹a, ํ˜•์‹b) : ํ˜•์‹a๋ฅผ ํ˜•์‹b๋กœ ๋ณ€ํ™˜ */ SELECT CAST('2022-12-31 12:00:00' AS DATE); /***************์ผ๋ฐ˜ ํ•จ์ˆ˜***************/ /* IFNULL(A, B) : A๊ฐ€ NULL์ด๋ฉด B๋ฅผ ๋ฐ˜ํ™˜, ์•„๋‹ˆ๋ฉด A ๋ฐ˜ํ™˜ */ SELECT IFNULL(NULL, 0); SELECT IFNULL('NULL์ด ์•„๋‹˜', 0); /* CASE WHEN [์กฐ๊ฑด1] THEN [๋ฐ˜ํ™˜1] WHEN [์กฐ๊ฑด2] THEN [๋ฐ˜ํ™˜2] ELSE [๋‚˜๋จธ์ง€] END : ์—ฌ๋Ÿฌ ์กฐ๊ฑด๋ณ„๋กœ ๋ฐ˜ํ™˜๊ฐ’ ์ง€์ • */ SELECT * ,CASE WHEN GENDER = 'MAN' THEN '๋‚จ์„ฑ' ELSE '์—ฌ์„ฑ' END FROM CUSTOMER; /***************ํ•จ์ˆ˜ ์ค‘์ฒฉ ์‚ฌ์šฉ***************/ SELECT * ,YEAR(JOIN_DATE) AS ๊ฐ€์ž…์—ฐ๋„ ,LENGTH( YEAR(JOIN_DATE) ) AS ๊ฐ€์ž…์—ฐ๋„_๋ฌธ์ž์ˆ˜ FROM CUSTOMER;
SQL
๋ณต์‚ฌ

๋ณต์ˆ˜ํ–‰ ํ•จ์ˆ˜

์—ฌ๋Ÿฌ ํ–‰๋“ค์ด ํ•˜๋‚˜์˜ ๊ฒฐ๊ณผ๊ฐ’์œผ๋กœ ๋ฐ˜ํ™˜๋˜๋Š” ํ•จ์ˆ˜. ์ฃผ๋กœ GROUP BY ์ ˆ๊ณผ ํ•จ๊ป˜ ์‚ฌ์šฉ๋จ
ex. SUM(), COUNT() ..
/***************์ง‘๊ณ„ ํ•จ์ˆ˜***************/ SELECT COUNT(ORDER_NO) AS ๊ตฌ๋งคํšŸ์ˆ˜ /* ํ–‰์ˆ˜ */ ,COUNT(DISTINCT MEM_NO) AS ๊ตฌ๋งค์ž์ˆ˜ /* ์ค‘๋ณต์ œ๊ฑฐ๋œ ํ–‰์ˆ˜ */ ,SUM(SALES_QTY) AS ๊ตฌ๋งค์ˆ˜๋Ÿ‰ /* ํ•ฉ๊ณ„ */ ,AVG(SALES_QTY) AS ํ‰๊ท ๊ตฌ๋งค์ˆ˜๋Ÿ‰ /* ํ‰๊ท  */ ,MAX(ORDER_DATE) AS ์ตœ๊ทผ๊ตฌ๋งค์ผ์ž /* ์ตœ๋Œ€ */ ,MIN(ORDER_DATE) AS ์ตœ์ดˆ๊ตฌ๋งค์ผ์ž /* ์ตœ์†Œ */ FROM SALES; /* DISTINCT: ์ค‘๋ณต์ œ๊ฑฐ */ /***************๊ทธ๋ฃน ํ•จ์ˆ˜***************/ /* WITH ROLLUP : GROUP BY ์—ด๋“ค์„ ์˜ค๋ฅธ์ชฝ์—์„œ ์™ผ์ชฝ์ˆœ์œผ๋กœ ๊ทธ๋ฃน (์†Œ๊ณ„, ํ•ฉ๊ณ„)*/ SELECT YEAR(JOIN_DATE) AS ๊ฐ€์ž…์—ฐ๋„ ,ADDR ,COUNT(MEM_NO) AS ํšŒ์›์ˆ˜ FROM CUSTOMER GROUP BY YEAR(JOIN_DATE) ,ADDR WITH ROLLUP; /***************์ง‘๊ณ„ ํ•จ์ˆ˜ + GROUP BY***************/ SELECT MEM_NO ,SUM(SALES_QTY) AS ๊ตฌ๋งค์ˆ˜๋Ÿ‰ FROM SALES GROUP BY MEM_NO; /* ํ™•์ธ */ SELECT * FROM SALES WHERE MEM_NO = '1000970';
SQL
๋ณต์‚ฌ

์œˆ๋„์šฐ ํ•จ์ˆ˜

ํ–‰๊ณผ ํ–‰๊ฐ„์˜ ๊ด€๊ณ„๋ฅผ ์ •์˜ํ•˜์—ฌ ๊ฒฐ๊ณผ ๊ฐ’์„ ๋ฐ˜ํ™˜ํ•˜๋Š” ํ•จ์ˆ˜.
ex. ์ˆœ์œ„ํ•จ์ˆ˜(RANK()..),
/***************์ˆœ์œ„ ํ•จ์ˆ˜***************/ /* ROW_NUMBER: ๋™์ผํ•œ ๊ฐ’์ด๋ผ๋„ ๊ณ ์œ ํ•œ ์ˆœ์œ„ ๋ฐ˜ํ™˜ (1,2,3,4,5...) */ /* RANK: ๋™์ผํ•œ ๊ฐ’์ด๋ฉด ๋™์ผํ•œ ์ˆœ์œ„ ๋ฐ˜ํ™˜ (1,2,3,3,5...) */ /* DENSE_RANK: ๋™์ผํ•œ ๊ฐ’์ด๋ฉด ๋™์ผํ•œ ์ˆœ์œ„ ๋ฐ˜ํ™˜(+ ํ•˜๋‚˜์˜ ๋“ฑ์ˆ˜๋กœ ์ทจ๊ธ‰) (1,2,3,3,4...) */ SELECT ORDER_DATE ,ROW_NUMBER() OVER (ORDER BY ORDER_DATE ASC) AS ๊ณ ์œ ํ•œ_์ˆœ์œ„_๋ฐ˜ํ™˜ ,RANK() OVER (ORDER BY ORDER_DATE ASC) AS ๋™์ผํ•œ_์ˆœ์œ„_๋ฐ˜ํ™˜ ,DENSE_RANK() OVER (ORDER BY ORDER_DATE ASC) AS ๋™์ผํ•œ_์ˆœ์œ„_๋ฐ˜ํ™˜_ํ•˜๋‚˜์˜๋“ฑ์ˆ˜ FROM SALES; /* ์ˆœ์œ„ ํ•จ์ˆ˜+ PARTITION BY: ๊ทธ๋ฃน๋ณ„ ์ˆœ์œ„ */ SELECT MEM_NO ,ORDER_DATE ,ROW_NUMBER() OVER (PARTITION BY MEM_NO ORDER BY ORDER_DATE ASC) AS ๊ณ ์œ ํ•œ_์ˆœ์œ„_๋ฐ˜ํ™˜ ,RANK() OVER (PARTITION BY MEM_NO ORDER BY ORDER_DATE ASC) AS ๋™์ผํ•œ_์ˆœ์œ„_๋ฐ˜ํ™˜ ,DENSE_RANK() OVER (PARTITION BY MEM_NO ORDER BY ORDER_DATE ASC) AS ๋™์ผํ•œ_์ˆœ์œ„_๋ฐ˜ํ™˜_ํ•˜๋‚˜์˜๋“ฑ์ˆ˜ FROM SALES; /***************์ง‘๊ณ„ ํ•จ์ˆ˜(๋ˆ„์ )***************/ SELECT ORDER_DATE ,SALES_QTY ,'-' AS ๊ตฌ๋ถ„ ,COUNT(ORDER_NO) OVER (ORDER BY ORDER_DATE ASC) AS ๋ˆ„์ _๊ตฌ๋งคํšŸ์ˆ˜ ,SUM(SALES_QTY) OVER (ORDER BY ORDER_DATE ASC) AS ๋ˆ„์ _๊ตฌ๋งค์ˆ˜๋Ÿ‰ ,AVG(SALES_QTY) OVER (ORDER BY ORDER_DATE ASC) AS ๋ˆ„์ _ํ‰๊ท ๊ตฌ๋งค์ˆ˜๋Ÿ‰ ,MAX(SALES_QTY) OVER (ORDER BY ORDER_DATE ASC) AS ๋ˆ„์ _๊ฐ€์žฅ๋†’์€๊ตฌ๋งค์ˆ˜๋Ÿ‰ ,MIN(SALES_QTY) OVER (ORDER BY ORDER_DATE ASC) AS ๋ˆ„์ _๊ฐ€์žฅ๋‚ฎ์€๊ตฌ๋งค์ˆ˜๋Ÿ‰ FROM SALES; /* ์ง‘๊ณ„ ํ•จ์ˆ˜(๋ˆ„์ )+ PARTITION BY: ๊ทธ๋ฃน๋ณ„ ์ง‘๊ณ„ ํ•จ์ˆ˜(๋ˆ„์ ) */ SELECT MEM_NO ,ORDER_DATE ,SALES_QTY ,'-' AS ๊ตฌ๋ถ„ ,COUNT(ORDER_NO) OVER (PARTITION BY MEM_NO ORDER BY ORDER_DATE ASC) AS ๋ˆ„์ _๊ตฌ๋งคํšŸ์ˆ˜ ,SUM(SALES_QTY) OVER (PARTITION BY MEM_NO ORDER BY ORDER_DATE ASC) AS ๋ˆ„์ _๊ตฌ๋งค์ˆ˜๋Ÿ‰ ,AVG(SALES_QTY) OVER (PARTITION BY MEM_NO ORDER BY ORDER_DATE ASC) AS ๋ˆ„์ _ํ‰๊ท ๊ตฌ๋งค์ˆ˜๋Ÿ‰ ,MAX(SALES_QTY) OVER (PARTITION BY MEM_NO ORDER BY ORDER_DATE ASC) AS ๋ˆ„์ _๊ฐ€์žฅ๋†’์€๊ตฌ๋งค์ˆ˜๋Ÿ‰ ,MIN(SALES_QTY) OVER (PARTITION BY MEM_NO ORDER BY ORDER_DATE ASC) AS ๋ˆ„์ _๊ฐ€์žฅ๋‚ฎ์€๊ตฌ๋งค์ˆ˜๋Ÿ‰ FROM SALES;
SQL
๋ณต์‚ฌ