SQL(Structured Query Language)은 관계형 데이터베이스를 조작하기 위한 언어
1. 비교 연산자
2. 논리연산자
3. Where절에 들어가는 연산자
+LIKE심화 : %는 와일드카드다, 어떤 값이 와도 상관없음을 의미
1. 데이터 검색(select)과 데이터 갱신
2. 동시성 제어
3. 장애 대응
4. 보완
MySQL, Oracle, PostgreSQL 등이 모두 이 관계형 데이터베이스
해커랭크 문제풀이
# Revising the select query 1
select *
from city
where countrycode = 'USA' and
population > 100000
# select by id
select *
from city
where id = 1661
# weather observation station 6
select distinct city
from station
where city like 'a%'
or city like 'e%'
or city like 'i%'
or city like 'o%'
or city like 'u%'
select distinct city
from station
where left(city, 1) in ('a', 'i', 'e', 'o', 'u')
select distinct city
from station
where substring(city, 1, 1) in ('a', 'i', 'e', 'o', 'u')
#weather observation station 12
select distinct city
from station
where left(city,1) not in ('a', 'i', 'e', 'o', 'u')
and right(city, 1) not in ('a', 'i', 'e', 'o', 'u')
select distinct city
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'
# employee names
select name
from employee
order by name # 알파벳 순서대로 출력, alphabetical order
# employee salaries
select name
from employee
where months < 10 and salary > 2000
order by employee_id
# Higher Than 75 Marks
select name
from students
where marks > 75
order by right(name,3), id
select name
from students
where marks > 75
order by substr(name,-3), id
# weather observation station 15
select round(long_w,4)
from station
where lat_n in (
select max(lat_n)
from station
where lat_n < 137.2345
)
select round(long_w, 4)
from station
where lat_n < 137.2345
order by lat_n desc
limit 1 -- order와 limit을 사용해서 최대 or 최소를 추출할 수 있음
| [멋사 SQL 3day] AARRR, 매출분석, 고객세분화분석 (1) | 2022.11.23 |
|---|---|
| [멋사 SQL 3day] Join, Union/Union all (0) | 2022.11.22 |
| [멋사 SQL 2day] RFM Segmentation, 테이블 피봇 (0) | 2022.10.04 |
| [멋사 SQL 2day] 집계함수, group by, having, case when 조건문 (0) | 2022.10.04 |