-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathProject 1 SQL.sqbpro
More file actions
691 lines (288 loc) · 11 KB
/
Project 1 SQL.sqbpro
File metadata and controls
691 lines (288 loc) · 11 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
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
<?xml version="1.0" encoding="UTF-8"?><sqlb_project><db path="WSDA_Music.db" readonly="0" foreign_keys="1" case_sensitive_like="0" temp_store="0" wal_autocheckpoint="1000" synchronous="2"/><attached/><window><main_tabs open="structure browser pragmas query" current="0"/></window><tab_structure><column_width id="0" width="300"/><column_width id="1" width="0"/><column_width id="2" width="100"/><column_width id="3" width="3930"/><column_width id="4" width="0"/><expanded_item id="0" parent="1"/><expanded_item id="3" parent="0"/><expanded_item id="1" parent="1"/><expanded_item id="2" parent="1"/><expanded_item id="3" parent="1"/></tab_structure><tab_browse><table title="Track" custom_title="0" dock_id="1" table="4,5:mainTrack"/><dock_state state="000000ff00000000fd00000001000000020000024800000215fc0100000001fb000000160064006f0063006b00420072006f00770073006500310100000000000002480000015900ffffff000002480000000000000004000000040000000800000008fc00000000"/><default_encoding codec=""/><browse_table_settings><table schema="main" name="Album" show_row_id="0" encoding="" plot_x_axis="" unlock_view_pk="_rowid_" freeze_columns="0"><sort/><column_widths><column index="1" value="56"/><column index="2" value="287"/><column index="3" value="54"/></column_widths><filter_values/><conditional_formats/><row_id_formats/><display_formats/><hidden_columns/><plot_y_axes/><global_filter/></table><table schema="main" name="Artist" show_row_id="0" encoding="" plot_x_axis="" unlock_view_pk="_rowid_" freeze_columns="0"><sort/><column_widths><column index="1" value="52"/><column index="2" value="183"/></column_widths><filter_values/><conditional_formats/><row_id_formats/><display_formats/><hidden_columns/><plot_y_axes/><global_filter/></table><table schema="main" name="Customer" show_row_id="0" encoding="" plot_x_axis="" unlock_view_pk="_rowid_" freeze_columns="0"><sort/><column_widths><column index="1" value="75"/><column index="2" value="79"/><column index="3" value="103"/><column index="4" value="300"/><column index="5" value="300"/><column index="6" value="159"/><column index="7" value="55"/><column index="8" value="119"/><column index="9" value="87"/><column index="10" value="159"/><column index="11" value="151"/><column index="12" value="239"/><column index="13" value="89"/></column_widths><filter_values/><conditional_formats/><row_id_formats/><display_formats/><hidden_columns/><plot_y_axes/><global_filter/></table><table schema="main" name="Employee" show_row_id="0" encoding="" plot_x_axis="" unlock_view_pk="_rowid_" freeze_columns="0"><sort><column index="5" mode="0"/></sort><column_widths><column index="1" value="75"/><column index="2" value="71"/><column index="3" value="71"/><column index="4" value="159"/><column index="5" value="67"/><column index="6" value="159"/><column index="7" value="159"/><column index="8" value="223"/><column index="9" value="87"/><column index="10" value="39"/><column index="11" value="55"/><column index="12" value="71"/><column index="13" value="143"/><column index="14" value="143"/><column index="15" value="199"/></column_widths><filter_values/><conditional_formats/><row_id_formats/><display_formats/><hidden_columns/><plot_y_axes/><global_filter/></table><table schema="main" name="Invoice" show_row_id="0" encoding="" plot_x_axis="" unlock_view_pk="_rowid_" freeze_columns="0"><sort/><column_widths><column index="1" value="62"/><column index="2" value="77"/><column index="3" value="159"/><column index="4" value="300"/><column index="5" value="159"/><column index="6" value="72"/><column index="7" value="119"/><column index="8" value="104"/><column index="9" value="63"/></column_widths><filter_values/><conditional_formats/><row_id_formats/><display_formats/><hidden_columns/><plot_y_axes/><global_filter/></table><table schema="main" name="Track" show_row_id="0" encoding="" plot_x_axis="" unlock_view_pk="_rowid_" freeze_columns="0"><sort/><column_widths><column index="1" value="52"/><column index="2" value="199"/><column index="3" value="58"/><column index="4" value="84"/><column index="5" value="56"/><column index="6" value="247"/><column index="7" value="76"/><column index="8" value="87"/><column index="9" value="59"/></column_widths><filter_values/><conditional_formats/><row_id_formats/><display_formats/><hidden_columns/><plot_y_axes/><global_filter/></table><table schema="main" name="V_AvgTotal" show_row_id="0" encoding="" plot_x_axis="" unlock_view_pk="_rowid_" freeze_columns="0"><sort/><column_widths><column index="1" value="88"/></column_widths><filter_values/><conditional_formats/><row_id_formats/><display_formats/><hidden_columns/><plot_y_axes/><global_filter/></table></browse_table_settings></tab_browse><tab_sql><sql name="SELECT*">SELECT FirstName, LastName
FROM Customer
</sql><sql name="ORDER BY & LIMIT*">/*
CREATED BY SMASJOAN
CREATED DATE 23/08/2025
DESCRIPTION: This query displays all custumer first, last names and email adresses
*/
SELECT
FirstName AS [Customer First Name],
LastName AS "Customer Last Name",
Email AS EMAIL
From Customer
ORDER BY
FirstName ASC,
LastName DESC
LIMIT 10</sql><sql name="WHERE*">SELECT
InvoiceDate,
BillingAddress,
BillingCity,
total,
CASE
WHEN total < 2.00 THEN "Baseline Purchase"
WHEN total BETWEEN 2.00 AND 6.99 THEN "Low Purchase"
WHEN total BETWEEN 7.00 AND 15.00 THEN "Target Purchase"
ELSE "Top Performer"
End AS "Purchase Type"
FROM
Invoice
WHERE
"Purchase Type"= "Top Performer"
ORDER BY
BillingCity</sql><sql name="JOINS*">SELECT
E.FirstName,
E.LastName,
E.EmployeeId,
C.FirstName,
C.LastName,
C.SupportRepId,
I.CustomerId,
I.total
FROM
Invoice AS I
INNER JOIN
Customer AS C
ON
I.CustomerId = C.CustomerId
INNER JOIN
Employee AS E
ON
C.SupportRepId = E.EmployeeId
Order BY
I.total ASC
LIMIT 10</sql><sql name="Text Functions*">SELECT
FirstName,
LastName,
Address,
FirstName||" "||LastName||" "||Address||","||City||" "||State||" "||PostalCode AS [Mailing Address],
length(postalcode),
substr(postalcode,1,5) AS [5 Digit PostalCode],
upper(firstname) AS [Firstname All Caps],
lower(lastname) AS [LastName All Lowe]
FROM
Customer
WHERE
Country = "USA"
</sql><sql name="Date Functions*">SELECT
LastName,
FirstName,
BirthDate,
strftime("%Y-%m-%d",BirthDate) AS [BirthDate NO Timecode],
strftime("%Y-%m-%d","now")-strftime("%Y-%m-%d",BirthDate) AS Age
FROM
Employee</sql><sql name="Aggregate Funct*">/*
Created By: SMasjoan
Date: 24/08/25
Description: Aggr Funct| What are our all time global sales?
*/
SELECT
SUM(total) As [Total Sales],
AVG(total) AS [Average Sales],
MAX(total) AS [Maximum Sale],
MIN(total) AS [Minimum Sale],
Count(*) As [Sales Count]
FROM
Invoice
</sql><sql name="Nesting Function*">SELECT
SUM(total) As [Total Sales],
round (AVG(total),2) AS [Average Sales],
MAX(total) AS [Maximum Sale],
MIN(total) AS [Minimum Sale],
Count(*) As [Sales Count]
FROM
Invoice
</sql><sql name="GROUP BY*">/*
Created by: SMasjoan
Date:24/08/25
Desc: What are the avg invoice totals by City for only cities that start with L
*/
SELECT
BillingCity,
round(Avg(total),2)
FROM
Invoice
WHERE
BillingCity Like "L%"
Group By
BillingCity
Order By
BillingCity
</sql><sql name="GROUP BY2*">/*
Created by: SMasjoan
Date:24/08/25
Desc: What are the avg invoice grater than $5.00 for cities starting with B
*/
SELECT
BillingCity,
round(Avg(total),2)
FROM
Invoice
WHERE
BillingCity Like "B%"
Group By
BillingCity
HAVING
avg(total)>5
Order By
BillingCity
</sql><sql name="GROUP BY 3*">/*
Created by: SMasjoan
Date:24/08/25
Desc: Grouping by more than one field at the time| what avg invoice by Billing country and city?
*/
SELECT
BillingCountry,
BillingCity,
round(Avg(total),2)
FROM
Invoice
Group By
BillingCountry, BillingCity
Order By
BillingCountry</sql><sql name="SUBQUERY*">/*
Created by: SMasjoan
Date:24/08/25
Desc: Subquery | Agregate funct
*/
SELECT
BillingCity,
AVG(total),
(select avg(total) from Invoice) AS [Global Average]
FROM
Invoice
GROUP By
BillingCity
Order By
total Desc</sql><sql name="SUBQUERY2*">/*
Created by: SMasjoan
Date:24/08/25
Desc: Subquery | Agregate nonfunct
*/
SELECT
InvoiceDate,
BillingAddress,
BillingCity
FROM
Invoice
WHERE
InvoiceDate IN
(SELECT
InvoiceDate
FROM
Invoice
WHERE
InvoiceID IN (251,252,254))
</sql><sql name="SUBQUERY & DISTINCT*">/*
Created by: SMasjoan
Date:24/08/25
Desc: Subquery & Distinct | Which Track arte not selling?
*/
SELECT
TrackId,
Composer,
Name
FROM
Track
WHERE
TrackID Not In
(SELECT
DISTINCT
TrackId
FROM
InvoiceLine
Order by
TrackId)</sql><sql name="SUBQUERY 3*">/*
Created by: SMasjoan
Date:24/08/25
Desc: tracks never sold
*/
SELECT
t.TrackID As "Track ID",
t.Name As "Track Name",
t.Composer AS Composer,
g.Name AS Genre
FROM Track AS t
Inner Join
Genre As g
On
t.GENREID = g.GENREID
Where
t.TRACKID NOT IN
(Select
Distinct
li.TrackID
FROM
InvoiceLine AS li)
Order By
t.TrackID
;</sql><sql name="Views*">/*
Created by: SMasjoan
Date:24/08/25
Desc: View
*/
Create VIEW V_AvgTotal AS
SELECT
round(avg(total),2) As "Average Total"
FROM
Invoice
</sql><sql name="View and Joins*">/*
Created by: SMasjoan
Date:24/08/25
Desc: View and Joins
*/
CREATE VIEW V_Tracks_InvoiceLine AS
SELECT
il.InvoiceId,
il.UnitPrice,
il.Quantity,
t.Name,
t.Composer,
t.Milliseconds
FROM
InvoiceLine il
INNER JOIN
Track t
On
il.TrackId = t.TrackId</sql><sql name="DML*">/*
Created by: SMasjoan
Date:24/08/25
Desc: DML
*/
INSERT INTO
Artist (Name)
VALUES ("Bob Maely")</sql><sql name="DML 2*">/*
Created by: SMasjoan
Date:24/08/25
Desc: DML | updating DATABASE
*/
UPDATE
Artist
SET
Name = "Damian Marley"
WHERE
ArtistId = 276</sql><sql name="DML 3*">/*
Created by: SMasjoan
Date:24/08/25
Desc: DML | Delete Data
*/
Delete FROM
Artist
WHERE
ArtistId = 276</sql><sql name="SQL 22*">SELECT
count(*)
FROM
Invoice
WHERE
BillingCity like "Brussels"</sql><sql name="SQL 23*">SELECT
COUNT(Name) AS TracksWithoutComposer,
Composer
FROM Track
GROUP BY
Composer
ORDER By
TracksWithoutComposer DESC</sql><sql name="SQL 24*">SELECT
count(*)
FROM
Customer
WHERE
LastName Like "g%"</sql><current_tab id="22"/></tab_sql></sqlb_project>