728x90
목차
1. LIKE 검색
2. NULL값
3. NULL함수
4. GROUP BY/HAVING

 

1. LIKE 검색

LIKE 검색이란?

  • 정확한 키워드를 모를 경우 일부만으로 검색하는 방법
  • 와일드카드(%, _)를 사용하여 패턴 매칭
  • SELECT 컬럼명 FROM 테이블명 WHERE 컬럼명 LIKE 패턴
  • 와일드카드(Wildcard)
  • LIKE의 검색은 매칭하기 위해 DBMS에 부담이 많으므로 LIKE에 OR와 같은 논리조건자를 중복해서 사용하지 않는게 좋음

바람직하지 않은 ex : select * from 테이블명 where like 컬럼명1 like … or 컬럼명2 like …;

select CountryCode from city where CountryCode like ‘K%’;
select CountryCode from city where CountryCode like ‘%K’;
select CountryCode from city where CountryCode like ‘%K%’;
select CountryCode from city where CountryCode like ‘K__’;

 

2. NULL값

NULL값

  • NULL이란 해당 컬럼의 값이 없다는 의미(해당 컬럼이 NULL값을 허용하는 경우)
  • NULL값을 가지고 있는 컬럼을 검색하려면 is NULL
  • NULL이 아닌 값을 가지고 있는 컬럼을 검색하려면 is not NULL
select count(*) from country where LifeExpectancy is NULL;
select count(*) from country where LifeExpectancy is not NULL;

 

3. NULL함수

NULL 함수란?

  • 숫자 컬럼을 연산해야할 때 NULL을 처리해주는 함수
  • NULL값이 나오면 다른 값(주로 0)으로 대체해서 계산에 문제없도록 처리
  • IFNULL/COALESCE(MySQL), ISNULL(SQL Server), NVL(oracle)
  • 숫자연산/집합함수(예: sum())의 경우는 처리가 내장되어 있음
  • 직접 함수나 쿼리에 넣는 경우는 NULL함수를 사용해야함
select avg(LifeExpectancy) from country;
select avg(IFNULL(LifeExpectancy, 0)) from country;
select count(LifeExpectancy) from country;
select count(IFNULL(LifeExpectancy, 0)) from country;

 

4. GROUP BY/HAVING

GROUP BY

  • 집합함수와 같이 사용해 그룹별 연산을 적용한다.
  • SELECT 컬럼명, 집합함수명(컬럼명) FROM 테이블명 GROUP BY 컬럼명;
select CountryCode, count(CountryCode) from city group by CountryCode;

 

HAVING

  • HAVING은 집합연산에 WHERE 조건절 대체로 사용
select CountryCode, count(CountryCode) from city group by CountryCode having count(CountryCode) >= 70;
728x90

+ Recent posts