나의 발자취

SQL 기본 쿼리 (1) select, from, where, order by, limit, select distinct, is not(<>), union, between, like, and, or 본문

Backend

SQL 기본 쿼리 (1) select, from, where, order by, limit, select distinct, is not(<>), union, between, like, and, or

달모드 2024. 9. 11. 16:39

select * from customer;

 

-- as "String"

select cust_id, contact_name, company_name, mileage * 1.1 as "10% 추가" from customer;

select cust_id, contact_name, company_name, mileage from customer where mileage >= 100;

 

-- order by

-- limit

select cust_id, contact_name, company_name, mileage, city

from customer

where city = '서울'

order by mileage desc

limit 3

 

 

-- distinct: 중복된 이름을 제거한 도시를 추출

select distinct city from customer;

SELECT COUNT(DISTINCT city) AS distinct_city_count FROM customer;

 

-- is not

select * from customer where contact_position <> '대표'

select * from customer where contact_position != '대표'

 

-- union // 합치면서 중복 제거 (중복된 결과도 출력하려면 union all) - 속성 개수와 타입이 동일해야함.

select cust_id, contact_name, mileage, city from customer where city = '부산'

union

select cust_id, contact_name, mileage, city from customer where mileage < 100;

 

-- or

select * from customer where contact_position in ('대표','사원');

select * from customer where contact_position = '대표' or contact_position = '사원';

 

-- between

-- and

select * from customer where mileage between 100 and 200;

select * from customer where mileage = 100 and mileage = 200;

 

 

-- like

select * from customer where region like '서울%';

select * from customer where region like '%광역시';

 

-- 대전, 서울,컨택포지션은 대표나 사원

select * from customer where contact_position in ('대표','사원') and city in ('대전', '서울');

Comments