일 | 월 | 화 | 수 | 목 | 금 | 토 |
---|---|---|---|---|---|---|
1 | 2 | 3 | 4 | |||
5 | 6 | 7 | 8 | 9 | 10 | 11 |
12 | 13 | 14 | 15 | 16 | 17 | 18 |
19 | 20 | 21 | 22 | 23 | 24 | 25 |
26 | 27 | 28 | 29 | 30 | 31 |
Tags
- 파이게임
- 자바 스트림
- 구글로그인
- SQL 문제풀이
- 스프링
- 오라클
- 프로그래머스
- 코틀린 클래스
- LeetCode SQL
- 자바스크립트
- spring boot
- mysql
- SQL
- kotlin 클래스
- Spring
- 스프링시큐리티
- js
- springboot
- pygame
- oracle
- Leetcode
- 스프링부트 채팅서버
- 자바8
- java8
- MSA
- SQL프로그래머스
- javascript
- python
- oauth2
- 스프링부트
Archives
- Today
- Total
웅겹살의 프로그래밍 삼매경
[SQL] postgreSQL 기본문법 정리 본문
728x90
postgreSQL DB | 데이터베이스 확인
select * from pg_catalog.pg_namespace;
postgreSQL schema | 스키마 확인
select * from pg_catalog.pg_tables;
postgreSQL DB, schema | 특정 스키마, 데이터베이스 조회
SELECT * FROM pg_catalog.pg_tables WHERE schemaname = 'public' AND tablename LIKE 'Weath%';
postgreSQL diagram Visualization | 우클릭으로 Diagram Visualization 확인 (DATAGRIP)
postgreSQL table | 테이블명 변경
alter table "1.book2" rename to book; alter table "3.user2" rename to "user";
postgreSQL column | 컬럼명 변경
alter table book rename column c1 to Book_id; alter table book rename column c2 to Title; alter table book rename column c3 to isbn; alter table book rename column c4 to 분야_1; alter table book rename column c8 to 저자;
postgreSQL select, from, order by | 내림 차순 desc
SELECT c8, c2 FROM Weather ORDER BY c8 DESC;
PostgreSQL Data type | 데이터 타입 변경 numeric 실수형으로
ALTER TABLE Weather ALTER COLUMN c8 TYPE NUMERIC USING c8::NUMERIC; ALTER TABLE Weather ALTER COLUMN c6 TYPE NUMERIC USING c6::NUMERIC; ALTER TABLE Weather ALTER COLUMN c5 TYPE NUMERIC USING c5::NUMERIC;
조건 내림 차순
select c8, c2, c3 from Weather where c2 in ('Seoul', 'Busan') and c8 > 3 order by c8 desc;
JOIN
select "Weather".* from "Weather", "TimeProvince" where ("Weather".c3="TimeProvince".c1) and ("Weather".c2="TimeProvince".c3);
max-min 컬럼 파생
SELECT "Weather".c6 , "Weather".c5 , "Weather".c6-"Weather".c5 As Weather_gap FROM "Weather", "TimeProvince";
특정 값 횟수 조회
SELECT "Weather".c3 , count(1) as Freq FROM "Weather" GROUP BY "Weather".c3 ORDER BY "Weather".c3 DESC;
다수 특정 값 횟수 조회
SELECT "Weather".c3 , "Weather".c2 , count(1) as Freq FROM "Weather" GROUP BY "Weather".c3, "Weather".c2 ORDER BY "Weather".c3 DESC;
postgreSQL join | JOIN 후 (max-min) 파생
select "Weather".* , "Weather".c6-"Weather".c5 As Weather_gap FROM "Weather", "TimeProvince" WHERE ("Weather".c3 = "TimeProvince".c1) and ("Weather".c2 = "TimeProvince".c3);
postgreSQL groupby | Join 후 평균 온도
select "Weather".c2 , "Weather".c3 , sum("Weather".c6 - "Weather".c5) As TotalWeather , count(1) as Freq , avg("Weather".c6 - "Weather".c5) AS AvgWeather from "Weather", "TimeProvince" where "Weather".c3 = "TimeProvince".c1 and "Weather".c2 = "TimeProvince".c3 group by "Weather".c2, "Weather".c3;
Join 후 도시별 누적환자수 집계
SELECT "TimeProvince".c3 ,count(1) as Num_Patients FROM "TimeProvince", "PatientInfo" WHERE "TimeProvince".c3 = "PatientInfo".c5 GROUP BY "TimeProvince".c3;
Join 후 도시별,성별 누적환자 수 집계
SELECT "TimeProvince".c3, "PatientInfo".c2, count(1) as Num_Patients FROM "TimeProvince", "PatientInfo" WHERE "TimeProvince".c3 = "PatientInfo".c5 GROUP BY "TimeProvince".c3, "PatientInfo".c2;
도시별, 성별, 연령별 누적환자 수 집계
SELECT "PatientInfo".c2, "PatientInfo".c3, "PatientInfo".c5, count(1) as Num_Patients FROM "PatientInfo" GROUP BY "PatientInfo".c5, "PatientInfo".c2, "PatientInfo".c3;
최대,최소 환자수 집계
SELECT MAX(Num_Patients) as Max_num_patients ,MIN(Num_Patients) as Min_num_patients FROM ( SELECT "TimeProvince".c3 , count(1) as Num_Patients FROM "TimeProvince", "PatientInfo" WHERE "TimeProvince".c3 " atientInfo".c5 GROUP BY "TimeProvince".c3 ) AS derivedTable;
도시별,성별 환자수 10000명 이상 결과조회
SELECT "TimeProvince".c3 , "PatientInfo".c2 , count(1) as Num_Patients FROM "TimeProvince", "PatientInfo" WHERE "TimeProvince".c3 " atientInfo".c5 GROUP BY "TimeProvince".c3, "PatientInfo".c2 HAVING count(1) >= 10000;
테이블 조인, 컬럼 합치기(concat)
select id, Concat(last_name, ' ', first_name) as names, c4 from name inner join genre ON name.id = genre.c1;
728x90
'SQL > SQL공부' 카테고리의 다른 글
[SQL] 프로시저의 장단점 (1) | 2022.10.13 |
---|---|
[SQL] 오라클 힌트 오라클 hint (sql 튜닝) (0) | 2022.10.11 |
[SQL] postgreSQL ARRAY 배열함수 unnest, array_agg, array_to_string (1) | 2022.03.26 |
[SQL] Oracle CONCAT, || (문자열 합치기) (0) | 2021.06.11 |
[SQL] Oracle TO_CHAR (0) | 2021.06.10 |
Comments