Gt the list of products that have been purchased more than 10 times, along with the…
- User
Gt the list of products that have been purchased more than 10 times, along with the total quantity sold. drop table if exists products; CREATE TABLE products ( product_id INT, product_name VARCHAR(100) ); CREATE TABLE purchases ( purchase_id INT, product_id INT ); INSERT INTO products VALUES (1, 'Laptop'), (2, 'Smartphone'), (3, 'Headphones'), (4, 'Tablet'); INSERT INTO purchases VALUES (1, 1), (2, 1), (3, 2), (4, 1), (5, 3), (6, 1), (7, 4), (8, 1), (9, 1), (10, 1), (11, 1), (12, 2), (13, 3); answer pertaining to mysql
- Assistant
To get the list of products that have been purchased more than 10 times, along with the total quantity sold, you can execute the following SQL query:
sqlSELECT p.product_name, COUNT(*) AS total_quantity_sold FROM purchases pu JOIN products p ON pu.product_id = p.product_id GROUP BY p.product_id HAVING total_quantity_sold > 10;This query works as follows:
- It selects the product's name from the
productstable. - It counts the number of times each product appears in the
purchasestable, effectively counting how many times each product has been purchased. - It joins the
purchasestable with theproductstable on theproduct_idto get theproduct_name. - It groups the results by
product_idfrom theproductstable.
- It selects the product's name from the
- User
09:15:04 SELECT p.product_id,product_name, COUNT(*) AS total_quantity_sold FROM purchases pu JOIN products p ON pu.product_id = p.product_id GROUP BY p.product_id HAVING total_quantity_sold > 10 Error Code: 1055. Expression #2 of SELECT list is not in GROUP BY clause and contains nonaggregated column 'sql-practice.p.product_name' which is not functionally dependent on columns in GROUP BY clause; this is incompatible with sql_mode=only_full_group_by 0.000 sec
- Assistant
The error you encountered is due to MySQL's
ONLY_FULL_GROUP_BYSQL mode, which requires that all columns in theSELECTclause not aggregated be included in theGROUP BYclause unless they are functionally dependent on the grouped columns.To resolve this issue, you can adjust the
GROUP BYclause to include all non-aggregated columns like so:sqlSELECT p.product_id, p.product_name, COUNT(*) AS total_quantity_sold FROM purchases pu JOIN products p ON pu.product_id = p.product_id GROUP BY p.product_id, p.product_name HAVING total_quantity_sold > 10;This query should now comply with
ONLY_FULL_GROUP_BY. It groups by bothproduct_idandproduct_nameand includes them in theSELECTclause.However, given your current data, there are no products purchased more than 10 times, so while this query will execute without error, it will return no results. You may want to adjust your dataset to include more purchases if you wish to test and validate the query.
- User
Find the percentage contribution of MacBook Pro and iPhone to total revenue, rounded to two decimal places.
Dataset: DROP TABLE IF EXISTS products; CREATE TABLE products (product_id INT PRIMARY KEY, product_name VARCHAR(100), price DECIMAL(10, 2), quantity_sold INT); INSERT INTO products (product_id, product_name, price, quantity_sold) VALUES (1, 'iPhone', 899.00, 600), (2, 'iMac', 1299.00, 150), (3, 'MacBook Pro', 1499.00, 500), (4, 'AirPods', 499.00, 800), (5, 'Accessories', 199.00, 300);
- Assistant
To find the percentage contribution of the
MacBook ProandiPhoneto the total revenue, you can follow these steps:- First, calculate the revenue for each product by multiplying
pricebyquantity_sold. - Sum up the revenue of the
MacBook ProandiPhone. - Calculate the total revenue from all products.
- Calculate the percentage contribution of each product to the total revenue.
Here's the SQL query that performs these calculations and returns the results rounded to two decimal places:
sql - First, calculate the revenue for each product by multiplying