0% found this document useful (0 votes)
11 views11 pages

SUBQUERYY

Uploaded by

Budiansyah The
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
11 views11 pages

SUBQUERYY

Uploaded by

Budiansyah The
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 11

Nama : BUDIANSYAH.

t
Nrp : 160721019
-- 4 ---
select c.CustomerID,c.contactName,count(o.OrderID) AS berapaKaliOrder
from orders o inner join customers c on o.CustomerID = c.CustomerID
group by c.customerID,c.contactName;

-- 5 --
select c.customerID,count(orderID) as jumlahNotaOrder
from customers as c
inner join orders as o on c.customerID = o.customerID
group by c.customerID
having count(orderID) > 5;
-- 6 --
select *, (Unitprice * Quantity) as totalTransaksi
from orderDetails;
-- 6 subQuery ---
select*,min(tabelbaru.totalTransaksi)
from (select *, (Unitprice * Quantity) as totalTransaksi
from orderDetails) as tabelbaru
group by tabelbaru.OrderID desc
limit 1 ;
-- 7 --
select O.OrderID,O.OrderDate,C.ContactName,S.CompanyName
from customers as C
inner join orders as O on C.CustomerID = O.CustomerID
inner join shippers as S on S.ShipperID = O.ShipVia;
-- 8 ---
select *,(UnitPrice*Quantity) as nominalTransaksi
from orderDetails
having (UnitPrice*Quantity) > 150;
-- 9 --
select c.CategoryName,p.QuantityPerUnit,c.CategoryID
from products as p inner join categories as c on p.categoryID = c.categoryID
group by CategoryName;
-- 10 --
select *, (Unitprice * Quantity) as totalTransaksi
from orderDetails as od inner join orders as o on od.OrderID = o.OrderID
inner join customers as c on c.CustomerID = o.CustomerID
where c.Country = 'Germany';

sub querry

1.a
SELECT COUNT(*) as jumlah, c.customerID
FROM customers as c
INNER JOIN orders as o on o.customerID = c.customerID
GROUP BY c.customerID
HAVING jumlah = (SELECT COUNT(*) as jumlah
FROM customers as c
INNER JOIN orders as o on o.customerID = c.customerID
GROUP BY c.customerID
ORDER BY jumlah DESC
LIMIT 1);
1.b
-- step 2
SELECT p.ProductID, p.ProductName, SUM(od.UnitPrice*od.Quantity) as TotalPenjualan
FROM products as p
INNER JOIN orderdetails as od ON od.productID = p.productID
INNER JOIN orders as o ON o.orderID = od.orderID
WHERE o.orderDate between '2011-08-01' and '2011-08-31'
GROUP BY p.productID
HAVING TotalPenjualan = (SELECT SUM(od.UnitPrice*od.Quantity) as TotalPenjualan
FROM products as p
INNER JOIN orderdetails as od ON od.productID = p.productID
INNER JOIN orders as o ON o.orderID = od.orderID
WHERE o.orderDate between '2011-08-01' and '2011-08-31'
GROUP BY p.productID
ORDER BY TotalPenjualan desc
LIMIT 1);
1.c
SELECT p.productID, p.productName,
(SELECT s.companyName
FROM orderdetails as od
INNER JOIN orders as o ON o.orderID = od.orderID
INNER JOIN customers as c ON c.customerID = o.customerID
INNER JOIN shippers as s ON s.shipperID = o.shipVia
WHERE od.productID = p.productID
GROUP BY c.customerID
ORDER BY SUM(od.quantity) DESC
LIMIT 1) as companyName
FROM products as p
ORDER BY p.productID;

nomor 2
-- 2.2 A --
select orderID,avg(tabelbaru.totalJual)
from (select *,(UnitPrice * Quantity) as totalJual from orderdetails od) as tabelbaru
group by orderID;

-- 2.2 B --
select * ,(UnitPrice*Quantity)
from orderdetails AS OD
group by orderID
having (UnitPrice*Quantity) > (select orderID,avg(tabelbaru.totalJual)
from (select *,(UnitPrice * Quantity) as totalJual from orderdetails od) as tabelbaru
group by orderID);

nomor 3
SELECT p.ProductID, p.ProductName, SUM(od.UnitPrice*od.Quantity) as TotalPenjualan
FROM products as p
INNER JOIN orderdetails as od ON od.productID = p.productID
INNER JOIN orders as o ON o.orderID = od.orderID
WHERE o.orderDate between '2011-08-01' and '2011-08-31'
GROUP BY p.productID
HAVING TotalPenjualan = (SELECT SUM(od.UnitPrice*od.Quantity) as TotalPenjualan
FROM products as p
INNER JOIN orderdetails as od ON od.productID = p.productID
INNER JOIN orders as o ON o.orderID = od.orderID
WHERE o.orderDate between '2011-08-01' and '2011-08-31'
GROUP BY p.productID
ORDER BY TotalPenjualan desc
LIMIT 1);
-- 3b --
select c.customerID,c.companyName,Total
from customers as c inner join
(select c.customerID,c.companyName,sum(od.UnitPrice * od.Quantity) as Total
from products as p
INNER JOIN orderdetails as od ON od.productID = p.productID
INNER JOIN orders as o ON o.orderID = od.orderID
INNER JOIN customers c ON c.customerID = o.customerID
group by c.customerID) as t on c.customerID = t.customerID;
-- 4a --
select *
from products as p
left JOIN orderdetails as od ON od.productID = p.productID
left JOIN orders as o ON o.orderID = od.orderID
left JOIN customers c ON c.customerID = o.customerID
group by p.productID
;

select c.customerID,c.companyName
from products as p
inner JOIN orderdetails as od ON od.productID = p.productID
inner JOIN orders as o ON o.orderID = od.orderID
inner JOIN customers c ON c.customerID = o.customerID
where c.customerID = 0
group by p.productID;

select *,c.customerID,c.companyName
from products as p
inner JOIN orderdetails as od ON od.productID = p.productID
inner JOIN orders as o ON o.orderID = od.orderID
inner JOIN customers c ON c.customerID = o.customerID
where p.UnitsOnOrder = 0
group by p.productID;
-- 4 STEP 1 --
select c.customerID,c.companyName
from customers AS C LEFT JOIN orders as o on C.customerID = o.customerID
where o.customerID is Null;

-- 4 STEP 2 --
SELECT c.customerID,c.companyName
from customers AS C
where c.customerID NOT IN
(
select c.customerID
from products as p
inner JOIN orderdetails as od ON od.productID = p.productID
inner JOIN orders as o ON o.orderID = od.orderID
inner JOIN customers c ON c.customerID = o.customerID
group by c.customerID
);
-- 4 STEP 2 --
select c.cutomerID,c.companyName
from cutomers as c LEFT JOIN orders as o on C.customerID = o.customerID
where c.customerID =
(select c.customerID,c.companyName
from customers AS C LEFT JOIN orders as o on C.customerID = o.customerID
where o.customerID is Null);

select c.customerID,c.companyName
from products as p
inner JOIN orderdetails as od ON od.productID = p.productID
inner JOIN orders as o ON o.orderID = od.orderID
inner JOIN customers c ON c.customerID = o.customerID
group by c.customerID;

-- 4.2 step 1 --
select e.EmployeeID,e.FirstName,count(e.employeeID)*0.1
from employees as e inner join orders as o on e.employeeID = o.employeeID
group by e.employeeID;

-- 4.2 step 2 --
select e.EmployeeID,e.FirstName,count(e.employeeID) as komisi
from employees as e inner join orders as o on e.employeeID = o.employeeID
group by e.employeeID
having komisi = (select count(e.employeeID)*0.1 from employees as e inner join orders as o
on e.employeeID = o.employeeID
group by e.employeeID);
SELECT e.EmployeeID, e.FirstName, sub.total_orders * 0.1
FROM employees as e
INNER JOIN orders as o ON e.employeeID = o.employeeID
INNER JOIN (
SELECT EmployeeID, COUNT(OrderID) as total_orders
FROM orders
GROUP BY EmployeeID
) as sub ON e.EmployeeID = sub.EmployeeID
GROUP BY e.EmployeeID, e.FirstName;

-- 4.3 step 1 --
select count(e.employeeID)as nominal
from employees as e inner join orders as o
on e.EmployeeID = o.EmployeeID
group by e.employeeID;

-- 4.4 step 2--


select e.employeeID,e.FirstName,max(totalorders) as nominal
from employees as e
inner join orders as o on e.employeeID = o.employeeID
inner join (
select o.EmployeeID,count(OrderID) totalorders
from orders as o
group by o.employeeID
order by o.employeeID DESC
)
as sub on e.employeeID = sub.employeeID
GROUP BY e.EmployeeID, e.FirstName
;

-- alternatid step 1 --
select e.employeeID,e.FirstName,count(OrderID) totalorders
from employees as e
inner join orders as o on e.employeeID = o.employeeID
group by o.employeeID
having totalorders =
(select max(sub.total_orders)
from (select count(orderID)AS total_orders
from orders group by employeeID) AS sub);

-- alternatid step 2 --
select e.employeeID,e.FirstName,max(totalorders) as nominal
from employees as e
inner join orders as o on e.employeeID = o.employeeID
inner join (
select o.EmployeeID,count(OrderID) totalorders
from orders as o
group by o.employeeID
having totalorders =
(
select max(sub.total_orders)
from (select count(orderID)AS total_orders
from orders group by employeeID) AS sub
)
)
as sub on e.employeeID = sub.employeeID
GROUP BY e.EmployeeID, e.FirstName;

--
SELECT e.employeeID,e.FirstName,max(totalorders) as nominal
FROM employees as e
INNER JOIN orders as o on e.employeeID = o.employeeID
INNER JOIN (
SELECT o.EmployeeID, COUNT(o.OrderID) as totalorders
FROM orders as o
GROUP BY o.EmployeeID
HAVING totalorders = (
SELECT MAX(total_orders)
FROM (
SELECT COUNT(OrderID) as total_orders
FROM orders
GROUP BY EmployeeID
) as sub
)
) as sub on e.employeeID = sub.EmployeeID
GROUP BY e.employeeID, e.FirstName;

-- 6.1 step 1 --
select sum(p.UnitsInStock)
from Products as p
group by p.SupplierID;
-- 6.1 step 2 --
select s.supplierID,s.companyName,max(sub.totalnilai)
from suppliers as s inner join (select sum(p.UnitsInStock) as totalnilai
from Products as p
group by p.SupplierID) as sub ON s.supplierID = sub.supplierID;

select s.supplierID,s.companyName,max(p.UnitsInStok) as totalNilaiTertinggi


from suppliers as s inner join products as p on s.supplierID = p.supplierID
group by s.supplierID
having totalNilaiTertinggi =
(select max(sub.totalOrders) from (select sum(p.UnitsInStock)
from Products as p group by p.SupplierID) as sub);

You might also like