ABOUT ME

-

Today
-
Yesterday
-
Total
-
  • [SQL] 조건문 Where cluase 비교연산 사용법
    데이터 분석/DB & SQL 2020. 4. 25. 14:09

    조건문 Where cluase 은 쿼리 질의결과에서 일부 결과만 필터링할 때 활용한다.
    이번 글을 정리하면서 Case Sensitive / Insensitive 에 대해서 명확하게 이해하게 되었다.
    Mac > Numbers > Column 클릭 > 우측 Filter 탭 기능을 자세히 보게 되었다. =/!= 검색과 LIKE 검색으로 보인다면 성공!

    Numbers 컬럼 문자열에 대한 값 필터링 기능

     

     

    =  동치연산자, 그 외 비교연산자

    조건문의 기본은 = 동치를 포함한 비교연산자이다.

    An introduction to the WHERE clause and comparison operators. One way to restrict the data presented from a query is to use a WHERE clause. WHERE is used to return data that matches a specified condition and is combined with the comparison operators =,!=,>,<,>=, and<=. (...) Queries involving text strings are also case sensitive

    SELECT year, month, count, animal_type FROM austin_animal_center_intakes_by_month
    WHERE animal_type = "Cat"


    이 글에서는 그 외 다양한 필터링을 제공하는 비교연산 Keywords 를 몇가지 정리했다.

    - LIKE 와 NOT LIKE - 문자열 유사검색 
    - BETWEEN - 범위형 검색
    - IN 과 NOT IN - 하나의 필드 여러개 값 복수검색

    물론 SQL 조건문 where clause 에서 사용하는 연산자들이 있지만 이 글에서는 제외.

    - 구문 조건들을 연결해주는 논리연산자: AND, OR, NOT  [Logical Operations]
    - 구문 조건값을 계산해주는 산술연산자: +, -, *, /  [Arithmetic Operations]

     

    LIKE 와 NOT LIKE - 문자열 유사검색 

    The LIKE and NOT LIKE expressions allow you to specify a set of similar values to be either returned or excluded from a query’s results. LIKE can take two wildcards, % and _ which indicate either all characters or a single character. The wildcard % is used to take the place of all possible strings that occur in its position in the search string.The wildcard to indicate the presence of a single character is the underscore _ which can be used either singly or in multiples to indicate a set number of characters. LIKE and NOT LIKE searches are case-insensitive.


    LIKE 와 NOT LIKE 로 유사한 값을 가진 row 를 구분지을 수 있다. 유사검색 이다.
    LIKE 와 NOT LIKE 조건검색은 대소문자를 구분하지 않는다. case-insensitive 즉 A 와 a 를 같은 글자으로 취급한다.
    LIKE 와 NOT LIKE 는 두개의 와일트카드가 있다: % (어떠한 글자), _ (글자 하나)
    % 와 _ 에 대해서 와일드카드가 아니라 문자 그대로 literally 해석하고 싶다면 Escape 와일드카드 앞에 ! 를 붙인다. 


    breed LIKE "%wolfhound%"
    breed LIKE "%!_%" 

    SELECT patient, description, reasondescription
    FROM
     medication
    WHERE description LIKE "%Acetaminophen%"
    AND (description NOT LIKE "%hydrocodone" OR description NOT LIKE "%oxycodone")
    ORDER BY medications.description DESC


    Tip. 논리연산자 적용 순서를 위해 () 괄호를 잘 활용한다.
    Tip. 같은 컬럼 description 에 대한 NOT LIKE 문이어도 구문을 2개로 분리해서 작성해주어야 한다.
    Ref. [LIKE and NOT LIKE]

     

     

    BETWEEN - 범위형 검색

    The keyword BETWEEN returns a range of data between two values. It can be used with numbers, text strings and dates, and is most commonly found in the WHERE clause. Though it isn’t clearly illustrated by the last query, note that BETWEEN returns everything between two values inclusive of the specified vales.

    BETWEEN 은 두 값 범위 사이의 데이터 (A 이상 B 이하 inclusive) 를 응답한다. 숫자형, 문자형, 날짜형에 사용가능하고 조건문에서 주로 활용된다. 


    SELECT observations.patient, observations.description, observations.value
    FROM observations
    WHERE description = "Quality adjusted life years"
    AND value BETWEEN 1 AND --- 1 이상 5 이하


    Tip. Between 키워드는 두개의 값을 적어야 하는 문법으로 BETWEEN a AND b 로 쓴다.
    Tip. 물론 NOT BETWEEN 으로도 활용할 수 있다. 

    Tip. 논리연산자 적용 순서를 위해 () 괄호를 잘 활용한다.
    Tip. 같은 컬럼 description 에 대한 NOT LIKE 문이어도 구문을 2개로 분리해서 작성해주어야 한다.
    Ref. [BETWEEN]

     

     

    IN 과 NOT IN - 하나의 필드 여러개 값 복수검색

    An introduction to the expressions IN and NOT IN. IN and NOT IN are logical operators which restrict the results returned from a query to either belong or not belong to a specific list of values. The values can be either numeric or string.

    IN 과 NOT IN 은 논리연산자로 특정한 값이 속하거나 속하지 않은 결과를 응답한다. 숫자형과 문자형에 사용할 수 있다.


    SELECT sales_agent, product, account
    FROM sales_pipeline
    WHERE product IN ("GTX Basic", "GTX Plus Basic", "GTX Plus Pro", "GTX Pro")
    AND deal_Stage = "Won" AND account
    NOT IN ("Bioholding", "Bioplex", "Condax", "Faxquote")


     

     

    IS NULL 과 IS NOT NULL - NULL 처리

    The expressions IS NULL and IS NOT NULL are used in WHEREclauses to either display rows for which there is no value in a specified column or to show only those rows which have a value in the specified column.

    SELECT monthyear, animal_type, outcome_type, outcome_subtype 


    Tip. NULL 값에 대한 처리는 아무리 강조해도 부족하다.
    Ref. [IS NULL and IS NOT NULL]

    댓글

Designed by Tistory.