Please submit your answer either in SQL code: The following questions are based
ID: 3699549 • Letter: P
Question
Please submit your answer either in SQL code:
The following questions are based on the sample database provided on W3schools.com (30 points in total, 3points/each except Q.9).
1. Find out the total number of orders (NumberOfOrders) made by each customer (customerID, customerName, city, country), including those who did not make any order, and sort the result by customerID.
2. Find out the total number of orders (NumberOfOrders) made by each customer (customerID, customerName, city, country), not including those who did not make any order, and sort the result by customerID.
3. Find out the total number of orders (NumberOfOrders) made by each customer (customerID, customerName, city, country) who are not from USA and made no less than 3 orders, and sort the result first by country and second by NumberOforders in descending order.
4. Find out the total number of orders (NumOrdersShipped) shipped out by each shipper (shipperID, name, phone), and sort the result by NumOrdersShipped in descending order.
5. Find out suppliers (SupplierID, name, city, country, and phone) who are from either Germany or USA, and supplying no less than 3 products. Order the result by country.
Explanation / Answer
Hello there,
I am writing all the queries, which are ready to run on any database system:
1. SELECT Customers.CustomerID,count(Orders.OrderId) as NumberOfOrders FROM Orders INNER JOIN Customers ON Orders.CustomerID = Customers.CustomerID group by Customers.CustomerID order by Customers.CustomerID;
in this, we have join between customer and order table, and applied group funcion count on orderId and use group by also.
2. SELECT Customers.CustomerID,count(Orders.OrderId) as NumberOfOrders FROM Orders INNER JOIN Customers ON Orders.CustomerID = Customers.CustomerID where Orders.OrderId is not null group by Customers.CustomerID order by Customers.CustomerID;
in this we don't have to include the customer which dont have any order so i applied fiiter on order id as it is not null then use it as valid value.
3. SELECT Customers.CustomerID,count(Orders.OrderId) as NumberOfOrders FROM Orders INNER JOIN Customers ON Orders.CustomerID = Customers.CustomerID where Customers.country not in('USA') and NumberOfOrders>3 group by Customers.CustomerID order by Customers.Country,NumberOfOrders desc;
in the 3rd one, if customer is not from USA and orders are more than 3 then show the result.
4. SELECT Shipper.ShipperID,count(Shipper.ShipperID) as NumberOfOrdersShipped FROM Orders INNER JOIN Shipper ON Orders.ShipperId = Shipper.ShipperId group by Shipper.ShipperId order by NumberOfOrdersShipped desc;
this is showing number of orders shipped and order by desc with NumberOfOrdersShipped.
5. SELECT SupplierID,name,city,country,phone FROM Orders INNER JOIN Supplier ON Orders.SupplierId = Supplier.SupplierId where count(supplierId)>3 and country in ('USA','GERMANY') order by country ;
this query is about, the orders which are made by supplier from USA and GERMANY and supplierId count is more than 3.
Hope, you got the answers, feel free to ask any doubts.
Thank you