Working With Expressions - Grouping and Summarizing Data - 16.05.20
Working With Expressions - Grouping and Summarizing Data - 16.05.20
Please open FarmersMarketDatabase SQL file and run it. You can download it
from your lecturer’ avesis page.
Q1)
Get a list of the customer IDs of customers who made purchases on each
market date: and sort your result by first the market date and then by the
customer ID.
Sample Output
Q2)
Write an SQL code that extracts
number of purchases made by each
customer_id in each date.
A2)
USE `farmers_market`;
Q3)
Write an SQL code that extracts the sum of
quantities and number of different
purchases made by each customer_id in
each date.
A3)
USE `farmers_market`;
Q4)
Write an SQL code that extracts the price which
is calculated by multiplying quantity with
cost_to_customer_per_qty for the
customer_id=3.
A4)
USE `farmers_market`;
Q5)
Write an SQL code that extracts the total
money spent in each date for the
customer_id=3.
A5)
USE `farmers_market`;
Q6)
Write an SQL code that extracts the total
money spent in each vendor id for the
customer_id=3.
A6)
USE `farmers_market`;
Q7)
Write an SQL code that extracts the total
money spent by each customer.
A7)
USE `farmers_market`;
SELECT customer_id,
SUM(quantity * cost_to_customer_per_qty)
AS total_spent
FROM customer_purchases
GROUP BY customer_id
ORDER BY customer_id;
Q8)
Write an SQL code that extracts the customers
that spends more than 3000$.
A8)
USE `farmers_market`;
SELECT customer_id,
SUM(quantity * cost_to_customer_per_qty)
AS total_spent
FROM customer_purchases
GROUP BY customer_id
HAVING total_spent >3000
ORDER BY total_spent DESC;
Q9)
Write an SQL code that extracts the total
money spent in each vendor id for the
customer_id 3, 4 and 5.
Q10)
Write an SQL code that extracts the total
money spent in each vendor id for the
customer_id 1,2, 3, 4 and 5.
Q11)
Write an SQL code that extracts the vendor
inventory table sorted by original price in
increasing order.
A11)
USE `farmers_market`;
SELECT *FROM vendor_inventory
ORDER BY original_price
LIMIT 10;
Q12)
Write an SQL code that extracts the minimum
and the maximum price in the vendor inventory
table.
A12)
USE `farmers_market`;
SELECT MIN(original_price)
AS minimum_price,
MAX(original_price)
AS maximum_price
FROM vendor_inventory;
Q13)
Write an SQL code that extracts the product
count in each day.
Sort the results by date.
A13)
USE `farmers_market`;
SELECT market_date,
COUNT(product_id) AS product_count
FROM vendor_inventory
GROUP BY market_date
ORDER BY market_date;
Q14)
Write an SQL code that extracts number of
different products offered and average product
price for each vendor id between the dates
2019-07-03 and 2019-07-17.
SELECT vendor_id,
COUNT(DISTINCT product_id)
AS different_products_offered,
AVG(original_price)
AS average_product_price
FROM vendor_inventory
WHERE market_date
BETWEEN '2019-07-03' AND '2019-07-17’
GROUP BY vendor_id
ORDER BY vendor_id;
Q15)
Write an SQL code that extracts
• number of different products offered,
• value of inventory which is defined as sum of quantity times original price,
• inventory item count which is defined as sum of quantities,
• average item price which is defined as value of inventory divided by inventory item count, and
• average product price
for each vendor id between the dates 2019-07-03 and 2019-07-17.
Q17)
Write an SQL code that finds minimum and
maximum sale date for each vendor id.
A17)
USE `farmers_market`;
SELECT vendor_id,
min(market_date),
max(market_date)
FROM vendor_inventory
GROUP BY vendor_id
ORDER BY min(market_date), max(market_date);
Q18)
Write an SQL code that finds number of vendors with
inventory in each month of the database.
Q19)
Write an SQL code that finds how many
products are there inside each category.
A19)
USE `farmers_market`;
USE `farmers_market`;
A19)
USE `farmers_market`;
SELECT pc.product_category_id,
pc.product_category_name,
count(product_id) AS count_of_products
FROM product_category AS pc
LEFT JOIN product AS p
ON pc.product_category_id = p.product_category_id
GROUP BY pc.product_category_id;
A19)
USE `farmers_market`;
SELECT pc.product_category_id,
pc.product_category_name,
count(product_id) AS count_of_products
FROM product_category AS pc
INNER JOIN product AS p
ON pc.product_category_id = p.product_category_id
GROUP BY pc.product_category_id;
A19)
USE `farmers_market`;
SELECT pc.product_category_id,
pc.product_category_name,
count(product_id) AS count_of_products
FROM product_category AS pc
RIGHT JOIN product AS p
ON pc.product_category_id = p.product_category_id
GROUP BY pc.product_category_id;
Q20)
Write an SQL code that finds product id,
product name, minimum price and
maximum price for each product inside the
vendor inventory table.
A20)
USE `farmers_market`;
A20)
USE `farmers_market`;
A20)
USE `farmers_market`;
price is defined as
quantity*cost_to_customer_per_qty
and average_spend_by_customer is defined as
average price.
SELECT c.customer_id,
c.customer_first_name,
c.customer_last_name,
AVG(cp.quantity*cp.cost_to_customer_per_qty) AS
average_spend_by_customer
FROM customer AS c
LEFT JOIN customer_purchases AS cp
ON c.customer_id = cp.customer_id
GROUP BY c.customer_id, c.customer_first_name,
c.customer_last_name;
price is defined as
quantity*cost_to_customer_per_qty
and average_spend_by_customer is defined as
average price.
SELECT c.customer_id,
c.customer_first_name,
c.customer_last_name,
AVG(cp.quantity*cp.cost_to_customer_per_qty) AS
average_spend_by_customer
FROM customer_purchases AS cp
RIGHT JOIN customer AS c
ON cp.customer_id = c.customer_id
GROUP BY c.customer_id, c.customer_first_name,
c.customer_last_name;
USE `farmers_market`;
SELECT market_date,
ROUND(SUM(quantity * cost_to_customer_per_qty),2)
AS total_sales
FROM customer_purchases
GROUP BY market_date
ORDER BY market_date;
SELECT MIN(cp.market_date)
AS first_market_date_of_week,
ROUND(SUM(cp.quantity * cp.cost_to_customer_per_qty),2)
AS weekly_sales
FROM farmers_market.customer_purchases AS cp
LEFT JOIN market_date_info AS md
ON cp.market_date = md.market_date
GROUP BY md.market_year, md.market_week
ORDER BY md.market_year, md.market_week;