human
Schema:
Customers (customer_id, customer_name, customer_details)
Invoices (invoice_number, invoice_date, invoice_details)
Orders (order_id, customer_id, order_status, date_order_placed, order_details)
Products (product_id, product_name, product_details)
Order_Items (order_item_id, product_id, order_id, order_item_status, order_item_details)
Shipments (shipment_id, order_id, invoice_number, shipment_tracking_number, shipment_date, other_shipment_details)
Shipment_Items (shipment_id, order_item_id)
Please answer with SQL.
assistant
Sure, I understand.
human
For each customer who has at least two orders, find the customer name and number of orders made.
assistant
SELECT T2.customer_name , count(*) FROM orders AS T1 JOIN customers AS T2 ON T1.customer_id = T2.customer_id GROUP BY T2.customer_id HAVING count(*) >= 2
human
Find the name of the customers who have at most two orders.
assistant
SELECT T2.customer_name FROM orders AS T1 JOIN customers AS T2 ON T1.customer_id = T2.customer_id GROUP BY T2.customer_id HAVING count(*) <= 2
human
List the name of all the distinct customers who have orders with status 'Packing'.
assistant
SELECT DISTINCT T1.customer_name FROM customers AS T1 JOIN orders AS T2 ON T1.customer_id = T2.customer_id WHERE T2.order_status = 'Packing'
human
Find the details of all the distinct customers who have orders with status 'On Road'.
assistant
SELECT DISTINCT T1.customer_details FROM customers AS T1 JOIN orders AS T2 ON T1.customer_id = T2.customer_id WHERE T2.order_status = 'On Road'
human
Find the id of the customers who have order status both 'On Road' and 'Shipped'.
assistant
SELECT T1.customer_id FROM customers AS T1 JOIN orders AS T2 ON T1.customer_id = T2.customer_id WHERE T2.order_status = 'On Road' INTERSECT SELECT T1.customer_id FROM customers AS T1 JOIN orders AS T2 ON T1.customer_id = T2.customer_id WHERE T2.order_status = 'Shipped'
human
Find the names of the customers who have order status both 'On Road' and 'Shipped'.
assistant
SELECT T1.customer_name FROM customers AS T1 JOIN orders AS T2 ON T1.customer_id = T2.customer_id WHERE T2.order_status = "On Road" INTERSECT SELECT T1.customer_name FROM customers AS T1 JOIN orders AS T2 ON T1.customer_id = T2.customer_id WHERE T2.order_status = "Shipped" ;