본문 바로가기

SQL

[SQL] SQL 쿼리 (1)

classicmodels 데이터베이스를 기반으로 한 15문제를 풀어보겠습니다.

use classicmodels;

describe customers;
describe employees;
describe offices;
describe orderdetails;
describe orders;
describe payments;
describe productlines;
describe products;

 

- employees 테이블 예시

# 1. 모든 고객의 이름 조회
select customerName
from customers;


# 2. 모든 사원의 정보 조회
select *
from employees;


# 3. 이름(firstName)이 'L'로 시작하는 사원의 이름, 사원번호, 이메일 조회
select employeeNumber, firstName, email
from employees
where firstname like 'L%';


# 4. 이름(firstname)의 두 번째 글자가 'e'인 사원의 이름과 직무 조회
select firstname, jobtitle
from employees
where firstname like '_e%';


# 5. 사원의 이름을 오름차순 정렬
select firstname
from employees
order by firstname;


# 6. 고객의 이름을 내림차순 정렬
select customerName
from customers
order by customerName desc;


# 7. 가격이 100 이상인 상품의 코드와 가격
select productcode, priceeach
from orderdetails
where priceeach >= 100;


# 8. 가격이 100 이상인 상품의 수
select count(*) as tot_product
from orderdetails
where priceeach >= 100;


# 9. 주(state) 정보가 없는 사무실의 코드 
select officecode
from offices
where state is null;


# 10. 주문일자가 2004년 이후인 상품의 모든 정보
select *
from orders
where orderdate >= '2004-01-01';


# 11. 모든 사원의 이름과 국적
select firstname, lastname, country
from employees as A, offices as B
where A.officecode = B.officecode;


# 12. 고객의 고객번호, 이름, 전화번호, 도시, 주문번호, 주문일자
select A.customernumber, customername, phone, city, ordernumber, orderdate
from customers as A, orders as B
where A.customernumber = B.customernumber;


# 13. 사무실 코드가 1, 2, 3인 사원의 모든 정보
select *
from employees
where officecode in ( 1, 2, 3 );


# 14. 사무실별 사원의 수
select officecode, count(employeenumber) as tot_employees
from employees
group by officecode;


# 15. 미국인 사원의 모든 정보
select *
from employees as A , offices as B
where A.officeCode = B.officeCode and 
		B.country = 'USA';

 

'SQL' 카테고리의 다른 글

[SQL] SQL 쿼리 (6)  (2) 2024.03.05
[SQL] SQL 쿼리 (5)  (0) 2024.02.21
[SQL] SQL 쿼리 (4)  (0) 2024.02.21
[SQL] SQL 쿼리 (3)  (0) 2024.02.21
[SQL] SQL 쿼리 (2)  (0) 2024.02.19