728x90
SELECT MAX(price) AS 'MAX_PRICE'
FROM PRODUCT;
SELECT
WAREHOUSE_ID,
WAREHOUSE_NAME,
ADDRESS,
coalesce(FREEZER_YN, 'N') AS 'FREEZER_YN'
FROM FOOD_WAREHOUSE
WHERE ADDRESS LIKE '%경기도%'
ORDER BY 1 ;
SELECT
FACTORY_ID,
FACTORY_NAME,
ADDRESS
FROM FOOD_FACTORY
WHERE ADDRESS LIKE '%강원도%'
ORDER BY 1 ;
SELECT
ANIMAL_ID,
NAME,
DATE_FORMAT(DATETIME,'%Y-%m-%d') AS '날짜'
FROM ANIMAL_INS
ORDER BY 1;
SELECT DR_NAME,
DR_ID,
MCDP_CD,
DATE_FORMAT(HIRE_YMD, '%Y-%m-%d')
FROM DOCTOR
WHERE MCDP_CD IN ('GS','CS')
ORDER BY HIRE_YMD DESC;
--틀린 코드
SELECT PRODUCT_ID,
PRODUCT_NAME,
PRODUCT_CD,
CATEGORY,
MAX(PRICE) AS 'PRICE'
FROM FOOD_PRODUCT;
--결과 : PRICE 부분만 가장 큰 값을 가져오고, 나머지는 맨 위에 값만 가지고 옴
정답:
--1. PRICE를 기준으로 내림차순 정렬하고 1개만 추출,
SELECT *
FROM FOOD_PRODUCT
ORDER BY PRICE DESC
LIMIT 1;
--2. 1.의 경우 최댓값이 2개인 경우 오답처리 되므로, MAX, 서브쿼리 사용
SELECT *
FROM FOOD_PRODUCT
WHERE PRICE = (SELECT MAX(PRICE) FROM FOOD_PRODUCT);
SELECT ANIMAL_ID
FROM ANIMAL_INS
WHERE NAME IS NULL
ORDER BY ANIMAL_ID;
SELECT
COUNT(*) AS 'USERS'
FROM USER_INFO
WHERE age BETWEEN 20 and 29
AND joined LIKE '2021%'
--에러
SELECT ANIMAL_ID, NAME,
IF (SEX_UPON_INTAKE LIKE 'Neutered%' OR 'Spayed%','O','X') AS '중성화'
FROM ANIMAL_INS
ORDER BY ANIMAL_ID
--정답
SELECT ANIMAL_ID, NAME,
IF (SEX_UPON_INTAKE LIKE 'Neutered%' OR SEX_UPON_INTAKE LIKE 'Spayed%','O','X') AS '중성화'
FROM ANIMAL_INS
ORDER BY ANIMAL_ID
라이브세션 실습쿼리
-- 1.거주하는 도시(city)별로 고객수 조회(고객수가 큰 도시부터 정렬)
SELECT
customer_city ,
COUNT(*)
FROM customers c
GROUP BY 1
ORDER BY 2 DESC ;
-- 2.거주하는 주(state)별로 고객수를 조회하고, 고객수가 10명 이상인 주(state)만 조회(고객수가 큰 도시부터 정렬)
SELECT
customer_state,
COUNT(*) as cnt
FROM customers c
GROUP BY customer_state
HAVING cnt >= 10
ORDER BY cnt DESC ;
-- 3.고객테이블에서 10명 샘플링하기
SELECT
*
from customers c
order by rand()
limit 10;
https://dydatablog.tistory.com/40
728x90
'내일배움캠프 > TIL' 카테고리의 다른 글
[TIL]241202_JOIN,서브쿼리,팀 프로젝트 주제정하기 (1) | 2024.12.02 |
---|---|
[TIL]241129_코드카타 (2) | 2024.11.29 |
[TIL]241127_코드카타, SQL 연습문제4 (0) | 2024.11.27 |
[TIL]SQL코드카타, SQL과제Lv.2,3 (0) | 2024.11.26 |
[TIL]241125_SQL자격증준비 (1) | 2024.11.25 |