์ฐ์ฐ์ ๋ฐ ํจ์
์ฐ์ฐ์
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
๋ณต์ฌ