0% found this document useful (0 votes)
9 views1 page

SQL 1

Uploaded by

Anish Mishra
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
9 views1 page

SQL 1

Uploaded by

Anish Mishra
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 1

Code Glossary

Module 4: Analyzing Food Preferences and Food Items


1. To find the number of orders of each food_type i.e. veg and non-veg:
SELECT fi.food_type, SUM(oi.quantity) AS items_quatity FROM orders_items oi
LEFT JOIN food_items fi ON oi.item_id = fi.item_id
GROUP BY fi.food_type;

2. To group the different entries for veg and non-veg using CASE statement
and then find the number of orders of each food_type i.e. veg and non-veg:
SELECT t2.food_type_new, SUM(t1.quantity) AS item_quantity
FROM orders_items t1
LEFT JOIN (
SELECT item_id,
CASE
WHEN food_type LIKE 'veg%' THEN 'veg'
ELSE 'non-veg'
END AS food_type_new
FROM food_items
) t2 ON t1.item_id = t2.item_id
GROUP BY t2.food_type_new;

3. To find the number of orders received by each restaurant from the dataset:
SELECT r.restaurant_name, r.id, r.cuisine, SUM(quantity) as item_quantity
FROM restaurants r
LEFT JOIN food_items fi ON r.id = fi.restaurant_id
LEFT JOIN orders_items o ON fi.item_id = o.item_id
GROUP BY r.id
ORDER BY item_quantity;

4. To find the number of restaurants that received no orders from the dataset:
SELECT r.restaurant_name, r.restaurant_id, r.cuisine, SUM(quantity) as
item_quantity
FROM restaurants r
LEFT JOIN food_items fi ON r.restaurant_id = fi.restaurant_id
LEFT JOIN orders_items o ON fi.item_id = o.item_id
GROUP BY r.restaurant_id
HAVING item_quantity IS NULL
ORDER BY item_quantity;

You might also like