Database: tracking_orders

CREATE TABLE `Customers` (
`customer_id` INTEGER PRIMARY KEY,
`customer_name` VARCHAR(80),
`customer_details` VARCHAR(255)
);
CREATE TABLE `Invoices` (
`invoice_number` INTEGER PRIMARY KEY,
`invoice_date` DATETIME,
`invoice_details` VARCHAR(255)
);
CREATE TABLE `Orders` (
`order_id` INTEGER PRIMARY KEY,
`customer_id` INTEGER NOT NULL,
`order_status` VARCHAR(10) NOT NULL,
`date_order_placed` DATETIME NOT NULL,
`order_details` VARCHAR(255),
FOREIGN KEY (`customer_id` ) REFERENCES `Customers`(`customer_id` )
);
CREATE TABLE `Products` (
`product_id` INTEGER PRIMARY KEY,
`product_name` VARCHAR(80),
`product_details` VARCHAR(255)
);
CREATE TABLE `Order_Items` (
`order_item_id` INTEGER PRIMARY KEY,
`product_id` INTEGER NOT NULL,
`order_id` INTEGER NOT NULL,
`order_item_status` VARCHAR(10) NOT NULL,
`order_item_details` VARCHAR(255),
FOREIGN KEY (`order_id` ) REFERENCES `Orders`(`order_id` ),
FOREIGN KEY (`product_id` ) REFERENCES `Products`(`product_id` )
);
CREATE TABLE `Shipments` (
`shipment_id` INTEGER PRIMARY KEY,
`order_id` INTEGER NOT NULL,
`invoice_number` INTEGER NOT NULL,
`shipment_tracking_number` VARCHAR(80),
`shipment_date` DATETIME,
`other_shipment_details` VARCHAR(255),
FOREIGN KEY (`order_id` ) REFERENCES `Orders`(`order_id` ),
FOREIGN KEY (`invoice_number` ) REFERENCES `Invoices`(`invoice_number` )
);
CREATE TABLE `Shipment_Items` (
`shipment_id` INTEGER NOT NULL,
`order_item_id` INTEGER NOT NULL,
FOREIGN KEY (`order_item_id` ) REFERENCES `Order_Items`(`order_item_id` ),
FOREIGN KEY (`shipment_id` ) REFERENCES `Shipments`(`shipment_id` )
);

Question: What is the id of the most recent order?
SQL: SELECT order_id FROM orders ORDER BY date_order_placed DESC LIMIT 1

Question: Find the id of the order made most recently.
SQL: SELECT order_id FROM orders ORDER BY date_order_placed DESC LIMIT 1

Question: what are the order id and customer id of the oldest order?
SQL: SELECT order_id ,  customer_id FROM orders ORDER BY date_order_placed LIMIT 1

Question: Find the order id and customer id associated with the oldest order.
SQL: SELECT order_id ,  customer_id FROM orders ORDER BY date_order_placed LIMIT 1

Question: Find the id of the order whose shipment tracking number is "3452".
SQL: SELECT order_id FROM shipments WHERE shipment_tracking_number = "3452"

Question: Which order's shipment tracking number is "3452"? Give me the id of the order.
SQL: SELECT order_id FROM shipments WHERE shipment_tracking_number = "3452"

Question: Find the ids of all the order items whose product id is 11.
SQL: SELECT order_item_id FROM order_items WHERE product_id = 11

Question: Find all the order items whose product id is 11. What are the order item ids?
SQL: SELECT order_item_id FROM order_items WHERE product_id = 11

Question: List the name of all the distinct customers who have orders with status "Packing".
SQL: 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"

Question: Which customers have orders with status "Packing"? Give me the customer names.
SQL: 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"

Question: Find the details of all the distinct customers who have orders with status "On Road".
SQL: 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"

Question: What are the distinct customers who have orders with status "On Road"? Give me the customer details?
SQL: 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"

Question: What is the name of the customer who has the most orders?
SQL: SELECT T1.customer_name FROM customers AS T1 JOIN orders AS T2 ON T1.customer_id  =  T2.customer_id GROUP BY T1.customer_id ORDER BY count(*) DESC LIMIT 1

Question: Which customer made the most orders? Find the customer name.
SQL: SELECT T1.customer_name FROM customers AS T1 JOIN orders AS T2 ON T1.customer_id  =  T2.customer_id GROUP BY T1.customer_id ORDER BY count(*) DESC LIMIT 1

Question: What is the customer id of the customer who has the most orders?
SQL: SELECT T1.customer_id FROM customers AS T1 JOIN orders AS T2 ON T1.customer_id  =  T2.customer_id GROUP BY T1.customer_id ORDER BY count(*) DESC LIMIT 1

Question: Find the id of the customer who made the most orders.
SQL: SELECT T1.customer_id FROM customers AS T1 JOIN orders AS T2 ON T1.customer_id  =  T2.customer_id GROUP BY T1.customer_id ORDER BY count(*) DESC LIMIT 1

Question: Give me a list of id and status of orders which belong to the customer named "Jeramie".
SQL: SELECT T2.order_id ,  T2.order_status FROM customers AS T1 JOIN orders AS T2 ON T1.customer_id = T2.customer_id WHERE T1.customer_name = "Jeramie"

Question: Which orders are made by the customer named "Jeramie"? Give me the order ids and status.
SQL: SELECT T2.order_id ,  T2.order_status FROM customers AS T1 JOIN orders AS T2 ON T1.customer_id = T2.customer_id WHERE T1.customer_name = "Jeramie"

Question: Find the dates of orders which belong to the customer named "Jeramie".
SQL: SELECT T2.date_order_placed FROM customers AS T1 JOIN orders AS T2 ON T1.customer_id = T2.customer_id WHERE T1.customer_name = "Jeramie"

Question: What are the dates of the orders made by the customer named "Jeramie"?
SQL: SELECT T2.date_order_placed FROM customers AS T1 JOIN orders AS T2 ON T1.customer_id = T2.customer_id WHERE T1.customer_name = "Jeramie"

Question: Give me the names of customers who have placed orders between 2009-01-01 and 2010-01-01.
SQL: SELECT T1.customer_name FROM customers AS T1 JOIN orders AS T2 ON T1.customer_id = T2.customer_id WHERE T2.date_order_placed >= "2009-01-01" AND T2.date_order_placed <= "2010-01-01"

Question: Which customers made orders between 2009-01-01 and 2010-01-01? Find their names.
SQL: SELECT T1.customer_name FROM customers AS T1 JOIN orders AS T2 ON T1.customer_id = T2.customer_id WHERE T2.date_order_placed >= "2009-01-01" AND T2.date_order_placed <= "2010-01-01"

Question: Give me a list of distinct product ids from orders placed between 1975-01-01 and 1976-01-01?
SQL: SELECT DISTINCT T2.product_id FROM orders AS T1 JOIN order_items AS T2 ON T1.order_id = T2.order_id WHERE T1.date_order_placed >= "1975-01-01" AND T1.date_order_placed <= "1976-01-01"

Question: What are the distinct ids of products ordered between 1975-01-01 and 1976-01-01??
SQL: SELECT DISTINCT T2.product_id FROM orders AS T1 JOIN order_items AS T2 ON T1.order_id = T2.order_id WHERE T1.date_order_placed >= "1975-01-01" AND T1.date_order_placed <= "1976-01-01"

Question: Find the names of the customers who have order status both "On Road" and "Shipped".
SQL: 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"

Question: Which customers have both "On Road" and "Shipped" as order status? List the customer names.
SQL: 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"

Question: Find the id of the customers who have order status both "On Road" and "Shipped".
SQL: 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"

Question: Which customers have both "On Road" and "Shipped" as order status? List the customer ids.
SQL: 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"

Question: When was the order placed whose shipment tracking number is 3452? Give me the date.
SQL: SELECT T1.date_order_placed FROM orders AS T1 JOIN shipments AS T2 ON T1.order_id = T2.order_id WHERE T2.shipment_tracking_number = 3452

Question: On which day was the order placed whose shipment tracking number is 3452?
SQL: SELECT T1.date_order_placed FROM orders AS T1 JOIN shipments AS T2 ON T1.order_id = T2.order_id WHERE T2.shipment_tracking_number = 3452

Question: What is the placement date of the order whose invoice number is 10?
SQL: SELECT T1.date_order_placed FROM orders AS T1 JOIN shipments AS T2 ON T1.order_id = T2.order_id WHERE T2.invoice_number = 10

Question: On what day was the order with invoice number 10 placed?
SQL: SELECT T1.date_order_placed FROM orders AS T1 JOIN shipments AS T2 ON T1.order_id = T2.order_id WHERE T2.invoice_number = 10

Question: List the count and id of each product in all the orders.
SQL: SELECT count(*) ,  T3.product_id FROM orders AS T1 JOIN order_items AS T2 JOIN products AS T3 ON T1.order_id = T2.order_id AND T2.product_id = T3.product_id GROUP BY T3.product_id

Question: For each product, return its id and the number of times it was ordered.
SQL: SELECT count(*) ,  T3.product_id FROM orders AS T1 JOIN order_items AS T2 JOIN products AS T3 ON T1.order_id = T2.order_id AND T2.product_id = T3.product_id GROUP BY T3.product_id

Question: List the name and count of each product in all orders.
SQL: SELECT T3.product_name ,  count(*) FROM orders AS T1 JOIN order_items AS T2 JOIN products AS T3 ON T1.order_id = T2.order_id AND T2.product_id = T3.product_id GROUP BY T3.product_id

Question: For each product, show its name and the number of times it was ordered.
SQL: SELECT T3.product_name ,  count(*) FROM orders AS T1 JOIN order_items AS T2 JOIN products AS T3 ON T1.order_id = T2.order_id AND T2.product_id = T3.product_id GROUP BY T3.product_id

Question: Find the ids of orders which are shipped after 2000-01-01.
SQL: SELECT order_id FROM shipments WHERE shipment_date > "2000-01-01"

Question: Which orders have shipment after 2000-01-01? Give me the order ids.
SQL: SELECT order_id FROM shipments WHERE shipment_date > "2000-01-01"

Question: Find the id of the order which is shipped most recently.
SQL: SELECT order_id FROM shipments WHERE shipment_date  =  (SELECT max(shipment_date) FROM shipments)

Question: Which order has the most recent shipment? Give me the order id.
SQL: SELECT order_id FROM shipments WHERE shipment_date  =  (SELECT max(shipment_date) FROM shipments)

Question: List the names of all distinct products in alphabetical order.
SQL: SELECT DISTINCT product_name FROM products ORDER BY product_name

Question: Sort all the distinct products in alphabetical order.
SQL: SELECT DISTINCT product_name FROM products ORDER BY product_name

Question: List the ids of all distinct orders ordered by placed date.
SQL: SELECT DISTINCT order_id FROM orders ORDER BY date_order_placed

Question: What are ids of the all distinct orders, sorted by placement date?
SQL: SELECT DISTINCT order_id FROM orders ORDER BY date_order_placed

Question: What is the id of the order which has the most items?
SQL: SELECT T1.order_id FROM orders AS T1 JOIN order_items AS T2 ON T1.order_id = T2.order_id GROUP BY T1.order_id ORDER BY count(*) DESC LIMIT 1

Question: Which order deals with the most items? Return the order id.
SQL: SELECT T1.order_id FROM orders AS T1 JOIN order_items AS T2 ON T1.order_id = T2.order_id GROUP BY T1.order_id ORDER BY count(*) DESC LIMIT 1

Question: What is the name of the customer who has the largest number of orders?
SQL: SELECT T1.customer_name FROM customers AS T1 JOIN orders AS T2 ON T1.customer_id = T2.customer_id GROUP BY T1.customer_id ORDER BY count(*) DESC LIMIT 1

Question: Find the name of the customer who made the most orders.
SQL: SELECT T1.customer_name FROM customers AS T1 JOIN orders AS T2 ON T1.customer_id = T2.customer_id GROUP BY T1.customer_id ORDER BY count(*) DESC LIMIT 1

Question: Find the invoice numbers which are created before 1989-09-03 or after 2007-12-25.
SQL: SELECT invoice_number FROM invoices WHERE invoice_date < "1989-09-03" OR invoice_date > "2007-12-25"

Question: What are the invoice numbers created before 1989-09-03 or after 2007-12-25?
SQL: SELECT invoice_number FROM invoices WHERE invoice_date < "1989-09-03" OR invoice_date > "2007-12-25"

Question: Find the distinct details of invoices which are created before 1989-09-03 or after 2007-12-25.
SQL: SELECT DISTINCT invoice_details FROM invoices WHERE invoice_date < "1989-09-03" OR invoice_date > "2007-12-25"

Question: What are the distinct details of invoices created before 1989-09-03 or after 2007-12-25?
SQL: SELECT DISTINCT invoice_details FROM invoices WHERE invoice_date < "1989-09-03" OR invoice_date > "2007-12-25"

Question: For each customer who has at least two orders, find the customer name and number of orders made.
SQL: 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

Question: Which customers have made at least two orders? Give me each customer name and number of orders made.
SQL: 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

Question: Find the name of the customers who have at most two orders.
SQL: 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

Question: What are the names of the customers who have made two or less orders?
SQL: 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

Question: List the names of the customers who have once bought product "food".
SQL: SELECT T1.customer_name FROM customers AS T1 JOIN orders AS T2 JOIN order_items AS T3 JOIN products AS T4 ON T1.customer_id = T2.customer_id AND T2.order_id = T3.order_id AND T3.product_id = T4.product_id WHERE T4.product_name = "food" GROUP BY T1.customer_id HAVING count(*)  >=  1

Question: What are the names of the customers who bought product "food" at least once?
SQL: SELECT T1.customer_name FROM customers AS T1 JOIN orders AS T2 JOIN order_items AS T3 JOIN products AS T4 ON T1.customer_id = T2.customer_id AND T2.order_id = T3.order_id AND T3.product_id = T4.product_id WHERE T4.product_name = "food" GROUP BY T1.customer_id HAVING count(*)  >=  1

Question: List the names of customers who have once canceled the purchase of the product "food" (the item status is "Cancel").
SQL: SELECT T1.customer_name FROM customers AS T1 JOIN orders AS T2 JOIN order_items AS T3 JOIN products AS T4 ON T1.customer_id = T2.customer_id AND T2.order_id = T3.order_id AND T3.product_id = T4.product_id WHERE T3.order_item_status = "Cancel" AND T4.product_name = "food" GROUP BY T1.customer_id HAVING count(*)  >=  1

Question: Which customers have ever canceled the purchase of the product "food" (the item status is "Cancel")?
SQL: SELECT T1.customer_name FROM customers AS T1 JOIN orders AS T2 JOIN order_items AS T3 JOIN products AS T4 ON T1.customer_id = T2.customer_id AND T2.order_id = T3.order_id AND T3.product_id = T4.product_id WHERE T3.order_item_status = "Cancel" AND T4.product_name = "food" GROUP BY T1.customer_id HAVING count(*)  >=  1