* 별칭 : as 컬럼명
* 조건절
1. where
2. between : 범위지정
3. in : 열거
4. like : 패턴검색, '김__'/'김%'
5. distinct : 중복값 제거
6. null 조건 : null상태 검사, is null/is not null
7. order by : 정렬, asc/desc
8. months_between : 날짜
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 | -- 컬럼 리스트 -- : 테이블로부터 가져올 컬럼 명단 -- : 컬럼명 or *(모든 컬럼) desc table tblCountry; select * from tblCountry; --결과 테이블, 결과셋, Resultset, 레코드셋(Record Set) select name from tblCountry; --단일 컬럼 select name, capital from tblCountry; --2개 select name, area * 2 from tblCountry; --연산 가능 select name, name from tblCountry; --중복 컬럼 select name, '수도 : ' || name from tblCountry; --중복 컬럼 select name, name, area, area*2 from tblCountry; -- 결과셋의 컬럼명의 별칭(Alias) 붙이기 -- : 컬럼명 as 별칭 -- : 컬럼명 as "별칭" -- : 자바에서는 별칭을 인식한다. 의미있게 짓기 select name, name as 국가명, area, area*2 as 면적 from tblCountry; select name as name2 from tblCountry; select name as "나라 이름" from tblCountry; --공백 (추천x) select name as "select" from tblCountry; --예약어 사용 (추천x) -- 상수 표현 -- 1. 숫자 상수 -- : 10 -- : 2.5 -- 2. 문자(열) 상수 -- : '문자열' (o) -- : "문자열" (x) -- 3. 날짜/시간 상수 -- : '2014-01-10' <- date 타입 (문맥에 따라 다름) -- : '2014년 월 10일' <- 지원(x) -- 문자열 연산자 -- : 문자열 + 문자열 <- concat -- : '문자열' || '문자열' select '국가명 : ' || name as 국가명 from tblCountry; select '국가명 : ' + name as 국가명 from tblCountry; --사용x select * from employees; select first_name || '' || last_name as name from employees; -- select는 상수나 연산의 결과나 함수의 결과를 출력 select 10; select 10 from employees; select 10 from dual; --가상 테이블 select 10 + 20 from dual; --별 쓸모 없는., select sysdate from dual; --현재 시간 얻어오기 -- 연산자 -- 1. 산술 연산자 -- : +, -, *, / -- 2. 비교 연산자 -- : >, >=, <, <=, =(==), <>(!=) -- 3. 논리 연산자 -- : and(&&), or(||), not(!) -- 4. 문자열 연산자 -- : || -- 5. SQL 연산자(java : obj instanceof class) -- : in, between, like, is null, not null, any, all., -- select 컬럼리스트 from 테이블 where절 order by절 -- 1. 컬럼 리스트 -- : 원하는 컬럼만 가져오기 -- : 수직 필터링 -- 2. where 절★★ -- : 원하는 행만 가져오기 -- : 수평 테이블 -- : 주로 비교연산식, 논리연산식을 사용, 함수를 사용 select * from tblCountry --모든 행(14행) select * from tblCountry where 행을 구분하기 위한 조건; select * from tblCountry where cont = 'AS'; select * from tblCountry where area >= 500; select * from tblCountry where area < 500; -- ask] 인구수가 5000이 넘는 나라의 국가명과 인구수를 가져오시오. select * from tblCountry; --1.모든 데이터(원본) 가져와서 확인하기 select name, popu from tblCountry where popu >= 5000; -- ask] 동시에 가져오는 거 어떻게? -- 1. 면적(area)이 100 초과하는 나라의 국가명(name)과 면적(area)을 출력하시오. select name, area from tblCountry where area > 100; -- 2. 아시아(AS) 대륙에 속한 국가명(name)을 출력하시오. select name from tblCountry where cont = 'AS'; -- 3. 아시아와 유럽에 속한 국가명을 출력하시오. select name from tblCountry where cont = 'AS' or cont = 'EU'; -- 4. 아시아에 속하지 않는 국가명을 출력하시오. select name from tblCountry where cont <> 'AS'; -- 5. 인구수가 10000 이상이고 면적이 100이상인 국가명, 인구수, 면적을 출력하시오. select name, popu, area from tblCountry where popu >= 10000 and area >=100; -- insa 테이블 desc table insa; --1.구성확인하기 select * from insa; --2.데이터확인하기 -- 6. 기획부 직원들을 출력하시오. select name from insa where buseo = '기획부'; -- 7. 서울시(city)에 사는 직원 중 직위(jikwi)가 부장인 사람의 이름(name)과 주민번호(ssn)을 출력하시오. select name, ssn from insa where city = '서울' and jikwi = '부장'; -- 8. 기본급(basicpay)과 수당(sudang)을 합쳐서 1,500,000원 이상인 직원 중 서울 사람만 출력하시오. select name from insa where basicpay+sudang>1500000 and city ='서울'; -- 9. 수당이 150,000원 이하인 직원 중 사원과 대리만 출력하시오. select * from insa where (sudang <= 150000) and (jikwi ='사원' or jikwi ='대리'); -- 10. 수당을 제외한 기본 연봉이 2천만원 이상이며 서울에 살며 직위가 과장 이상인 사람만 출력하시오. select * from insa where (basicsudang * 12 >= 20000000) and city = '서울' and (jikwi = '과장' or jikwi = '부장'); select * from insa where not (basicsudang * 12 >= 20000000) and city = '서울' and (jikwi = '과장' or jikwi = '부장'); -- 범위 조건 -- 1. 숫자 -- 2. 날짜/시간 -- 3. 문자(코드값 -> 숫자) SELECT * FROM tblCountry where popu > 10000; --between -- : where절에서 사용(조건) -- : 범위 지정 -- : 컬럼명 between 최소값 and 최대값 ->inclusive --ex1) SELECT * FROM tblCountry where popu >= 5000 and popu <= 10000; --영국, 이집트 SELECT * FROM tblCountry where popu <= 10000 and 5000 <= popu; --영국, 이집트 SELECT * FROM tblCountry where popu between 5000 and 10000; --비트윈으로 표현, 가독성 높음 --ex2) select first_name, hire_date from employees; --2005~2006년 사이 select first_name, hire_date from employees where hire_date >= '2005-01-01' and hire_date <= '2006-12-31'; select first_name, hire_date from employees where hire_date between '2005-01-01' and '2006-12-31'; --비트윈 --ex3) select * from insa where name >= '나' and name <= '사'; --in -- : where 절에서 사용(조건) -- : 열거형 조건(제시된 값 중 하나라도 만족하명 통과) -- ; 컬럼명 in (열거값) select * from tblCountry where cont = 'AS' or cont = 'EU'; --7개 select * from tblCountry where cont in ('AS', 'EU'); select * from insa where buseo in ('기획부', '총무부', '영업부'); select * from insa where not buseo in ('기획부', '총무부', '영업부'); select * from insa where buseo not in ('기획부', '총무부', '영업부'); --****** -- like -- : where절에서 사용(조건) -- : 문자열 대상 -- : 패턴 조건(특정한 패턴을 가지는 문자열을 검색) -- : 컬럼명 like 패턴문자열 -- like 패턴 요소 -- 1. _ : 임의의 문자 1개 -- 2. % : 임의의 문자 0~여러개 --김씨성만 출력하시오. select * from insa where name like '김__'; select * from insa where name like '__수'; select * from insa where name like '_인_'; --연락처가 010으로 시작하는 사람 SELECT * FROM insa where tel like '010-____-____'; --직위가 *장 SELECT * FROM insa where jikwi like '_장'; --홍씨성만 출력하시오. -> (홍길동, 홍민, 홍준) -> '홍__' -> '홍%' SELECT * FROM insa where name like '홍%'; -- select * from employees; select * from employees where first_name like 'D%'; select * from employees where first_name like '%n'; select * from employees where first_name like '%ll%'; -- null 조건 -- : 해당 컬럼이 nul상태인지 검사 -- : 컬럼명 is null -- : 컬럼명 is not null select * from tblCountry; -- 인구수가 아직 기재 안 된 나라? SELECT * FROM tblCountry where popu = null; 표현(x) SELECT * FROM tblCountry where popu is null; -- 인구수가 기재된 나라? SELECT * FROM tblCountry where not popu is null; SELECT * FROM tblCountry where popu is not null; --******* -- 도서관 -> 대여 -> 대여날짜/반납날짜 SELECT * FROM insa; SELECT * FROM insa where tel is null; -- distinct -- : 결과셋의 특정 컬럽값이 중복되면.. 중복값을 제외 -- : 컬럼리스트에서 사용 -- : distinct 컬럼명 -- : 언제쓰는지..?? -> 중복값 제거 후 출력 -- tblCountry 에서 대륙이 어떤것이 있습니까? select cont from tblCountry; select distinct cont from tblCountry; -- insa에는 어떤 부서가 있습니까? select buseo from insa; select distinct buseo from insa; -- 서울사는 직원들은 어떤 부서에 근무합니까? SELECT distinct buseo FROM insa where city = '서울'; -- 영업부 직원들은 사는곳이 어디입니까? select distinct city from insa where buseo = '영업부'; -- 주의! 상황을 고려해서.. 사용(distinct와 다른 컬럼은 동시에 select x) select distinct city, name from insa where buseo = '영업부'; -- order by 절 -- : 정렬 -- : 오른차순, 내림차순 -- : 정렬대상 -> 숫자, 날짜, 문자(열) -- : 결과셋의 레코드(행)을 정렬 -- : order by 컬럼명 asc(desc) -- 인구수가 적은나라부터.. SELECT * FROM tblCountry order by popu asc; --오름차순 SELECT * FROM tblCountry order by popu desc; --내림차순 -- 인구수가 적은 나라부터..(기재된 나라만..null 빼고) SELECT * FROM tblCountry where popu is not null order by popu asc; --이름을 가나다라순으로 정렬하시오. SELECT * FROM insa order by name asc; --입사일 신입-고참 순으로., SELECT * FROM insa order by ibsadate desc; --급여와 수당이 많은 순서대로., SELECT * FROM insa order by basicpay + sudang desc; -- months_between -- : 두날짜 데이터의 차이를 구하는 함수 -- : number month_between(date, date) -- : 두날짜 차이 -> 월(단위) select '2015-01-10' - '2014-05-25' from dual; select months_between('2015-01-10', '2014-05-25') from dual; --7.5개월.. select * from insa; --입사일.. select months_between(sysdate, ibsadate) from insa; select months_between(sysdate, ibsadate) / 12 from insa; --ask] --insa 테이블 --1. 사원 연락처가 010을 사용하며 서울에 거주하는 직원의 이름, 직위, 연락처를 출력하시오. select name, jikwi, tel from insa where tel like '010-____-____'; select name, jikwi, tel from insa where tel like '010%' and city = '서울'; --t --2. 이름이 '동'으로 끝나고 서울에 사는 사람? select name from insa where name like '__동' and city = '서울'; select * from insa where name like '%동' and city = '서울'; --t --3. 성이 '이'씨이고 이름이 '숙으로 끝나는 사람? select name from insa where name like '이_숙'; select * from insa where name like '이%숙'; --t --4. 이름에 '길'자가 들어간 사람? select name from insa where name like '%길%'; select * from insa where name like '%길%'; --t --5. 여직원(ssn)의 이름과 몇 년차(months_between) 인지 출력하시오. **맞는지재검 select name, months_between(sysdate, ibsadate)/12 from insa where ssn like '______-2______'; select name, months_between(sysdate, ibsadate) / 12 from insa where ssn like '%-2%'; --t -- order by --6. 입사날짜가 2000년 이후에 입사한 직원들을 고참순으로 출력? select name from insa where ibsadate between '2000-01-01' and '2015-01-07' order by ibsadate desc; select * from insa where ibsadate >= '2000-01-01' order by ibsadate desc;--t --7. 기획부 직원들을 출력 -> 신입사원순으로 출력? select name from insa where buseo = '기획부' order by ibsadate asc; select * from insa where buseo = '기획부' order by ibsadate asc; --t --8. 기획부/총무부/개발부 직원들을 고참순으로 출력? (in + order by) select name from insa where buseo in ('기획부', '총무부', '개발부') order by ibsadate desc; --9. 기획부/총무부/개발부 직원들 중 입사년도가 1999년~2003년 사이인 직원들을 오래된 순으로..(in, between, order by) select name from insa where buseo in ('기획부', '총무부', '개발부') and ibsadate between '1999-01-01' and '2003-01-01' order by ibsadate desc; select * from insa where buseo in ('기획부', '총무부', '개발부') and ibsadate between '1999-01-01' and '2003-12-31' order by ibsadate asc; --t --10. 근무년수 16년차 이상인 사람이 근무하는 부소는 어떤 부서들이 있습니까? 부서명을 가나다순으로 정렬 (distinct, order by, months_between) **못품 select distinct buseo from insa where months_between(sysdate, ibsadate) / 12 >= 16 order by buseo asc; | cs |
'WEB > ORACLE' 카테고리의 다른 글
오라클 테이블 관계(PK,FK,JOIN) (0) | 2015.05.22 |
---|---|
오라클 테이블 생성/삭제/수정/순서 (0) | 2015.05.22 |
오라클 함수(count,sum,avg,max,min/round,floor,trunc,ceil,mod/upper,lower,initcap,substr,length,replace,decode/sysdate,last_dat,add months/to_char,to_date) (0) | 2015.05.22 |
INTRO (0) | 2015.05.22 |
오라클 셋팅 (0) | 2015.05.22 |