IT/SQL

[MySQL] ๊ธฐ๋ณธ SQL ๋ฌธ๋ฒ• ์ด์ •๋ฆฌ & ๋ชจ์Œ

zi0_0 2025. 1. 2. 09:06

 

โœ”๏ธ ์ฃผ์˜
 - SELECT ์ ˆ์—์„œ ์ƒ์„ฑํ•œ ๋ณ„์นญ์„ Where ์ ˆ์—์„œ ์‚ฌ์šฉํ•  ์ˆ˜ ์—†์Œ
- Partition๊ณผ ๊ฐ™์€ ์œˆ๋„์šฐ ํ•จ์ˆ˜๋Š” Where ์ ˆ์—์„œ ์‚ฌ์šฉํ•  ์ˆ˜ ์—†์Œ 
- ์„œ๋ธŒ์ฟผ๋ฆฌ ๋งŒ๋“ค๊ณ , ๋ณ„์นญ ์•ˆ ์‚ฌ์šฉํ•ด๋„ ๋งŒ๋“ค์–ด์•ผ ํ•จ 

 

โญ๏ธ SQL ์ฟผ๋ฆฌ ์ˆœ์„œ
1. SELECT
2. FROM
3. JOIN
4. WHERE
5. GROUP BY
6. HAVING
7. ORDER BY
8. LIMIT

 

 

๐Ÿ“ NULL ์ฒ˜๋ฆฌ ๊ด€๋ จ ํ•จ์ˆ˜ 

NULL ์ฒ˜๋ฆฌ 

: ์ปฌ๋Ÿผ์˜ ๊ฐ’์ด NULL์ธ ๊ฒฝ์šฐ ์ฒ˜๋ฆฌํ•ด์ฃผ๋Š” ํ•จ์ˆ˜ 
๋ฐ์ดํ„ฐ๋ฒ ์ด์Šค ์ฟผ๋ฆฌ์—์„œ ๋ˆ„๋ฝ๋˜๊ฑฐ๋‚˜ ์ •์˜๋˜์ง€ ์•Š์€ ๊ฐ’์„ ์ฒ˜๋ฆฌํ•˜๋Š” ๋ฐ ํ™œ์šฉ 
  • NULL ๊ฐ’ ํ™•์ธ
    • `IS NULL` : ๊ฐ’์ด NULL์ธ์ง€ ํ™•์ธ 
    • `IS NOT NULL` : ๊ฐ’์ด NULL์ด ์•„๋‹Œ์ง€ ํ™•์ธ 
    • `ISNULL(expr)` : NULL์ด๋ฉด 1 ๋ฐ˜ํ™˜, ์•„๋‹ˆ๋ฉด 0์„ ๋ฐ˜ํ™˜ 
  • NULL๊ฐ’ ๋Œ€์ฒด
    • `IFNULL` : ์ปฌ๋Ÿผ ๊ฐ’์ด NULL์„ ๋ฐ˜ํ™˜ํ•  ๋•Œ ๋‹ค๋ฅธ ๊ฐ’์œผ๋กœ ์ถœ๋ ฅํ•  ์ˆ˜ ์žˆ๋„๋ก ํ•˜๋Š” ํ•จ์ˆ˜
      • ex) SELECT IFNULL(์นผ๋Ÿผ๋ช…, 'null์ผ ๊ฒฝ์šฐ ๋Œ€์ฒด ๊ฐ’') FROM ํ…Œ์ด๋ธ”๋ช…;
    • `COALESCE` : ์ง€์ •ํ•œ ํ‘œํ˜„์‹๋“ค ์ค‘์— NULL์ด ์•„๋‹Œ ์ฒซ ๋ฒˆ์งธ ๊ฐ’์„ ๋ฐ˜ํ™˜
      • ex) SELECT COALESCE(์นผ๋Ÿผ๋ช…1, ์นผ๋Ÿผ๋ช…2, ์นผ๋Ÿผ๋ช…3, 0) : ์ด๋Ÿฐ์‹์œผ๋กœ ์ˆœ์„œ๋Œ€๋กœ NULL์ด ์•„๋‹Œ๊ฑฐ ๋ฐ˜ํ™˜
    • `NULLIF`
      • NULL(expr1, expr2) : ๋‘ ํ‘œํ˜„์‹์ด ๊ฐ™์œผ๋ฉด NULL ๋ฐ˜ํ™˜, ๋‹ค๋ฅด๋ฉด expr1 ๋ฐ˜ํ™˜ 
      • ์ฃผ๋กœ ๋‘ ๊ฐ’์ด ๊ฐ™์„ ๋•Œ NULL ์ฒ˜๋ฆฌ๋ฅผ ์œ„ํ•ด ์‚ฌ์šฉ
      • ex)SELECT product_id, NULLIF(discount, price) AS adjusted_discount FROM products;
    • `CASE` ๊ตฌ๋ฌธ ์‚ฌ์šฉ
      • case ๊ตฌ๋ฌธ์œผ๋กœ NULL ๊ฐ’์„ ์›ํ•˜๋Š” ๋ฐฉ์‹์œผ๋กœ ์ฒ˜๋ฆฌ ๊ฐ€๋Šฅ 
      • ex) CASE WHEN salary IS NULL THEN ~~~

 

 

๐Ÿ“ ์กฐ๊ฑด ์—ฐ์‚ฐ์ž 

BETWEEN 

: WHERE ์ ˆ ๋‚ด ํŠน์ • ๊ฐ’์ด ์ฃผ์–ด์ง„ ๋ฒ”์œ„ ๋‚ด์— ํฌํ•จ๋˜๋Š”์ง€ ํ™•์ธํ•  ๋•Œ ์‚ฌ์šฉ
์ˆซ์ž, ๋‚ ์งœ, ์‹œ๊ฐ„, ๋ฌธ์ž์—ด ๋“ฑ ๋‹ค์–‘ํ•œ ๋ฐ์ดํ„ฐ ํƒ€์ž…์—์„œ ์‚ฌ์šฉ ๊ฐ€๋Šฅ 
  • ex) WHERE age BETWEEN 20 AND 29;
  • ์™œ ์‚ฌ์šฉ? : WHERE ~ AND 20<= AGE <= 29; ์ด๋Ÿฐ ์‹์œผ๋กœ ๊ธฐํ˜ธ๋กœ๋Š” ์—ฐ์†์ ์ธ ์กฐ๊ฑด ๋น„๊ต๊ฐ€ ์•ˆ๋จ 

 

IN

: ํŠน์ • ๊ฐ’์ด ์ฃผ์–ด์ง„ ๊ฐ’ ๋ชฉ๋ก ์•ˆ์— ์žˆ๋Š”์ง€ ํ™•์ธํ•˜๋Š” ๋ฐ ์‚ฌ์šฉ
  • SELECT ์นผ๋Ÿผ๋ช… `IN` (val1, val2, val3, ..., val N)

 

* ํ™œ์šฉ : ์•„๋ž˜์ฒ˜๋Ÿผ ๊ด„ํ˜ธ๋ฅผ ์‚ฌ์šฉํ•ด์„œ ๋‘๊ฐ€์ง€ ์นผ๋Ÿผ์— ๋Œ€ํ•ด ์กฐ๊ฑด์„ ์„ค์ •ํ•  ์ˆ˜๋„ ์žˆ์Œ 

SELECT * FOOD_TYPE
FROM REST_INFO
WHERE (FOOD_TYPE, FAVORITES) IN (
    SELECT 
    	FOOD_TYPE
        , MAX(FAVORITES)
    FROM REST_INFO
    GROUP BY FOOD_TYPE
    )
ORDER BY FOOD_TYPE DESC

 

IF 

: ์กฐ๊ฑด์„ ํ‰๊ฐ€ํ•˜๊ณ , ํ•ด๋‹น ์กฐ๊ฑด์ด ์ฐธ์ธ์ง€ ๊ฑฐ์ง“์ธ์ง€์— ๋”ฐ๋ผ ๋‹ค๋ฅธ ๊ฐ’์„ ๋ฐ˜ํ™˜
  • IF(condition, true_value, false_value) : condition์ด true ๋ฉด 2๋ฒˆ์งธ ๊ฐ’, false๋ฉด 3๋ฒˆ์งธ ๊ฐ’
  • ex) SELECT IF(1 > 0, 'Yes', 'No') AS result;
    • IF(salary > 5000, 'High', 'Low')
SELECT
    ANIMAL_ID
    , NAME
    , IF(SEX_UPON_INTAKE LIKE 'Neutered%' 
         OR SEX_UPON_INTAKE LIKE 'Spayed%', 'O', 'X') AS ์ค‘์„ฑํ™”
FROM ANIMAL_INS
ORDER BY ANIMAL_ID

 

 

๐Ÿ“ ์ง‘๊ณ„ & ์œˆ๋„์šฐ ํ•จ์ˆ˜ 

MAX, MIN, ABS, AVG, SUM

* ์ตœ๋Œ“๊ฐ’์— ํ•ด๋‹นํ•˜๋Š” ์ˆ˜๊ฐ€ 1๊ฐœ๋กœ ๋ณด์žฅ๋  ๋•Œ 
SELECT MAX(DATETIME) AS ์‹œ๊ฐ„
FROM ANIMAL_INS

* ์ตœ๋Œ“๊ฐ’์— ํ•ด๋‹นํ•˜๋Š” ์ˆ˜๊ฐ€ ์—ฌ๋Ÿฌ๊ฐœ ์žˆ์„ ์ˆ˜ ์žˆ์„ ๋•Œ 
SELECT DATETIME AS ์‹œ๊ฐ„
FROM ANIMAL_INS
WHERE DATETIME = (SELECT MAX(DATETIME)
                  FROM ANIMAL_INS);
* MAX ํ•จ์ˆ˜ ํ™œ์šฉ ์˜ˆ์ œ (์ข‹์œผ๋‹ˆ๊นŒ ์ฐธ๊ณ )
SELECT
    CAR_ID 
    , CASE
        WHEN MAX('2022-10-16' BETWEEN START_DATE AND END_DATE) = 1 THEN '๋Œ€์—ฌ์ค‘'
        ELSE '๋Œ€์—ฌ ๊ฐ€๋Šฅ'
        END AS AVAILABILITY
FROM CAR_RENTAL_COMPANY_RENTAL_HISTORY
GROUP BY CAR_ID
ORDER BY CAR_ID DESC

group by๋ฅผ ์ˆ˜ํ–‰ํ•˜๋ฉด car_id ๋ผ๋ฆฌ ๋ฌถ์ด๊ฒŒ ๋˜๋Š”๋ฐ ์ด๋•Œ when์˜ max ์ ˆ ์•ˆ์— ์กฐ๊ฑด์„ ํ†ตํ•ด ์•„๋ž˜์ฒ˜๋Ÿผ ์ถœ๋ ฅ
- ์ฐธ์ด๋ฉด 1 
- ๊ฑฐ์ง“์ด๋ฉด 0
๋”ฐ๋ผ์„œ max ํ•จ์ˆ˜์— ์˜ํ•ด ๋Œ€์—ฌ์ค‘์ด ํ•œ๋ฒˆ์ด๋ผ๋„ ์žˆ์—ˆ์œผ๋ฉด 1์„ ์ถœ๋ ฅํ•˜๊ฒŒ ๋˜๋ฉฐ
๊ทธ๊ฒƒ์ด =1 ์— ์กฐ๊ฑด์— ๋งž์•„ then '๋Œ€์—ฌ์ค‘'์œผ๋กœ ๋‚˜์˜ค๊ฒŒ ๋˜๋Š” ๊ฒƒ

 

COUNT

: ํ–‰์˜ ๊ฐœ์ˆ˜๋ฅผ ๊ณ„์‚ฐํ•˜๋Š” ๋ฐ ์‚ฌ์šฉ
  • `COUNT(*)` : ํ…Œ์ด๋ธ”์˜ ๋ชจ๋“  ํ–‰์„ ๊ณ„์‚ฐ (NULL์˜ ์—ฌ๋ถ€์— ์ƒ๊ด€์—†์ด ๋ชจ๋“  ํ–‰์„ ํฌํ•จํ•ด์„œ ๊ณ„์‚ฐ)
  • `COUNT(์นผ๋Ÿผ๋ช…)` : ํ•ด๋‹น ์—ด์— NULL ๊ฐ’์ด ์žˆ๋Š” ๊ฒฝ์šฐ ์ œ์™ธ 
  • `COUNT(DISTINCT ์นผ๋Ÿผ๋ช…)` : ํŠน์ • ์—ด์˜ ๊ณ ์œ ๊ฐ’ ๊ฐœ์ˆ˜๋ฅผ ๊ณ„์‚ฐ, ์ค‘๋ณต๋œ ๊ฐ’์€ ์ œ์™ธํ•˜๊ณ  ๊ณ„์‚ฐ 

* ์—ด์˜ NULL ์œ ๋ฎค๋ฅผ ๊ณ ๋ คํ•ด์„œ COUNT(*)์™€ COUNT(์นผ๋Ÿผ๋ช…) ์ค‘ ์ ์ ˆํ•˜๊ฒŒ ๊ณ ๋ คํ•ด์•ผ ํ•œ๋‹ค 

 

OVER

: ์œˆ๋„์šฐ ํ•จ์ˆ˜์˜ ๋™์ž‘ ๋ฒ”์œ„๋ฅผ ์ง€์ • 
- ์œˆ๋„์šฐ ํ•จ์ˆ˜๋ฅผ ์‚ฌ์šฉํ•˜๊ธฐ ์œ„ํ•ด ํ•„์š”ํ•จ ๊ตฌ๋ฌธ

 

* ์ฐธ๊ณ ํ•˜๊ธฐ ์ข‹์€ ์ฟผ๋ฆฌ ์˜ˆ์ œ : Group by ๋ฌด์‹œํ•˜๊ณ  ์ „์ฒด ์ง‘๊ณ„ํ•˜๊ธฐ 

* ๊ฐ ์ง์›์ด ๋‹ด๋‹นํ•œ ๋งค์ถœ์˜ ์ดํ•ฉ์„ ์ „์ฒด ๋งค์ถœ ๋Œ€๋น„ ์–ผ๋งˆ์ธ์ง€ ๋น„์œจ ๊ณ„์‚ฐ 
SELECT 
    E.EmployeeId
    , SUM(I.Total) / SUM(SUM(I.Total)) OVER() AS TOTAL_SALES
FROM 
    Invoice AS I
    JOIN Customer AS C
    ON I.CustomerId = C.CustomerId
    JOIN Employee AS E
    ON C.SupportRepId = E.EmployeeId
GROUP BY E.EmployeeId;

 

  • GROUP BY ์— ์˜ํ•ด ์ง์› ์•„์ด๋””๋ฅผ ๊ธฐ์ค€์œผ๋กœ ์ง‘๊ณ„ ๋˜์ง€๋งŒ, `SUM(~) OVER()`๋ฅผ ์ˆ˜ํ–‰ํ•˜๋ฉด ์ „์ฒด ํ•ฉ๊ณ„๋ฅผ ๊ตฌํ•  ์ˆ˜ ์žˆ์Œ
  • ์ฃผ์˜ ) SUM(I.Total) OVER () ~ ์ด๋ ‡๊ฒŒ ์ˆ˜ํ–‰ํ•˜๋ฉด ์˜ค๋ฅ˜๋‚จ -> `SUM(SUM(~)) OVER()` ์ด๋ ‡๊ฒŒ ํ•ด์•ผ ํ•จ 

 

PARTITON BY

: ์œˆ๋„์šฐ ํ•จ์ˆ˜์˜ ์ผ๋ถ€๋กœ, ๋ฐ์ดํ„ฐ๋ฅผ ํŠน์ • ๊ธฐ์ค€์œผ๋กœ ๋‚˜๋ˆ„๊ณ  ๊ฐ ํŒŒํ‹ฐ์…˜ ๋‚ด์—์„œ ๊ณ„์‚ฐ์„ ์ˆ˜ํ–‰

* PARTITION BY๋Š” WHERE์ ˆ์— ์‚ฌ์šฉํ•  ์ˆ˜ ์—†์Œ 

๐Ÿ’ก PARTITION BY์™€ GROUP BY์˜ ์ฐจ์ด
- `GROUP BY` : ๋ฐ์ดํ„ฐ๋ฅผ ๊ทธ๋ฃนํ™”ํ•˜๊ณ , ๊ทธ๋ฃน๋‹น ํ•˜๋‚˜์˜ ๊ฒฐ๊ณผ๋ฅผ ๋ฐ˜ํ™˜
- `PARTITION BY` : ๋ฐ์ดํ„ฐ๋ฅผ ํŒŒํ‹ฐ์…˜์œผ๋กœ ๋‚˜๋ˆ„์ง€๋งŒ, ๋ชจ๋“  ํ–‰์„ ์œ ์ง€ํ•˜๋ฉฐ ์ถ”๊ฐ€ ๊ณ„์‚ฐ์„ ์ˆ˜ํ–‰ 

 

* ์ข‹์€ ํ€ด๋ฆฌ ์˜ˆ์‹œ ์ฝ”๋“œ ๋ชจ์Œ

-- ์˜ˆ์ œ. ์Œ์‹ ์ข…๋ฅ˜๋ณ„๋กœ ๊ฐ€์žฅ ๋งŽ์€ ์ฆ๊ฒจ์ฐพ๊ธฐ๋ฅผ ๋ฐ›์€ ์Œ์‹์ ์„ ์กฐํšŒํ•˜๋Š” ์ฟผ๋ฆฌ
SELECT
    FOOD_TYPE
    , REST_ID
    , REST_NAME
    , FAVORITES
FROM (
    SELECT *
        , MAX(FAVORITES) OVER (PARTITION BY FOOD_TYPE) AS MAX_FAV
    FROM REST_INFO
    ) AS M
WHERE FAVORITES = MAX_FAV
ORDER BY FOOD_TYPE DESC
* GROUP BY๊ฐ€ ์žˆ๋Š” ์ฟผ๋ฆฌ์˜ ๊ฒฝ์šฐ 

-- ์˜ˆ์ œ 1. ๊ฐ ์—ฐ๋„๋ณ„๋กœ ํŠธ๋ž™๋ณ„ ํŒ๋งค๋Ÿ‰๊ณผ ์ด ๋งค์ถœ ์ค‘์— ๊ฐ€์žฅ ๋งŽ์ด ํŒ๋งค๋œ ํŠธ๋ž™
SELECT *
FROM (
	SELECT 
		YEAR(InvoiceDate) AS year
		, TrackId AS track_id
		, COUNT(Quantity) AS sales_cnt
		, SUM(Quantity * UnitPrice) AS total_sales
		, MAX(COUNT(Quantity)) OVER (PARTITION BY YEAR(InvoiceDate)) AS max_cnt
	FROM data
	GROUP BY 1, 2
    ) AS z
WHERE sales_cnt = max_cnt;

-- ์˜ˆ์ œ 2. ๊ฐ ์ง์›๋ณ„ ๊ณ ๊ฐ์˜ ์ด๋งค์ถœ ์ค‘์— ์ตœ๋Œ€ ๋งค์ถœ์„ ๊ธฐ๋กํ•œ ๊ณ ๊ฐ 
SELECT *
FROM (
	SELECT 
		SupportRepId
        	, CustomerId AS cust_id
		, SUM(Total) AS sales_sum
		, MAX(SUM(Total)) OVER (PARTITION BY SupportRepId) AS sales_max
	FROM data
	GROUP BY CustomerId
	) AS sub
WHERE sales_sum = sales_max

 

RANK

: ์ˆœ์œ„ ๊ณ„์‚ฐ ๊ด€๋ จ ํ•จ์ˆ˜ 
  • `ROW_NUMBER()`: ๊ณ ์œ  ์ˆœ์œ„ ๊ณ„์‚ฐ
  • `RANK()`: ์ˆœ์œ„๋ฅผ ๋ถ€์—ฌํ•˜๋ฉฐ ์ค‘๋ณต ์ˆœ์œ„๋ฅผ ํ—ˆ์šฉ(๋‹ค์Œ ์ˆœ์œ„๋ฅผ ๊ฑด๋„ˆ๋œ€)
  • `DENSE_RANK()`: ์ค‘๋ณต ์ˆœ์œ„๋ฅผ ํ—ˆ์šฉํ•˜๋˜ ๋‹ค์Œ ์ˆœ์œ„๋ฅผ ๊ฑด๋„ˆ๋›ฐ์ง€ ์•Š์Œ
  • `PERCENT_RANK()`: ํŠน์ • ํ–‰์˜ ์ˆœ์œ„๋ฅผ ๋ฐฑ๋ถ„์œจ (%)๋กœ ๊ณ„์‚ฐ * 0.0~1.0 ๊ฐ’ ๋ฐ˜ํ™˜ 

 

* ์ฐธ๊ณ ํ•˜๊ธฐ ์ข‹์€ ์˜ˆ์ œ 

* ํผ์„ผํŠธ๋กœ ๋“ฑ๊ธ‰ ๋ถ„๋ฅ˜ํ•˜๊ธฐ 
SELECT 
    ID
    , (CASE 
        WHEN PER_RANK <= 0.25 THEN 'CRITICAL'
        WHEN PER_RANK <= 0.5 THEN 'HIGH'
        WHEN PER_RANK <= 0.75 THEN 'MEDIUM'
        ELSE 'LOW'
      END) AS COLONY_NAME
FROM (SELECT 
        ID, 
        PERCENT_RANK() OVER (ORDER BY SIZE_OF_COLONY DESC) AS PER_RANK
     FROM ECOLI_DATA
     ) AS D
ORDER BY ID
SELECT *
FROM (
	SELECT 
		YEAR(InvoiceDate) AS year
		, t.TrackId AS track_id
		, t.Name AS track_name
		, art.Name AS artist_name
		, COUNT(il.Quantity) AS sales_cnt
		, SUM(il.Quantity * il.UnitPrice) AS total_sales
		, MAX(COUNT(il.Quantity)) OVER (PARTITION BY YEAR(InvoiceDate)) AS max_cnt
	FROM data
	GROUP BY 1, 2
    ) AS z
WHERE sales_cnt = max_cnt;

 

LAG / LEAD

: ์ด์ „ / ๋‹ค์Œ ํ–‰์˜ ๊ฐ’์„ ๊ฐ€์ ธ์˜ค๋Š” ์œˆ๋„์šฐ ํ•จ์ˆ˜ 
- ํ˜„์žฌ ํ–‰์„ ๊ธฐ์ค€์œผ๋กœ ํŠน์ • ์—ด์˜ ์ด์ „ / ๋‹ค์Œ ๊ฐ’์„ ์ฐธ์กฐํ•  ์ˆ˜ ์žˆ๋„๋ก ํ•จ 
  • `LAG(์นผ๋Ÿผ๋ช…, offset, ๊ธฐ๋ณธ๊ฐ’)` 
    • offset : ๋ช‡ ๋ฒˆ์งธ ์ด์ „์˜ ๊ฐ’์„ ๊ฐ€์ ธ์˜ฌ์ง€ (default: 1)
    • ๊ธฐ๋ณธ๊ฐ’ : ์ด์ „ ๊ฐ’์ด ์—†์„ ๋•Œ ๋ฐ˜ํ™˜ํ•  ๊ธฐ๋ณธ ๊ฐ’ (default: NULL)
  • ํ™œ์šฉ : LAG(์นผ๋Ÿผ๋ช…, offset, ๊ธฐ๋ณธ๊ฐ’) OVER (PARTITION BY ์นผ๋Ÿผ๋ช… ORDER BY ์นผ๋Ÿผ๋ช…)

 

* ๊ธฐ๋ณธ ์˜ˆ์ œ : ์ฆ๊ฐ์œจ ๊ตฌํ•˜๊ธฐ

SELECT (ํ˜„์žฌ๋งค์ถœ - ๊ณผ๊ฑฐ๋งค์ถœ) / ๊ณผ๊ฑฐ๋งค์ถœ * 100
FROM (SELECT 
	*
        , LAG(ํ˜„์žฌ๋งค์ถœ, 1) OVER (ORDER BY ๋‚ ์งœ์—ด) AS ๊ณผ๊ฑฐ๋งค์ถœ
	FROM data
    ) AS sub

 

 

* ์–ด๋ ค์šด ํ™œ์šฉ ์˜ˆ์ œ 

WITH genre_sale AS (
SELECT 
    g.GenreId
    , YEAR(InvoiceDate) AS year
    , SUM(il.UnitPrice * il.Quantity) AS cur_sales
    , LAG(SUM(il.UnitPrice * il.Quantity)) OVER (PARTITION BY g.GenreId ORDER BY YEAR(InvoiceDate)) AS pre_sales
FROM data
GROUP BY 1, 2
)

SELECT
    GenreId
    , year
    , cur_sales
    , ROUND((cur_sales - pre_sales) / pre_sales * 100, 2) AS growth_rate
FROM genre_sale;

 

 

๐Ÿ“ ์ˆซ์ž ์ฒ˜๋ฆฌ ๊ด€๋ จ ํ•จ์ˆ˜ 

TRUNCATE()

: ์ˆซ์ž๋ฅผ ๋ฒ„๋ฆฌ๋ฅผ ์ž๋ฆฟ์ˆ˜ ์•„๋ž˜๋กœ ๋ฒ„๋ฆผ 
* ๋ฒ„๋ฆผ์ด๋ž€ ๋ฐ˜์˜ฌ๋ฆผํ•˜์ง€ ์•ˆํ˜ธ, ์ง€์ •ํ•œ ์ž๋ฆฌ์ˆ˜ ์ดํ›„์˜ ๊ฐ’์„ ๋‹จ์ˆœํžˆ ์ œ๊ฑฐํ•˜๋Š” ๊ฒƒ์„ ์˜๋ฏธ != ROUND()
  • SELECT TRUNCATE(12345.1234567 ,1)  >>  12345.1
  • SELECT TRUNCATE(12345.1234567 ,4)  >>  12345.1234
  • SELECT TRUNCATE(12345.1234567 ,-1)  >>  12340
  • SELECT TRUNCATE(12345.1234567 ,-2)   >>  12300 

* ์ •์ˆ˜ ์ž๋ฆฌ๋ฅผ ๋ฒ„๋ฆด ๋•Œ๋Š” - ๊ธฐํ˜ธ ํ™œ์šฉ 

  • `ROUND(์ˆซ์ž, ์ž๋ฆฟ์ˆ˜)`
    • SELECT TRUNCATE(123.567, 2) >> 123.57

 

 

๐Ÿ“ ๋ฌธ์ž์—ด ์ฒ˜๋ฆฌ ๊ด€๋ จ ํ•จ์ˆ˜ 

LIKE

: ๋ฌธ์ž์—ด ํŒจํ„ด์„ ๊ฒ€์ƒ‰, ๋น„๊ตํ•  ๋•Œ ์‚ฌ์šฉํ•˜๋Š” ์—ฐ์‚ฐ์ž 
์ฃผ๋กœ ๊ฒ€์ƒ‰ ์กฐ๊ฑด์—์„œ ํŠน์ • ํŒจํ„ด๊ณผ ์ผ์น˜ํ•˜๋Š” ๊ฐ’์„ ์ฐพ๊ธฐ ์œ„ํ•ด ์‚ฌ์šฉ๋จ
  • `%` ํ™œ์šฉ๋ฒ•
    • '์†'์œผ๋กœ ์‹œ์ž‘ํ•˜๋Š” 4๊ธ€์ž ๋ฐ์ดํ„ฐ ์กฐํšŒ : ~ LIKE '์†___'
    • '์†'์œผ๋กœ ์‹œ์ž‘ํ•˜๋Š” ๋ชจ๋“  ๋ฐ์ดํ„ฐ ์กฐํšŒ : ~ LIKE '์†%'
    • '๋ธ”'์ด ๋“ค์–ด๊ฐ€๋Š” ๋ชจ๋“  ๋ฐ์ดํ„ฐ ์กฐํšŒ : ~ LIKE '%์†%'
  • `_` ํ™œ์šฉ๋ฒ•
    • %์™€ ๊ฐ™์ง€๋งŒ, ์ •ํ™•ํžˆ 1๊ฐœ์˜ ๋ฌธ์ž๋ฅผ ๋Œ€์ฒดํ•˜๊ณ  ์‹ถ์„ ๋•Œ ์‚ฌ์šฉ

 

REGEXP

: ์ •๊ทœ ํ‘œํ˜„์‹์„ ํ™œ์šฉํ•ด ๋ฌธ์ž์—ด ํŒจํ„ด์„ ๊ฒ€์ƒ‰ํ•˜๋Š” ๋ฐ ํ™œ์šฉ
๊ธฐ์กด ์—ฐ์‚ฐ์ž๋ณด๋‹ค ๋ณต์žกํ•œ ๋ฌธ์ž์—ด ์กฐ๊ฑด ๊ฐ€๋Šฅ 
๊ธฐํ˜ธ ์„ค๋ช… ์˜ˆ์‹œ  
. ์ž„์˜์˜ ํ•œ ๋ฌธ์ž์™€ ์ผ์น˜ 'a.b' acb, a_b
^ ๋ฌธ์ž์—ด์˜ ์‹œ์ž‘์„ ์˜๋ฏธ '^hello' hello world, hello hi
$ ๋ฌธ์ž์—ด์˜ ๋์„ ์˜๋ฏธ 'world$' hello word, hi word
* ์•ž์˜ ๋ฌธ์ž๊ฐ€ 0๋ฒˆ ์ด์ƒ ๋ฐ˜๋ณต๋จ 'ab*c' ac, abc, abbc, abbbc
+ ์•ž์˜ ๋ฌธ์ž๊ฐ€ 1๋ฒˆ ์ด์ƒ ๋ฐ˜๋ณต๋จ 'ab+c' abc, abbc, ac (x)
? ์•ž์˜ ๋ฌธ์ž๊ฐ€ 0๋ฒˆ ๋˜๋Š” 1๋ฒˆ ๋‚˜ํƒ€๋‚จ 'colou?r' color, colour
[abc] 'a', 'b', 'c' ์ค‘ ํ•˜๋‚˜์™€ ์ผ์น˜ '[abc]' apple, banna, cherry 
[a-z] ์†Œ๋ฌธ์ž ์•ŒํŒŒ๋ฒณ ์ค‘ ํ•˜๋‚˜์™€ ์ผ์น˜    
[A-Z] ๋Œ€๋ฌธ์ž ์•ŒํŒŒ๋ฒณ ์ค‘ ํ•˜๋‚˜์™€ ์ผ์น˜    
[0-9] ์ˆซ์ž ์ค‘ ํ•˜๋‚˜์™€ ์ผ์น˜    
| OR ์—ฐ์‚ฐ์ž ์—ญํ•   'cat|dog' i have a cat, dog is good
* ์ž์ฃผ ์‚ฌ์šฉํ•˜๋Š” ๋ฐฉ์‹ 
SELECT CAR_TYPE
FROM CAR_RENTAL_COMPANY_CAR
WHERE OPTIONS REGEXP '์—ด์„ ์‹œํŠธ|๊ฐ€์ฃฝ์‹œํŠธ|ํ†ตํ’์‹œํŠธ'

 

SUBSTR

: ๋ฌธ์ž์—ด์—์„œ ํŠน์ • ๋ถ€๋ถ„์„ ์ถ”์ถœํ•˜๋Š” ์‚ฌ์šฉ
  • `SUBSTR(str, ์‹œ์ž‘ ์œ„์น˜, ๊ธธ์ด)`
    • SELECT SUBSTR('Hello World', 1, 5) = Hello
    • SELECT SUBSTR('Hello World',7 ) = 7๋ฒˆ ์œ„์น˜๋ถ€ํ„ฐ ๋๊นŒ์ง€ 
  • `SUBSTR(str, - ์‹œ์ž‘ ์œ„์น˜, ๊ธธ์ด)`
    • ์Œ์ˆ˜ ์‚ฌ์šฉ : ๋’ค์—์„œ๋ถ€ํ„ฐ ๋ฌธ์ž์—ด ์ž๋ฅด๊ธฐ 
    • SELECT SUBSTR('Hello World', -5, 3) = wor

 

SUBSTRING_INDEX

: ๊ตฌ๋ถ„์ž๋ฅผ ๊ธฐ์ค€์œผ๋กœ ๋ฌธ์ž์—ด์„ ๋ถ„๋ฆฌํ•˜์—ฌ ํŠน์ • ๋ถ€๋ถ„์„ ์ถ”์ถœํ•˜๋Š” ํ•จ์ˆ˜ 
  • `SUBSTRING_INDEX(str, ๊ตฌ๋ถ„์ž, ๊ฐœ์ˆ˜)`
  • SELECT SUBSTRING_INDEX('apple,banana,cherry', ',', 2) = apple,banana (์•ž์—์„œ 2๊ฐœ ์ถ”์ถœ)
  • SELECT SUBSTRING_INDEX('apple,banana,cherry', ',', -1) =  cherry (๋งˆ์ง€๋ง‰ ์ถ”์ถœ) 

 

CONCAT

: ๋‘ ๊ฐœ ์ด์ƒ์˜ ๋ฌธ์ž์—ด์„ ํ•˜๋‚˜๋กœ ์—ฐ๊ฒฐ (ํ•ฉ์น˜๊ธฐ) ํ•˜๋Š” ๋ฐ ์‚ฌ์šฉ
  • `CONCAT`
    • ์—ฌ๋Ÿฌ ๋ฌธ์ž์—ด์„ ๋‹จ์ˆœํžˆ ๋ถ™์—ฌ์„œ ์‚ฌ์šฉ
    • ์œ„์˜ ex) CONCAT(CITY, '-', STREET_ADDRESS1, '-', STREET_ADDRESS2)
  • `CONCAT_WS`
    • ๊ตฌ๋ถ„์ž๋ฅผ ์ง€์ •ํ•˜์—ฌ ์—ฌ๋Ÿฌ ๋ฌธ์ž์—ด์„ ๊ฒฐํ•ฉ
    • ์œ„์˜ ex) CONCAT_WS(' ', CITY, STREET_ADDRESS1, STREET_ADDRESS2)
  • ํ•˜๋‚˜๋ผ๋„ NULL ๊ฐ’์ด ํฌํ•จ๋˜๋ฉด ๊ฒฐ๊ณผ๋Š” NULL์ด ๋ฐ˜ํ™˜๋จ <- `IFNULL` ์ด๋‚˜ `COALESCE` ํ™œ์šฉํ•ด์„œ ์ฒ˜๋ฆฌ

* 3๊ฐœ ์ด์ƒ์˜ ๋ฌธ์ž์—ด์„ ๊ฒฐํ•ฉํ•  ๋•Œ๋Š” concat_ws๊ฐ€ ๋” ๊ฐ„ํŽธํ•˜๋‹ค.

 

 

๐Ÿ“ ๋‚ ์งœ ๊ด€๋ จ ํ•จ์ˆ˜ 

DATE_FORMAT

: ๋‚ ์งœ์™€ ์‹œ๊ฐ„์„ ์ง€์ •๋œ ํ˜•์‹์œผ๋กœ ๋ณ€ํ™˜ํ•˜๋Š” ๋ฐ ์‚ฌ์šฉ 
  • ์ผ๋ฐ˜์ ์œผ๋กœ ๊ฐ€์žฅ ๋งŽ์ด ์‚ฌ์šฉํ•˜๋Š” ํ˜•์‹ : `DATE_FORMAT(๋‚ ์งœ ์ปฌ๋Ÿผ, '%Y-%m-%d')`
  • ํŠน์ • ์—ฐ๋„์˜ ํ–‰๋งŒ ~~ ์ด๋ ‡๊ฒŒ ์žˆ์„ ๋•Œ ์•„๋ž˜ 3๊ฐ€์ง€ ๋ฐฉ์‹ ๋ชจ๋‘ ํ™œ์šฉ ๊ฐ€๋Šฅ (date ex: 2021-02-01)
    • WHERE date `LIKE` '2021%'
    • `DATE_FORMAT`(date, '%Y') = 2021
    • `YEAR`(date)
ํฌ๋งท ์ฝ”๋“œ ์„ค๋ช… ์˜ˆ์‹œ  ๊ฒฐ๊ณผ
%Y 4์ž๋ฆฌ ์—ฐ๋„ '2025-01-02 15:30:45' 2025
%y 2์ž๋ฆฌ ์—ฐ๋„ '2025-01-02 15:30:45' 25
%M ์›” ์ด๋ฆ„ (์ „์ฒด) '2025-01-02 15:30:45' January
%m ์›” (2์ž๋ฆฌ ์ˆซ์ž) '2025-01-02 15:30:45' 01
%b ์›” ์ด๋ฆ„ (์ถ•์•ฝํ˜•) '2025-01-02 15:30:45' Jan
%d ์ผ (2์ž๋ฆฌ ์ˆซ์ž) '2025-01-02 15:30:45' 02
%e ์ผ (์ˆซ์ž, ์•ž์— 0 ์—†์Œ) '2025-01-02 15:30:45' 2
%W ์š”์ผ ์ด๋ฆ„ (์ „์ฒด) '2025-01-02 15:30:45' Thursday
%a ์š”์ผ ์ด๋ฆ„ (์ถ•์•ฝํ˜•) '2025-01-02 15:30:45' Thu
%H ์‹œ๊ฐ„ (24์‹œ๊ฐ„, 2์ž๋ฆฌ) '2025-01-02 15:30:45' 15
%h ์‹œ๊ฐ„ (12์‹œ๊ฐ„, 2์ž๋ฆฌ) '2025-01-02 15:30:45' 03
%i ๋ถ„ (2์ž๋ฆฌ ์ˆซ์ž) '2025-01-02 15:30:45' 30
%s ์ดˆ (2์ž๋ฆฌ ์ˆซ์ž) '2025-01-02 15:30:45' 45
%p ์˜ค์ „ / ์˜คํ›„ (AM / PM) '2025-01-02 15:30:45' PM
%% % ๋ฌธ์ž ์ถœ๋ ฅ  '2025-01-02 15:30:45' %

 

 

์ถ”๊ฐ€ ํ•จ์ˆ˜ 

: DATE_FORMAT์„ ์“ฐ์ง€ ์•Š๊ณ , ๋‚ ์งœ ๋ฐ์ดํ„ฐ๋ฅผ ํŠน์ • ์š”์†Œ๋กœ ๋ฐ”๋กœ ๋ณ€ํ™˜ํ•  ์ˆ˜ ์žˆ๋Š” ํ•จ์ˆ˜ 
ํ•จ์ˆ˜ ์„ค๋ช… ์˜ˆ์‹œ ๊ฒฐ๊ณผ 
YEAR(date) ์—ฐ๋„  YEAR('2025-01-02') 2025
MONTH(date) ์›” MONTH('2025-01-02') 1
DAY(date) ์ผ DAY('2025-01-02') 2
HOUR(time) ์‹œ๊ฐ„ HOUR('15:30:00') 15
MINUTE(time) ๋ถ„ MINUTE('15:30:00') 30
SECOND(time) ์ดˆ SECOND('15:30:45') 45
DAYNAME(date) ์š”์ผ ์ด๋ฆ„  DAYNAME('2025-01-02') Thursday
MONTHNAME(date) ์›” ์ด๋ฆ„  MONTHNAME('2025-01-02') January
QUARTER(date) ๋ถ„๊ธฐ (1~4) QUARTER('2025-04-15') 2
WEEK(date) ์ฃผ ๋ฒˆํ˜ธ WEEK('2025-01-02') 1
DAYOFYEAR(date) ์—ฐ๋„ ๊ธฐ์ค€ ๋ช‡ ๋ฒˆ์งธ ๋‚ ์ธ์ง€ DAYOFYEAR('2025-01-02') 2
TO_DAYS(date) ๋‚ ์งœ์ผ ์ผ (day)๋กœ ๋ณ€ํ™˜ TO_DAYS('2025-01-02') 738850
TIME_TO_SEC(time) ์‹œ๊ฐ„์„ ์ดˆ(second)๋กœ ๋ณ€ํ™˜ TIME_TO_SEC('01:02:03') 3723
SEC_TO_TIME(sec) ์ดˆ๋ฅผ ์‹œ๊ฐ„ ํ˜•์‹์œผ๋กœ ๋ณ€ํ™˜  SEC_TO_TIME(3723) 01:02:03

 

๋‚ ์งœ ํ™œ์šฉ ํ•จ์ˆ˜

: ์ž์ฃผ ์“ฐ์ด๋Š” ๋‚ ์งœ ํ™œ์šฉ ํ•จ์ˆ˜ ์ •๋ฆฌ 
ํ•จ์ˆ˜ ์„ค๋ช… ์˜ˆ์‹œ ๊ฒฐ๊ณผ 
DATEDIFF ๋‘ ๋‚ ์งœ ๊ฐ„์˜ ์ฐจ์ด ๋ฐ˜ํ™˜  DATEDIFF('2023-12-31', '2023-01-01') 364
TIMESTAMPDIFF ์ง€์ •๋œ ๋‹จ์œ„์˜ ์ฐจ์ด ๋ฐ˜ํ™˜  TIMESTAMPDIFF(DAY, '2023-01-01', '2023-12-31');
TIMESTAMPDIFF(MONTH, '2023-01-01', '2023-12-31');
364;
11;
CURDATE ํ˜„์žฌ ๋‚ ์งœ ๋ฐ˜ํ™˜ (์‹œ๊ฐ„ ์ œ์™ธ) SELECT CURDATE();
SELECT
CURRENT_DATE();
2025-01-02

 

๋‚ ์งœ ๋น„๊ต ์—ฐ์‚ฐ

: ๊ธฐํ˜ธ๋ฅผ ์‚ฌ์šฉํ•ด์„œ ๋‘ ๋‚ ์งœ๋ฅผ ๋น„๊ตํ•  ์ˆ˜ ์žˆ์Œ 
  • ํฐ ๊ฐ’์ผ์ˆ˜๋ก ๋” ๋‚˜์ค‘์˜ ์‹œ๊ฐ„์ด๋‹ค !
SELECT
    I.ANIMAL_ID
    , I.NAME
FROM ANIMAL_INS AS I
    JOIN ANIMAL_OUTS AS O
    ON I.ANIMAL_ID = O.ANIMAL_ID
WHERE I.DATETIME > O.DATETIME -- ๋ณดํ˜ธ์‹œ์ž‘์ผ > ์ž…์–‘์ผ (๋ณดํ˜ธ ์‹œ์ž‘์ผ๋ณด๋‹ค ์ž…์–‘์ผ์ด ๋” ๋น ๋ฆ„)
ORDER BY I.DATETIME

 

 

 

 

 

 

Copy