Q51) Categories 테이블에서 CategoryID,CategoryName,Description을 가져올때, Products 테이블에서 ProductID가 1인 ProductName을 가져와라
Select c.CategoryID, c.CategoryName, c.Description, p.ProductName from Products p join Categories c on p.CategoryID=c.CategoryID where p.ProductID=1
Q52) Categories 테이블에서 CategoryID, CategoryName, Description을 가져온다.
이때 (가져올때 조건, 서브쿼리 내용은) Products 테이블에서 ProductName이 Chais인 CategoryID
Select CategoryID, CategoryName,Description From Categories where CategoryID = (Select CategoryID FROM Products Where Productname = 'Chais')
Q53) Categories 테이블에서 CategoryID, CategoryName, Description을 가져온다 이때 (가져올때 조건, 서브쿼리 내용은) Products 테이블에서 Price가 50 초과인 CategoryID중에서 CategoryID를 가져온다 [CategoryID는 Price가 50 초과인 것들 중에서 가져온다. hint : IN 함수 ~중에서 가져오기]
Select c.CategoryID, c.CategoryName, c.Description from Categories c join Products p on p.CategoryID=c.CategoryID where p.price>50 group by p.categoryid order by p.CategoryID
SELECT c.CategoryID, c.CategoryName, c.Description
FROM Categories c
WHERE c.CategoryID IN (
SELECT p.CategoryID
FROM Products p
WHERE p.price > 50
)
ORDER BY c.CategoryID;
Q54)
Categories 테이블에서 CategoryID, CategoryName, Description을 가져오는데
서브쿼리 내용 : CategoryID가 Products 테이블에서 Price가 50초과인 CategorytID에 해당되는
[hint : any 하나이상에 해당된다 = 어떤 값에 해당된다 = ..중에 하나에 해당된다]
select CategoryID, CategoryName, Description from Categories where CategoryID = any (select CategoryID from Products where Price > 50);
SELECT CategoryID, CategoryName, Description FROM Categories WHERE CategoryID IN (SELECT CategoryID FROM Products WHERE Price > 50);
Q55)
Categories 테이블에서 CategoryID와 CategoryName을 가져온다
서브쿼리1. Products 테이블의 CategoryID와 Categories 테이블의 CategoryID가 같은 Produects 테이블에서의 최대(MAX) Price를 MaximumPrice 라는 이름으로 출력
서브쿼리2. Products 테이블의 CategoryID와 Categories 테이블의 CategoryID가 같은 Produects 테이블에서의 평균(AVG) Price를 AveragePrice 라는 이름으로 출력
Select c.CategoryID, c.CategoryName, (select max(price) from Products p where c.CategoryID=p.CategoryID) as MaximumPrice, (select avg(price) from Products p where
c.CategoryID=p.CategoryID) from Categories c;
Q56)
Products 테이블과 Suppliers 테이블을 JOIN,
Products테이블의 SupplierID와 Suppliers테이블의 SupplierID가 같고 Products테이블에서 ProductName을 가져오고, Suppliers테이블에서 SupplierName을 가져와서 ‘by'로 연결하고
컬럼이름은 Product로 한다.
Suppliers테이블에서 Phone을 가져오고, Products테이블에서 Price를 가져온다
Price는 50초과를 가져오고, ProductName으로 order by 한다 [order by default값은 asc 오름차순]
select concat(p.ProductName,' by ', s.SupplierName) AS Product, s.Phone, p.Price from Products p join Suppliers s on s.SupplierID=p.SupplierID where Price>50 order by Productname
Q57)
Customers 테이블에는 있는데 Suppliers 테이블에는 없어도 출력시켜라 -> left or right join
반대로 Suppliers 테이블에는 있는데 Customers테이블에는 없어도 출력시켜라 -> left or right join
SELECT c.CustomerName, c.City, c.Country, s.SupplierName
FROM Customers c
LEFT JOIN Suppliers s ON c.CustomerName = s.SupplierName
WHERE s.SupplierName IS NULL;
SELECT c.CustomerName, c.City, c.Country, s.SupplierName
FROM Customers c
RIGHT JOIN Suppliers s ON c.CustomerName = s.SupplierName
WHERE c.CustomerName IS NULL;
Q58)
위의 쿼리문에서 left join이나 right join을 할 때
CustomerName이 없는 경우에는 No Customer 라고 출력되고
SupplierName이 없는 경우에는 No Supplier 가 출력되도록 하라
SELECT
case when c.CustomerName is NULL then 'No Customer' Else c.customername End CustomerName,
case when s.SupplierName is NULL then 'No Supplier' Else s.suppliername End SupplierName,
c.City, c.Country
FROM Customers c left join Suppliers s on c.City = s.city and c.country=s.country;
SELECT
case when c.CustomerName is NULL then 'No Customer' Else c.customername End CustomerName,
case when s.SupplierName is NULL then 'No Supplier' Else s.suppliername End SupplierName,
c.City, c.Country
FROM Customers c right join Suppliers s on c.City = s.city and c.country=s.country;
Q59)
Orders 테이블과 OrderDetails 테이블을 CROSS JOIN하라
SELECT * from Orders CROSS JOIN OrdersDetails
Orders 테이블의 CustomerID와 OrderDetails 테이블의 ProductID를 가져와라
Select o.CustomerID, od.ProductID from Orders o cross join OrderDetails od;
'Engineer > SQL' 카테고리의 다른 글
MYSQL 연습하기 with w3school - 1 (0) | 2023.08.03 |
---|