-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathQUERY.sql
More file actions
260 lines (240 loc) · 7.07 KB
/
Copy pathQUERY.sql
File metadata and controls
260 lines (240 loc) · 7.07 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
-- =========================================================================
-- SYSTEM: Football Ticket Booking System Database Setup Template
-- DESCRIPTION: Pseudo-DDL Template for Table Creation & Data Insertion
-- INSTRUCTIONS: Replace 'TYPE' and the constraint placeholders with your own
-- actual data types, relational keys, and check criteria.
-- =========================================================================
-- DROP TABLES IF THEY ALREADY EXIST TO PREVENT CONFLICTS
DROP TABLE IF EXISTS Bookings;
DROP TABLE IF EXISTS Matches;
DROP TABLE IF EXISTS Users;
-- =========================================================================
-- 1. CREATE USERS TABLE
-- =========================================================================
CREATE TABLE Users (
user_id serial PRIMARY KEY,
full_name varchar(255) NOT NULL,
email varchar(255) UNIQUE NOT NULL,
role varchar(50) CHECK (role IN ('Ticket Manager', 'Football Fan')) NOT NULL,
phone_number varchar(50)
);
-- =========================================================================
-- 2. CREATE MATCHES TABLE
-- =========================================================================
CREATE TABLE Matches (
match_id serial PRIMARY KEY,
fixture varchar(255) NOT NULL,
tournament_category varchar(255) NOT NULL,
base_ticket_price decimal(8, 2) CHECK (base_ticket_price >= 0) NOT NULL,
match_status varchar(255) CHECK (
match_status IN (
'Available',
'Selling Fast',
'Sold Out',
'Postponed'
)
) NOT NULL
);
-- =========================================================================
-- 3. CREATE BOOKINGS TABLE
-- =========================================================================
CREATE TABLE Bookings (
booking_id serial PRIMARY KEY,
user_id int REFERENCES users (user_id) NOT NULL,
match_id int REFERENCES matches (match_id) NOT NULL,
seat_number varchar(255),
payment_status varchar(255) CHECK (
payment_status IN ('Pending', 'Confirmed', 'Cancelled', 'Refunded')
),
total_cost decimal(8, 2) CHECK (total_cost >= 0) NOT NULL
);
-- =========================================================================
-- DATA SEEDING: INSERT SAMPLE DATA INTO USERS
-- =========================================================================
INSERT INTO
Users (user_id, full_name, email, role, phone_number)
VALUES
(
1,
'Tanvir Rahman',
'tanvir@mail.com',
'Football Fan',
'+8801711111111'
),
(
2,
'Asif Haque',
'asif@mail.com',
'Football Fan',
'+8801722222222'
),
(
3,
'Sajjad Rahman',
'sajjad@mail.com',
'Ticket Manager',
'+8801733333333'
),
(
4,
'Jannat Ara',
'jannat@mail.com',
'Football Fan',
NULL
);
-- =========================================================================
-- DATA SEEDING: INSERT SAMPLE DATA INTO MATCHES
-- =========================================================================
INSERT INTO
Matches (
match_id,
fixture,
tournament_category,
base_ticket_price,
match_status
)
VALUES
(
101,
'Real Madrid vs Barcelona',
'Champions League',
150.00,
'Available'
),
(
102,
'Man City vs Liverpool',
'Premier League',
120.00,
'Selling Fast'
),
(
103,
'Bayern Munich vs PSG',
'Champions League',
130.00,
'Available'
),
(
104,
'AC Milan vs Inter Milan',
'Serie A',
90.00,
'Sold Out'
),
(
105,
'Juventus vs Roma',
'Serie A',
80.00,
'Available'
);
-- =========================================================================
-- DATA SEEDING: INSERT SAMPLE DATA INTO BOOKINGS
-- =========================================================================
INSERT INTO
Bookings (
booking_id,
user_id,
match_id,
seat_number,
payment_status,
total_cost
)
VALUES
(501, 1, 101, 'A-12', 'Confirmed', 150.00),
(502, 1, 102, 'B-04', 'Confirmed', 120.00),
(503, 2, 101, 'A-13', 'Confirmed', 150.00),
(504, 2, 101, NULL, NULL, 150.00),
(505, 3, 102, 'C-20', 'Pending', 120.00);
-- =========================================================================
-- Query 1: Retrieve all upcoming football matches belonging to the 'Champions League' where the match status is 'Available'.
-- =========================================================================
SELECT
match_id,
fixture,
round(base_ticket_price) as base_ticket_price
FROM
matches
WHERE
tournament_category = 'Champions League'
AND match_status = 'Available';
-- =========================================================================
-- Query 2: Search for all users whose full names start with 'Tanvir' or contain the phrase 'Haque' (case-insensitive).
-- =========================================================================
SELECT
user_id,
full_name,
email
FROM
users
WHERE
full_name LIKE 'Tanvir%'
OR full_name ILIKE '%Haque%'
-- =========================================================================
-- Query 3: Retrieve all booking records where the payment status is missing (NULL), replacing the empty result with 'Action Required'.
-- =========================================================================
SELECT
booking_id,
user_id,
match_id,
coalesce(payment_status, 'Action Required') AS "systematic_status"
FROM
bookings
WHERE
payment_status IS NULL
-- =========================================================================
-- Query 4: Retrieve match booking details along with the User's full name and the scheduled Match fixture teams.
-- =========================================================================
SELECT
booking_id,
full_name,
fixture,
round(total_cost) as total_cost
FROM
bookings AS b
INNER JOIN users AS u ON b.user_id = u.user_id
INNER JOIN matches AS m ON b.match_id = m.match_id
-- =========================================================================
-- Query 5: Display a comprehensive list of all users and their booking IDs, ensuring that fans who have never bought a ticket are still listed.
-- =========================================================================
SELECT
u.user_id,
full_name,
booking_id
FROM
users AS u
LEFT JOIN bookings AS b ON b.user_id = u.user_id
ORDER BY
u.user_id
-- =========================================================================
-- Query 6: Find all ticket bookings where the total cost is strictly higher than the average cost of all ticket bookings.
-- =========================================================================
SELECT
booking_id,
match_id,
round(total_cost) as total_cost
FROM
bookings
WHERE
total_cost > (
SELECT
avg(total_cost)
FROM
bookings
)
-- =========================================================================
-- Query 7: Retrieve the top 2 most expensive matches sorted by base ticket price, skipping the absolute highest premium match.
-- =========================================================================
SELECT
match_id,
fixture,
round(base_ticket_price) as base_ticket_price
FROM
matches
ORDER BY
base_ticket_price DESC
LIMIT
2
OFFSET
1