Write a SQL query to select the product id, year, quantity, and price for the first year…
- User
Write a SQL query to select the product id, year, quantity, and price for the first year of every product sold.
Dataset: DROP TABLE IF EXISTS Sales; CREATE TABLE Sales ( sale_id INT, product_id INT, year INT, quantity INT, price INT ); INSERT INTO Sales (sale_id, product_id, year, quantity, price) VALUES (1, 100, 2008, 10, 5000), (2, 100, 2009, 12, 5000), (7, 200, 2011, 15, 9000), (3, 100, 2010, 8, 5500), (4, 100, 2011, 15, 4800), (5, 200, 2013, 20, 8500), (6, 200, 2006, 18, 9200), (8, 300, 2012, 25, 7500), (9, 300, 2009, 30, 7200), (10, 300, 2013, 22, 6800); Use the RANK() function to identify the first year for each product.
- Assistant
To find the first year of sales for each product in the given dataset using the
RANK()function, you can use the following SQL query. This query does the following:- Selects the relevant columns along with the first year of sales for each product.
- Uses the
RANK()function to rank the years for every product. - Filters the result to get only the first year for each product.
Here's the SQL query:
WITH RankedSales AS ( SELECT product_id, year, quantity, price, RANK() OVER (PARTITION BY product_id ORDER BY year) AS year_rank FROM Sales ) SELECT product_id, year, quantity, price FROM RankedSales WHERE year_rank = 1;Explanation:
- The
WITHclause creates a Common Table Expression (CTE) namedRankedSales. - Inside the CTE, we use
RANK()withPARTITION BY product_idto partition the data byproduct_idand order by . This gives a ranking of years for each product.
- User
Identify the products with sales above the average for their category.
Dataset: CREATE TABLE products ( product_id INT, product_name VARCHAR(100), category_name VARCHAR(100) ); CREATE TABLE sales ( sale_id INT, product_id INT, sales_amount DECIMAL(10, 2) ); INSERT INTO products VALUES (1, 'Laptop', 'Electronics'), (2, 'Smartphone', 'Electronics'), (3, 'Tablet', 'Electronics'), (4, 'Office Chair', 'Furniture'); INSERT INTO sales VALUES (1, 1, 50000.00), (2, 2, 30000.00), (3, 1, 70000.00), (4, 4, 20000.00); 💡 Hint: Find products exceeding average category sales.
- Assistant
To identify products with sales above the average for their category, we need to follow these steps:
- Calculate the average sales amount for each category from the
salestable by joining it with theproductstable. - Compare each product's total sales with the average sales of their respective category.
- Calculate the average sales amount for each category from the
- User
List all students who have participated in both Hackathon A and Hackathon B in the past year.
Dataset: CREATE TABLE hackathon_a ( student_id INT, student_name VARCHAR(100), participation_date DATE ); CREATE TABLE hackathon_b ( student_id INT, student_name VARCHAR(100), participation_date DATE ); INSERT INTO hackathon_a VALUES (1, 'Anil Sharma', '2023-04-01'), (2, 'Rita Verma', '2023-05-10'), (3, 'Amit Singh', '2023-08-15'); INSERT INTO hackathon_b VALUES (1, 'Anil Sharma', '2023-06-20'), (2, 'Rita Verma', '2023-07-05'), (3, 'Rahul Roy', '2023-09-25');
- Assistant
To list all students who have participated in both Hackathon A and Hackathon B in the past year, you can use an SQL query that performs an inner join between the two tables. The join should be based on
student_id, as this is the common identifier. Here's how you can write the SQL query:SELECT a.student_id, a.student_name FROM hackathon_a a JOIN hackathon_b b ON a.student_id = b.student_id WHERE a.participation_date >= DATE_SUB(CURDATE(), INTERVAL 1 YEAR) AND b.participation_date >= DATE_SUB(CURDATE(), INTERVAL 1 YEAR);Explanation:
-
Selecting Columns: The query selects the
student_idandstudent_namefrom Hackathon A ().
-