본문 바로가기
Database/MySQL

SELECT

by 서초록 2021. 5. 13.

가로, 로우는 데이터 한개의미

세로줄은 데이터의 특성을 의미

 

select

from 

limit 10 : 샘플데이터 10개만 조회

 

해커랭크 select all

 

비교연산자 : 특정컬럼이 특정값을 가지는 데이터만 불러오기위해 사용

 -, <>, >=, <=, >, <

 

-숫자뿐만 아니라 문자 비교도 가능

select * from customers 

where customername < "B" -- 고객이름이 'A'로 시작하는 모든 데이터를 가져옴

 

조건을 결합하고싶을 땐

select * from customers

where customername < "B" and country = 'Germany' -- 두가지 조건을 모두 만족하는 데이터를 가져옴

 

select * from customers

where customername < "B" or country = 'Germany' -- 둘 중 하나만 만족할 때

 

문자열 패턴찾기

select *

from customers

where country like '%r%' -- r이 들어가는 데이터 

   %(와일드카드) :어떤 것이 들어와도 상관없다, 안들어와도 괜찮다

 

select *

from customers

where country in ('Germany', 'France')

--where country = 'Germany' or country = 'France' -- 구문이 너무 길이지니까 

 

select  *

from customers

where customerd >=3 and customerid <= 5

--where customerid between 3 and 5

 

select * from customers

where customerid IS NULL -- 데이터가 없는 경우를 조회

--null, NaN(not a number) '비어있는 값', 숫자도 문자도 아니다

 

select *

from customers 

where country = 'Brazil'

찾고싶은 조건이 명확할 땐 like보단 =을 사용하는게 속도가 빠름

 

select *
from customers

where country like 'B_____'

   몇 개의 문자가 올지 정함

   - : 한 글자 와일드 카드

 

select * 

from customers

where discoutn like '__\%'

where discount like '__\%' -- 두 자리수 할인 받는 고객정보를 조회

   퍼센트(%)를 쓰려면 %앞에 이스케이프(예약어로서의 의미를 탈출한다), 백슬래시를 달아줌

 

각 db마다 이스케이프 다르니 검색해볼것

 

 

해커랭크

revising the select query1

select * from city

where countrycode = 'usa'

and population > 100000

 

select *

from city where id = 1661

 

 

 

 

weather observation satation6

select distinct city  --중복값 제외

from sation

where city like 'a%'

orc city like 'e%'

or city like 'i%'

or city like 'o%'

or city like 'u%'

 

weather observation satation12 

select *

from station

where city not like 'a%' -- a로 시작하지않는 조건 

 

답 : 

select *

from station

where city not like 'a%'

and city not like 'e%'

and city not like 'i%'

and city not like 'o%'

and city not like 'u%'

and city not like 'a%'

and city not like 'e%'

and city not like 'i%'

and city not like 'o%'

and city not like 'u%'

 

 

'Database > MySQL' 카테고리의 다른 글

ORDER BY  (0) 2021.05.13
UNION, UNION ALL  (0) 2021.05.09
JOIN  (0) 2021.05.09
CASE문  (0) 2021.05.09
GROUP BY, HAVING절  (0) 2021.04.28