[Database] SQL Assignment 3

2024. 4. 2. 14:42ใ†ComputerScience/Database

 

 

 

 

 

 

 

 

1. ํ˜„์žฌ ๋‚ ์งœ๋ฅผ ํ‘œ์‹œํ•˜๋Š” ์งˆ์˜๋ฅผ ์ž‘์„ฑํ•˜๊ณ  ์—ด ๋ ˆ์ด๋ธ”์„ Data๋กœ ์ง€์ •ํ•˜์‹ญ์‹œ์˜ค.

SELECT sysdate as "Date" 
FROM DUAL

-- from ์ ˆ์„ ๊ผญ ์จ์•ผ ํ•˜๋‹ˆ๊นŒ, dummy table๋กœ DUAL ์ œ๊ณต 

 

 

 

2. ๊ฐ ์‚ฌ์›์— ๋Œ€ํ•ด ์‚ฌ์› ๋ฒˆํ˜ธ, ์ด๋ฆ„ , ๊ธ‰์—ฌ ๋ฐ 15% ์ธ์ƒ๋œ ๊ธ‰์—ฌ๋ฅผ ์ •์ˆ˜๋กœ ํ‘œ์‹œํ•˜์‹ญ์‹œ์˜ค. ์ธ์ƒ๋œ ๊ธ‰์—ฌ ์—ด์˜ ๋ ˆ์ด๋ธ”์„ New Salary ๋กœ ์ง€์ •ํ•˜์‹ญ์‹œ์˜ค.

SELECT employee_id, last_name, salary, (salary * 1.15) AS "New Salary"
FROM employees

 

 

 

3. ์ด๋ฆ„์ด J, A๋˜๋Š” M์œผ๋กœ ์‹œ์ž‘ํ•˜๋Š” ๋ชจ๋“  ์‚ฌ์›์˜ ์ด๋ฆ„(์ฒซ ๊ธ€์ž๋Š” ๋Œ€๋ฌธ์ž๋กœ, ๋‚˜๋จธ์ง€ ๊ธ€์ž๋Š” ์†Œ๋ฌธ์ž๋กœ ํ‘œ์‹œ) ๋ฐ ์ด๋ฆ„ ๊ธธ์ด๋ฅผ ํ‘œ์‹œํ•˜๋Š” ์งˆ์˜๋ฅผ ์ž‘์„ฑํ•˜๊ณ  ๊ฐ ์—ด์— ์ ํ•ฉํ•œ ๋ ˆ์ด๋ธ”์„ ์ง€์ •ํ•˜์‹ญ์‹œ์˜ค. ๊ฒฐ๊ณผ๋ฅผ ์‚ฌ์›์˜ ์ด๋ฆ„์— ๋”ฐ๋ผ ์ •๋ ฌํ•˜์‹ญ์‹œ์˜ค.

SELECT INITCAP(last_name), LENGTH(last_name) AS "name length"
FROM employees
WHERE last_name LIKE 'J%' OR last_name LIKE 'A%' OR last_name LIKE 'M%'
ORDER BY last_name;

 

// SUBSTR ํ™œ์šฉํ•œ ๋ฐฉ๋ฒ•

SELECT INITCAP(last_name), LENGTH(last_name) AS "name length"
FROM employees
WHERE SUBSTR(last_name,1,1) IN ('J', 'A', 'M')
ORDER BY last_name

 

 

4. ๊ฐ ์‚ฌ์›์˜ ์ด๋ฆ„์„ ํ‘œ์‹œํ•˜๊ณ  ๊ทผ๋ฌด ๋‹ฌ ์ˆ˜(์ž…์‚ฌ์ผ๋กœ๋ถ€ํ„ฐ ํ˜„์žฌ๊นŒ์ง€์˜ ๋‹ฌ ์ˆ˜)๋ฅผ ๊ณ„์‚ฐํ•˜์—ฌ ์—ด ๋ ˆ์ด๋ธ”์„ MONTHS_WORKED๋กœ ์ง€์ •ํ•˜์‹ญ์‹œ์˜ค.  ๊ฒฐ๊ณผ๋Š” ์ •์ˆ˜๋กœ ๋ฐ˜์˜ฌ๋ฆผํ•˜์—ฌ ํ‘œ์‹œํ•˜๊ณ  ๊ทผ๋ฌด ๋‹ฌ ์ˆ˜๋ฅผ ๊ธฐ์ค€์œผ๋กœ ์ •๋ ฌํ•˜์‹ญ์‹œ์˜ค.SELECT last_name, ROUND(MONTHS_BETWEEN(sysdate, hire_date)) AS "MONTHS_WORKED"
FROM employees
ORDER BY ROUND(MONTHS_BETWEEN(sysdate, hire_date))
-- ๋‹ค๋ฅธ DBMS ์‚ฌ์šฉ ์‹œ, ๋ณ„์นญ ์ฃผ๋Š”๊ฒŒ ์•ˆ ๋จน๋Š” ๊ฒฝ์šฐ๊ฐ€ ์žˆ๋‹ค. 
-- ๊ทธ๋ƒฅ ROUND(MONTHS_BETWEEN(sysdate, hire_date))๋กœ ์ •๋ ฌํ•ด๋„ okay

 

 

 

5. ๊ฐ ์‚ฌ์›์— ๋Œ€ํ•ด ๋‹ค์Œ ํ•ญ๋ชฉ์„ ์ƒ์„ฑํ•˜๋Š” ์งˆ์˜๋ฅผ ์ž‘์„ฑํ•˜์‹ญ์‹œ์˜ค. <employee last name> earn <salary> monthly but wants <3 times salary> ์—ด ๋ ˆ์ด๋ธ”์„ Dream Salaries๋กœ ์ง€์ •ํ•˜์‹ญ์‹œ์˜ค.

SELECT last_name, last_name || ' earn ' || salary || ' monthly but wants ' || salary*3 AS "Dream Salaries" 
FROM employees 

 

 

 

6.๋ชจ๋“  ์‚ฌ์›์˜ ์ด๋ฆ„๊ณผ ๊ธ‰์—ฌ๋ฅผ ํ‘œ์‹œํ•˜๋Š” ์งˆ์˜๋ฅผ ์ž‘์„ฑํ•˜์‹ญ์‹œ์˜ค. ๊ธ‰์—ฌ๋Š” 15์ž ๊ธธ์ด๋กœ ์™ผ์ชฝ์— $๊ธฐํ˜ธ๊ฐ€ ์ฑ„์›Œ์ง„ ํ˜•์‹์œผ๋กœ ํ‘œ๊ธฐํ•˜๊ณ  ์—ด ๋ ˆ์ด๋ธ”์„ SALARY๋กœ ์ง€์ •ํ•˜์‹ญ์‹œ์˜ค.

SELECT last_name, LPAD(salary, 15, '$') "SALARY"
FROM employees

 

 

 

7.์‚ฌ์›์˜ ์ด๋ฆ„, ์ž…์‚ฌ์ผ ๋ฐ ๊ธ‰์—ฌ ๊ฒ€ํ† ์ผ์„ ํ‘œ์‹œํ•˜์‹ญ์‹œ์˜ค. ๊ธ‰์—ฌ ๊ฒ€ํ† ์ผ์€ ์—ฌ์„ฏ ๋‹ฌ์ด ๊ฒฝ๊ณผํ•œ ํ›„ ์ฒซ ๋ฒˆ์งธ ์›”์š”์ผ์ž…๋‹ˆ๋‹ค. ์—ด ๋ ˆ์ด๋ธ”์„ REVIEW๋กœ ์ง€์ •ํ•˜๊ณ  ๋‚ ์งœ๋Š”“Monday, the Thirty-First of July, 2000”๊ณผ ๊ฐ™์€ ํ˜•์‹์œผ๋กœ ํ‘œ์‹œ๋˜๋„๋ก ์ง€์ •ํ•˜์‹ญ์‹œ์˜ค.

SELECT last_name, hire_date, NEXT_DAY(ADD_MONTHS(hire_date, 6), 'Monday') "REVIEW"
FROM employees

 

 

 

8. ์ด๋ฆ„, ์ž…์‚ฌ์ผ ๋ฐ ์—…๋ฌด ์‹œ์ž‘ ์š”์ผ์„ ํ‘œ์‹œํ•˜๊ณ  ์—ด ๋ ˆ์ด๋ธ”์„ DAY๋กœ ์ง€์ •ํ•˜์‹ญ์‹œ์˜ค. Monday๋ฅผ ์‹œ์ž‘์œผ๋กœ ํ•ด์„œ ์š”์ผ์„ ๊ธฐ์ค€์œผ๋กœ ๊ฒฐ๊ณผ๋ฅผ ์ •๋ ฌํ•˜์‹ญ์‹œ์˜ค.

SELECT last_name, hire_date, TO_CHAR(hire_date, 'DAY') "DAY"
FROM employees
ORDER BY TO_CHAR(hire_date - 1, 'D')

 

-- 'd'='D'๋Š” ์š”์ผ์„ ์ˆซ์ž๋กœ ๋ณ€ํ™˜
-- hire_date = ์ˆ˜์š”์ผ -> ์ˆ˜์š”์ผ์— ํ•ด๋‹นํ•˜๋Š” 4 

-- sunday๊ฐ€ 1, ์›”์š”์ผ์ด 2, ๋”ฐ๋ผ์„œ ์ „๋‚ ์„ ๊ธฐ์ค€์œผ๋กœ (-1) ์ •๋ ฌ

-- ์›”์š”์ผ์ด ์ผ์š”์ผ์ด ๋˜๊ณ , ์ผ์š”์ผ์€ ํ† ์š”์ผ ์ฆ‰ ์ œ์ผ ํฐ ๊ฐ’์ด ๋จ

 

 

 

9.์‚ฌ์›์˜ ์ด๋ฆ„๊ณผ ์ปค๋ฏธ์…˜์„ ํ‘œ์‹œํ•˜๋Š” ์งˆ์˜๋ฅผ ์ž‘์„ฑํ•˜์‹ญ์‹œ์˜ค. ์ปค๋ฏธ์…˜์„ ๋ฐ›์ง€ ์•Š๋Š” ์‚ฌ์›์ผ ๊ฒฝ์šฐ“No Commission”์„ ํ‘œ์‹œํ•˜์‹ญ์‹œ์˜ค. ์—ด ๋ ˆ์ด๋ธ”์€ COMM์œผ๋กœ ์ง€์ •ํ•˜์‹ญ์‹œ์˜ค.

SELECT last_name, NVL(TO_CHAR(commission_pct), 'No Commission') "COMM"
FROM employees

 

-- NVL, Null-value - ์ฒซ๋ฒˆ์งธ params๊ฐ€ null์ด๋ฉด, ๋‘๋ฒˆ์งธ params ์ถœ๋ ฅ (null์ด ์•„๋‹ˆ๋ฉด ์›๋ž˜ ๊ฐ’ ์ถœ๋ ฅ) 

-- ์™œ TO_CHAR? - ํ•˜๋‚˜์˜ attribute๋Š” ๋™์ผํ•œ type ๋งŒ ์ถœ๋ ฅ์ด ๊ฐ€๋Šฅํ•˜๋‹ˆ๊นŒ, type์„ ๋งž์ถฐ์ฃผ๊ธฐ ์œ„ํ•ด์„œ 

 

 

 

10.์‚ฌ์›์˜ ์ด๋ฆ„์„ ํ‘œ์‹œํ•˜๊ณ  ๊ธ‰์—ฌ ์ด์•ก์„ ๋ณ„ํ‘œ(*)๋กœ ๋‚˜ํƒ€๋‚ด๋Š” ์งˆ์˜๋ฅผ ์ž‘์„ฑํ•˜์‹ญ์‹œ์˜ค. ๊ฐ ๋ณ„ํ‘œ๋Š” 1,000๋‹ฌ๋Ÿฌ๋ฅผ ๋‚˜ํƒ€๋ƒ…๋‹ˆ๋‹ค. ๊ธ‰์—ฌ๋ฅผ ๊ธฐ์ค€์œผ๋กœ ๋ฐ์ดํ„ฐ๋ฅผ ๋‚ด๋ฆผ์ฐจ์ˆœ์œผ๋กœ ์ •๋ ฌํ•˜๊ณ  ์—ด ๋ ˆ์ด๋ธ”์„ EMPLOYEES_AND_THEIR_SALARIES๋กœ ์ง€์ •ํ•˜์‹ญ์‹œ์˜ค.

SELECT last_name, RPAD('*', TRUNC(salary/1000), '*') AS "EMPLOYEES_AND_THEIR_SALARIES"
FROM employees
ORDER BY salary DESC

 

-- RPAD - ์ถœ๋ ฅํ•  ๊ณต๊ฐ„์„ ์žก์•„๋‘” ๋‹ค์Œ, ์ถœ๋ ฅ ๊ณต๊ฐ„์ด ์žกํžŒ ๋‚˜๋จธ์ง€ ๊ณต๊ฐ„์„ ์ฑ„์›Œ์ฃผ๊ธฐ ์œ„ํ•œ ํ•จ์ˆ˜ -> ์˜ค๋ฅธ์ชฝ์„ ๋ณ„ํ‘œ๋กœ ์ฑ„์šฐ์ž

-- ์ด ์นธ์˜ ๊ฐœ์ˆ˜๊ฐ€ TRUNC(salary/1000) - ์•ž์ด๋“  ๋’ค๋“  ์‹น ๋‹ค '*' ๋กœ ์ฑ„์šฐ๋Š” ๊ฒƒ์ด ๋ชฉ์  

-- ์ผ๋‹จ '*'๋ฅผ ์ฐ๊ณ , ๋‚˜๋จธ์ง€ ๋’ท ์นธ๋“ค๋„ ๋‹ค TRUNC(salary/1000) size ๋งŒํผ '*' ์„ ์ฑ„์šฐ์ž.

 

 

 

11. DECODE ํ•จ์ˆ˜๋ฅผ ์‚ฌ์šฉํ•˜์—ฌ ๋‹ค์Œ ๋ฐ์ดํ„ฐ์— ๋”ฐ๋ผ JOB_ID ์—ด์˜ ๊ฐ’์„ ๊ธฐ์ค€์œผ๋กœ ๋ชจ๋“  ์‚ฌ์›์˜ ๋“ฑ๊ธ‰์„ ํ‘œ์‹œํ•˜๋Š” ์งˆ์˜๋ฅผ ์ž‘์„ฑํ•˜์‹ญ์‹œ์˜ค.

 

์—…๋ฌด             ๋“ฑ๊ธ‰

AD_PRES    A

ST_MAN      B

IT_PROG     C

SA_REP       D

ST_CLERK   E

๊ธฐํƒ€               0

 

SELECT last_name, job_id,
DECODE(job_id, 'AD_PRES', 'A',
     'ST_MAN', 'B',
     'IT_PROG', 'C',
     'SA_REP', 'D',
     'ST_CLERK', 'E',
     '0') AS "GRADE"

FROM employees

 

 

 

12. 11๋ฒˆ ๋ฌธ์ œ์˜ ๋ช…๋ น๋ฌธ์„ CASE ๊ตฌ๋ฌธ์„ ์‚ฌ์šฉํ•˜์—ฌ ์žฌ์ž‘์„ฑํ•˜์‹ญ์‹œ์˜ค.

SELECT last_name, job_id,
    CASE job_id 
    WHEN 'AD_PRES' THEN 'A'
    WHEN 'ST_MAN' THEN 'B'
    WHEN 'IT_PROG' THEN 'C'
    WHEN 'SA_REP' THEN 'D'
    WHEN 'ST_CLERK' THEN 'E'
    ELSE '0'
END AS "GRADE"
FROM employees

 

 

 

 

 

* ์ถ”๊ฐ€ ์˜ˆ์ œ 

SELECT last_name, salary,
    DECODE(TRUNC(salary/5000) - 1, 
    -1, '์–ดํœด~~~', 
    0, '์–ดํœด~~~', 
    1, '์˜ค์ผ€์ด', 
    'Good!') AS "MESSAGE"
FROM employees



SELECT last_name, salary,
    CASE WHEN salary < 5000 THEN '์–ดํœด~~~' 
    WHEN salary < 10000 THEN '์˜ค์ผ€์ด' 
    ELSE 'Good!'
END AS "MESSAGE"
FROM employees

-- DECODE๋Š” ๋™๋“ฑ ์กฐ๊ฑด๋งŒ ๊ฐ€๋Šฅํ•˜๋‹ค (ํฌ๋‹ค, ์ž‘๋‹ค ํ‘œํ˜„์ด X)
-- CASE๋ฌธ์€ ๋Œ€์†Œ ๋น„๊ต ํ‘œํ˜„ ๊ฐ€๋Šฅ, ์ด๋•Œ CASE WHEN salary ์ด๋ ‡๊ฒŒ ๋ณ€๊ฒฝ

 

 

 

 

 

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

[Database] SQL Assignment 6  (1) 2024.04.19
[Database] SQL Assignment 5  (2) 2024.04.19
[Database] SQL Assignment 4  (0) 2024.04.02
[Database] SQL Assignment 2  (0) 2024.04.01