SQL Training: Exercises on Joins

Exercise 6:

1. WAQ to display details of Customers with Orders.

2. WAQ to display details of Customers and their Orders made in 2006.

3. WAQ to display details of Sales personnel including their Customers.

4. WAQ to display details of Customers that made Orders in February.

5. WAQ to display details of Products including its Total no. of Orders.

6. WAQ to display details of Products including its Total Sales.

7. WAQ to display details of Customers that have bought keyboards.

8. WAQ to display details of Salespersons and their customers, including the total number of Orders by each Customer.

9. WAQ to display details of Orders sorted both by Products and by year. Solve and create the most
efficient view.

10. WAQ to display names (first and last) of all Customers and their relevant Sales person information.

MY SOLUTIONS:

-----------------------------------------------
-----------------------------------------------
--JOINS EXERCISE

SELECT c.customer_Id
      ,c.Customer_Fname
      ,c.Customer_Lname
  ,o.order_id
  FROM dbo.Customer c
INNER JOIN dbo.orders o
     ON c.customer_Id = o.customer_Id


SELECT c.customer_Id
      ,c.Customer_Fname
      ,c.Customer_Lname
  ,o.order_id
  FROM dbo.Customer c
INNER JOIN dbo.orders o
     ON c.customer_Id = o.customer_Id
AND YEAR(o.order_date) = 2006


SELECT DISTINCT s.salesperson_Id
               ,s.salesperson_Fname
               ,s.SalesPerson_Lname
           ,c.customer_Id
               ,c.Customer_Fname
               ,c.Customer_Lname
        FROM dbo.SalesPerson s
 INNER JOIN dbo.orders o
       ON s.salesperson_Id = o.salesperson_id
 INNER JOIN dbo.Customer c
       ON o.customer_Id = c.customer_Id


SELECT c.customer_Id
      ,c.Customer_Fname
      ,c.Customer_Lname
  ,o.order_id
  FROM dbo.Customer c
INNER JOIN dbo.orders o
     ON c.customer_Id = o.customer_Id
AND MONTH(o.order_date) = 2

SELECT p.product_id
      ,p.product_Name
  ,p.price
  ,COUNT(o.order_id) AS TOTORDERS
 FROM dbo.product p
 INNER JOIN dbo.orders o
        ON p.product_id = o.product_id
GROUP BY p.product_id
        ,p.product_Name
    ,p.price

SELECT p.product_id
      ,p.product_Name
  ,p.price
  ,SUM(o.Order_Qty) AS TOTSALES
 FROM dbo.product p
 INNER JOIN dbo.orders o
        ON p.product_id = o.product_id
GROUP BY p.product_id
        ,p.product_Name
    ,p.price

SELECT DISTINCT
       c.customer_Id
      ,c.Customer_Fname
      ,c.Customer_Lname
 FROM dbo.product p
 INNER JOIN dbo.orders o
        ON p.product_id = o.product_id
 INNER JOIN dbo.Customer c
        ON o.customer_Id = c.customer_Id
 WHERE p.product_Name = 'keyboard'



 SELECT DISTINCT
                c.customer_Id
               ,c.Customer_Fname
               ,c.Customer_Lname
               ,s.salesperson_Fname
               ,s.SalesPerson_Lname
   ,COUNT(o.order_id) AS TOTALORDERSPERCUSTOMER
        FROM dbo.SalesPerson s
 INNER JOIN dbo.orders o
       ON s.salesperson_Id = o.salesperson_id
 INNER JOIN dbo.Customer c
       ON o.customer_Id = c.customer_Id
 GROUP BY c.customer_Id
         ,c.Customer_Fname
         ,c.Customer_Lname
         ,s.salesperson_Fname
         ,s.SalesPerson_Lname

SELECT
       o.order_id
      ,o.Order_Qty
  ,o.order_date
  ,o.product_id
  ,o.customer_Id
  ,o.salesperson_Id
 FROM dbo.orders o
 INNER JOIN dbo.product p
        ON o.product_id = p.product_id
ORDER BY o.product_id,YEAR(o.order_date)

SELECT DISTINCT
                c.customer_Id
               ,c.Customer_Fname
               ,c.Customer_Lname
   ,s.salesperson_Fname
               ,s.SalesPerson_Lname
        FROM dbo.Customer c
 INNER JOIN dbo.orders o
       ON c.customer_Id = o.customer_Id
 INNER JOIN dbo.SalesPerson s
       ON o.salesperson_Id = s.salesperson_id


Comments

Popular posts from this blog

SQL Training: Exercises on Stored Procedures and Functions

SQL Training: Exercise on Clauses,String and Mathematical functions,Date Time and Aggregate Functions

SQL Training: Exercises on Functions