-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathSPP_SQL3_WineDW_Query.sql
More file actions
283 lines (223 loc) · 9.03 KB
/
SPP_SQL3_WineDW_Query.sql
File metadata and controls
283 lines (223 loc) · 9.03 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
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
-----------------------------------
--Programmer: Sooorya Parthiban
--Reference: SQL GitHub Project 3
--Wine Database Management
-----------------------------------
--Creating the Wine Database
CREATE DATABASE WineDatabase
GO
--Creating a wine table
USE WineDatabase
GO
CREATE TABLE WineList (
WineID INT IDENTITY(1,1) PRIMARY KEY NOT NULL,
WineName VARCHAR(155) NOT NULL,
WineCategory VARCHAR(50) NOT NULL,
WinePrice DECIMAL NOT NULL,
WineAlcoholContent FLOAT NOT NULL,
WineQuality INT NOT NULL
);
--Inserting values into the wine table.
--Inserting Champagne & Sparkling category wine data.
INSERT INTO WineList (WineName, WineCategory, WinePrice, WineAlcoholContent, WineQuality)
VALUES
('Jacobs Creek$', 'Champagne/Sparkling Wine', 9.79, 10, 7),
('Brown Brothers$', 'Champagne/Sparkling Wine', 12.79, 12, 7),
('Henkell Dry-Sec$', 'Champagne/Sparkling Wine', 19.89, 12, 7),
('Veuve Du Vernay Rose$', 'Champagne/Sparkling Wine', 14.79, 12, 9),
('Petit Cordon$', 'Champagne/Sparkling Wine', 34.79, 12.5, 9),
('Drappier Carte Brut Champagne$', 'Champagne/Sparkling Wine', 49.79, 12.5, 9),
('Mumm Cordon Rouge Champagne$', 'Champagne/Sparkling Wine', 49.79, 14, 9),
('Moet & Chandon Imperial Brut$', 'Champagne/Sparkling Wine', 54.99, 12.5, 9);
--Inserting Red Wine category wine data.
INSERT INTO WineList (WineName, WineCategory, WinePrice, WineAlcoholContent, WineQuality)
VALUES
('McGuigan Black Label Red$', 'Red Wine', 8.79, 12, 8),
('Ormond Rich Ruby$', 'Red Wine', 10.79, 12, 7),
('Riunite Lambrusco Reggiano$', 'Red Wine', 11.79, 12, 9),
('Pasqua Montepulciano$', 'Red Wine', 19.99, 12, 9),
('Taylors Promised Land Shiraz Cabernet$', 'Red Wine', 10.79, 12, 8),
('Music Bay Merlot$', 'Red Wine', 11.79, 12.5, 9),
('Barossa Valley Estate Cabernet Sauvignon$', 'Red Wine', 17.79, 12.5, 9);
--Inserting Chardonnay category wine data.
INSERT INTO WineList (WineName, WineCategory, WinePrice, WineAlcoholContent, WineQuality)
VALUES
('Cleanskin Chardonnay$', 'Chardonnay', 6.89, 12, 7),
('Whitecliff Chardonnay$', 'Chardonnay', 8.79, 11, 7),
('Riverstone Chardonnay$', 'Chardonnay', 9.79, 11, 7),
('Brancott Estate Chardonnay$', 'Chardonnay', 10.79, 12.5, 8),
('Selaks Reserve Chardonnay$', 'Chardonnay', 11.79, 12, 8),
('Mission Estate Winery Hawkes Bay Chardonnay$', 'Chardonnay', 12.79, 12.5, 9),
('Yealands Reserve Chardonnay$', 'Chardonnay', 13.79, 12, 8),
('Oyster Bay Chardonnay$', 'Chardonnay', 13.89, 12, 9),
('Mission Estate Winery Reserve Chardonnay$', 'Chardonnay', 19.79, 12.5, 9);
--Inserting Pinot Noir category wine data.
INSERT INTO WineList (WineName, WineCategory, WinePrice, WineAlcoholContent, WineQuality)
VALUES
('Jacobs Creek Pinot Noir$', 'Pinot Noir', 8.79, 11, 7),
('Ngatarawa Stables Pinot Noir$', 'Pinot Noir', 9.79, 12.5, 7),
('Old Coach Road Pinot Noir$', 'Pinot Noir', 10.79, 12, 8),
('Gunn Estate Pinot Noir$', 'Pinot Noir', 11.79, 11, 8),
('Arrogant Frog Lily Pad Pinot Noir$', 'Pinot Noir', 12.79, 12, 8),
('Ngatarawa Stables Reserve Pinot Noir$', 'Pinot Noir', 13.79, 12, 8),
('Taylors Pinot Noir$', 'Pinot Noir', 14.79, 11.5, 8),
('Oyster Bay Pinot Noir$', 'Pinot Noir', 17.79, 12.5, 9),
('Villa Maria Cellar Selection$', 'Pinot Noir', 19.79, 12.5, 9),
('Rabbit Ranch Pinot Noir$', 'Pinot Noir', 23.79, 12, 9),
('Rockburn Pinot Noir$', 'Pinot Noir', 39.79, 12.5, 9);
--Inserting Sauvignon Blanc category wine data.
INSERT INTO WineList (WineName, WineCategory, WinePrice, WineAlcoholContent, WineQuality)
VALUES
('Jacobs Creek Sauvignon Blanc$', 'Sauvignon Blanc', 8.79, 11, 7),
('Ngatarawa Stables Sauvignon Blanc$', 'Sauvignon Blanc', 9.79, 11.5, 8),
('Clearwater Cove Sauvignon Blanc$', 'Sauvignon Blanc', 10.79, 11, 8),
('Brown Brothers Moscato Sauvignon Blanc$', 'Sauvignon Blanc', 12.79, 11.5, 8),
('Graham Nortons Own Sauvignon Blanc$', 'Sauvignon Blanc', 13.79, 11, 8),
('Seifried Sauvignon Blanc$', 'Sauvignon Blanc', 13.79, 11.5, 9),
('Villa Maria Cellar Selection$', 'Sauvignon Blanc', 14.79, 12.5, 9),
('Rapaura Springs Reserve Sauvignon Blanc$', 'Sauvignon Blanc', 14.79, 12, 9);
--Checking the data in the 'WineTable'
SELECT *
FROM WineList; --43 Rows returned.
--------
--VIEWS
--------
--Creating views to display the Champagne And Sparkling Wines
USE WineDatabase
GO
DROP VIEW IF EXISTS vChampagneAndSparklingWines
GO
CREATE VIEW vChampagneAndSparklingWines
AS
SELECT WineList.WineName, WineList.WineCategory, WineList.WinePrice,
WineList.WineAlcoholContent, WineList.WineQuality
FROM WineList
WHERE WineList.WineCategory = 'Champagne/Sparkling Wine'
GO
SELECT * FROM vChampagneAndSparklingWines; --8 Rows Returned.
--Creating views to display the Red Wines
USE WineDatabase
GO
DROP VIEW IF EXISTS vRedWines
GO
CREATE VIEW vRedWines
AS
SELECT WineList.WineName, WineList.WineCategory, WineList.WinePrice,
WineList.WineAlcoholContent, WineList.WineQuality
FROM WineList
WHERE WineList.WineCategory = 'Red Wine'
GO
SELECT * FROM vRedWines; --7 Rows Returned.
--Creating views to display the Chardonnay Wines
USE WineDatabase
GO
DROP VIEW IF EXISTS vChardonnayWines
GO
CREATE VIEW vChardonnayWines
AS
SELECT WineList.WineName, WineList.WineCategory, WineList.WinePrice,
WineList.WineAlcoholContent, WineList.WineQuality
FROM WineList
WHERE WineList.WineCategory = 'Chardonnay'
GO
SELECT * FROM vChardonnayWines; --9 Rows Returned.
--Creating views to display the Pinot Noir Wines
USE WineDatabase
GO
DROP VIEW IF EXISTS vPinotNoirWines
GO
CREATE VIEW vPinotNoirWines
AS
SELECT WineList.WineName, WineList.WineCategory, WineList.WinePrice,
WineList.WineAlcoholContent, WineList.WineQuality
FROM WineList
WHERE WineList.WineCategory = 'Pinot Noir'
GO
SELECT * FROM vPinotNoirWines; --11 Rows Returned.
--Creating views to display the Sauvignon Blanc Wines
USE WineDatabase
GO
DROP VIEW IF EXISTS vSauvignonBlancWines
GO
CREATE VIEW vSauvignonBlancWines
AS
SELECT WineList.WineName, WineList.WineCategory, WineList.WinePrice,
WineList.WineAlcoholContent, WineList.WineQuality
FROM WineList
WHERE WineList.WineCategory = 'Sauvignon Blanc'
GO
SELECT * FROM vSauvignonBlancWines; --8 Rows Returned.
-------------------
--STORED PROCEDURES
-------------------
--Creating a stored procedure to choose wines based on wine quality.
USE WineDatabase
GO
DROP PROCEDURE IF EXISTS spSelectWineQuality
GO
CREATE PROCEDURE spSelectWineQuality (@WineQuality INT)
AS
SELECT WineList.WineName, WineList.WineCategory, WineList.WinePrice,
WineList.WineAlcoholContent, WineList.WineQuality
FROM WineList
WHERE WineList.WineQuality >= @WineQuality
ORDER BY WineList.WineQuality ASC
GO
--I want wines of 8 or above quality.
EXEC spSelectWineQuality 8; --33 Rows Returned.
--Creating a stored procedure to choose wines based on category and price.
USE WineDatabase
GO
DROP PROCEDURE IF EXISTS spSelectWineTypeAndPrice
GO
CREATE PROCEDURE spSelectWineTypeAndPrice (@WineType VARCHAR(50), @WineCost INT)
AS
SELECT WineList.WineName, WineList.WineCategory, WineList.WinePrice,
WineList.WineAlcoholContent, WineList.WineQuality
FROM WineList
WHERE WineList.WineCategory = @WineType AND WineList.WinePrice <= @WineCost
ORDER BY WineList.WinePrice ASC
GO
--I want a red wine that is below 15 dollars.
EXEC spSelectWineTypeAndPrice 'Red Wine', 15; --5 Rows Returned
--Creating a stored procedure to choose wines based on wine's alcohol content and type.
USE WineDatabase
GO
DROP PROCEDURE IF EXISTS spSelectWineTypeAndAlcoholPercent
GO
CREATE PROCEDURE spSelectWineTypeAndAlcoholPercent (@WineType varchar(50), @WineAlcoholContent INT)
AS
SELECT WineList.WineName, WineList.WineCategory, WineList.WinePrice,
WineList.WineAlcoholContent, WineList.WineQuality
FROM WineList
WHERE WineList.WineCategory = @WineType AND WineList.WineAlcoholContent >= @WineAlcoholContent
ORDER BY WineList.WineAlcoholContent ASC
GO
--I want Pinot Noir type wines that have an alcohol content of 12 or above.
EXEC spSelectWineTypeAndAlcoholPercent 'Pinot Noir', 12; --8 Rows Returned.
--Creating a stored procedure to choose wines based on wine's quality and type.
USE WineDatabase
GO
DROP PROCEDURE IF EXISTS spSelectWineTypeAndQuality
GO
CREATE PROCEDURE spSelectWineTypeAndQuality (@WineType varchar(50), @WineQuality INT)
AS
SELECT WineList.WineName, WineList.WineCategory, WineList.WinePrice,
WineList.WineAlcoholContent, WineList.WineQuality
FROM WineList
WHERE WineList.WineCategory = @WineType AND WineList.WineQuality >= @WineQuality
ORDER BY WineList.WineQuality ASC
GO
--I want Pinot Noir type wines that have an quality of 8 or above.
EXEC spSelectWineTypeAndQuality 'Pinot Noir', 8 ; --9 Rows Returned.
--Creating a CASE statement to categorize wines based on the quality
SELECT WineList.WineQuality,
CASE
WHEN WineList.WineQuality = 9 THEN 'BEST'
WHEN WineList.WineQuality = 8 THEN 'GOOD'
ELSE 'AVERAGE'
END AS WineQualityCategory
FROM WineList;
----------
--THE END
----------