+-----------------------------+---------+ | Column Name | Type | +-----------------------------+---------+ | delivery_id | int | | customer_id | int | | order_date | date | | customer_pref_delivery_date | date | +-----------------------------+---------+ delivery_id is the primary key of this table. The table holds information about food delivery to customers that make orders at some date and specify a preferred delivery date (on the same order date or after it).
If the preferred delivery date of the customer is the same as the order date then the order is called immediate otherwise it's called scheduled.
Write an SQL query to find the percentage of immediate orders in the table, rounded to 2 decimal places.
The query result format is in the following example:
Result table: +----------------------+ | immediate_percentage | +----------------------+ | 33.33 | +----------------------+ The orders with delivery id 2 and 3 are immediate while the others are scheduled.
Solutions
straight forward
# Write your MySQL query statement belowSELECTROUND(COUNT(*) / (SELECTCOUNT(*) FROM Delivery), 4) *100as immediate_percentageFROM DeliveryWHERE order_date = customer_pref_delivery_date
SUM
# Write your MySQL query statement belowSELECTROUND(SUM(order_date=customer_pref_delivery_date) /COUNT(*), 4) *100as immediate_percentageFROM Delivery