SQL select query, is this a correct solution? - mysql

I have been practicing SQL online and I am having doubt in one exercise. In the following database:
product (prod id, product name, product price, product manufacturer)
customer (cust id, customer name, customer address)
rating (prod id, cust id, rating date, rating stars)
sale (sale id, prod id, cust id, sale quantity, sale cost)
"For all cases in which the same customer rated the same product
more than once, and in some point in time gave it a lower rating
than before, return the customer name, the name of the product,
and the lowest star rating that was given."
I am not entirely sure about the answer, this is what I got:
SELECT customer_name,
product_name,
min(rating_stars)
FROM customer,
product,
rating
WHERE rating_stars < SOME
(SELECT rating_stars
FROM customer,
product,
rating)
GROUP BY customer,
product,
rating
Could any of you have a check in the code and see if it is correct?

Related

How to get pin codes without zeros in it?

Write a query to display the customer_id,customer full name ,city,pincode,and order details (order id, product class desc, product desc, subtotal(product_quantity * product_price)) for orders shipped to cities whose pin codes do not have any 0s in them. Sort the output on customer name and subtotal. (52 ROWS) [NOTE: TABLE TO BE USED - online_customer, address, order_header, order_items, product, product_class].
These are the attributes of each table:
Address- ADDRESS_ID, ADDRESS_LINE1,ADDRESS_LINE2,CITY,STATE,PINCODE,COUNTRY
ONLINE_CUSTOMER- CUSTOMER_ID, CUSTOMER_FNAME, CUSTMER_LNAME, CUSTOMER_EMAIL,CUSTOMER_PHONE, ADDRESS_ID, CUSTOMER_CREATION_DATE,CUSTOMER_USERNAME, CUSTOMER_GENDER
ORDER_HEADER- ORDER_ID, CUSTOMER_ID, ORDER_DATE, ORDER_STATUS, PAYMENT_MODE, PAYMENT_DATE, ORDER_SHIPMENT_DATE,SHIPPER_ID
ORDER_ITEMS- ORDER_ID, PRODUCT_ID, PRODUCT_QUANTITY
PRODUCT- PRODUCT_ID, PRODUCT_DESC, PRODUCT_CLASS_CODE, PRODUCT_PRICE, PRODUCT_QUANTITY_AVAIL, LEN, WIDTH, HEIGHT, WEIGHT
PRODUCT_CLASS- PRODUCT_CLASS_CODE, PRODUCT_CLASS_DESC
Can anyone help me on how to filter pincodes without any zeros in it and order status as shipped.
Test if no zeros exist
where length(replace(pincode,'0','')) = length(pincode)
or
where instr(pincode,'0') = 0
or you could use regex or test valid pincodes by downloading and validating

MySQL - Average number of particular product sold on date

I need to write SQL query for "average number of particular product sold by date. On each day is sold min one product".
SELECT AVG (COUNT (PID))
FROM SOLD
GROUP BY DATE, PID;
P.S. PID means Product ID.
Is this query okay?
Should this give right answer?
Consider Using distinct count of date columns
SELECT PID,
COUNT(PID)/COUNT(distinct date_) as "Avg.Product Sold By Days"
FROM SOLD
GROUP BY PID;
You can try this sql query below. Basically, it will return the average number of 'SALES' for each product you have. It will group by each distinct product ID. Please provide us the data structure your of table and etc.
SELECT product_ID, trans_date
Sum(sales_of_product) / COUNT(DISTINCT sold_transaction) AS 'avg'
FROM SOLD
GROUP BY product_ID

Query to tell me if customer is in 3 queries

I have three queries;
Q1. Returns all customer records (id, name, year amount) where the customer has spent money with us.
Q2. Returns all customers records (id,name, year of regis, number) where the customer has a telephone number.
Q3. Returns all customer records (id,name, year, amount) where the customer has a purchased a specific product.
I want a new query that will return a list of all customers ids, names if they appear in any of the 3 queries, along with the latest year. How do I do that?
First, add all the results of the 3 queries together using UNION, then group the records by id and select the first name (or any name really, because they must be all the same) and the maximum year.
Try this:
SELECT id, FIRST(name), MAX(year)
FROM (
SELECT id, name, year FROM q1
UNION
SELECT id, name, year FROM q2
UNION
SELECT id, name, year FROM q3) AS q
GROUP BY id

Select rows from sales table when sale date is > than the lowest order_date in order table for each ProductID

I Have 2 tables, one for Product sales and another for Product orders from suppliers.
Sales table contains : (ProductID, Sale date, customer ID, etc)
Orders table contains (ProductID, Order date, Supplier ID etc).
Each product on sales table was sold many times and each product of Orders table was ordered many times.
For each product ID, I want to query the sales tables to return only rows where the sale dates of that product are higher than the earliest order date for that product. For example, there may be a product that was sold even before the very first time it was ordered from the suppliers, for each productID, I want to eliminate those rows with such dates and only keep those that came after the earliest order from suppliers. Any ideas on how to go about doing that in SQL, MySQL or NetezzaSQL? Thanks !
Sales Table:
ProductID | Sale_date | CustomerID
Orders Table :
ProductID | Order_Date | SupplierID
Any help or tips would be appreciated!
select s.*, o.*
from sales s
inner join (select product.id,min(order_date) min_order_date, supplier_id from orders) o
on s.product_id = o.product_id and s.sale_date > o.min_order_date
If I understood correctly the above should do the trick!
In any case dont hesitate to comment here with more questions :)
Ted

Is this query correct?

The question is:- For the Star Model write an OLAP query that retrieves the quantity, the total income and discount
with respect to each city, type of furniture and month.
This is what I have done and want to know is this correct
SELECT Quantity, Income, Discount, City, Type, Month
FROM Sales, Customer, Furniture, Time
WHERE Count(quantity,income,discount)
GROUP BY City, Type, Month;
You are misisng to do the JOIN of the tables.
You probably want to JOIN your tables.
Sales, Customer, Furniture and Time
Also try to remove the AND from your queries like
SELECT Quantity, Income, Discount, City, Type , Month