Skip to main content

SQL Join Query - Building Complex Join Query - Day 5 #100days of SQL

 Hi ,

In this blog post will check the join queries

SQL Join function below diagram shows difference possibilities

 



Below queries has been executed on the video podcast


-- will see about join query from the db table schema diagram

-- will check about product and transaction history table on join  query


select * from Production.Product

 -- 504 records

select * from Production.TransactionHistory 

-- 113443 records


-- inner join

select * from Production.Product p inner join Production.TransactionHistory t

on

p.ProductID = t.ProductID 


-- full join or full outer join

select * from Production.Product p full outer join Production.TransactionHistory t

on

p.ProductID = t.ProductID


-- right join or right outer join

select * from Production.Product p right outer join Production.TransactionHistory t

on

p.ProductID = t.ProductID


-- left join or left outer join

select * from Production.Product p left outer join Production.TransactionHistory t

on

p.ProductID = t.ProductID


 -- now will complex join and other sql query operations


select 

p.ProductID, p.StandardCost, p.ListPrice,

t.ActualCost, t.Quantity

from Production.Product p cross join Production.TransactionHistory t

where

p.class is not null and 

t.ActualCost <> 0.00 and 

p.StandardCost <> 0.00 and

p.ListPrice <> 0.00 and

p.Name like '%Road%' and

p.ProductNumber like '%7'


-- will try with group by 

-- from the sql query execution cross join performance takes too long time compared with inner join

-- so prefer only inner join, full join , right / left join sql query operations

select 

Top 100

p.ProductID, AVG(p.StandardCost) as AvgStdCost, AVG(p.ListPrice) as AvgListPrice,

Avg(t.ActualCost) as AvgActualCost, Count(t.Quantity) as TotalQuantity,

Row_number() over (order by AVG(p.StandardCost) desc  ) as StandardCostRank,

Rank() over (order by AVG(p.StandardCost) desc  ) as DistinctStandardCostRank,

Dense_Rank() over (order by AVG(p.StandardCost) desc  ) as DistinctNextSeqStandardCostRank

from Production.Product p inner join Production.TransactionHistory t

on

p.ProductID = t.ProductID 

where

p.class is not null and 

t.ActualCost <> 0.00 and 

p.StandardCost <> 0.00 and

p.ListPrice <> 0.00 and

p.Name like '%Road%'

--p.ProductNumber like '%7'

group by

p.ProductID

Order by

AvgStdCost Desc


-- now will do more sql query operations with more than two tables

-- will check in db schema

-- from db schema simple flow will join Sales person with product 

-- Sales person Business EntityId connecting with salesperson id foreignkey SalesOderHeader table

-- finding outliers from max min sales deviations totally 14 records has min max deviations in salesdata

-- thanks for watching

select 

p.ProductID, AVG(sp.Bonus) as AverageBonus, Avg(sp.SalesQuota)  as AverageSalesQuota,

Avg(sp.SalesYTD)  as AverageSalesYTD,Avg(sp.SalesLastYear) as AverageSalesLY,

Avg(sod.UnitPrice)  as AverageUP, Avg(sod.UnitPriceDiscount)  as AverageUPD, 

Avg(sod.LineTotal)  as AverageLineTotal,

(Max(sp.SalesYTD) - Avg(sp.SalesYTD)) as MaxSalesDeviation,

(Avg(sp.SalesYTD) - Min(sp.SalesYTD)) as MinSalesDeviation

from 

Sales.SalesPerson sp

inner join 

Sales.SalesOrderHeader soh

on

sp.BusinessEntityID = soh.SalesPersonID

inner join 

Sales.SalesOrderDetail sod -- salesorderid

on

soh.SalesOrderID = sod.SalesOrderID

inner join

Sales.SpecialOfferProduct sop 

on

sop.SpecialOfferID = sod.SalesOrderDetailID

inner join

Production.Product p

on

p.ProductID = sop.ProductID

group by

p.ProductID

having

(Max(sp.SalesYTD) - Avg(sp.SalesYTD)) <> 0.00 and

(Avg(sp.SalesYTD) - Min(sp.SalesYTD)) <> 0.00

order by

p.ProductID 




Comments

Popular posts from this blog

Java Swing MySql JDBC: insert data into database

Program import javax.swing.*; import java.awt.*; import java.awt.event.*; import java.sql.*; public class insertswing implements ActionListener {   JFrame fr;JPanel po;   JLabel l1,l2,main;   JTextField tf1,tf2;   GridBagConstraints gbc;   GridBagLayout go;   JButton ok,exit; public insertswing(){ fr=new JFrame("New User Data "); Font f=new Font("Verdana",Font.BOLD,24); po=new JPanel(); fr.getContentPane().add(po); fr.setVisible(true); fr.setSize(1024,768); fr.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); po.setBackground(Color.WHITE); go=new GridBagLayout(); gbc=new GridBagConstraints(); po.setLayout(go); main=new JLabel("Enter User Details "); main.setFont(f); l1=new JLabel("Name  :");tf1=new JTextField(20); l2=new JLabel("User Name  :");tf2=new JTextField(20); ok=new JButton("Accept"); exit=new JButton("Exit"); gbc.anchor=GridBagConstraints.NORTH;gbc.gridx=5;gbc.gridy=0; go.s...

JSP and Servlet Form Submission without page refresh show results on the same page using Jquery AJAX

Code Snippet HTML Form  <form id='ajaxform' name='ajaxform' action='ajaxformexample' method='post'>  First Name: <input type='text' id='firstname' name='firstname' size='30' required/><br/>  Last Name: <input type='text' id='lastname' name='lastname' size='30'required/><br/>  Email:  <input type='email' id='emailid' name='emailid' size='30'required/><br/>  Password:  <input type='password' id='pwd' name='pwd' size='30'required/><br/>  <input type='Submit'/>   <div id='content'> </div> </form> the above HTML Form uses post method and url servlet redirect to " ajaxformexample " Javascript Code  var form = $('#ajaxform'); // id of form tag  form.submit(function () {  $.ajax({  ...

Guidewire Related Interview Question and answers part 1

common Guidewire questions and answers 20 Guidewire BC Q&A Top 100 Guidewire Interview FAQ Guidewire Claimcenter 20 Interview Questions Guidewire Rating concepts