|
| 1 | +USE roc; |
| 2 | + |
| 3 | +CREATE TABLE IF NOT EXISTS restaurants ( |
| 4 | + id INT PRIMARY KEY AUTO_INCREMENT, |
| 5 | + name VARCHAR(100), |
| 6 | + city VARCHAR(50), |
| 7 | + cuisine VARCHAR(50), |
| 8 | + rating FLOAT, |
| 9 | + rating_count INT, |
| 10 | + cost INT |
| 11 | +); |
| 12 | + |
| 13 | +INSERT INTO restaurants (name, city, cuisine, rating, rating_count, cost) VALUES |
| 14 | +('Spicy Delight', 'Mumbai', 'Indian', 4.5, 120, 500), |
| 15 | +('Tandoori Treats', 'Delhi', 'Indian', 4.2, 95, 450), |
| 16 | +('Sushi World', 'Bangalore', 'Japanese', 4.7, 150, 800), |
| 17 | +('Pizza Hub', 'Mumbai', 'Italian', 4.0, 110, 600), |
| 18 | +('Pasta Paradise', 'Delhi', 'Italian', 3.8, 85, 550), |
| 19 | +('Burger Junction', 'Chennai', 'Fast Food', 4.1, 130, 400), |
| 20 | +('BBQ Nation', 'Bangalore', 'BBQ', 4.6, 200, 900), |
| 21 | +('Chaat Corner', 'Delhi', 'Street Food', 4.3, 180, 300), |
| 22 | +('Dimsum Den', 'Mumbai', 'Chinese', 4.4, 140, 700), |
| 23 | +('Biryani Bliss', 'Hyderabad', 'Indian', 4.8, 250, 650), |
| 24 | +('Taco House', 'Bangalore', 'Mexican', 4.2, 160, 750), |
| 25 | +('Steakhouse', 'Chennai', 'American', 4.5, 140, 1000); |
| 26 | + |
| 27 | +-- Verify inserted data |
| 28 | +SELECT * FROM restaurants; |
| 29 | + |
| 30 | +-- 1. Create new column containing average rating of restaurants throught the dataset |
| 31 | +select *, round(avg(rating) over(),2) as 'avg_rating' from restaurants; |
| 32 | + |
| 33 | +-- 2. Create new column containing average rating_count of restaurants throught the dataset |
| 34 | +select *, round(avg(rating_count) over(),0) as 'avg_rating_count' from restaurants; |
| 35 | + |
| 36 | +-- 3. Create new column containing average cost of restaurants throught the dataset |
| 37 | +select *, round(avg(cost) over(),0) as 'avg_cost' from restaurants; |
| 38 | + |
| 39 | +-- 4. Create column containing average, min, max of cost,rating,rating_count of restaurants throught the dataset |
| 40 | +select id, name, city, cuisine, rating, |
| 41 | + round(max(rating) over(), 2) as 'max_rating', |
| 42 | + round(avg(rating) over(), 2) as 'avg_rating', |
| 43 | + round(min(rating) over(), 2) as 'min_rating', |
| 44 | + |
| 45 | + round(max(cost) over(), 2) as 'max_cost', |
| 46 | + round(avg(cost) over(), 2) as 'avg_cost', |
| 47 | + round(min(cost) over(), 2) as 'min_cost' |
| 48 | + |
| 49 | +from restaurants; |
| 50 | + |
| 51 | +-- 5. Create column containing average cost of the city where that specific restaurant is from |
| 52 | +select *, round(avg(cost) over( partition by city) ) as 'avg_cost' from restaurants; |
| 53 | + |
| 54 | +-- 6. Create column containing average cost of the cuisine which that specific restaurant is serving |
| 55 | +select *, round(avg(cost) over( partition by cuisine) ) as 'avg_cost' from restaurants; |
| 56 | + |
| 57 | +-- 7. Create both column together |
| 58 | +select *, |
| 59 | + round(avg(cost) over( partition by city) ) as 'avg_cost_city', |
| 60 | + round(avg(cost) over( partition by cuisine) ) as 'avg_cost_cuisine' |
| 61 | +from restaurants; |
| 62 | + |
| 63 | +-- 8. List the restaurants whose cost is more than the average cost of the restaurants? |
| 64 | +select * from restaurants where cost > (select avg(cost) from restaurants); |
| 65 | +select * from (select *, avg(cost) over() as 'avg_cost' from restaurants) t where t.cost > t.avg_cost; |
| 66 | + |
| 67 | + |
| 68 | +-- 10. List the restaurants whose cuisine cost is more than the average cost? |
| 69 | +select * from (select *, avg(cost) over(partition by cuisine) as 'avg_cost' from restaurants) t where t.cost > t.avg_cost; |
0 commit comments