MY SQL Queries
MY SQL Queries
FROM coffee_shop_sales
MONTH(transaction_date) AS month,
FROM
coffee_shop_sales
WHERE
GROUP BY
MONTH(transaction_date)
ORDER BY
MONTH(transaction_date);
Explaination
SELECT clause:
FROM coffee_shop_sales
MONTH(transaction_date) AS month,
ROUND(COUNT(transaction_id)) AS total_orders,
(COUNT(transaction_id) - LAG(COUNT(transaction_id), 1)
FROM
coffee_shop_sales
WHERE
GROUP BY
MONTH(transaction_date)
ORDER BY
MONTH(transaction_date);
TOTAL QUANTITY SOLD
SELECT SUM(transaction_qty) as Total_Quantity_Sold
FROM coffee_shop_sales
MONTH(transaction_date) AS month,
ROUND(SUM(transaction_qty)) AS total_quantity_sold,
(SUM(transaction_qty) - LAG(SUM(transaction_qty), 1)
FROM
coffee_shop_sales
WHERE
GROUP BY
MONTH(transaction_date)
ORDER BY
MONTH(transaction_date);
CALENDAR TABLE – DAILY SALES, QUANTITY and TOTAL ORDERS
SELECT
SUM(transaction_qty) AS total_quantity_sold,
COUNT(transaction_id) AS total_orders
FROM
coffee_shop_sales
WHERE
If you want to get exact Rounded off values then use below query to get the result:
SELECT
FROM
coffee_shop_sales
WHERE
FROM (
SELECT
FROM
coffee_shop_sales
WHERE
GROUP BY
transaction_date
) AS internal_query;
Query Explanation:
This inner subquery calculates the total sales (unit_price * transaction_qty) for each date in
May. It filters the data to include only transactions that occurred in May by using the
MONTH() function to extract the month from the transaction_date column and filtering for
May (month number 5).
The GROUP BY clause groups the data by transaction_date, ensuring that the total sales are
aggregated for each individual date in May.
The outer query calculates the average of the total sales over all dates in May. It references
the result of the inner subquery as a derived table named internal_query.
The AVG() function calculates the average of the total_sales column from the derived table,
giving us the average sales for May.
DAY(transaction_date) AS day_of_month,
FROM
coffee_shop_sales
WHERE
GROUP BY
DAY(transaction_date)
ORDER BY
DAY(transaction_date);
COMPARING DAILY SALES WITH AVERAGE SALES – IF GREATER THAN “ABOVE AVERAGE” and
LESSER THAN “BELOW AVERAGE”
SELECT
day_of_month,
CASE
ELSE 'Average'
END AS sales_status,
total_sales
FROM (
SELECT
DAY(transaction_date) AS day_of_month,
SUM(unit_price * transaction_qty) AS total_sales,
FROM
coffee_shop_sales
WHERE
GROUP BY
DAY(transaction_date)
) AS sales_data
ORDER BY
day_of_month;
SALES BY WEEKDAY / WEEKEND:
SELECT
CASE
ELSE 'Weekdays'
END AS day_type,
FROM
coffee_shop_sales
WHERE
GROUP BY
CASE
ELSE 'Weekdays'
END;
SALES BY STORE LOCATION
SELECT
store_location,
FROM coffee_shop_sales
WHERE
MONTH(transaction_date) =5
GROUP BY store_location
product_category,
FROM coffee_shop_sales
WHERE
MONTH(transaction_date) = 5
GROUP BY product_category
product_type,
FROM coffee_shop_sales
WHERE
MONTH(transaction_date) = 5
GROUP BY product_type
LIMIT 10
SALES BY DAY | HOUR
SELECT
SUM(transaction_qty) AS Total_Quantity,
COUNT(*) AS Total_Orders
FROM
coffee_shop_sales
WHERE
CASE
ELSE 'Sunday'
END AS Day_of_Week,
FROM
coffee_shop_sales
WHERE
CASE
ELSE 'Sunday'
END;
HOUR(transaction_time) AS Hour_of_Day,
FROM
coffee_shop_sales
WHERE
GROUP BY
HOUR(transaction_time)
ORDER BY
HOUR(transaction_time);