Repository navigation
Expand file tree
/
Copy pathJoining Tables.sql
More file actions
79 lines (69 loc) · 2.38 KB
/
Copy pathJoining Tables.sql
File metadata and controls
79 lines (69 loc) · 2.38 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
USE [70-761]
--JOIN Person Table to Transaction Table
SELECT * FROM Person P
JOIN Transactions T
ON P.PersonID = T.PersonID
--Check to See Which Persons have made no transactions
SELECT P.PersonID,P.PersonName,P.Email FROM Person P
Left JOIN Transactions T
ON P.PersonID = T.PersonID
WHERE T.PersonID IS NULL
--Check to See which transaction Persons not listed in Person Table
SELECT T.* FROM Person P
Right JOIN Transactions T
ON P.PersonID = T.PersonID
WHERE P.PersonID IS NULL AND T.PersonID IS NOT NULL
--Check to see those transactions which have no PersonID
SELECT T.* FROM Person P
Right JOIN Transactions T
ON P.PersonID = T.PersonID
WHERE P.PersonID IS NULL AND T.PersonID IS NULL
--Check to See Which Department has the most transactions
SELECT TOP 1 DeptName,COUNT(*) Transactions FROM Transactions T
JOIN Person P
ON P.PersonID = T.PersonID
JOIN Departments D
ON P.Department = D.DeptID
GROUP BY DeptName
ORDER BY Transactions DESC
--Check to See Which Department has the least transactions
SELECT TOP 1 DeptName,COUNT(*) Transactions FROM Transactions T
JOIN Person P
ON P.PersonID = T.PersonID
JOIN Departments D
ON P.Department = D.DeptID
GROUP BY DeptName
ORDER BY Transactions
--Check to See Each Departments Total Transactions Amount
SELECT DeptName,FORMAT(SUM(Amount),'C') Total FROM Transactions T
JOIN Person P
ON P.PersonID = T.PersonID
JOIN Departments D
ON P.Department = D.DeptID
GROUP BY DeptName
ORDER BY SUM(Amount) DESC
--Check Total Transaction Statistics
SELECT PersonName, FORMAT(SUM(Amount),'C') TotalTransactions
,FORMAT(MAX(Amount),'C') MAXAmt
,FORMAT(AVG(Amount),'C') AVGAmt
,FORMAT(MIN(Amount),'C') MINAmt
,COUNT(*) Transactions
,DeptName FROM Departments D
JOIN Person P
ON D.DeptID = P.Department
JOIN Transactions T
ON P.PersonID = T.PersonID
GROUP BY PersonName,DeptName
ORDER BY SUM(Amount) DESC
--Retrieve the best performing Employee by Total Transactions Amount
SELECT TOP 1 PersonName, FORMAT(SUM(Amount),'C') TotalTransactions FROM Person P
JOIN Transactions T
ON P.PersonID = T.PersonID
GROUP BY PersonName
ORDER BY SUM(Amount) DESC
--Retrieve the least performing Employee by Total Transactions Amount
SELECT TOP 1 PersonName, FORMAT(SUM(Amount),'C') TotalTransactions FROM Person P
JOIN Transactions T
ON P.PersonID = T.PersonID
GROUP BY PersonName
ORDER BY SUM(Amount)