개발로드

[MySQL]select 기초 연습예제 본문

SQL

[MySQL]select 기초 연습예제

위대한개발자 2024. 2. 6. 18:14

use jointestdb;


1) customertbl테이블에서 gender가 "남"인 자료만  모든 컬럼을 검색
검색 : select ,   gender가 "남"이라는 조건 : where 


select * from customertbl where gender='남';

 

2) customertbl테이블에서 address가 "서울 송파"인 자료들 중
custid, cname, phone만 검색

 

select custid,cname,phone from customertbl where address='서울 송파';

 

3) customertbl테이블에서 address가 "서울 관악"인 자료들 중
custid, cname만 검색하되 custid는 고객ID, cname은 고객명으로
컬럼명을 출력


select custid as '고객ID', cname as '고객명' from customertbl where address='서울 관악';

 

4) customertbl테이블에서  address가 "서울 동작" 이고 gender가 "남"인
자료 중 cname과  phone만 검색하되,  cname은 고객명으로  phone은 휴대폰으로
컬럼명을 표시


select cname as '고객명', phone as '휴대폰' from customertbl where address ='서울 동작' and gender='남';

 

5) producttbl에서 price가 1000이상 2300이하인 자료들의 pcode, pname 검색
단 pcode는 상품코드, pname은 상품명으로 컬럼명을 표시


select pcode as '상품코드', pname as '상품명' from producttbl where price between 1000 and 2300;

 

6)  producttbl에서 price가 2000이상이거나 700이하인 자료들의
pname과 region을 검색하되, pname은 상품명, region은 원산지로 컬럼명 표시


select pname as '상품명', region as '원산지' from producttbl where price between 2000 and 7000;

 

7)  producttbl에서 price가 1000미만이거나 pname이 오렌지인 자료들의
pname과 region을 검색하되, pname은 상품명, region은 원산지로 컬럼명 표시


select pname as '상품명', region as '원산지' from producttbl where price<1000 and pname='오렌지';

 

8) producttbl에서 pname이 "바나나", "오렌지", "망고"인 자료들의 
pcode, pname 검색. 단 pcode는 상품코드, pname은 상품명으로 컬럼명을 표시


select pcode as '상품코드', pname as '상품명' from producttbl where pname in('바나나','오렌지','망고');

 

9) customertbl테이블에서 cname의 성이 "박"씨, "정"씨, "김" 씨인 자료들의
custid, cname, phone를 검색하되, custid는 고객ID, cname은 고객명으로 컬럼명 표시


select custid as '고객ID', cname as '고객명', phone from customertbl where cname like '박%' or 
cname like '정%' or cname like '김%';

 

10) customertbl테이블에서 phone가 null인 자료들의 custid, cname 검색


select custid,cname from customertbl where phone is null;

 

11) customertbl테이블에서 phone가 null인 아닌 자료들의 custid, cname 검색


select custid,cname from customertbl where phone is not null;

 

12) customertbl테이블에서 address를 검색하되 중복된 자료는 하나만 표시


select distinct address from customertbl;


13) productbl 테이블에서 pname(과일이름)이 '바'로 시작하거나 price가 1000이상 2500이하인 자료들의 pcode(상품 코드), pname(과일이름), region(생산지)를 검색하시오.


select pcode as '상품 코드', pname as '과일이름', region as '생산지' from producttbl where pname like '바%' and
price between 1000 and 2500;

 


14) pordertbl 테이블에서 custid가 'c1', 'c3', 'c5'가 아닌 자료들의
 orderid(주문아이디), pcode(제품코드), custid(고객코드), amount(주문 수량)을
검색 

select orderid as '주문 아이디', pcode  as '제품코드', custid as '고객코드', amount as '주문 수량'
from pordertbl where custid not in ('c1','c3','c5');

 


15) producttbl테이블에서 가격을 기준으로 오름차순 정렬하여 검색하시오.


select * from producttbl order by price asc;

16) pordertbl테이블에서 custid의 중복을 제외하고 주문한 고객 코드(custid)를 검색하시오.

 distinct 컬럼명 - 컬럼에서 중복된 자료를 제외한다.


select distinct custid as '고객 코드' from pordertbl;


마치며

MySQL의 select의 간단한 예제를 풀어보았습니다.

 

여유가 되신다면 제 GitHub에 오셔서 좋은 코드들을 구경해주세요!

 

https://github.com/gimpo5975?tab=repositories

 

gimpo5975 - Overview

gimpo5975 has 2 repositories available. Follow their code on GitHub.

github.com