Queries Solved 24june
Queries Solved 24june
SELECT
*
FROM
farmers_market.product;
#select the product IDs and their corresponding product names & sizes from the
product table.
SELECT
product_id,
product_name,
product_size
FROM
farmers_market.product;
#If we want to sort this entire data in descending order of a particular column, in
this case by product_id?
SELECT
product_id,
product_name,
product_size
FROM
farmers_market.product
ORDER BY
product_id DESC;
SELECT
product_id,
product_name,
product_size
FROM
farmers_market.product
ORDER BY
product_id ASC;
#Question: How to sort the purchases by market date (most recent) and transaction
time (most recent) both?
SELECT
*
FROM
farmers_market.customer_purchases
ORDER BY
market_date DESC,
transaction_time DESC;
SELECT
*
FROM
farmers_market.customer_purchases
ORDER BY
market_date DESC,
transaction_time;
SELECT
*
FROM
farmers_market.customer_purchases
ORDER BY
transaction_time,
market_date DESC;
#Question: Your manager is only interested in looking at the 10 most recent
transactions in the customer_purchases table.
SELECT
*
FROM
farmers_market.customer_purchases
ORDER BY
market_date DESC,
transaction_time DESC
LIMIT
10;
#Question: What if the manager asks you to fetch the 2nd & 3rd most recent purchase
(skip the 1st one)?
SELECT
*
FROM
farmers_market.customer_purchases
ORDER BY
market_date DESC,
transaction_time DESC
LIMIT
2
OFFSET
1;
#Question: What if the manager asks you to fetch the 8,9,10,11th most recent purchase
(skip the 7 lines)?
SELECT
*
FROM
farmers_market.customer_purchases
ORDER BY
market_date DESC,
transaction_time DESC
LIMIT
4
OFFSET
7; #14,1,1,10
#Question: 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 amount?
SELECT
market_date,
customer_id AS c_id,
vendor_id,
quantity,
cost_to_customer_per_qty,
ROUND(quantity * cost_to_customer_per_qty,2) AS total_amount #inline calculations
FROM
farmers_market.customer_purchases;
#case sensitive
SELECT
*
FROM
farmers_market.product;
#####DCS
SELECT
*
FROM
farmers_market.product
WHERE
product_id = 5
OR product_id = 8;