[ํ”„๋กœ๊ทธ๋ž˜๋จธ์Šค] SQL LEVEL 2 ์ „์ฒด ๋ฌธ์ œ /Oracle /์˜ค๋ผํด
Engineering/SQL

[ํ”„๋กœ๊ทธ๋ž˜๋จธ์Šค] SQL LEVEL 2 ์ „์ฒด ๋ฌธ์ œ /Oracle /์˜ค๋ผํด

728x90
๋ฐ˜์‘ํ˜•

 

 

 

 

๐Ÿšฉ์ตœ์†Ÿ๊ฐ’ ๊ตฌํ•˜๊ธฐ

SELECT MIN(DATETIME)
    FROM ANIMAL_INS

๐Ÿšฉ๋™๋ฌผ ์ˆ˜ ๊ตฌํ•˜๊ธฐ

SELECT COUNT(ANIMAL_ID)
    FROM ANIMAL_INS

๐Ÿšฉ์ค‘๋ณต ์ œ๊ฑฐํ•˜๊ธฐ

SELECT COUNT(DISTINCT(NAME))
FROM ANIMAL_INS
WHERE NAME IS NOT NULL

๐ŸšฉNULL ์ฒ˜๋ฆฌํ•˜๊ธฐ

SELECT ANIMAL_TYPE, NVL(NAME, 'No name'), SEX_UPON_INTAKE
FROM ANIMAL_INS
ORDER BY ANIMAL_ID

๐Ÿšฉ๊ณ ์–‘์ด์™€ ๊ฐœ๋Š” ๋ช‡ ๋งˆ๋ฆฌ๊ฐ€ ์žˆ์„๊นŒ

SELECT ANIMAL_TYPE, COUNT(*) AS count
FROM ANIMAL_INS
GROUP BY ANIMAL_TYPE
ORDER BY ANIMAL_TYPE

๐Ÿšฉ๋™๋ช… ๋™๋ฌผ ์ˆ˜ ์ฐพ๊ธฐ

SELECT NAME, COUNT(*) AS COUNT
FROM ANIMAL_INS
GROUP BY NAME
HAVING COUNT(NAME) > 1
ORDER BY NAME

๐Ÿšฉ์ž…์–‘ ์‹œ๊ฐ ๊ตฌํ•˜๊ธฐ

SELECT HOUR, COUNT(*) COUNT
FROM (SELECT TO_CHAR(DATETIME, 'HH24') HOUR
     FROM ANIMAL_OUTS)
WHERE HOUR >= 9 AND HOUR < 20
GROUP BY HOUR
ORDER BY HOUR

๐Ÿšฉ๋ฃจ์‹œ์™€ ์—˜๋ผ ์ฐพ๊ธฐ

SELECT ANIMAL_ID, NAME, SEX_UPON_INTAKE
FROM ANIMAL_INS
WHERE NAME IN ( 'Lucy', 'Ella', 'Pickle', 'Rogan', 'Sabrina', 'Mitty')
ORDER BY ANIMAL_ID

๐Ÿšฉ์ด๋ฆ„์— el์ด ๋“ค์–ด๊ฐ€๋Š” ๋™๋ฌผ ์ฐพ๊ธฐ

SELECT ANIMAL_ID, NAME
FROM ANIMAL_INS
WHERE (NAME LIKE '%El%' OR NAME LIKE '%el%') AND ANIMAL_TYPE = 'Dog'
ORDER BY NAME

๐Ÿšฉ์ค‘์„ฑํ™” ์—ฌ๋ถ€ ํŒŒ์•…ํ•˜๊ธฐ

SELECT ANIMAL_ID, NAME,
CASE WHEN SEX_UPON_INTAKE LIKE 'Spayed%' THEN 'O'
WHEN SEX_UPON_INTAKE LIKE 'Neutered%' THEN 'O'
ELSE 'X'
END
AS ์ค‘์„ฑํ™”
FROM ANIMAL_INS
ORDER BY ANIMAL_ID

๐ŸšฉDATETIME์—์„œ DATE๋กœ ํ˜• ๋ณ€ํ™˜

SELECT ANIMAL_ID, NAME, TO_CHAR(DATETIME, 'YYYY-MM-DD') ๋‚ ์งœ
FROM ANIMAL_INS
ORDER BY ANIMAL_ID

 

๋ฐ˜์‘ํ˜•

'Engineering > SQL' ์นดํ…Œ๊ณ ๋ฆฌ์˜ ๋‹ค๋ฅธ ๊ธ€

[ํ”„๋กœ๊ทธ๋ž˜๋จธ์Šค] SQL LEVEL 1 ์ „์ฒด /Oracle /์˜ค๋ผํด /SELECT  (0) 2020.09.26