Skip to content

Commit 3a70d47

Browse files
committed
Added sp and view and updated joins
1 parent 20436da commit 3a70d47

File tree

7 files changed

+405
-0
lines changed

7 files changed

+405
-0
lines changed

Joins/Practices.sql

+116
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,116 @@
1+
-- Which Supplier solding which products?
2+
3+
SELECT Suppliers.SupplierID, Suppliers.CompanyName, Products.ProductID, Products.ProductName
4+
FROM Suppliers
5+
JOIN Products ON Suppliers.SupplierID = Products.SupplierID
6+
WHERE Suppliers.SupplierID = 1;
7+
8+
9+
-- Customers who did not ordered
10+
11+
SELECT Orders.OrderID, Customers.ContactName
12+
FROM Customers
13+
LEFT JOIN Orders ON Orders.CustomerID = Customers.CustomerID
14+
WHERE Orders.OrderID IS NULL;
15+
16+
17+
-- My sellings by products.
18+
19+
SELECT P.ProductName as [Product Name],
20+
sum(O.Quantity) as Unit,
21+
sum(O.UnitPrice) as [Unit Price],
22+
sum(O.Discount) as Discount,
23+
sum(O.Quantity * (O.UnitPrice * (1 - O.Discount))) as Profit
24+
FROM Products as P
25+
JOIN [Order Details] as O ON O.ProductID = P.ProductID
26+
GROUP BY P.ProductName
27+
ORDER BY 1;
28+
29+
30+
-- My sellings by categories.
31+
32+
SELECT C.CategoryName as [Category Name],
33+
sum(O.Quantity) as Unit,
34+
sum(O.UnitPrice) as [Unit Price],
35+
sum(O.Discount) as Discount,
36+
sum(O.Quantity * (O.UnitPrice * (1 - O.Discount))) as Profit
37+
FROM Categories as C
38+
JOIN Products as P ON P.CategoryID = C.CategoryID
39+
JOIN [Order Details] as O ON O.ProductID = P.ProductID
40+
GROUP BY C.CategoryName
41+
ORDER BY 1;
42+
43+
44+
-- Total freights by ship company.
45+
46+
SELECT S.CompanyName as Shipper,
47+
sum(O.Freight) as Freight
48+
FROM Shippers as S
49+
JOIN Orders as O ON S.ShipperID = O.ShipVia
50+
GROUP BY S.CompanyName;
51+
52+
53+
-- Best customer
54+
55+
SELECT TOP 1 C.CustomerID as Customer,
56+
sum(OD.Quantity * (OD.UnitPrice * (1 - OD.Discount))) as Total
57+
FROM Customers as C
58+
JOIN Orders as O ON C.CustomerID = O.CustomerID
59+
JOIN [Order Details] as OD ON O.OrderID = OD.OrderID
60+
GROUP BY C.CustomerID
61+
ORDER BY Total DESC;
62+
63+
64+
-- Total product selling by supplier company
65+
66+
SELECT S.CompanyName as Supplier,
67+
P.ProductName as Product,
68+
sum(OD.Quantity) as [Total Quantity]
69+
FROM Suppliers as S
70+
JOIN Products as P ON S.SupplierID = P.SupplierID
71+
JOIN [Order Details] as OD ON P.ProductID = OD.ProductID
72+
GROUP BY S.CompanyName, P.ProductName
73+
ORDER BY 3 DESC;
74+
75+
-- Order
76+
-- Customer
77+
-- Employee
78+
-- Date
79+
-- Shipper
80+
-- Price
81+
-- Product's category
82+
-- Product's supplier
83+
84+
SELECT O.OrderID,
85+
C.CompanyName as Customer,
86+
E.FirstName+ ' '+ E.LastName as [Employee Name],
87+
O.OrderDate as [Date],
88+
S.CompanyName as [Shipper],
89+
OD.Quantity as Quantity,
90+
OD.UnitPrice as [Unit Price],
91+
OD.Discount as Discount,
92+
sum(OD.Quantity * (OD.UnitPrice * ( 1 - OD.Discount ))) as Total,
93+
Cat.CategoryName as Category,
94+
Sup.CompanyName as Supplier
95+
FROM Orders as O
96+
JOIN Customers as C ON O.CustomerID = C.CustomerID
97+
JOIN Employees as E ON O.EmployeeID = E.EmployeeID
98+
JOIN Shippers as S ON O.ShipVia = S.ShipperID
99+
JOIN [Order Details] as OD ON O.OrderID = OD.OrderID
100+
JOIN Products as P ON OD.ProductID = P.ProductID
101+
JOIN Categories as Cat ON P.CategoryID = Cat.CategoryID
102+
JOIN Suppliers as Sup ON P.SupplierID = Sup.SupplierID
103+
GROUP BY
104+
O.OrderID,
105+
C.CompanyName,
106+
E.FirstName,
107+
E.LastName,
108+
O.OrderDate,
109+
S.CompanyName,
110+
OD.Quantity,
111+
OD.UnitPrice,
112+
OD.Discount,
113+
Cat.CategoryName,
114+
Sup.CompanyName
115+
ORDER BY
116+
O.OrderID;

Joins/README.md

+1
Original file line numberDiff line numberDiff line change
@@ -12,4 +12,5 @@ A JOIN used to combine rows from two or more tables, based on a related column b
1212
- **SELF JOIN:** A self join is a unique type of join where a table is joined with itself.
1313
- **CROSS JOIN:** A cross join, also known as a Cartesian join, returns the Cartesian product of the two tables, meaning each row from the first table is combined with each row from the second table.
1414
<br />
15+
1516
![img1](https://miro.medium.com/v2/resize:fit:1400/0*rFMChX4SAmQ9RzF9.png)

Stored Procedures/Examples.sql

+92
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,92 @@
1+
-- CREATE Stored Procedure
2+
3+
-- We can create a procedure with CREATE PROCEDURE statement and if we want add a parameter, using @ before the parameter.
4+
-- Example
5+
6+
CREATE PROCEDURE OrdersByPrice
7+
(
8+
@OrderBeginPrice real,
9+
@OrderEndPrice real,
10+
)
11+
as
12+
BEGIN
13+
SELECT *
14+
FROM ExampleOrderDetail
15+
WHERE Price BETWEEN @OrderBeginPrice AND @OrderEndPrice
16+
ORDER BY Price
17+
END;
18+
19+
-- Users can list orders by the between specific prices in this procedure. Thus users now performs same operation by calling the procedure instead of typing a queary.
20+
21+
-- EXECUTE Stored Procedure
22+
-- Example
23+
24+
EXEC OrdersByPrice
25+
@OrderBeginPrice = 4,
26+
@OrderEndPrice = 20
27+
28+
29+
-- ALTER Stored Procedure
30+
31+
-- We can update a procedure with ALTER PROCEDURE statement.
32+
-- Example
33+
34+
ALTER PROCEDURE OrdersByPrice
35+
@OrderBeginPrice real,
36+
@OrderEndPrice real
37+
as
38+
BEGIN
39+
SELECT *
40+
FROM ExampleOrderDetail
41+
WHERE Price BETWEEN @OrderBeginPrice AND @OrderEndPrice
42+
-- ORDER BY Price
43+
END;
44+
45+
46+
-- DROP Stored Procedure
47+
48+
-- We can delete a procedure with DROP PROCEDURE statement.
49+
-- Example
50+
51+
DROP PROCEDURE OrdersByPrice;
52+
53+
54+
-- ENCRYPTED Stored Procedure
55+
56+
-- We can create a encrypted procedure with the same statement as the create procedure but we must add WITH ENCRYPTION statement before the AS.
57+
-- Now we can not use design in the encrypted procedure but we can update with ALTER statement and delete with DROP statement the procedure.
58+
-- Example
59+
60+
CREATE PROCEDURE OrdersByPrice
61+
(
62+
@OrderBeginPrice real,
63+
@OrderEndPrice real,
64+
)
65+
WITH ENCRYPTION
66+
as
67+
BEGIN
68+
SELECT *
69+
FROM ExampleOrderDetail
70+
WHERE Price BETWEEN @OrderBeginPrice AND @OrderEndPrice
71+
ORDER BY Price
72+
END;
73+
74+
75+
-- RECOMPILE Stored Procedure
76+
77+
-- Stored Procedures are usually storing in Plan Cache. But if we use RECOMPILE statement when creating a procedure, it does not storing in Plan Cache and is recreated each time the procedure is executed.
78+
-- Example
79+
80+
CREATE PROCEDURE OrdersByPrice
81+
(
82+
@OrderBeginPrice real,
83+
@OrderEndPrice real,
84+
)
85+
WITH RECOMPILE
86+
as
87+
BEGIN
88+
SELECT *
89+
FROM ExampleOrderDetail
90+
WHERE Price BETWEEN @OrderBeginPrice AND @OrderEndPrice
91+
ORDER BY Price
92+
END;

Stored Procedures/Practices.sql

+42
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,42 @@
1+
-- Employee by first name and last name
2+
3+
CREATE PROCEDURE EmployeeByName
4+
(
5+
@FirstName varchar(50)
6+
@LastName varchar(50)
7+
)
8+
as
9+
BEGIN
10+
SELECT *
11+
FROM Employees
12+
WHERE FirstName = @FirstName AND LastName = @LastName
13+
END;
14+
15+
16+
EXEC EmployeeByName
17+
@FirstName = 'Andrew'
18+
@LastName = 'Fuller'
19+
20+
21+
-- Selling products by name
22+
23+
CREATE PROCEDURE SellingProductsByName
24+
(
25+
@ProductName varchar(50)
26+
)
27+
as
28+
BEGIN
29+
SELECT P.ProductName as [Product Name],
30+
sum(O.Quantity) as Unit,
31+
sum(O.UnitPrice) as [Unit Price],
32+
sum(O.Discount) as Discount,
33+
sum(O.Quantity * (O.UnitPrice * (1 - O.Discount))) as Profit
34+
FROM Products as P
35+
JOIN [Order Details] as O ON O.ProductID = P.ProductID
36+
WHERE P.ProductName = @ProductName
37+
GROUP BY P.ProductName;
38+
END;
39+
40+
41+
EXEC SellingProductsByName
42+
@ProductName = 'Alice Mutton'

Stored Procedures/README.md

+5
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
# Stored Procedures
2+
3+
When we do CRUD operations in the database we repeating and compiling the code for each operation. In this situation causes both time and performance loss. But Stored Procedure allows us to write code according to expressions used in programming. Thus we save time without having to do the same operations every time.
4+
5+
![img1](https://f4n3x6c5.stackpathcdn.com/UploadFile/3d39b4/basic-of-stored-procedures/Images/SP2.jpg)

0 commit comments

Comments
 (0)