๋ณธ๋ฌธ ๋ฐ”๋กœ๊ฐ€๊ธฐ
DB/Oracle

[Oracle] ORDER BY NULL ๊ฐ’ ์ •๋ ฌํ•˜๊ธฐ - NULLS FIRST, NULLS LAST

by yunamom 2022. 4. 14.
๋ฐ˜์‘ํ˜•

์•ˆ๋…•ํ•˜์„ธ์š” yunamom ์ž…๋‹ˆ๋‹ค :D

 

Oracle ๋ฐ์ดํ„ฐ๋ฒ ์ด์Šค์—์„œ NULL ์ •๋ ฌํ•˜๋Š” ๋ฐฉ๋ฒ•์„ ํฌ์ŠคํŒ…ํ•˜๊ฒ ์Šต๋‹ˆ๋‹ค.

NULLS FIRST : ์ •๋ ฌํ•˜๊ณ ์ž ํ•˜๋Š” NULL ๋ฐ์ดํ„ฐ๋“ค์„ ๋ฐ์ดํ„ฐ ์•ž์— ๋‚˜์˜ค๊ฒŒ ํ•œ๋‹ค.
NULLS LAST  : ์ •๋ ฌํ•˜๊ณ ์ž ํ•˜๋Š” NULL ๋ฐ์ดํ„ฐ๋“ค์„ ๋ฐ์ดํ„ฐ ๋’ค์— ๋‚˜์˜ค๊ฒŒ ํ•œ๋‹ค.

์‚ฌ์šฉ๋ฐฉ๋ฒ•์€ ๋‹ค์Œ๊ณผ ๊ฐ™๋‹ค.

Order BY [์ •๋ ฌํ•˜๊ณ ์ž ํ•˜๋Š” ์ปฌ๋Ÿผ๋ช…] [DESC/ASC] NULLS FIRST

Default ๊ฐ’์œผ๋กœ NULL ์ด ๋จผ์ € ํ‘œ์‹œ๋ฉ๋‹ˆ๋‹ค.

-- ์˜ˆ์‹œ
SELECT 
   substr(S.sno,1,1)sno1, 
   substr(S.sno,2,2)sno2, 
   substr(S.sno,4,2)sno3, 
   S.sname, E.ekor, E.emath, E.eeng, E.ehist ,
   (E.ekor+E.emath+E.eeng+E.ehist) total
FROM student_tbl_03 S 
   LEFT JOIN 
     exam_tbl_03 E 
ON S.sno = E.sno 
ORDER BY total DESC;

/*
์ถœ๋ ฅํ™”๋ฉด

SNO1 SNO2     SNO3     SNAME		EKOR	  EMATH       EENG	EHIST	   TOTAL
---- -------- -------- ---------- ---------- ---------- ---------- ---------- ----------
1    01       03       ์žฅ๋ฏฟ์Œ
1    01       05       ์ง„ํ‰ํ™”
1    01       06       ์ฐจ๊ณต๋‹จ
1    01       04       ์ตœ์‚ฌ๋ž‘
1    01       01       ๊น€ํ–‰๋ณต             98         91         95         90        374
1    01       02       ์ด์ถ•๋ณต             87         89         92         82        350
*/

NULLS LAST ์ถ”๊ฐ€์ž‘์„ฑํ›„ 

-- ์˜ˆ์‹œ
SELECT 
   substr(S.sno,1,1)sno1, 
   substr(S.sno,2,2)sno2, 
   substr(S.sno,4,2)sno3, 
   S.sname, E.ekor, E.emath, E.eeng, E.ehist ,
   (E.ekor+E.emath+E.eeng+E.ehist) total
FROM student_tbl_03 S 
   LEFT JOIN 
     exam_tbl_03 E 
ON S.sno = E.sno 
ORDER BY total DESC NULLS LAST;

/*
์ถœ๋ ฅํ™”๋ฉด

SNO1 SNO2     SNO3     SNAME		EKOR	  EMATH       EENG	EHIST	   TOTAL
---- -------- -------- ---------- ---------- ---------- ---------- ---------- ----------
1    01       01       ๊น€ํ–‰๋ณต             98         91         95         90        374
1    01       02       ์ด์ถ•๋ณต             87         89         92         82        350
1    01       05       ์ง„ํ‰ํ™”
1    01       04       ์ตœ์‚ฌ๋ž‘
1    01       06       ์ฐจ๊ณต๋‹จ
1    01       03       ์žฅ๋ฏฟ์Œ

*/

 

300x250

์ฝ”๋“œ