Custom Search
 


SQL Views in MySQL Northwind database


SQL view is a virtual dataset which does not contain any data. You can use view just like a normal table in queries, but the result of the view actually comes from a base table or another view. If data is changed in the underlying table, the same change is automatically reflected in the view.

The following 7 views are created in Northwind database.

  1. vwProducts_Above_Average_Price
  2. vwQuarterly_Orders_by_Product
  3. vwInvoice
  4. vwUnitsInStock
  5. vwSalesByCategory
  6. vw10Most_Expensive_Products
  7. vwCustomer_Supplier_by_City

Below is the SQL script to create the 7 views.

-- Create view vwProducts_Above_Average_Price
create or replace view vwProducts_Above_Average_Price
as
select distinct ProductName, UnitPrice
from Products
where UnitPrice > (select avg(UnitPrice) from Products)
order by UnitPrice;

-- Create view vwQuarterly_Orders_by_Product
create or replace view vwQuarterly_Orders_by_Product
as
select a.ProductName,
d.CompanyName,
year(OrderDate) as OrderYear,
format(sum(case quarter(c.OrderDate) when '1'
then b.UnitPrice*b.Quantity*(1-b.Discount) else 0 end), 0) "Qtr 1",
format(sum(case quarter(c.OrderDate) when '2'
then b.UnitPrice*b.Quantity*(1-b.Discount) else 0 end), 0) "Qtr 2",
format(sum(case quarter(c.OrderDate) when '3'
then b.UnitPrice*b.Quantity*(1-b.Discount) else 0 end), 0) "Qtr 3",
format(sum(case quarter(c.OrderDate) when '4'
then b.UnitPrice*b.Quantity*(1-b.Discount) else 0 end), 0) "Qtr 4"
from Products a
inner join Order_Details b on a.ProductID = b.ProductID
inner join Orders c on c.OrderID = b.OrderID
inner join Customers d on d.CustomerID = c.CustomerID
where c.OrderDate between date('1997-01-01') and date('1997-12-31')
group by a.ProductName,
d.CompanyName,
year(OrderDate)
order by a.ProductName, d.CompanyName;

-- Create view vwInvoice
create or replace view vwInvoice
as
select distinct b.ShipName,
b.ShipAddress,
b.ShipCity,
b.ShipRegion,
b.ShipPostalCode,
b.ShipCountry,
b.CustomerID,
c.CompanyName,
c.Address,
c.City,
c.Region,
c.PostalCode,
c.Country,
concat(d.FirstName, ' ', d.LastName) as Salesperson,
b.OrderID,
b.OrderDate,
b.RequiredDate,
b.ShippedDate,
a.CompanyName as ShipperName,
e.ProductID,
f.ProductName,
e.UnitPrice,
e.Quantity,
e.Discount,
e.UnitPrice * e.Quantity * (1 - e.Discount) as ExtendedPrice,
b.Freight
from Shippers a
inner join Orders b on a.ShipperID = b.ShipVia
inner join Customers c on c.CustomerID = b.CustomerID
inner join Employees d on d.EmployeeID = b.EmployeeID
inner join Order_Details e on b.OrderID = e.OrderID
inner join Products f on f.ProductID = e.ProductID
order by b.ShipName;

-- Create view vwUnits_In_Stock
create or replace view vwUnits_In_Stock
as
select c.CategoryName as "Product Category",
case when s.Country in
('UK','Spain','Sweden','Germany','Norway',
'Denmark','Netherlands','Finland','Italy','France')
then 'Europe'
when s.Country in ('USA','Canada','Brazil')
then 'America'
else 'Asia-Pacific'
end as "Supplier Continent",
sum(p.UnitsInStock) as UnitsInStock
from Suppliers s
inner join Products p on p.SupplierID=s.SupplierID
inner join Categories c on c.CategoryID=p.CategoryID
group by c.CategoryName,
case when s.Country in
('UK','Spain','Sweden','Germany','Norway',
'Denmark','Netherlands','Finland','Italy','France')
then 'Europe'
when s.Country in ('USA','Canada','Brazil')
then 'America'
else 'Asia-Pacific'
end;

-- Create view vwSales_By_Category
create or replace view vwSales_By_Category
as
select distinct a.CategoryID,
a.CategoryName,
b.ProductName,
sum(round(y.UnitPrice * y.Quantity * (1 - y.Discount), 2)) as ProductSales
from Order_Details y
inner join Orders d on d.OrderID = y.OrderID
inner join Products b on b.ProductID = y.ProductID
inner join Categories a on a.CategoryID = b.CategoryID
where d.OrderDate between date('1997/1/1') and date('1997/12/31')
group by a.CategoryID, a.CategoryName, b.ProductName
order by a.CategoryName, b.ProductName, ProductSales;

-- Create view vw10Most_Expensive_Products
create or replace view vw10Most_Expensive_Products
as
select distinct ProductName as Ten_Most_Expensive_Products,
UnitPrice
from Products
order by UnitPrice desc
limit 10;

-- Create view vwCustomer_Supplier_by_City
create or replace view vwCustomer_Supplier_by_City
as
select City, CompanyName, ContactName, 'Customers' as Relationship
from Customers
union
select City, CompanyName, ContactName, 'Suppliers'
from Suppliers
order by City, CompanyName;




Other tutorials in this category

1. What is Northwind database in MySQL

2. Create Northwind database in MySQL

3. The Categories table in MySQL Northwind database

4. The Suppliers table in MySQL Northwind database

5. The Products table in MySQL Northwind database

6. The Customers table in MySQL Northwind database

7. The Employees table in MySQL Northwind database

8. The Shippers table in MySQL Northwind database

9. The Orders table in MySQL Northwind database

10. The Order Details table in MySQL Northwind database

Back to Tutorial Index Page


Copyright © 2024 GeeksEngine.com. All Rights Reserved.

This website is hosted by HostGator.

No portion may be reproduced without my written permission. Software and hardware names mentioned on this site are registered trademarks of their respective companies. Should any right be infringed, it is totally unintentional. Drop me an email and I will promptly and gladly rectify it.

 
Home | Feedback | Terms of Use | Privacy Policy