-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathCognifiz Internship task Queries.sql
More file actions
276 lines (242 loc) · 8.26 KB
/
Copy pathCognifiz Internship task Queries.sql
File metadata and controls
276 lines (242 loc) · 8.26 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
SELECT TOP (1000) [Restaurant ID]
,[Restaurant Name]
,[Country Code]
,[City]
,[Address]
,[Locality]
,[Locality Verbose]
,[Longitude]
,[Latitude]
,[Cuisines]
,[Average Cost for two]
,[Currency]
,[Has Table booking]
,[Has Online delivery]
,[Is delivering now]
,[Switch to order menu]
,[Price range]
,[Aggregate rating]
,[Rating color]
,[Rating text]
,[Votes]
FROM [Restaurant].[dbo].['Dataset $']
---- Level 1 ----
---TASK1 ----
-
WITH TopCuisines AS (
SELECT TOP 3
Cuisines,
COUNT(*) AS CuisineCount
FROM [Restaurant].[dbo].['Dataset $']
GROUP BY Cuisines
ORDER BY CuisineCount DESC
),
RestaurantCount AS (
SELECT COUNT(*) AS TotalRestaurants
FROM [Restaurant].[dbo].['Dataset $']
),
CuisinePercentages AS (
SELECT tc.Cuisines,
tc.CuisineCount,
(tc.CuisineCount * 100.0 / rc.TotalRestaurants) AS Percentage
FROM TopCuisines tc
CROSS JOIN RestaurantCount rc
)
SELECT Cuisines,
CuisineCount,
Percentage
FROM CuisinePercentages;
------task 2 ------
;WITH CityRestaurantCount AS (
-- Step 1: Identify the city with the highest number of restaurants
SELECT TOP 1 WITH TIES
City,
COUNT(*) AS RestaurantCount
FROM [Restaurant].[dbo].['Dataset $']
GROUP BY City
ORDER BY COUNT(*) DESC
),
CityAverageRating AS (
-- Step 2: Calculate the average rating for restaurants in each city
SELECT City,
AVG([Aggregate rating]) AS AverageRating
FROM [Restaurant].[dbo].['Dataset $']
GROUP BY City
)
-- Step 3: Determine the city with the highest average rating
SELECT TOP 1
'City with Highest Number of Restaurants' AS Metric,
City,
RestaurantCount AS Value
FROM CityRestaurantCount
UNION ALL
SELECT TOP 1
'City with Highest Average Rating' AS Metric,
City,
AverageRating AS Value
FROM CityAverageRating
ORDER BY Value DESC; -- Order by highest average rating
-----task 3 ----
WITH PriceRangeDistribution AS (
SELECT [Price range],
COUNT(*) AS RestaurantCount,
100.0 * COUNT(*) / SUM(COUNT(*)) OVER () AS Percentage
FROM [Restaurant].[dbo].['Dataset $'] -- Replace with your actual table name
GROUP BY [Price range]
)
SELECT [Price range],
RestaurantCount,
ROUND(Percentage, 2) AS Percentage
FROM PriceRangeDistribution
ORDER BY [Price range];
------task4---
WITH OnlineDeliveryStats AS (
-- Step 1: Calculate the percentage of restaurants that offer online delivery
SELECT
SUM(CASE WHEN [Has Online delivery] = 'Yes' THEN 1 ELSE 0 END) AS RestaurantsWithDelivery,
COUNT(*) AS TotalRestaurants,
100.0 * SUM(CASE WHEN [Has Online delivery] = 'Yes' THEN 1 ELSE 0 END) / COUNT(*) AS PercentageWithDelivery
FROM [Restaurant].[dbo].['Dataset $'] -- Replace with your actual table name
),
AverageRatings AS (
-- Step 2: Compare the average ratings of restaurants with and without online delivery
SELECT
AVG(CASE WHEN [Has Online delivery] = 'Yes' THEN [Aggregate rating] ELSE NULL END) AS AvgRatingWithDelivery,
AVG(CASE WHEN [Has Online delivery] = 'No' THEN [Aggregate rating] ELSE NULL END) AS AvgRatingWithoutDelivery
FROM [Restaurant].[dbo].['Dataset $']
WHERE [Aggregate rating] IS NOT NULL -- Ensure ratings are not NULL
)
-- Final SELECT: Combine results from both steps
SELECT
RestaurantsWithDelivery,
TotalRestaurants,
ROUND(PercentageWithDelivery, 2) AS PercentageWithDelivery,
ROUND(AvgRatingWithDelivery, 2) AS AvgRatingWithDelivery,
ROUND(AvgRatingWithoutDelivery, 2) AS AvgRatingWithoutDelivery
FROM OnlineDeliveryStats, AverageRatings;
---------------level 2 -----------------------
-------task1---------
WITH RatingDistribution AS (
-- Step 1: Analyze the distribution of aggregate ratings
SELECT
FLOOR([Aggregate rating]) AS RatingFloor,
COUNT(*) AS RatingCount
FROM [Restaurant].[dbo].['Dataset $'] -- Replace with your actual table name
WHERE [Aggregate rating] IS NOT NULL -- Ensure ratings are not NULL
GROUP BY FLOOR([Aggregate rating])
),
MostCommonRating AS (
-- Step 2: Determine the most common rating range
SELECT TOP 1 WITH TIES
RatingFloor,
RatingCount
FROM RatingDistribution
ORDER BY RatingCount DESC
),
AverageVotes AS (
-- Step 3: Calculate the average number of votes received by restaurants
SELECT
AVG(Votes) AS AvgVotes
FROM [Restaurant].[dbo].['Dataset $'] -- Replace with your actual table name
WHERE Votes IS NOT NULL -- Ensure votes are not NULL
)
-- Final SELECT: Combine results from all steps
SELECT
'Rating Distribution' AS Metric,
RatingFloor AS RatingRange,
RatingCount AS Count
FROM RatingDistribution
UNION ALL
SELECT
'Most Common Rating Range' AS Metric,
RatingFloor AS RatingRange,
RatingCount AS Count
FROM MostCommonRating
UNION ALL
SELECT
'Average Votes Received' AS Metric,
NULL AS RatingRange, -- Placeholder for non-range metrics
ROUND(AvgVotes, 2) AS Count
FROM AverageVotes;
-------task2--------
WITH CuisineCombinations AS (
-- Step 1: Identify the most common combinations of cuisines
SELECT
Cuisines,
COUNT(*) AS CombinationCount
FROM [Restaurant].[dbo].['Dataset $'] -- Replace with your actual table name
WHERE Cuisines IS NOT NULL -- Ensure cuisines are not NULL
GROUP BY Cuisines
),
TopCuisineCombinations AS (
-- Step 2: Select top combinations by count
SELECT TOP 10
Cuisines,
CombinationCount
FROM CuisineCombinations
ORDER BY CombinationCount DESC
),
CuisineRatings AS (
-- Step 3: Calculate the average rating for each cuisine combination
SELECT
Cuisines,
AVG([Aggregate rating]) AS AvgRating
FROM [Restaurant].[dbo].['Dataset $'] -- Replace with your actual table name
WHERE Cuisines IS NOT NULL -- Ensure cuisines are not NULL
AND [Aggregate rating] IS NOT NULL -- Ensure ratings are not NULL
GROUP BY Cuisines
)
-- Final SELECT: Combine results from both steps
SELECT
'Most Common Cuisine Combinations' AS Metric,
tc.Cuisines AS CuisineCombination,
tc.CombinationCount AS Count,
cr.AvgRating AS AverageRating
FROM TopCuisineCombinations tc
LEFT JOIN CuisineRatings cr ON tc.Cuisines = cr.Cuisines
UNION ALL
SELECT
'Higher Ratings for Cuisine Combinations' AS Metric,
cr.Cuisines AS CuisineCombination,
NULL AS Count,
ROUND(cr.AvgRating, 2) AS AverageRating
FROM CuisineRatings cr
WHERE cr.Cuisines IN (
SELECT Cuisines
FROM TopCuisineCombinations
)
ORDER BY Metric, AverageRating DESC; -- Moved ORDER BY outside of the CTEs
----------task4 --------
WITH ChainRestaurants AS (
-- Step 1: Identify restaurant chains by grouping by Restaurant Name
SELECT
[Restaurant Name] AS ChainName,
COUNT(*) AS ChainCount,
AVG([Aggregate rating]) AS AvgRating,
SUM(Votes) AS TotalVotes
FROM [Restaurant].[dbo].['Dataset $']
WHERE [Restaurant Name] IS NOT NULL -- Ensure restaurant names are not NULL
GROUP BY [Restaurant Name]
HAVING COUNT(*) > 1 -- Adjust as needed to define what constitutes a chain (e.g., more than one location)
),
-- Step 2: Analyze ratings and popularity of different restaurant chains
ChainAnalysis AS (
SELECT
ChainName,
ChainCount,
AvgRating,
TotalVotes,
ROW_NUMBER() OVER (ORDER BY AvgRating DESC) AS RatingRank,
ROW_NUMBER() OVER (ORDER BY TotalVotes DESC) AS VotesRank
FROM ChainRestaurants
)
-- Final SELECT: Combine results from both steps
SELECT
ChainName,
ChainCount AS Locations,
AvgRating AS AverageRating,
TotalVotes AS TotalVotes,
RatingRank,
VotesRank
FROM ChainAnalysis
ORDER BY RatingRank;