post-custom-banner

โœ๐Ÿป 29์ผ ๊ณต๋ถ€ ์ด์•ผ๊ธฐ.



Scalar Functions

์‹ค์Šตํ™˜๊ฒฝ ๋งŒ๋“ค๊ธฐ

AWS RDS(database-1) zerobase์— ์ ‘์†
sandwich ๋ฐ์ดํ„ฐ ํ™•์ธ

Scalar Functions

์ž…๋ ฅ๊ฐ’์„ ๊ธฐ์ค€์œผ๋กœ ๋‹จ์ผ ๊ฐ’์„ ๋ฐ˜ํ™˜ํ•ด์ฃผ๋Š” ํ•จ์ˆ˜๋“ค์ด๋‹ค.

  • UCASE : ์˜๋ฌธ์„ ๋Œ€๋ฌธ์ž๋กœ ๋ณ€ํ™˜
  • LCASE : ์˜๋ฌธ์„ ์†Œ๋ฌธ์ž๋กœ ๋ณ€ํ™˜
  • MID : ๋ฌธ์ž์—ด ๋ถ€๋ถ„์„ ๋ฐ˜ํ™˜
  • LENGTH : ๋ฌธ์ž์—ด์˜ ๊ธธ์ด๋ฅผ ๋ฐ˜ํ™˜
  • ROUND : ์ง€์ •ํ•œ ์ž๋ฆฌ์—์„œ ์ˆซ์ž๋ฅผ ๋ฐ˜์˜ฌ๋ฆผ(0์ด ์†Œ์ˆ˜์  ์ฒซ์งธ์ž๋ฆฌ)
  • NOW : ํ˜„์žฌ ๋‚ ์งœ ๋ฐ ์‹œ๊ฐ„์„ ๋ฐ˜ํ™˜
  • FORMAT : ์ˆซ์ž๋ฅผ ์ฒœ๋‹จ์œ„ ์ฝค๋งˆ๊ฐ€ ์žˆ๋Š” ํ˜•์‹์œผ๋กœ ๋ฐ˜ํ™˜

UCASE & LCASE

SELECT UCASE('๋ฌธ์ž์—ด')

SELECT LCASE('๋ฌธ์ž์—ด')


MID & LENGTH & ROUND

SELECT MID('๋ฌธ์ž์—ด', ๋ฌธ์ž์—ด ๋ฐ˜ํ™˜ ์‹œ์ž‘ ์œ„์น˜, ๋ฐ˜ํ™˜ํ•  ๋ฌธ์ž์—ด ๊ธธ์ด)

SELECT LENGTH('๋ฌธ์ž์—ด') <-- ๋ฌธ์ž๊ฐ€ ์—†๋Š” ๊ฒฝ์šฐ : 0, ๊ณต๋ฐฑ : 1 -->

SELECT ROUND(์ˆซ์ž, ๋ฐ˜์˜ฌ๋ฆผํ•  ์†Œ์ˆ˜์  ์œ„์น˜) 
<-- ๋””ํดํŠธ๋Š” ์†Œ์ˆ˜์  ์ฒซ๋ฒˆ์งธ ์ž๋ฆฌ์—์„œ ๋ฐ˜์˜ฌ๋ฆผ -->
<-- ์ผ์˜ ์ž๋ฆฌ์—์„œ ๋ฐ˜์˜ฌ๋ฆผ : -1, ์‹ญ์˜ ์ž๋ฆฌ์—์„œ ๋ฐ˜์˜ฌ๋ฆผ : -2 ... -->
MID LENGTH

NOW & FORMAT

SELECT NOW()

SELECT FORMAT(์ˆซ์ž, ํ‘œ์‹œํ•  ์†Œ์ˆ˜์  ์œ„์น˜)
<-- ์ฒœ ๋‹จ์œ„์— ์ฝค๋งˆ๋ฅผ ํ‘œ์‹œํ•˜๋˜, ์†Œ์ˆ˜์ ์„ ์–ด๋””๊นŒ์ง€ ํ‘œ์‹œํ• ์ง€ ์„ค์ •ํ•  ์ˆ˜ ์žˆ์Œ -->
<-- 0 : ์†Œ์ˆ˜์ ์„ ํ‘œ์‹œํ•˜์ง€ ์•Š์Œ -->
<-- ๋ฐ˜ํ™˜๊ฐ’์€ ๋ฌธ์ž์—ด์ž„ -->
NOW




SQL Subquery

Subquery

ํ•˜๋‚˜์˜ SQL๋ฌธ(= ๋ฉ”์ธ์ฟผ๋ฆฌ) ์•ˆ์— ํฌํ•จ๋˜์–ด ์žˆ๋Š” ๋˜ ๋‹ค๋ฅธ SQL๋ฌธ์„ ์„œ๋ธŒ์ฟผ๋ฆฌ๋ผ ๋งํ•œ๋‹ค.

๋ฉ”์ธ ์ฟผ๋ฆฌ์™€ ์„œ๋ธŒ์ฟผ๋ฆฌ๋Š” ์ข…์†์ ์ธ ๊ด€๊ณ„์ด๋‹ค.

๋”ฐ๋ผ์„œ ์„œ๋ธŒ์ฟผ๋ฆฌ๋Š” ๋ฉ”์ธ์ฟผ๋ฆฌ์˜ ์ปฌ๋Ÿผ์ด ์‚ฌ์šฉ ๊ฐ€๋Šฅํ•˜์ง€๋งŒ, ๋ฉ”์ธ์ฟผ๋ฆฌ๋Š” ์„œ๋ธŒ์ฟผ๋ฆฌ์˜ ์ปฌ๋Ÿผ์„ ์‚ฌ์šฉํ•  ์ˆ˜ ์—†๋‹ค.


๐Ÿ“Œ ์„œ๋ธŒ์ฟผ๋ฆฌ ์‚ฌ์šฉ ์‹œ ์ฃผ์˜ ์‚ฌํ•ญ

  • ์„œ๋ธŒ์ฟผ๋ฆฌ๋Š” ๊ด„ํ˜ธ๋กœ ๋ฌถ์–ด์„œ ์‚ฌ์šฉํ•ด์•ผ ํ•œ๋‹ค
  • ๋‹จ์ผ ํ–‰ ํ˜น์€ ๋ณต์ˆ˜ ํ–‰ ๋น„๊ต ์—ฐ์‚ฐ์ž์™€ ํ•จ๊ป˜ ์‚ฌ์šฉ ๊ฐ€๋Šฅํ•˜๋‹ค.
  • ์„œ๋ธŒ์ฟผ๋ฆฌ์—์„œ๋Š” order by๋ฅผ ์‚ฌ์šฉํ•  ์ˆ˜ ์—†๋‹ค.

Subquery ์˜ ์ข…๋ฅ˜

  • Scalar Subquery(์Šค์นผ๋ผ ์„œ๋ธŒ์ฟผ๋ฆฌ) : SELECT ์ ˆ์— ์‚ฌ์šฉ
  • Inline View(์ธ๋ผ์ธ ๋ทฐ) : FROM์ ˆ์— ์‚ฌ์šฉ
  • Nested Subquery(์ค‘์ฒฉ ์„œ๋ธŒ์ฟผ๋ฆฌ) : WHERE ์ ˆ์— ์‚ฌ์šฉ

Scalar Subquery(์Šค์นผ๋ผ ์„œ๋ธŒ์ฟผ๋ฆฌ)

๊ฒฐ๊ณผ๊ฐ€ ํ•˜๋‚˜์˜ Column์ด ๋  ์ˆ˜ ์žˆ๊ฒŒ๋” ์„œ๋ธŒ์ฟผ๋ฆฌ๋ฅผ ์งœ์ฃผ์–ด์•ผ ํ•œ๋‹ค.

SELECT ์ปฌ๋Ÿผ์ด๋ฆ„1, (SELECT ์ปฌ๋Ÿผ์ด๋ฆ„2 FROM ํ…Œ์ด๋ธ”์ด๋ฆ„2 WHERE ์กฐ๊ฑด)
FROM ํ…Œ์ด๋ธ”์ด๋ฆ„1
WEHRE ์กฐ๊ฑด

์„œ์šธ์€ํ‰๊ฒฝ์ฐฐ์„œ์˜ ๊ฐ•๋„ ๊ฒ€๊ฑฐ ๊ฑด์ˆ˜์™€ ์„œ์šธ์‹œ ๊ฒฝ์ฐฐ์„œ ์ „์ฒด์˜ ํ‰๊ท  ๊ฐ•๋„ ๊ฒ€๊ฑฐ ๊ฑด์ˆ˜๋ฅผ ์กฐํšŒํ•˜๋ผ.


Inline View(์ธ๋ผ์ธ ๋ทฐ)

๋ฉ”์ธ์ฟผ๋ฆฌ์—์„œ๋Š” ์ธ๋ผ์ธ ๋ทฐ์—์„œ ์กฐํšŒํ•œ Column๋งŒ ์‚ฌ์šฉ ๊ฐ€๋Šฅํ•˜๋‹ค.

SELECT ๋ณ„๋ช…1.์ปฌ๋Ÿผ์ด๋ฆ„ , ๋ณ„๋ช…2.์ปฌ๋Ÿผ์ด๋ฆ„
FROM ํ…Œ์ด๋ธ”์ด๋ฆ„1 ๋ณ„๋ช…1, (SELECT ์ปฌ๋Ÿผ์ด๋ฆ„1, ์ปฌ๋Ÿผ์ด๋ฆ„2 FROM ํ…Œ์ด๋ธ”์ด๋ฆ„2) ๋ณ„๋ช…2
<-- ํ…Œ์ด๋ธ”2์—์„œ ์กฐํšŒํ•œ ํ…Œ์ด๋ธ”์„ ์ƒˆ๋กœ์šด ํ…Œ์ด๋ธ”๋กœ ์ƒ๊ฐํ•˜๊ณ  ์ด๋ฅผ ์ด์šฉํ•  ์ˆ˜ ์žˆ์Œ -->
WHERE ์กฐ๊ฑด

๊ฒฝ์ฐฐ์„œ ๋ณ„๋กœ ๊ฐ€์žฅ ๋งŽ์ด ๋ฐœ์ƒํ•œ ๋ฒ”์ฃ„ ๊ฑด์ˆ˜์™€ ๋ฒ”์ฃ„ ์œ ํ˜•์„ ์กฐํšŒํ•˜๋ผ.

๋จผ์ €, ๊ฒฝ์ฐฐ์„œ ๋ณ„๋กœ ๊ฐ€์žฅ ๋งŽ์ด ๋ฐœ์ƒํ•œ ๋ฒ”์ฃ„ ๊ฑด์ˆ˜๋ฅผ ์ถœ๋ ฅํ•˜๋Š” ํ…Œ์ด๋ธ”์„ ๋งŒ๋“ค๋ฉด ์•„๋ž˜์™€ ๊ฐ™๋‹ค.

์ด๋ ‡๊ฒŒ ๋งŒ๋“  ํ…Œ์ด๋ธ”์„ ์›๋ณธ ํ…Œ์ด๋ธ”๊ณผ ํ•ฉ์ณ์„œ ๊ฒฝ์ฐฐ์„œ ์ด๋ฆ„๊ณผ ๋ฐœ์ƒ ๊ฑด์ˆ˜๊ฐ€ ๋งž๋Š” ๋ฐ์ดํ„ฐ๋ฅผ ์ฐพ์•„ ๋ฒ”์ฃ„ ์œ ํ˜•์„ ์ถœ๋ ฅํ•ด์ฃผ๋Š” ์ฝ”๋“œ๋ฅผ ์งœ๋ฉด ๋œ๋‹ค.


Nested Subquery(์ค‘์ฒฉ ์„œ๋ธŒ์ฟผ๋ฆฌ)

  • ์ค‘์ฒฉ ์„œ๋ธŒ์ฟผ๋ฆฌ์˜ ์ข…๋ฅ˜
    • Single Row : ํ•˜๋‚˜์˜ ์—ด์„ ๊ฒ€์ƒ‰ํ•˜๋Š” ์„œ๋ธŒ์ฟผ๋ฆฌ
    • Multiple Row : ํ•˜๋‚˜ ์ด์ƒ์˜ ์—ด์„ ๊ฒ€์ƒ‰ํ•˜๋Š” ์„œ๋ธŒ ์ฟผ๋ฆฌ
    • Multiple Column : ํ•˜๋‚˜ ์ด์ƒ์˜ ํ–‰์„ ๊ฒ€์ƒ‰ํ•˜๋Š” ์„œ๋ธŒ์ฟผ๋ฆฌ

Single Row Subquery

์„œ๋ธŒ์ฟผ๋ฆฌ๊ฐ€ ๋น„๊ต์—ฐ์‚ฐ์ž(=, >, >=, <, <=, <>, !=)์™€ ์‚ฌ์šฉ๋˜๋Š” ๊ฒฝ์šฐ, ์„œ๋ธŒ์ฟผ๋ฆฌ์˜ ๊ฒ€์ƒ‰ ๊ฒฐ๊ณผ๋Š” ํ•œ ๊ฐœ์˜ ๊ฒฐ๊ณผ๊ฐ’(ํ•œ ๊ฐœ์˜ ํ–‰)์„ ๊ฐ€์ ธ์™€์•ผํ•œ๋‹ค.

SELECT ์ปฌ๋Ÿผ์ด๋ฆ„
FROM ํ…Œ์ด๋ธ”์ด๋ฆ„
WHERE ์ปฌ๋Ÿผ์ด๋ฆ„ = (SELECT ์ปฌ๋Ÿผ์ด๋ฆ„ FROM ํ…Œ์ด๋ธ”์ด๋ฆ„ WHERE ์กฐ๊ฑด)
			<-- ๊ด„ํ˜ธ๋Š” ๋ฌด์กฐ๊ฑด!! -->
ORDER by ์ปฌ๋Ÿผ์ด๋ฆ„


Multiple Row Subquery

1. IN
์„œ๋ธŒ์ฟผ๋ฆฌ ๊ฒฐ๊ณผ ์ค‘์— ํฌํ•จ๋˜๋Š” ๋ฐ์ดํ„ฐ๋ฅผ ์ถ”์ถœํ•˜๊ณ ์ž ํ•  ๋•Œ๋Š” IN์„ ์‚ฌ์šฉํ•  ์ˆ˜ ์žˆ๋‹ค.

SELECT ์ปฌ๋Ÿผ์ด๋ฆ„
FROM ํ…Œ์ด๋ธ”์ด๋ฆ„
WHERE ์ปฌ๋Ÿผ์ด๋ฆ„ IN (SELECT ์ปฌ๋Ÿผ์ด๋ฆ„ FROM ํ…Œ์ด๋ธ”์ด๋ฆ„ WHERE ์กฐ๊ฑด)
ORDER by ์ปฌ๋Ÿผ์ด๋ฆ„

2. EXISTS
์„œ๋ธŒ์ฟผ๋ฆฌ ๊ฒฐ๊ณผ ์ค‘์— ๊ฐ’์ด ์žˆ๋Š” ๋ฐ์ดํ„ฐ๋ฅผ ์ถ”์ถœํ•˜๊ณ ์ž ํ•  ๋•Œ๋Š” EXISTS์„ ์‚ฌ์šฉํ•  ์ˆ˜ ์žˆ๋‹ค.

SELECT ์ปฌ๋Ÿผ์ด๋ฆ„
FROM ํ…Œ์ด๋ธ”์ด๋ฆ„
WHERE EXISTS (SELECT ์ปฌ๋Ÿผ์ด๋ฆ„ FROM ํ…Œ์ด๋ธ”์ด๋ฆ„ WHERE ์กฐ๊ฑด)
ORDER by ์ปฌ๋Ÿผ์ด๋ฆ„

3. ANY
์„œ๋ธŒ์ฟผ๋ฆฌ ๊ฒฐ๊ณผ ์ค‘์— ์ตœ์†Œํ•œ ํ•˜๋‚˜๋ผ๋„ ๋งŒ์กฑํ•˜๋Š”(๋น„๊ต์—ฐ์‚ฐ์ž ์ด์šฉ)๋ฐ์ดํ„ฐ๋ฅผ ์ถ”์ถœํ•˜๊ณ ์ž ํ•  ๋•Œ๋Š” ANY์„ ์‚ฌ์šฉํ•  ์ˆ˜ ์žˆ๋‹ค.

SELECT ์ปฌ๋Ÿผ์ด๋ฆ„
FROM ํ…Œ์ด๋ธ”์ด๋ฆ„
WHERE ์ปฌ๋Ÿผ์ด๋ฆ„ = ANY (SELECT ์ปฌ๋Ÿผ์ด๋ฆ„ FROM ํ…Œ์ด๋ธ”์ด๋ฆ„ WHERE ์กฐ๊ฑด)
ORDER by ์ปฌ๋Ÿผ์ด๋ฆ„

4. ALL
์„œ๋ธŒ์ฟผ๋ฆฌ ๊ฒฐ๊ณผ๋ฅผ ๋ชจ๋‘ ๋งŒ์กฑํ•˜๋Š” ๋ฐ์ดํ„ฐ๋ฅผ ์ถ”์ถœํ•˜๊ณ ์ž ํ•  ๋•Œ๋Š” ALL์„ ์‚ฌ์šฉํ•  ์ˆ˜ ์žˆ๋‹ค.

SELECT ์ปฌ๋Ÿผ์ด๋ฆ„
FROM ํ…Œ์ด๋ธ”์ด๋ฆ„
WHERE ์ปฌ๋Ÿผ์ด๋ฆ„ = ALL (SELECT ์ปฌ๋Ÿผ์ด๋ฆ„ FROM ํ…Œ์ด๋ธ”์ด๋ฆ„ WHERE ์กฐ๊ฑด)
ORDER by ์ปฌ๋Ÿผ์ด๋ฆ„



Multiple Column Subquery

์„œ๋ธŒ์ฟผ๋ฆฌ ๋‚ด์— ๋ฉ”์ธ ์ฟผ๋ฆฌ ์ปฌ๋Ÿผ์ด ๊ฐ™์ด ์‚ฌ์šฉ๋˜๋Š” ๊ฒฝ์šฐ์— ์‚ฌ์šฉํ•œ๋‹ค.

SELECT ์ปฌ๋Ÿผ์ด๋ฆ„
FROM ํ…Œ์ด๋ธ”์ด๋ฆ„ ๋ณ„๋ช…1
WHERE (๋ณ„๋ช…1.์ปฌ๋Ÿผ์ด๋ฆ„, ๋ณ„๋ช…1.์ปฌ๋Ÿผ์ด๋ฆ„, ..) IN
	(SELECT ๋ณ„๋ช…2.์ปฌ๋Ÿผ์ด๋ฆ„, ๋ณ„๋ช…2.์ปฌ๋Ÿผ์ด๋ฆ„, ... FROM ํ…Œ์ด๋ธ”์ด๋ฆ„ ๋ณ„๋ช…2 WHERE ์กฐ๊ฑด)
ORDER by ์ปฌ๋Ÿผ์ด๋ฆ„

profile
๋ฐ์ดํ„ฐ ๋ถ„์„๊ฐ€(๊ฐ€ ๋˜๊ณ ํ”ˆ) ํ™ฉ์„ฑ๋ฏธ์ž…๋‹ˆ๋‹ค!
post-custom-banner

0๊ฐœ์˜ ๋Œ“๊ธ€