SQL group by, having 등 - 3월 16일

조준수·2023년 3월 29일
0

SQL 심화 6

14장 07실습 - Foreign Key 예제_

  1. 테이블 내 중복 없이 데이터 수 확인
    (1) 예시
    select count(distinct name) from police_station;

  2. 테이블 내 중복 없이 데이터 확인
    (1) 예시
    select distinct name from police_station;

  3. group by
    (1) 중복을 제거하고 정렬하여 보여준다.
    (2) 예시
    select c.police_station, p.name
    from crime_status c, police_station p
    where p.name like concat("서울", c.police_station, "경찰서")
    group by c.police_station, p.name;

  4. 테이블에 데이터 추가
    (1) 예시
    update crime_status c, police_station p
    set c.reference = p.name
    where p.name like concat("서울", c.police_station, "경찰서");

15장 01이론 - Count_

  1. 집계함수
    (1) count : 총 갯수를 계산해주는 함수
    (2) sum : 합계를 계산해주는 함수
    (3) avg : 평균을 계산해주는 함수
    (4) min : 가장 작은 값을 찾아주는 함수
    (5) max : 가장 큰 값을 찾아주는 함수
    (6) first : 첫 번째 결과값을 리턴하는 함수
    (7) last : 마지막 결과값을 리턴하는 함수

  2. count 문법
    (1) 예시
    select count(column)
    from tablename
    where condition;

15장 04이론 - Sum_

  1. sum 문법
    (1) 예시
    select sum(column)
    from tablename
    where condition;

15장 05이론 - Avg_

  1. avg 문법
    (1) 예시
    select avg(column)
    from tablename
    where codition;

  2. min 문법
    (1) 예시
    select min(column)
    from tablename
    where codition;

  3. max 문법
    (1) 예시
    select max(column)
    from tablename
    where codition;

SQL 심화 7

15장 08이론 - Group By_

  1. Group By
    (1) 그룹화하여 데이터를 조회
    (2) 예시
    select column1, column2
    from tablename
    where condition
    group by column1, column2
    order by column1, column2

  2. distinct
    (1) order by를 사용할 수 없다.

15장 09이론 - having_

  1. having
    (1) 조건에 집계함수가 포함되는 경우 where 대신 having 사용
    (2) 문법
    select column1, column2
    from tablename
    where condition
    group by column1, column2
    having condition (aggreagte functions)
    order by column1, column2

SQL 심화 8

16장 01이론 - Scalar Functions_

  1. Scalar Functions
    (1) 입력값을 기준으로 단일 값을 반환하는 함수
    (2) ucase : 영문을 대문자로 변환
    (3) lcase : 영문을 소문자로 변환
    (4) mid : 문자열 부분을 반환
    (5) length : 문자열의 길이를 반환
    (6) round : 지정한 자리에서 숫자를 반올림(0이 소수점 첫째 자리)
    (7) now : 현재 날짜 및 시간을 반환
    (8) format : 숫자를 천단위 콤마가 있는 형식으로 반환

  2. ucase
    (1) 문법
    select ucase(str);

  3. lcase
    (1) 문법
    select lcase(str);

  4. mid
    (1) 문법
    select mid(str, start_position, length);
    (2) str : 원본 문자열
    (3) start_position : 문자열 반환 시작 위치(첫 글자는 1, 마지막 글자는 -1)
    (4) length : 반환할 문자열 길이

  5. length
    (1) 문법
    select length(str);

  6. round
    (1) 문법
    select round(number, decimals_place);
    (2) number : 반올림할 대상
    (3) decimals_place : 반올림할 소수점 위치(option)
    (4) 반올림할 위치를 지정하지 않을 경우, 소수점 자리(0)에서 반올림

16장 02이론 - now_

  1. now
    (1) 문법
    select now( );

  2. format
    (1) 문법
    select format(number, decimal_place);
    (2) number : 포맷을 적용할 문자 혹은 숫자
    (3) decimal_place : 표시할 소수점 위치
    (4) format으로 변환하면 타입이 문자열이 되기 때문에 숫자끼리 비교하기 위해서는 round를 써줘야 한다.

profile
print(‘안녕하세요! 반갑습니다!’)

0개의 댓글