0% found this document useful (0 votes)
4 views

SQL CODES

Uploaded by

taiwobless123
Copyright
© © All Rights Reserved
Available Formats
Download as TXT, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
4 views

SQL CODES

Uploaded by

taiwobless123
Copyright
© © All Rights Reserved
Available Formats
Download as TXT, PDF, TXT or read online on Scribd
You are on page 1/ 3

SET search_path TO restaurant_orders;

CREATE TABLE restaurant_orders.menu_items(


menu_item_id INT,
Item_name TEXT,
Category TEXT,
Price FLOAT
);

CREATE TABLE restaurant_orders.order_details(


order_details_id INT,
order_id INT,
order_date DATE,
order_time TIME,
item_id TEXT
);
ALTER TABLE restaurant_orders.order_details
ALTER COLUMN item_id TYPE TEXT

ALTER TABLE restaurant_orders.menu_items


ALTER COLUMN menu_item_id TYPE TEXT

SELECT * FROM restaurant_orders.order_details


SELECT * FROM restaurant_orders.menu_items

--to see the orders generated by day


SELECT
ord.order_date,
COUNT(ord.order_id) AS totalorder
FROM
restaurant_orders.order_details AS ord
WHERE
ord.item_id IS NOT NULL
GROUP BY
ord.order_date
ORDER BY
totalorder DESC
LIMIT 1;

-- Category performance (Asian is the leading category)


SELECT
men.category,
COUNT(ord.order_id) AS total_order_by_category
FROM
restaurant_orders.order_details AS ord
RIGHT JOIN
restaurant_orders.menu_items AS men
ON
ord.item_id = men.menu_item_id
GROUP BY
men.category
ORDER BY
total_order_by_category DESC;

/* we also want to know item performance under the best performing


category, we are going to be making use of
CTE (Common Tables Expressions)*/
WITH Asian_item_performance AS (
SELECT
men.item_name,
COUNT(ord.order_id) AS total_order_by_Asian_item
FROM
restaurant_orders.menu_items AS men
JOIN
restaurant_orders.order_details AS ord
ON
men.menu_item_id = ord.item_id
WHERE
men.category = 'Asian'
GROUP BY
men.item_name
ORDER BY
total_order_by_Asian_item DESC
)
SELECT
*
FROM
Asian_item_performance
LIMIT 3;

--we need to know how our items are performing too


WITH item_performance AS (
SELECT
men.item_name,
COUNT(ord.order_id) AS total_order_by_item
FROM
restaurant_orders.menu_items AS men
JOIN
restaurant_orders.order_details AS ord
ON
men.menu_item_id = ord.item_id
GROUP BY
men.item_name
ORDER BY
total_order_by_item DESC
)
SELECT
*
FROM
item_performance;

--we need to look at the time that we have more sales


SELECT
ord.order_time,
COUNT(ord.order_id) AS total_order
FROM
restaurant_orders.order_details AS ord
GROUP BY
ord.order_time
ORDER BY
total_order DESC
LIMIT 20;

WITH order_time AS (
SELECT
ord.order_time,
COUNT(ord.order_id) AS total_orders,
CASE
WHEN ord.order_time >= '00:00:00' AND ord.order_time <= '11:59:00' THEN
'Morning'
WHEN ord.order_time >= '12:00:00' AND ord.order_time <= '17:00:00' THEN
'Afternoon'
ELSE 'Evening'
END AS time_of_day
FROM
restaurant_orders.order_details AS ord
WHERE ord.item_id IS NOT NULL
GROUP BY
ord.order_time
)
SELECT
time_of_day,
SUM(total_orders) AS total_orders
FROM order_time
GROUP BY time_of_day
ORDER BY total_orders DESC;

You might also like