0% found this document useful (0 votes)
45 views3 pages

New - SQL Qustions

This document contains the SQL code to create tables and insert sample data for a pizza delivery database. The tables created include customers, orders, runners, pizzas, and toppings. Several queries are then written to analyze the data, such as counting total and unique orders, delivered orders by runner, orders by pizza type, orders by customer, and the maximum number of items in an order.

Uploaded by

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

New - SQL Qustions

This document contains the SQL code to create tables and insert sample data for a pizza delivery database. The tables created include customers, orders, runners, pizzas, and toppings. Several queries are then written to analyze the data, such as counting total and unique orders, delivered orders by runner, orders by pizza type, orders by customer, and the maximum number of items in an order.

Uploaded by

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

NEW--SQL QUSTIONS

CREATE SCHEMA pizza_runner;


SET search_path = pizza_runner;

DROP TABLE IF EXISTS runners;


CREATE TABLE runners (
"runner_id" INTEGER,
"registration_date" DATE
);
INSERT INTO runners
("runner_id", "registration_date")
VALUES
(1, '2021-01-01'),
(2, '2021-01-03'),
(3, '2021-01-08'),
(4, '2021-01-15');

DROP TABLE IF EXISTS customer_orders;


CREATE TABLE customer_orders (
"order_id" INTEGER,
"customer_id" INTEGER,
"pizza_id" INTEGER,
"exclusions" VARCHAR(4),
"extras" VARCHAR(4),
"order_time" TIMESTAMP
);

INSERT INTO customer_orders


("order_id", "customer_id", "pizza_id", "exclusions", "extras", "order_time")
VALUES
('1', '101', '1', '', '', '2020-01-01 18:05:02'),
('2', '101', '1', '', '', '2020-01-01 19:00:52'),
('3', '102', '1', '', '', '2020-01-02 23:51:23'),
('3', '102', '2', '', NULL, '2020-01-02 23:51:23'),
('4', '103', '1', '4', '', '2020-01-04 13:23:46'),
('4', '103', '1', '4', '', '2020-01-04 13:23:46'),
('4', '103', '2', '4', '', '2020-01-04 13:23:46'),
('5', '104', '1', 'null', '1', '2020-01-08 21:00:29'),
('6', '101', '2', 'null', 'null', '2020-01-08 21:03:13'),
('7', '105', '2', 'null', '1', '2020-01-08 21:20:29'),
('8', '102', '1', 'null', 'null', '2020-01-09 23:54:33'),
('9', '103', '1', '4', '1, 5', '2020-01-10 11:22:59'),
('10', '104', '1', 'null', 'null', '2020-01-11 18:34:49'),
('10', '104', '1', '2, 6', '1, 4', '2020-01-11 18:34:49');

DROP TABLE IF EXISTS runner_orders;


CREATE TABLE runner_orders (
"order_id" INTEGER,
"runner_id" INTEGER,
"pickup_time" VARCHAR(19),
"distance" VARCHAR(7),
"duration" VARCHAR(10),
"cancellation" VARCHAR(23)
);
INSERT INTO runner_orders
("order_id", "runner_id", "pickup_time", "distance", "duration", "cancellation")
VALUES
('1', '1', '2020-01-01 18:15:34', '20km', '32 minutes', ''),
('2', '1', '2020-01-01 19:10:54', '20km', '27 minutes', ''),
('3', '1', '2020-01-03 00:12:37', '13.4km', '20 mins', NULL),
('4', '2', '2020-01-04 13:53:03', '23.4', '40', NULL),
('5', '3', '2020-01-08 21:10:57', '10', '15', NULL),
('6', '3', 'null', 'null', 'null', 'Restaurant Cancellation'),
('7', '2', '2020-01-08 21:30:45', '25km', '25mins', 'null'),
('8', '2', '2020-01-10 00:15:02', '23.4 km', '15 minute', 'null'),
('9', '2', 'null', 'null', 'null', 'Customer Cancellation'),
('10', '1', '2020-01-11 18:50:20', '10km', '10minutes', 'null');

DROP TABLE IF EXISTS pizza_names;


CREATE TABLE pizza_names (
"pizza_id" INTEGER,
"pizza_name" TEXT
);
INSERT INTO pizza_names
("pizza_id", "pizza_name")
VALUES
(1, 'Meatlovers'),
(2, 'Vegetarian');

DROP TABLE IF EXISTS pizza_recipes;


CREATE TABLE pizza_recipes (
"pizza_id" INTEGER,
"toppings" TEXT
);
INSERT INTO pizza_recipes
("pizza_id", "toppings")
VALUES
(1, '1, 2, 3, 4, 5, 6, 8, 10'),
(2, '4, 6, 7, 9, 11, 12');

DROP TABLE IF EXISTS pizza_toppings;


CREATE TABLE pizza_toppings (
"topping_id" INTEGER,
"topping_name" TEXT
);
INSERT INTO pizza_toppings
("topping_id", "topping_name")
VALUES
(1, 'Bacon'),
(2, 'BBQ Sauce'),
(3, 'Beef'),
(4, 'Cheese'),
(5, 'Chicken'),
(6, 'Mushrooms'),
(7, 'Onions'),
(8, 'Pepperoni'),
(9, 'Peppers'),
(10, 'Salami'),
(11, 'Tomatoes'),
(12, 'Tomato Sauce');
select * from customer_orders
select * from runner_orders
select * FROM pizza_names
select * FROM runners
select * FROM pizza_recipes
select * FROM pizza_toppings

--Q1--
select count(order_id) as total_no_of_order from customer_orders
--Q2--
select count (DISTINCT customer_id) as unique_no_of_order from customer_orders
--q3--
select count(order_id) as order_deleiverd from runner_orders
where duration !='null'

select * from customer_orders as c


join runner_orders as r
on c.order_id = r.order_id
where r.duration !='null'

select count(order_id) as order_delivered from runner_orders


where cancellation not like

--q4--

select p.pizza_name,count(p.pizza_name) as count_of_pizza_type from customer_orders


as c
join runner_orders as r
on c.order_id = r.order_id
join pizza_names as p
on c.pizza_id = p.pizza_id
where r.duration !='null'
group by p.pizza_name

--q5--
select customer_id,count(*) from customer_orders
group by customer_id

select c.customer_id,r.pizza_name,count(*) total_pizza_type_count from


customer_orders as c
join pizza_names as r
on c.pizza_id=r.pizza_id
group by customer_id,r.pizza_name
order by c.customer_id

--q6--

select c.order_id,count(*) as max_order from customer_orders as c


join runner_orders as r
on c.order_id = r.order_id
where r.duration !='null'
group by c.order_id
order by max_order desc
limit 1

You might also like