MYSQL

24.7.8 TIL (MYSQL 1주차~2주차)

박현국 2024. 7. 8. 16:01
약 이틀동안 mysql 1,2주차 강의를 모두 듣고 실습까지 완료하였다.
대학시절 sql 공부를 몇번 하였지만, 정확하게 개념이 부족하였고, 이번 공부를 계기로 개념을 바로 잡고자 기초부터 진행중이다.  

## 데이터 필터링 WHERE 절 ##

select *

from customers

where age = 21

 

## between(필터링-비교연산) ##

select *

from customers

where age between 10 and 21

 

## in(필터링-비교연산) ##

select *

from customers

WHERE age in (15,21,31)

 

## like(필터링-비교연산) ##

select *

from customers

where name like '김%'

 

## and, or, not(필터링-논리연산) ##

select *

from customers

where age > 20 and gender='female'

 

## 1주차 숙제 ##

select restaurant_name, customer_id

from food_orders

where food_preparation_time between 20 and 30

and cuisine_type = 'korean'

 

## count(1), count(distinct) 함수 행(ROW) 갯수##

select count(1) count_of_orders,

count(distinct customer_id) count_of_customers

from customers

 

select count(1) as total_count,

count(distinct pay_type) as count_of_pay_type

from payments

 

 

## 최대값, 최소값 ##

select min(price) min_price,

max(price) max_price

from food_orders

 

SELECT min(quantity) as min_quantity,

max(quantity) as max_quantity

FROM food_orders

 

 

## WHERE 절 (주문 금액이 30,000원 이상인 주문건의 갯수 구하기 ##

SELECT count(order_id) count_of_orders

from food_orders

where price>=30000

 

## 한국 음식의 주문 당 평균 음식가격 구하기 ##

select avg(price) as avg_price

from food_orders

where cuisine_type = 'korean'

 

## group by 범주별 연산 ##

select cuisine_type,

sum(price) as sum_of_price

from food_orders

group by cuisine_type

 

## 음식점별 주문 금액 최댓값 조회하기 ##

select restaurant_name,

max(price) max_price

from food_orders

group by restaurant_name

 

## 결제 타입별 가장 최근 결제일 조회하기 ##

select pay_type, max(date) recent_date

from payments

group by pay_type

 

## order by 오름차순(기본), 내림차순(desc) 정렬##

select cuisine_type, sum(price) sum_price

from food_orders

group by cuisine_type

order by sum(price) desc

 

## 음식점별 주문 금액 최댓값 조회하기 (최대값 기준 내림차순 정렬)##

SELECT restaurant_name, max(price) max_price

from food_orders

group by restaurant_name

order by max(price) desc

 

## 고객을 이름 순으로 오름차순으로 정렬하기 ##

select name

from customers

order by name

 

## 2주차 숙제 (음식 종류별 가장 높은 주문 금액과 가장 낮은 주문 금액을 조회하고, 가장 낮은 주문금액 순으로(내림차순) 정렬하기 ##

SELECT cuisine_type , min(price) min_price, max(price) max_price

from food_orders

group by cuisine_type

order by min(price) desc

 

 

아직까지는 쉬운 파트라서 큰 무리 없이 잘 완료하였다. mysql 자체 쿼리는 우리가 평소에 영어로 말하듯이 문법을 사용하여 작성할 수 있어서, 편리하다는 것이 가장 큰 장점인듯 하다.

 

'MYSQL' 카테고리의 다른 글

24.8.27(ERD 작성)  (0) 2024.08.27
24.7.11 (MYSQL 3주차, JAVA)  (0) 2024.07.11