SQL 1
SQL 1
Explanation:
o CASE is used to categorize orders into 'Weekday' or 'Weekend' based on the day of the
week.
o DAYOFWEEK extracts the day number from the order's purchase date.
o Days 2 to 6 are considered weekdays (Monday to Friday), while others are weekends.
o COUNT(order_id) counts the total orders for each category.
o GROUP BY groups the results by 'Weekday' or 'Weekend' to display the total orders for
each.
Average Payment Value: Weekday vs. Weekend
Purpose: Analyzing the average spending amount helps identify if there is a di erence in the
payment value during weekends versus weekdays. This insight allows businesses to focus on
driving higher-value transactions during slower periods.
Explanation:
o Similar CASE logic is used to categorize payments as 'Weekday' or 'Weekend'.
o AVG(payment_value) calculates the average payment amount for each group.
o JOIN is used to connect the orders with payment details.
o GROUP BY ensures the calculation is done separately for weekday and weekend
payments.
KPI 2: Total Number of Orders with Review Score 5 and Payment Type as Credit Card
Purpose: : This query focuses on identifying highly satisfied customers who use credit cards
for their purchases. Understanding this helps businesses understand which payment methods
are correlated with high satisfaction, potentially guiding loyalty programs or targeted
incentives.
Explanation:
o JOIN statements link the reviews, orders, and payments tables to access all necessary
information.
o WHERE clause filters for orders with a review score of 5 and payment type as
'credit_card'.
o COUNT(DISTINCT o.order_id) counts each unique order that meets the criteria,
ensuring no duplicate orders are counted.
Explanation:
o YEAR(...) extracts the year from the order approval date.
o COUNT(order_id) counts the number of orders approved each year.
o WHERE filters out any orders where the approval date is missing.
o GROUP BY Year groups counts by each year.
o ORDER BY Year sorts the results chronologically.
Explanation:
o JOIN clauses: Links order items, reviews, and products datasets.
o WHERE clause: Filters to include only 5-star reviews.
o COUNT function: Counts the number of 5-star reviews per product category.
o ORDER BY and LIMIT: Orders the results by the count and limits it to the top 5.