-
Notifications
You must be signed in to change notification settings - Fork 0
/
DML.sql
523 lines (361 loc) · 11.5 KB
/
DML.sql
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
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
USE EventManagementSystemDB
GO
-- Insert values into Users table
INSERT INTO ems.Users (FirstName, LastName, Email, MobileNo, UserType, RegistrationDate, LastLogin, IsActive)
VALUES
('Md. Rahman', 'Hossain', 'rahman.hossain@example.com', '01711 223 344', 'Organizer', GETDATE(), '2024-03-27 10:30:00', 0),
('Fatema', 'Akter', 'fatema.akter@example.com', '01822 334 455', 'Attendee', GETDATE(), NULL, 1),
('Shakil', 'Ahmed', 'shakil.ahmed@example.com', NULL, 'Attendee', GETDATE(), NULL, 1),
('Tasnim', 'Khan', 'tasnim.khan@example.com', '01633 445 566', 'Attendee', GETDATE(), NULL, 0),
('Farhan', 'Islam', 'farhan.islam@example.com', '01544 556 677', 'Organizer', GETDATE(), '2024-03-26 15:45:00', 1),
('Tasnim2', 'Khan2', 'abcsj.ugf@example.com', '01666 445 566', 'Attendee', GETDATE(), NULL, 0);
GO
SELECT * FROM ems.Users
GO
-- Insert sample event data rows
INSERT INTO ems.Events (EventName, EventDescription, StartDate, EndDate, Venue, UserID)
VALUES ('Software Development Conference', 'A gathering of developers to share knowledge and experiences.', GETDATE(), DATEADD(day, 2, GETDATE()), 'Grand Convention Center', 1);
INSERT INTO ems.Events (EventName, EventDescription, StartDate, EndDate, Venue, UserID)
VALUES ('Marketing Workshop', NULL, '2024-04-10 09:00:00', '2024-04-10 17:00:00', 'Company Auditorium', 2);
INSERT INTO ems.Events (EventName, EventDescription, StartDate, EndDate, Venue, UserID)
VALUES ('Networking Event for Entrepreneurs', 'Connect with fellow entrepreneurs and explore potential collaborations.',
DATEADD(day, 7, GETDATE()), DATEADD(day, 9, GETDATE()), 'The Sky Lounge', 3);
INSERT INTO ems.Events (EventName, EventDescription, StartDate, EndDate, Venue, UserID)
VALUES ('Data Science Hackathon', 'A weekend-long challenge to solve real-world data problems.', '2024-05-17 18:00:00', '2024-05-19 12:00:00', 'Innovation Hub', 1);
INSERT INTO ems.Events (EventName, EventDescription, StartDate, EndDate, Venue, UserID)
VALUES ('Business Summit 2024', 'Annual business summit bringing together industry leaders and entrepreneurs.', '2024-11-12 08:30:00', '2024-11-14 17:00:00', 'Radisson Blu Dhaka Water Garden', 4);
GO
SELECT * FROM ems.Events
GO
-- Insert Values Into Tickets table using TicketIDSequence
INSERT INTO ems.Tickets (EventID, TicketType, Price, AvailableQuantity, SoldQuantity)
VALUES
(1, 'General Admission', 50.00, 100, 0),
(2, 'VIP', 100.00, 50, 0),
(3, 'Regular', 30.00, 200, 0),
(4, 'Premium', 50.00, 100, 0),
(5, 'Standard', 25.00, 150, 0);
GO
-- Insert values into Registrations table
INSERT INTO ems.Registrations (EventID, UserID, TicketID, RegistrationDate)
VALUES
(1, 1, 1, SYSUTCDATETIME()),
(2, 2, 2, SYSUTCDATETIME());
GO
-- Insert values into Speakers table
INSERT INTO ems.Speakers (SpeakerName, SpeakerBio, SpeakerPhoto, IsActive, CreatedDate)
VALUES
('Md. Rahman Hossain', 'Md. Rahman Hossain is a renowned Bangladeshi entrepreneur and motivational speaker.', 0x, 1, SYSUTCDATETIME()),
('Fatema Akter', 'Fatema Akter is an accomplished Bangladeshi scientist and educator.', 0x, 1, SYSUTCDATETIME());
GO
-- Insert values into EventSpeakers table
INSERT INTO ems.EventSpeakers (EventID, SpeakerID)
VALUES
(1, 1),
(2, 2);
GO
-- Insert values into Sponsors table
INSERT INTO ems.Sponsors (SponsorName, WebsiteURL)
VALUES
('ABC Corporation', 'http://www.abccorporation.com'),
('XYZ Group', NULL);
GO
-- Insert values into EventSponsors table
INSERT INTO ems.EventSponsors (EventID, SponsorID)
VALUES
(1, 1),
(2, 2);
GO
-- Insert values into Payments table
INSERT INTO ems.Payments (UserID, Amount, PaymentDate, PaymentMethod, TransactionID)
VALUES
(1, 500.00, GETDATE(), 'Credit Card', 'CC1234567890'),
(2, 300.00, GETDATE(), 'Bank Transfer', 'BT987654321');
GO
-- Update
UPDATE [EventManagementSystemDB].[ems].[Users]
SET Email = 'newemail@example.com',
MobileNo = '01987 654 321'
WHERE UserID = 1;
GO
-- Delete
DELETE FROM [EventManagementSystemDB].[ems].[Users]
WHERE UserID = 6;
GO
-- Distinct
SELECT DISTINCT FirstName, LastName
FROM [EventManagementSystemDB].[ems].[Users];
GO
--Insert Into Copy Data From Another Table
SELECT *
INTO #tempPayment
FROM ems.Payments
GO
SELECT * FROM #tempPayment
GO
-- Truncate table
TRUNCATE TABLE #tempPayment
GO
-- Cross Join
SELECT *
FROM ems.Users
CROSS JOIN ems.Events;
GO
-- Self Join
SELECT u1.UserID AS UserID1, u1.FirstName AS FirstName1, u1.LastName AS LastName1,
u2.UserID AS UserID2, u2.FirstName AS FirstName2, u2.LastName AS LastName2
FROM ems.Users u1
JOIN ems.Users u2 ON u1.UserID <> u2.UserID;
GO
-- Group By & Having
SELECT UserType, COUNT(UserID) AS UserCount
FROM ems.Users
WHERE RegistrationDate > '2024-01-01'
GROUP BY UserType
HAVING COUNT(UserID) > 1;
GO
-- SubQuery
SELECT PaymentID, UserID, Amount, PaymentDate, PaymentMethod, TransactionID
FROM ems.Payments
WHERE UserID IN (SELECT UserID FROM ems.Users WHERE UserType = 'Organizer');
GO
-- Union
SELECT UserID, NULL AS FirstName, NULL AS LastName, Email, NULL AS MobileNo
FROM ems.Users
UNION
SELECT UserID, Amount, PaymentDate, PaymentMethod, TransactionID
FROM ems.Payments;
GO
-- Union All
SELECT UserID, NULL AS FirstName, NULL AS LastName, Email, NULL AS MobileNo
FROM ems.Users
UNION ALL
SELECT UserID, Amount, PaymentDate, PaymentMethod, TransactionID
FROM ems.Payments;
GO
-- CTE
WITH UpcomingRegistrations AS (
SELECT
r.RegistrationID,
e.EventName,
e.StartDate,
e.Venue,
u.FirstName,
u.LastName,
u.Email
FROM ems.Registrations r
INNER JOIN ems.Events e ON r.EventID = e.EventID
INNER JOIN ems.Users u ON r.UserID = u.UserID
WHERE e.EndDate >= GETDATE()
)
SELECT *
FROM UpcomingRegistrations;
GO
-- Execute the Stored- Procedure
EXEC RegisterUserForEvent @UserID = 1, @EventID = 1;
GO
SELECT * FROM dbo.GetUserUpcomingRegistrations(2);
GO
DECLARE @UserID INT = 1;
DECLARE @TotalSpent MONEY;
SELECT @TotalSpent = dbo.CalculateUserTotalSpent(@UserID);
-- Display the total amount spent by the user
SELECT @TotalSpent AS TotalSpent;
GO
--Mathematical Operator
SELECT 10+2 as [Sum]
GO
SELECT 10-2 as [Substraction]
GO
SELECT 10*3 as [Multiplication]
GO
SELECT 10/2 as [Divide]
GO
SELECT 10%3 as [Remainder]
GO
--Cast, Convert, Concatenation
SELECT 'Today : ' + CAST(GETDATE() as varchar)
Go
SELECT 'Today : ' + CONVERT(varchar,GETDATE(),1)
SELECT 'Today : ' + CONVERT(varchar,GETDATE(),2)
SELECT 'Today : ' + CONVERT(varchar,GETDATE(),3)
SELECT 'Today : ' + CONVERT(varchar,GETDATE(),4)
SELECT 'Today : ' + CONVERT(varchar,GETDATE(),5)
SELECT 'Today : ' + CONVERT(varchar,GETDATE(),6)
GO
--Isdate
SELECT ISDATE('2030-05-21')
--Datepart
SELECT DATEPART(MONTH,'2030-05-21')
--Datename
SELECT DATENAME(MONTH,'2030-05-21')
--Sysdatetime
SELECT Sysdatetime()
--UTC
SELECT GETUTCDATE()
GO
-- DATEDIFF
SELECT UserID, FirstName, LastName,
DATEDIFF(day, RegistrationDate, GETUTCDATE()) AS DaysSinceRegistered
FROM ems.Users
WHERE UserType = 'Attendee';
GO
-- ROLLUP
SELECT YEAR(RegistrationDate), MONTH(RegistrationDate) AS Month, COUNT(*) AS RegistrationsPerMonth
FROM ems.Registrations
GROUP BY YEAR(RegistrationDate), MONTH(RegistrationDate) WITH ROLLUP;
GO
-- GROUPING SETS
SELECT UserType, RegistrationDate, COUNT(*) AS RegistrationsByTypeAndDate
FROM ems.Users
GROUP BY GROUPING SETS ((UserType), (RegistrationDate), (UserType, RegistrationDate));
GO
-- CASE
SELECT UserID, FirstName, LastName,
CASE UserType WHEN 'Organizer' THEN 'Event Organizer' ELSE 'Attendee' END AS UserTypeName
FROM ems.Users;
GO
-- Between & And
SELECT EventName, StartDate, EndDate
FROM ems.Events
WHERE StartDate BETWEEN '2024-04-01' AND '2024-04-30';
GO
SELECT UserID, FirstName, LastName, Email
FROM ems.Users
WHERE UserType = 'Attendee' AND RegistrationDate > '2024-03-01';
GO
-- Wild-Card
SELECT EventName, SpeakerName
FROM ems.Events e
INNER JOIN ems.EventSpeakers es ON e.EventID = es.EventID
INNER JOIN ems.Speakers s ON es.SpeakerID = s.SpeakerID
WHERE SpeakerName LIKE '%Fatema Akter%' OR SpeakerName LIKE '%Machine Learning%';
GO
SELECT EventName, TicketType, SUM(SoldQuantity) AS TicketsSold
FROM ems.Tickets t
INNER JOIN ems.Events e ON t.EventID = e.EventID
GROUP BY EventName, ROLLUP(TicketType);
GO
SELECT u.UserType, r.RegistrationDate, COUNT(*) AS RegistrationsByTypeAndDate
FROM ems.Registrations r
INNER JOIN ems.Users u ON r.UserID = u.UserID
GROUP BY u.UserType, r.RegistrationDate;
GO
SELECT *
FROM ems.Events
WHERE EventID NOT IN (
SELECT EventID
FROM ems.EventSpeakers ES
JOIN ems.Speakers S ON ES.SpeakerID = S.SpeakerID
WHERE S.SpeakerName IN ('John', 'Jane')
);
GO
SELECT TOP 5 *
FROM ems.Events E
JOIN ems.Tickets T ON E.EventID = T.EventID
ORDER BY T.Price DESC;
GO
SELECT *
FROM ems.Events
WHERE EventName LIKE 'Market%';
GO
-- While Loop
DECLARE @Counter INT = 1;
WHILE @Counter <= 10
BEGIN
PRINT 'UserID: ' + CAST(@Counter AS NVARCHAR(10));
SET @Counter = @Counter + 1;
END;
GO
-- IIF
SELECT EventName,
StartDate,
IIF(CONVERT(DATE, StartDate) = CONVERT(DATE, GETDATE()), 'Today', 'Not Today') AS StartDateStatus
FROM ems.Events;
GO
-- CHOOSE
SELECT EventName,
EndDate,
CHOOSE(MONTH(EndDate), 'January', 'February', 'March', 'April', 'May', 'June', 'July', 'August', 'September', 'October', 'November', 'December') AS EndMonth
FROM ems.Events;
GO
-- ISNULL
SELECT EventName,
ISNULL(EventDescription, 'No Description Available') AS EventDescription
FROM ems.Events;
GO
SELECT
E.EventName,
E.Venue,
S.SponsorName
FROM
ems.Events E
JOIN
ems.EventSponsors ES ON E.EventID = ES.EventID
JOIN
ems.Sponsors S ON ES.SponsorID = S.SponsorID
WHERE
S.SponsorName IN ('ABC Corporation', 'Company B', 'Company C');
GO
-- COALESCE
SELECT
UserID,
COALESCE(Email, 'Email not provided') AS UserEmail
FROM
ems.Users;
GO
-- RANK, DENSE_RANK
SELECT
UserID,
RegistrationDate,
RANK() OVER (ORDER BY RegistrationDate ASC) AS RegistrationRank,
DENSE_RANK() OVER (ORDER BY RegistrationDate ASC) AS DenseRegistrationRank,
ROW_NUMBER() OVER (ORDER BY RegistrationDate ASC) AS RowNumberRegistrationRank
FROM
ems.Users;
GO
-- Aggregate Functions
SELECT
UserID,
Amount,
SUM(Amount) OVER(PARTITION BY UserID) AS TotalAmountPaid,
AVG(Amount) OVER(PARTITION BY UserID) AS AverageAmountPaid,
MAX(Amount) OVER(PARTITION BY UserID) AS MaxAmountPaid,
MIN(Amount) OVER(PARTITION BY UserID) AS MinAmountPaid
FROM
ems.Payments;
GO
-- CEILING, FLOOR, ROUND
SELECT
TicketType,
Price,
CEILING(Price) AS RoundedUpPrice,
FLOOR(Price) AS RoundedDownPrice,
ROUND(Price, 2) AS RoundedPrice
FROM
ems.Tickets;
GO
SELECT
EventID,
COUNT(RegistrationID) AS TotalRegistrations
FROM
ems.Registrations
GROUP BY
EventID;
GO
-- EXISTS
SELECT EventName, e.UserID
FROM ems.Events e
WHERE EXISTS (
SELECT 1 FROM ems.Users u
WHERE u.UserID = e.UserID AND u.IsActive = 1
);
GO
SELECT
SponsorID,
SponsorName,
WebsiteURL,
PARSENAME(REPLACE(REPLACE(WebsiteURL, 'https://', ''), 'http://', ''), 2) AS Domain
FROM
ems.Sponsors;
GO