-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathSelectTables.sql
More file actions
146 lines (127 loc) · 3.43 KB
/
SelectTables.sql
File metadata and controls
146 lines (127 loc) · 3.43 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
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
use TechShop
--BASIC SELECTS (Shows use of select and project):
---------------------------------
--CUSTOMER TABLE
select
CustomerID as "Customer_ID",
CustomerName as "Customer_Name",
CustomerPhone as "Phone_Number"
from Customer;
--STAFF TABLE:
select
StaffID as "Staff_Id",
StaffName as "Staff_Name",
StaffAddress as "Address"
from Staff;
--SUPPLIER TABLE:
select
SupplierID as "Supplier_ID",
SupplierName as "Supplier_Name",
SupplierContactNo as "Supplier_Contact_Number"
from Supplier;
--DEVICE TABLE:
select DeviceID as "Device_ID",
DeviceType as "Device_Brand",
DeviceName as "Device_Name",
ReleaseDate as "Release_Year",
Price as "Price"
from Devices;
--STOCK TABLE:
select
SerialNumber as "Serial_Number",
SupplierID as "Supplier_ID",
DeviceID as "Device_ID",
DateSupplied as "Date_Supplier",
-- ADDED DATA
StaffID as "Staff_ID",
CustomerID as "Customer_ID",
DateOfTransaction as "Date_Of_Transaction"
from Stock;
--JOINS:
---------------------------------
--Print out all sold items.(Inner Join)
select
SerialNumber,
DeviceName,
CustomerName,
StaffID,
DateOfTransaction
from Stock
inner join Devices D on (Stock.DeviceID = D.DeviceID)
inner join Customer C on Stock.CustomerID = C.CustomerID;
--Prints out all items whether they are sold or unsold, and the items not in stock. (Full Join)
select
SerialNumber,
DeviceName,
CustomerName,
StaffID,
DateOfTransaction
from Stock
full join Devices D on (Stock.DeviceID = D.DeviceID)
full join Customer C on Stock.CustomerID = C.CustomerID;
--Prints out all items that were in stock, but may have been sold. (Left Join)
select
SerialNumber,
DeviceName,
CustomerName,
StaffID,
DateOfTransaction
from Stock
left join Devices D on (Stock.DeviceID = D.DeviceID)
left join Customer C on Stock.CustomerID = C.CustomerID;
--Prints out all items not in stock (Except)
select
SerialNumber,
DeviceType,
DeviceName
from Stock
full join Devices D on (Stock.DeviceID = D.DeviceID)
except
select
SerialNumber,
DeviceType,
DeviceName
from Stock
full join Devices D on (Stock.DeviceID = D.DeviceID)
where SerialNumber is not null
--Prints out the intersection of the watches table and the apple products table. (Intersect)
select
DeviceName,
DeviceType,
DeviceBrand
from Devices
where DeviceType = 'Watch'
intersect
select
DeviceName,
DeviceType,
DeviceBrand
from Devices
where DeviceBrand = 'Apple'
--Prints out all the samsung and apple products in the table (Union)
select
DeviceName,
DeviceType,
DeviceBrand
from Devices
where DeviceBrand = 'Apple'
union
select
DeviceName,
DeviceType,
DeviceBrand
from Devices
where DeviceBrand = 'Samsung'
--Prints out all items sold by a member of the Simpsons family(Subquery)
select SerialNumber,
DeviceID,
DateOfTransaction,
StaffName
from Stock
join Staff S on Stock.StaffID = S.StaffID
where Stock.StaffID in (select StaffID from Staff where StaffAddress like '742 evergreen terrace')
--Prints out how many items each staff member has sold(Aggregation)
select Staff.StaffName, count(*) as Sold_Products from Staff
left join Stock S on Staff.StaffID = S.StaffID
where (S.StaffID is not null)
group by Staff.StaffName