๋ณธ๋ฌธ ๋ฐ”๋กœ๊ฐ€๊ธฐ
TIL

01/12(๊ธˆ) ๋‚ด์ผ๋ฐฐ์›€์บ ํ”„ Java 15์ผ์ฐจ TIL - SQL

by ๊ฐ์ž๊ฐœ๋ฐœ๊พผ 2024. 1. 12.

 

๐Ÿ“–  ์˜ค๋Š˜์˜ ํ•™์Šต ํ‚ค์›Œ๋“œ

  • SQL

 

๐Ÿฅ”  select ๋ฌธ๋ฒ• ์ˆœ์„œ

  1. select
  2. from
  3. where
  4. group by
  5. having
  6. order by
์…€ ํ”„ ์›จ ๊ตฌ ํ•ด ์˜ค

 

* ์ฟผ๋ฆฌ์˜ ์‹คํ–‰ ์ˆœ์„œ๋Š” from -> where -> group by -> having -> select -> order by

 

๐Ÿฅ”  Distinct

- ์ค‘๋ณต ํ–‰์„ ์ œ๊ฑฐ

 

ex) select distinct name from student;

 

 

๐Ÿฅ”  Null

SQL์—์„œ Null ๊ฐ’์€ ๊ฐ’์ด ์—†๋‹ค๋Š” ์˜๋ฏธ์ž…๋‹ˆ๋‹ค.

 

โ—๏ธ Null๊ฐ’์€ 0๊ณผ๋Š” ๋‹ค๋ฆ„

 

 

๐Ÿฅ”  IS NULL, IS NOT NULL

is null ์—ฐ์‚ฐ์ž๋Š” null ๊ฐ’, ์ฆ‰ ๋น„์–ด์žˆ๋Š” ๊ฐ’์„ ์ฐพ์„ ๋•Œ ์‚ฌ์šฉ๋˜๊ณ 

is not null ์—ฐ์‚ฐ์ž๋Š” null ๊ฐ’์ด ์•„๋‹Œ ๊ฐ’, ์ฆ‰ ๋น„์–ด์žˆ์ง€ ์•Š์€ ๊ฐ’์„ ์ฐพ์„ ๋•Œ ์‚ฌ์šฉ๋ฉ๋‹ˆ๋‹ค. 

 

 

 

 

๐Ÿฅ”  ์ค‘๋ณต ์ œ๊ฑฐํ•˜๊ธฐ ๋ฌธ์ œ

table : animal_ins

 

๋™๋ฌผ ๋ณดํ˜ธ์†Œ์— ๋“ค์–ด์˜จ ๋™๋ฌผ์˜ ์ด๋ฆ„์€ ๋ช‡ ๊ฐœ์ธ์ง€ ์กฐํšŒํ•˜๋Š” SQL ๋ฌธ์„ ์ž‘์„ฑํ•ด ์ฃผ์„ธ์š”.

์ด๋•Œ ์ด๋ฆ„(name)์ด NULL์ธ ๊ฒฝ์šฐ๋Š” ์ง‘๊ณ„ํ•˜์ง€ ์•Š์œผ๋ฉฐ ์ค‘๋ณต๋˜๋Š” ์ด๋ฆ„์€ ํ•˜๋‚˜๋กœ ์นฉ๋‹ˆ๋‹ค.

 

select count(distinct name)

from animal_ins

where name is not null;

 

 

๐Ÿ“š  ์˜ค๋Š˜์˜ ํšŒ๊ณ 

SQL์„ ๋ฐฐ์›Œ๋†“๊ณ  ๋ณต์Šต์„ ์•ˆ ํ•˜๋‹ค ๋ณด๋‹ˆ ๊นŒ๋จน๋Š” ๋ฌธ๋ฒ•๋“ค์ด ์ƒ๊ฒผ์Šต๋‹ˆ๋‹ค.

๋‹ค์‹œ ํ•œ๋ฒˆ ๊ธฐ์–ต ์†์— ๋•Œ๋ ค๋†“์ž๋ผ๋Š” ์ƒ๊ฐ์œผ๋กœ ๋ฌธ๋ฒ•์„ ์กฐ๊ธˆ ์ •๋ฆฌํ•ด ๋ณด์•˜์Šต๋‹ˆ๋‹ค.

 

์ข‹์€ ๊ฐœ๋ฐœ์ž๊ฐ€ ๋˜๊ธฐ ์œ„ํ•ด์„œ SQL๋„ ๊พธ์ค€ํ•˜๊ฒŒ ๊ณต๋ถ€ํ•˜๋„๋ก ๋…ธ๋ ฅํ•˜๊ฒ ์Šต๋‹ˆ๋‹ค.