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

Assignment:

Overview:
There are 4 CSVs present in the folder which contain a representative
data of 4 different tables in PSQL.
1. `category_id` in the `listingdata` table is ForeignKey to
`category` table on the `id` column
2. `brand_id` in the `listingdata` table is ForeignKey to `brands`
table on the `id` column
3. `listing_id` in the `listinghistory` table is ForeignKey to
`listingdata` table on the `id` column

Problems:

1. Write a PSQL query to identify all the unique categories sold by


any specific brand name. (You can assume the brand name to
be `JOCKEY`)
2. Write a PSQL query to find the average prices(selling_price) of
each category sold by any specific brand name. (You can
assume the brand name to be `JOCKEY`)
3. Write a PSQL query to find the average promotion value for
each category sold by any specific brand name in the month of
May. (You can assume the brand name to be `JOCKEY`)
Ans 1:
SELECT DISTINCT c.name AS category_name
FROM listingdata ld
JOIN brands b ON ld.brand_id = b.id
JOIN category c ON ld.category_id = c.id
WHERE b.name = 'JOCKEY';

Ans 2:
SELECT c.name, AVG(lh.selling_price) AS avg_price
FROM brands b
INNER JOIN listingdata ld ON b.id = ld.brand_id
INNER JOIN category c ON ld.category_id = c.id
INNER JOIN listinghistory lh ON lh.listing_id = ld.id
WHERE b.name = 'JOCKEY'
GROUP BY c.name;

Ans 3:

SELECT c.name, AVG(lh.selling_price - lh.original_price) AS avg_promotion


FROM brands b
INNER JOIN listingdata ld ON b.id = ld.brand_id
INNER JOIN category c ON ld.category_id = c.id
INNER JOIN listinghistory lh ON lh.listing_id = ld.id
WHERE b.name = 'JOCKEY'
AND EXTRACT(MONTH FROM lh.sold_date) = 5
GROUP BY c.name;

You might also like