SQL 문법 연습 1~3

김동욱·2024년 10월 15일

데이터베이스

목록 보기
3/7

1) 돈을 벌기 위해 일을 합시다!

⚡ **실제 데이터 베이스를 연결하기 전, SQL 문법을 탄탄하게 다져봅시다.**

아래와 같은 sparta_employees(직원) 테이블이 있습니다.

idnamepositionsalaryhire_date
1르탄이개발자300002022-05-01
2배캠이PM400002021-09-25
3구구이파트장350002023-06-01
4이션이팀장500002021-07-09
  1. sparta_employees 테이블에서 모든 직원의 이름(name)과 직급(position)을 선택하는 쿼리를 작성해주세요.
    select name, position from sparta_employees
  2. sparta_employees 테이블에서 중복 없이 모든 직급(position)을 선택하는 쿼리를 작성해주세요.
    select distinct position from sparta_employees
  3. sparta_employees 테이블에서 연봉(salary)이 40000과 60000 사이인 직원들을 선택하는 쿼리를 작성해주세요.
    select * from sparta_employees where salary between 40000 and 60000
  4. sparta_employees 테이블에서 입사일(hire_date)이 2023년 1월 1일 이전인 모든 직원들을 선택하는 쿼리를 작성해주세요.
    select * from sparta_employees where hire_date < '2023-01-01'




2) 이제 좀 벌었으니 flex 한 번 해볼까요?!

⚡ **실제 데이터 베이스를 연결하기 전, SQL 문법을 탄탄하게 다져봅시다.**

여러분이 구매하고 싶은 상품들의 정보가 있는 products(상품) 테이블이 아래에 있습니다.

idproduct_namepricecategory
1맥북 프로1200컴퓨터
2다이슨 청소기300생활가전
3갤럭시탭600컴퓨터
4드롱기 커피머신200주방가전
  1. products 테이블에서 제품 이름(product_name)과 가격(price)만을 선택하는 쿼리를 작성해주세요.
    select product_name, price from products
  2. products 테이블에서 제품 이름에 '프로'가 포함된 모든 제품을 선택하는 쿼리를 작성해주세요.
    select * from products where product_name like "%프로%"
  3. products 테이블에서 제품 이름이 '갤'로 시작하는 모든 제품을 선택하는 쿼리를 작성해주세요.
    select * from products where product_name like "갤%"
  4. products 테이블에서 모든 제품을 구매하기 위해 필요한 돈을 계산하는 쿼리를 작성해주세요.
    select sum(price) from products



3) 상품 주문이 들어왔으니 주문을 처리해봅시다!

이제 상품 주문이 들어왔으니 어떤 고객에게 어떤 주문이 들어왔는지를 파악할 수 있는 orders(주문) 테이블이 아래에 있습니다.

idcustomer_idproduct_idamountshipping_feeorder_date
171913500002023-11-01
213121100002023-11-02
36541200002023-11-05
4100832250002023-11-05
535611150002023-11-09
  1. orders 테이블에서 주문 수량(amount)이 2개 이상인 주문을 진행한 소비자의 ID(customer_id)만 선택하는 쿼리를 작성해주세요!
    select customer_id from orders where amount >= 2
  2. orders 테이블에서 2023년 11월 2일 이후에 주문된 주문 수량(amount)이 2개 이상인 주문을 선택하는 쿼리를 작성해주세요!
    select * from orders where order_date > '2023-11-02' and amount >= 2
  3. orders 테이블에서 주문 수량이 3개 미만이면서 배송비(shipping_fee)가 15000원보다 비싼 주문을 선택하는 쿼리를 작성해주세요!
    select * from orders where amount < 3 and shipping_fee > 15000
  4. orders 테이블에서 배송비가 높은 금액 순으로 정렬하는 쿼리를 작성해주세요!
    select * from orders order by shipping_fee desc
profile
갓겜만들어야지

0개의 댓글