Skip to content

Commit 0e2df79

Browse files
committed
Added Functions
1 parent 66e6493 commit 0e2df79

File tree

4 files changed

+183
-0
lines changed

4 files changed

+183
-0
lines changed
+58
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,58 @@
1+
-- Examples
2+
3+
-- COUNT
4+
SELECT COUNT(*) FROM Products;
5+
6+
-- SUM
7+
SELECT SUM(UnitPrice) FROM Products;
8+
9+
-- AVG
10+
SELECT AVG(UnitPrice) FROM Products;
11+
12+
-- MAX MIN
13+
SELECT MAX(UnitPrice), MIN(UnitPrice) FROM Products;
14+
15+
-- UCASE LCOSE
16+
SELECT UCASE(ProductName), LCASE(ProductName) FROM Products; --> Output -> TEST, test
17+
18+
-- SUBSTRING
19+
SELECT SUBSTRING(ProductName, 1, 5) FROM Products; --> Output -> Alice Mutton to Alice
20+
21+
-- ROUND
22+
SELECT ROUND(UnitPrice, 2) FROM Products; --> Output -> 21.35 to 21.40
23+
24+
-- GETDATE
25+
SELECT OrderID, GETDATE() as CurrentDateTime FROM Orders;
26+
27+
-- DATEDIFF
28+
SELECT OrderID, DATEDIFF(day, OrderDate, GETDATE()) as DaysFromOrderToNow FROM Orders;
29+
30+
31+
-- RANK ROWNUMBER
32+
33+
SELECT
34+
OrderID,
35+
CustomerID,
36+
OrderDate,
37+
RANK() OVER(ORDER BY OrderDate) as Rank,
38+
ROW_NUMBER() OVER(ORDER BY OrderDate) as RowNumber
39+
FROM Orders;
40+
41+
-- OUTPUT
42+
-- OrderID CustomerID OrderDATE Rank RowNumber
43+
-- 10248 VINET 1996-07-04 00:00:00.000 1 1
44+
45+
46+
-- LAG
47+
48+
SELECT
49+
OrderID,
50+
CustomerID,
51+
OrderDate,
52+
LAG(OrderDate) OVER(ORDER BY OrderDate) as PreviousOrderDate
53+
FROM Orders;
54+
55+
-- OUTPUT
56+
-- OrderID CustomerID OrderDATE PreviousOrderDate
57+
-- 10248 VINET 1996-07-04 00:00:00.000 NULL
58+
-- 10249 TOMSP 1996-07-05 00:00:00.000 1996-07-04 00:00:00.000

Functions/README.md

+9
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
# Functions
2+
3+
One of the most powerful features of SQL is the ability to use functions to perform various operations on the data in a database.
4+
5+
## Different Types of SQL Functions
6+
- **User Defined Functions:** These are functions that users create according to their own needs.
7+
- **Built-In Functions:** They are functions that are defined within SQL itself and are widely used.
8+
9+
![img1](https://www.complexsql.com/wp-content/uploads/2017/03/SQL-Functions-1.jpg)
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,62 @@
1+
-- Scalar Function -> returns one value
2+
3+
-- Example
4+
5+
CREATE FUNCTION AverageProductPrice()
6+
RETURNS money
7+
as
8+
BEGIN
9+
RETURN (SELECT AVG(UnitPrice) FROM Products)
10+
END
11+
12+
-- Test
13+
14+
SELECT dbo.AverageProductPrice() AS AveragePrice
15+
16+
17+
-- Table-Valued Function -> returns table
18+
19+
-- Example
20+
21+
CREATE FUNCTION OrdersByCity(@city nvarchar(15))
22+
RETURNS TABLE
23+
as
24+
RETURN
25+
(
26+
SELECT Orders.OrderID, Customers.CompanyName, Customers.ContactName
27+
FROM Orders
28+
JOIN Customers ON Orders.CustomerID = Customers.CustomerID
29+
WHERE Customers.City = @city
30+
)
31+
32+
-- Test
33+
34+
SELECT * FROM dbo.OrdersByCity('London')
35+
36+
37+
-- Multi-Statement Table-Valued -> returns multi statement table
38+
39+
-- Example
40+
41+
CREATE FUNCTION OrderDetailsByProduct(@ProductID int)
42+
RETURNS @orderDetails TABLE
43+
(
44+
OrderID int,
45+
ProductID int,
46+
UnitPrice money,
47+
Quantity smallint,
48+
Discount real
49+
)
50+
as
51+
BEGIN
52+
INSERT INTO @orderDetails
53+
SELECT OrderID, ProductID, UnitPrice, Quantity, Discount
54+
FROM [Order Details]
55+
WHERE ProductID = @ProductID
56+
57+
RETURN
58+
END
59+
60+
-- Test
61+
62+
SELECT * FROM dbo.OrderDetailsByProduct(1)
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,54 @@
1+
-- Orders by Employee
2+
3+
CREATE FUNCTION OrderDetailsByEmployee(@EmployeeID INT)
4+
RETURNS TABLE
5+
as
6+
RETURN
7+
(
8+
SELECT P.ProductID, P.ProductName, SUM(OD.UnitPrice * OD.Quantity * (1 - OD.Discount)) AS TotalPrice, SUM(OD.Quantity) as TotalQuantity, AVG(OD.Discount) as AvgDiscount, O.OrderDate
9+
FROM Employees as E
10+
JOIN Orders as O ON O.EmployeeID = E.EmployeeID
11+
JOIN [Order Details] as OD ON O.OrderID = OD.OrderID
12+
JOIN Products as P ON OD.ProductID = P.ProductID
13+
WHERE E.EmployeeID = @EmployeeID
14+
GROUP BY P.ProductID, P.ProductName, O.OrderDate
15+
)
16+
17+
-- Test
18+
SELECT * FROM OrderDetailsByEmployee(5)
19+
20+
21+
-- Product Name and Product Price by Employee
22+
-- If ID <= 0 returns ID = 0, ProductName = Invalid Employee ID, TotalPrice = 0
23+
24+
CREATE FUNCTION ProductDetailsByEmployee(@EmployeeID INT)
25+
RETURNS @ProductsByEmployee TABLE
26+
(
27+
ID INT,
28+
ProductName NVARCHAR(50),
29+
TotalPrice MONEY
30+
)
31+
as
32+
BEGIN
33+
IF @EmployeeID > 0
34+
BEGIN
35+
INSERT INTO @ProductsByEmployee (ID, ProductName, TotalPrice)
36+
SELECT P.ProductID, P.ProductName, SUM(OD.UnitPrice * OD.Quantity) as TotalPrice
37+
FROM Employees as E
38+
JOIN Orders as O ON E.EmployeeID = O.EmployeeID
39+
JOIN [Order Details] as OD ON OD.OrderID = O.OrderID
40+
JOIN Products as P ON P.ProductID = OD.ProductID
41+
WHERE E.EmployeeID = @EmployeeID
42+
GROUP BY P.ProductID, P.ProductName
43+
END
44+
ELSE
45+
BEGIN
46+
INSERT INTO @ProductsByEmployee (ID, ProductName, TotalPrice)
47+
VALUES (0, 'Invalid Employee ID', 0)
48+
END
49+
50+
RETURN
51+
END
52+
53+
-- Test
54+
SELECT * FROM ProductDetailsByEmployee(0)

0 commit comments

Comments
 (0)