-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathSQLNotes.txt
More file actions
360 lines (283 loc) · 7.9 KB
/
Copy pathSQLNotes.txt
File metadata and controls
360 lines (283 loc) · 7.9 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
347
348
349
350
351
352
353
354
355
356
357
358
359
360
//T-SQL Scripts
-- HERE
CREATE TABLE Tickets(
Id INT NOT NULL,
SeatLocation NVARCHAR(100) NOT NULL,
Price INT NOT NULL,
NumberSold INT NOT NULL DEFAULT 0,
PRIMARY KEY (Id)
);
INSERT INTO [Tickets]
([Id],[SeatLocation],[Price],[NumberSold])
VALUES
(1,'Box Level',105,4),
(2,'Dress Circle',75,2),
(3,'Main Floor',58,10),
(4,'Mid Balcony',38,0),
(5,'Upper Balcony',19,3)
SELECT COUNT(*) AS [Ticket Count] FROM Tickets
WHERE Price > 50
SELECT SUM(NumberSold) AS [Number Sold]
FROM Tickets
SELECT AVG(NumberSold) AS [Average Sold]
FROM Tickets
SELECT MIN(Price) AS [Minimum Price]
FROM Tickets
SELECT Max(Price) AS [Max Price]
FROM Tickets
SELECT SUM(NumberSold) AS 'Total Sold', SUM(Price * NumberSold) AS 'Total Revenue' FROM Tickets
INSERT INTO [Tickets]
([Id],[SeatLocation],[Price],[NumberSold])
VALUES
(6,'Box Level',105,4),
(7,'Box Level',75,2),
(8,'Box Level',58,10),
(9,'Mid Balcony',38,0),
(10,'Mid Balcony',19,3)
INSERT INTO [Avengers]
([Id],[CommonName],[HeroName],[PrimaryPower])
VALUES
(13,'Clark Kent', 'Superman','Strength'),
(14,'Bruce wayne', 'Batman','Tactician'),
(9,'Wally West', 'Flash','Speed'),
(10,'Barry Allen', 'God Speed','Speed'),
(11,'Tick', 'The Tick','Strength'),
(12,'Mr. Gonzales', 'Speedy','Speed')
SELECT PrimaryPower, COUNT(PrimaryPower) AS [Primary Power]
FROM Avengers
GROUP BY PrimaryPower
HAVING COUNT(PrimaryPower) > 1
– RELATIONSHIPS
CREATE TABLE Class(
Id INT PRIMARY KEY,
Title NVARCHAR(100) NOT NULL
);
CREATE TABLE Student(
Id INT PRIMARY KEY,
FullName NVARCHAR(100) NOT NULL,
ClassId INT FOREIGN KEY REFERENCES Class(Id)
);
INSERT INTO Class
([Id]
,[Title])
VALUES
(1,'.NET'),
(2,'Java'),
(3,'Front-End')
INSERT INTO Student
([Id]
,[FullName]
,[ClassId])
VALUES
(1,'G. Washington', 1),
(2,'M. Gandi', 1),
(3,'N. Mandela', null),
(4,'Q. Victoria', 2)
– LOOK UP TABLE
CREATE TABLE ComicUniverse(
Id INT IDENTITY(1,1),
UniverseName NVARCHAR(100) NOT NULL,
PRIMARY KEY (Id)
);
INSERT INTO ComicUniverse ([UniverseName])
VALUES('DC'),('Marvel'),('Other')
CREATE TABLE ComicUniverse(
Id INT IDENTITY(1,1),
UniverseName NVARCHAR(100) NOT NULL,
PRIMARY KEY (Id)
);
INSERT INTO ComicUniverse ([UniverseName])
VALUES('DC'),('Marvel'),('Other')
CREATE TABLE Authors(
Id INT IDENTITY (1,1) PRIMARY KEY,
FirstName NVARCHAR(50) NOT NULL,
LastName NVARCHAR(50) NOT NULL,
BirthDate DATETIME
);
CREATE TABLE ComicBooks
(
ID INT IDENTITY(1,1) PRIMARY KEY,
Title NVARCHAR(100) Not Null,
AuthorId INT FOREIGN KEY REFERENCES Authors(Id),
PartOfSeries NVARCHAR (10) null,
ComicHero NVARCHAR (100) Not Null,
UniverseId INT FOREIGN KEY REFERENCES ComicUniverse(Id)
);
CREATE TABLE BradsComicBooks
(
ID INT IDENTITY(1,1) PRIMARY KEY,
ComicId INT FOREIGN KEY REFERENCES ComicBooks(Id),
PricePaid DECIMAL
)
-- Add the column and create foreign key
ALTER TABLE Authors
ADD MostKnownWorkId INT FOREIGN KEY REFERENCES ComicBooks(Id);
– JOIN CODE
SELECT * FROM Student
SELECT * FROM Class
-- INNER JOIN or JOIN (Shows ALL data that's related)
SELECT s.FullName, c.Title
FROM Student AS s
JOIN Class AS c
ON s.ClassId = c.Id
ORDER BY c.Title
-- LEFT OUTER JOIN / LEFT JOIN
-- RIGHT OUTER JOIN / RIGHT JOIN
SELECT s.FullName, c.Title
FROM Student AS s
LEFT JOIN Class AS c
ON s.ClassId = c.Id
SELECT c.Title, s.FullName
FROM Class AS c
LEFT JOIN Student AS s
ON c.Id = s.ClassId
WHERE ISNULL(s.FullName,'') = ''
SELECT s.FullName, c.Title
FROM Student AS s
FULL JOIN Class as c
ON c.Id = s.ClassId
WHERE s.FullName IS NULL OR c.Id IS NULL
SELECT *
FROM Student
CROSS JOIN Class
ORDER BY FullName
SELECT c.Title, COUNT(s.FullName) AS StudentCount
FROM Student AS s
JOIN Class AS c
ON s.ClassId = c.Id
GROUP BY c.Title
SELECT cu.CompanyName,cu.EmailAddress,
c.AddressID, a.City, a.StateProvince, a.PostalCode
FROM [SalesLT].[CustomerAddress] AS c
JOIN [SalesLT].[Address] AS a
ON a.AddressId = c.AddressId
JOIN [SalesLT].[Customer] AS cu
ON cu.CustomerID = c.CustomerID
-- I want to see the order date, total due and company name, with highest total due
-- at the top of the result
SELECT s.OrderDate, s.TotalDue, c.CompanyName
FROM [SalesLT].[SalesOrderHeader] AS s
JOIN [SalesLT].[Customer] AS c
on c.customerID = s.customerID
ORDER BY s.TotalDue DESC
//T-SQL Scripts
-- CREATE TABLES
-- CREATE TABLE table_name
-- (column_name column_type, NULL/NOT NULL);
IF (EXISTS (SELECT *
FROM INFORMATION_SCHEMA.TABLES
WHERE TABLE_SCHEMA = 'dbo'
AND TABLE_NAME = 'Games'))
BEGIN
DROP TABLE Games
END
CREATE TABLE Games
(
Id BIGINT NOT NULL,
Title NVARCHAR(100) NOT NULL,
NumberOfPlayers INT NOT NULL,
PrimaryMechanism NVARCHAR(100) NOT NULL,
Review NVARCHAR(MAX) NULL,
PRIMARY KEY (Id)
);
IF (EXISTS (SELECT *
FROM INFORMATION_SCHEMA.TABLES
WHERE TABLE_SCHEMA = 'dbo'
AND TABLE_NAME = 'Avengers'))
BEGIN
DROP TABLE Avengers
END
CREATE TABLE Avengers(
Id BIGINT NOT NULL,
CommonName NVARCHAR(50) NOT NULL,
HeroName NVARCHAR(50) NOT NULL,
PrimaryPower NVARCHAR(100) NULL,
PRIMARY KEY (Id)
);
-- INSERT RECORDS
-- INSERT INTO table (column1, column2, … )
-- VALUES (expression1, expression2, …);
INSERT INTO [Avengers]
([Id],[CommonName],[HeroName],[PrimaryPower])
VALUES
(1,'Bruce Banner', 'Hulk','Strength'),
(2,'Steve Rogers', 'Captain America','Tactician'),
(3,'Thor', 'Thor','Lightening'),
(4,'Tony Stark', 'Iron Man','Genius'),
(5,'Natasha Romanov', 'Black Widow','Marksman'),
(6,'Clint Barton', 'Hawkeye','Archer')
INSERT INTO Games
([Id],[Title],[NumberOfPlayers],[PrimaryMechanism])
VALUES
(1,'Clank in "Space"',4,'Dice Roll'),
(2,'Clank',4,'Dice Roll')
CREATE TABLE Persons (
Id BIGINT IDENTITY(1,1) PRIMARY KEY,
LastName NVARCHAR(50) NOT NULL,
FirstName NVARCHAR(50),
Age int
);
INSERT INTO Persons
(LastName,FirstName,Age)
VALUES
('Tremaine', 'Scott',51),
('Tremaine', 'Griffin',14)
INSERT INTO Persons
(LastName,FirstName,Age)
VALUES
('Tremaine', 'Brennen',12)
-- SELECT
SELECT * FROM Avengers -- Try to avoid this, unless troubleshooting
SELECT CommonName AS 'Common Name', HeroName AS 'Also Known As', PrimaryPower AS 'Primary Power'
FROM Avengers
-- WHERE (filters the data)
SELECT TOP(1) Id, CommonName, HeroName, PrimaryPower
FROM Avengers
-- WHERE CommonName = 'brucE bAnnEr' OR HeroName = 'thor'
-- WHERE CommonName = 'brucE bAnnEr' AND PrimaryPower = 'strength'
-- WHERE (CommonName LIKE '%er%' AND PrimaryPower = 'strength') OR CommonName LIKE '%on%'
-- WHERE PrimaryPower NOT IN ('archer','tactician')
-- WHERE PrimaryPower IN ('archer','tactician')
-- WHERE CommonName > 'm'
WHERE Id BETWEEN 2 AND 5 -- INCLUSIVE
-- WHERE CommonName BETWEEN 'a' AND 'm'
ORDER BY HeroName DESC
-- UPDATE/DELETE
-- UPDATE [tableName] SET [ColumnName] = [Value]
-- DELETE FROM [tableName]
-- WHERE (both)
UPDATE persons SET LastName = 'Smith'
SELECT * FROM Persons
UPDATE Avengers SET PrimaryPower = 'Web Slinging'
WHERE HeroName LIKE 'spider%'
DELETE FROM Avengers
WHERE HeroName = 'Hawkeye'
SELECT * FROM Avengers
SELECT * FROM Persons
DELETE FROM Persons
TRUNCATE TABLE Persons
Get the name of the Avenger who's hero_name is Hulk.
SCRIPT (June)
SELECT * FROM Avengers
WHERE HeroName = 'hulk'
List all the Avengers in alphabetical order by hero_name.
SCRIPT (Dan)
SELECT CommonName, HeroName
FROM Avengers
ORDER BY HeroName
Add a new Avenger.
name: Cluck Kent
hero_name: Grant Chirpus
primary_power: Finding Bugs
SCRIPT (Brad)
INSERT INTO Avengers
(Id, HeroName, CommonName, PrimaryPower)
VALUES
(8, 'Grant Chirpus', 'Cluck Kent', 'Finding Bugs')
Change Thor's primary_power to Hammer.
UPDATE Avengers SET PrimaryPower = 'Hammer'
WHERE HeroName = 'Thor'
Remove Black Widow
SCRIPT (Eric)
DELETE from Avengers
where HeroName = 'Black Widow'