Location via proxy:   [ UP ]  
[Report a bug]   [Manage cookies]                

SQL Q&A

Download as docx, pdf, or txt
Download as docx, pdf, or txt
You are on page 1of 5

#Question1: In the customer purchases, we have quantity and cost per qty separate, query the

total amount that the customer has paid along with date, customer id, vendor_id, qty, cost per qty
and the total amt.?
select market_date, customer_id, vendor_id, quantity,
cost_to_customer_per_qty,round((quantity*cost_to_customer_per_qty),2) as Total_amt from
`farmers_market.customer_purchases`
order by Total_amt desc;

#Question2: We want to merge each customer’s name into a single column that contains the first
name, then a space, and then the last name.

select concat(customer_first_name, ' ', customer_last_name) as customer_full_name from


`farmers_market.customer`;

#Question3: In a customer’s full_name, we want the first_name to be in upper case followed by the
last_name as it is.
select concat(Upper(customer_first_name), ' ', customer_last_name) as customer_full_name from
`farmers_market.customer`;

#Question4: Extract all the product names that are part of product category 1
select * from `farmers_market.product`
where product_category_id = 1;

#Question5: Print a report of everything the customer_id 4 has ever purchased at the market,
sorted by date. Add total_amt column as well for each purchase.

select *,(quantity*cost_to_customer_per_qty) as total_amt from


`farmers_market.customer_purchases`
where customer_id = 4
order by market_date;

#Question6: Get all the product info for products with id between 3 and 8 (not inclusive) and of
products with id 10.

select * from `farmers_market.product`


where product_id > 3 and product_id < 8 or product_id = 10;

#Question7: Find the booth assignments for vendor_id 7 for all dates between April 3, 2019 and
May 16, 2019, including the 2 dates.

select * from `farmers_market.vendor_booth_assignments`


where vendor_id = 7 and market_date between '2019-04-03' and '2019-05-16';

#Question8: Return a list of customers with the following last names: [Diaz,Edwards, Wilson]

select * from `farmers_market.customer`


where lower(customer_last_name) in ('diaz', 'edwards', 'wilson');

#Jane Connor --> jANE cONNOR


select concat(lower(substring(customer_first_name,1,1)), upper(substring(customer_first_name,2)),
' ',
lower(substring(customer_last_name,1,1)), upper(substring(customer_last_name,2))) from
`farmers_market.customer`;

#Question9: We want to merge each customer’s name into a single column that contains the first
name, then a space, and then the last name. In the last name, just display the first character of the
last name. Write the first name part in the capitalized format. Put the last name part in capital.

select concat(upper(substring(customer_first_name,1,1)), lower(substring(customer_first_name,2)),


' ', substring(customer_last_name,1)) from `farmers_market.customer`;

#Question10: You want to get data about a customer you knew as Jerry but you are not sure if they
are listed as Jeremy or Jeremiah or Jerry. Get all customers whose name has “er” in between their
first names.

select * from `farmers_market.customer`


where lower(customer_first_name) like '%er%';

#Question11: Find all of the products from the product table which don’t have sizes mentioned.

select * from `farmers_market.product`


where product_size is null or trim(product_size) = ''
order by product_size;

#Question12: Analyze purchases made at the market on days when it rained.

select * from `farmers_market.customer_purchases`


where market_date in (select market_date from `farmers_market.market_date_info`
where market_rain_flag = 1);

#Question13: List down all the product details where product_category_name contains “Fresh” in
it.

select * from `farmers_market.product`


where product_category_id in (select product_category_id from `farmers_market.product_category`
where lower(product_category_name) like '%fresh%');

#Question14: Find out which vendors primarily sell fresh products and which don’t.

select *, case when (lower(vendor_type) like '%fresh%') then '1' else '0' end as fresh_flag from
`farmers_market.vendor`;

#Question15: Put the total cost to customer purchases into bins of -under $5.00,$5.00–
$9.99,$10.00–$19.99, or $20.00 and over.
select *,
case
when(total_cost < 5) then 'under5'
when(total_cost between 5 and 9.99) then 'under10'
when(total_cost between 10 and 19.99) then 'under20'
else 'over20'
end as bins
from (select *,round(quantity*cost_to_customer_per_qty,2) as total_cost from
`farmers_market.customer_purchases`);

#Question16: Get a list of customers' zip codes for customers who made a purchase on 2019-04-
06.

select distinct(t1.customer_id), t1.customer_zip from `farmers_market.customer` as t1 inner join


`farmers_market.customer_purchases` as t2 on t1.customer_id = t2.customer_id
where t2.market_date = '2019-04-06'
order by t1.customer_id;

#Question17: List all the products along with their product category name.

select t1.product_name, t2.product_category_name from `farmers_market.product` as t1 left join


`farmers_market.product_category` as t2 on t1.product_category_id = t2.product_category_id;

#Question18: Find out the customers who are new to the market.--> the customers who don't have
any purchase history::

select * from `farmers_market.customer` as t1 left join `farmers_market.customer_purchases` as


t2 on t1.customer_id = t2.customer_id
where t2.customer_id is null;

#Question19: Find out the customers who are either new to the market or have deleted their
account from the market.

select * from `farmers_market.customer` as t1 full outer join


`farmers_market.customer_purchases` as t2 on t1.customer_id = t2.customer_id
where t2.customer_id is null or t1.customer_id is null

#Question20 :: Recommend new countries to the users which they have not visited yet

select u.user, c.travel_to from `Non_Equi_Joins.Country` as c join `Non_Equi_Joins.User` as u on


c.travel_to != u.country_visited
order by u.user;

#Question21 :: Correlated Subquery:: find all employees with a salary higher than their average
departmental salary.

select e1.employee_id from `employee.employees` as e1


where e1.salary > (select avg(salary) from `employee.employees` as e2 where e1.department_id
= e2.department_id)
#Question22 :: Count the number of purchases each customer made per market date.

select customer_id,market_date,count(*) as no_of_purchases from


`farmers_market.customer_purchases`
group by customer_id, market_date
order by customer_id;

#Question23 :: Calculate total purchases ever made for every customer

select customer_id, count(*) as tot_purchases from `farmers_market.customer_purchases`


group by customer_id;

#Question 24 :: Calculate the total quantity purchased by each customer per market_date.

select customer_id, market_date, round(sum(quantity),2) as tot_quantity from


`farmers_market.customer_purchases`
group by customer_id, market_date;

#Question 25 :: Calculate the total quantity purchased by each customer per market_date and sort
the output for every market date based on the quantity values (higher quantity value comes first)

select customer_id, market_date, round(sum(quantity),2) as tot_quantity from


`farmers_market.customer_purchases`
group by customer_id, market_date
order by market_date, tot_quantity desc;

#Question 26 :: How many different kinds of products were purchased by each customer on each
market date?

select customer_id, market_date, count(distinct(product_id)) as kind_of_products from


`farmers_market.customer_purchases`
group by customer_id, market_date

#Question 27 :: Calculate the total price paid by customer_id 3 per market_date.

select market_date, round(sum(quantity*cost_to_customer_per_qty),2) as tot_price from


`farmers_market.customer_purchases`
where customer_id = 3
group by market_date
#Question 28 :: Calculate the total spend by customer_id 3 per market_date but only those records
where total spend is more than 12$.

select market_date, round(sum(quantity*cost_to_customer_per_qty),2) as tot_price from


`farmers_market.customer_purchases`
where customer_id = 3
group by market_date
having tot_price >= 12;
#Question 29:: Question: We want to get the most and least expensive items per product category,
considering the fact that each vendor sets their own prices and can adjust prices per customer.

You might also like