0% found this document useful (0 votes)
4 views

sql-queries-used-in-class

Uploaded by

shishuranjan
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
4 views

sql-queries-used-in-class

Uploaded by

shishuranjan
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 4

#Questions: Your manager wants to see all the unique customer IDs present in the

customer_purchases table. How would you get this data?

select distinct(customer_id) from farmers_market.customer_purchases


order by customer_id;

select distinct customer_id,market_date from farmers_market.customer_purchases


order by customer_id;

#Question: Find all of the products from the product table which don’t have their
sizes mentioned.
select * from farmers_market.product
where product_size is null;

select *,ifnull(product_size,"Not Available") as product_size


from farmers_market.product;

select *, ifnull(product_size,"NA") as new_size from farmers_market.product;

select * from farmers_market.product


where product_size = ' ';

#final query
select * from farmers_market.product
where product_size is null
or trim(product_size) = '';

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

select
product_id,
market_date,
quantity,
cost_to_customer_per_qty,
ROUND(quantity * cost_to_customer_per_qty,2) as tot_amount
from farmers_market.customer_purchases
where market_date IN
(
select market_date from farmers_market.market_date_info
where market_rain_flag = 1
);

#Question: 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%'
)
order by product_id;

#quiz 1
#Select columns from DB.table1 where col1 in (select col1, col2 from DB.table2);
#Select columns from DB.table1 where col1 = (select col1 from DB.table2 limit 1);
#Select * from DB.tables3 where col1 in (select col1 from DB.table1) and col2 in
(select col2 from DB.table2)

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

select
vendor_id,
vendor_name,
vendor_type,
if(lower(vendor_type) like '%fresh%','Freshly Produced','Not Freshly Produced') as
vendor_category
from farmers_market.vendor;
#with case
select
vendor_id,
vendor_name,
vendor_type,
CASE
WHEN lower(vendor_type) like '%fresh%' THEN 'Freshly Produce'
WHEN lower(vendor_type) like '%meat%' THEN 'Non Veg Stuff'
ELSE 'Not Fresh Produce'
END as vendor_category
from farmers_market.vendor;

##DCS
select *,
ifnull(product_size,"Not Available") as product_size,
ifnull(product_qty_type,"Not Available") as product_typr
from farmers_market.product;

You might also like