Let's learn some new commands on sql and do some sample questions!
- Replace
- Substring
- Concat
- If
- Case
select restaurant_name "Original_name",
replace(restaurant_name, 'Blue', 'Pink') "Modified_name"
from food_orders
where restaurant_name like '%Blue Ribbon%'
select addr "Original Address",
substr(addr, 1, 2) "City"
from food_orders
where addr like '%서울특별시%'
select addr "Original Address",
concat('The ', substr(addr, 1, 2)) "City"
from food_orders
where addr like '%서울특별시%'
Syntax:
case when Condition1 then Result1
when Condition2 then Result2
else Result3
end as "title(optional)"
Create a colunm with restaurant name and its address according to the format below and get the total number of orders and the total number of quantity ordered from that restaurant
SELECT concat('[', substr(addr, 1,2), '] ', restaurant_name, ' (', cuisine_type ,')') "new_string",
count(*) "total nbr of orders", sum(quantity) "total nbr of items ordered"
FROM food_orders fo
group by restaurant_name

For each food order we want to calculate the delivery fee. The calculation for delivery fee has the following conditions:
select restaurant_name, order_id, delivery_time,
case when substr(addr, 1, 2)='서울' and delivery_time >30 then price*0.1*1.1
when substr(addr, 1, 2)='서울' and delivery_time >25 then price*0.05*1.1
when substr(addr, 1, 2)<>'서울' and delivery_time >30 then price*0.1
when substr(addr, 1, 2)<>'서울' and delivery_time >25 then price*0.05
else 0
end as 'delivery fee'
from food_orders fo
select restaurant_name,
order_id,
delivery_time,
price,
addr,
case when delivery_time>25 and delivery_time<=30 then price*0.05*(if(addr like '%서울%', 1.1, 1))
when delivery_time>30 then price*1.1*(if(addr like '%서울%', 1.1, 1))
else 0 end "수수료"
from food_orders
The delivery fee for weekend is 3500 KRW and for week days it is 3000 KRW. When the number of items order (quantity) is greater than 3, there is an additional 20% charge in delivery fee.
select restaurant_name, price, quantity, day_of_the_week,
case when day_of_the_week='Weekend' then 3500*(if (quantity>3,1.2,1))
else 3000*(if (quantity>3,1.2,1))
end as 'additional delivery fee'
from food_orders

select order_id,
price,
quantity,
day_of_the_week,
if(day_of_the_week='Weekday', 3000, 3500)*(if(quantity<=3, 1, 1.2)) "additional delivery fee"
from food_orders
