Database: department_store

Question: Find the name and address of the customers who have both New and Pending orders.
SQL: SELECT T1.customer_name ,  T1.customer_address FROM customers AS T1 JOIN customer_orders AS T2 ON T1.customer_id  =  T2.customer_id WHERE T2.order_status_code  =  "New" INTERSECT SELECT T1.customer_name ,  T1.customer_address FROM customers AS T1 JOIN customer_orders AS T2 ON T1.customer_id  =  T2.customer_id WHERE T2.order_status_code  =  "Pending"

Question: Return ids of all the products that are supplied by supplier id 2 and are more expensive than the average price of all products.
SQL: SELECT T1.product_id FROM product_suppliers AS T1 JOIN products AS T2 ON T1.product_id  =  T2.product_id WHERE T1.supplier_id  =  2 AND T2.product_price  >  (SELECT avg(product_price) FROM products)

Question: What is the id and name of the department store that has both marketing and managing department?
SQL: SELECT T2.dept_store_id ,  T2.store_name FROM departments AS T1 JOIN department_stores AS T2 ON T1.dept_store_id  =  T2.dept_store_id WHERE T1.department_name  =  "marketing" INTERSECT SELECT T2.dept_store_id ,  T2.store_name FROM departments AS T1 JOIN department_stores AS T2 ON T1.dept_store_id  =  T2.dept_store_id WHERE T1.department_name  =  "managing"

Question: What are the ids of the two department store chains with the largest number of department stores?
SQL: SELECT dept_store_chain_id FROM department_stores GROUP BY dept_store_chain_id ORDER BY count(*) DESC LIMIT 2

Question: What is the id of the department with the least number of staff?
SQL: SELECT department_id FROM staff_department_assignments GROUP BY department_id ORDER BY count(*) LIMIT 1

Question: For each product type, return the maximum and minimum price.
SQL: SELECT product_type_code ,  max(product_price) ,  min(product_price) FROM products GROUP BY product_type_code

Question: Find the product type whose average price is higher than the average price of all products.
SQL: SELECT product_type_code FROM products GROUP BY product_type_code HAVING avg(product_price)  >  (SELECT avg(product_price) FROM products)

Question: Find the id and name of the staff who has been assigned for the shortest period.
SQL: SELECT T1.staff_id ,  T1.staff_name FROM staff AS T1 JOIN Staff_Department_Assignments AS T2 ON T1.staff_id  =  T2.staff_id ORDER BY date_assigned_to - date_assigned_from LIMIT 1

Question: Return the names and ids of all products whose price is between 600 and 700.
SQL: SELECT product_name ,  product_id FROM products WHERE product_price BETWEEN 600 AND 700

Question: Find the ids of all distinct customers who made order after some orders that were Cancelled.
SQL: SELECT DISTINCT customer_id FROM Customer_Orders WHERE order_date  >  (SELECT min(order_date) FROM Customer_Orders WHERE order_status_code  =  "Cancelled")

Question: What is id of the staff who had a Staff Department Assignment earlier than any Clerical Staff?
SQL: SELECT staff_id FROM Staff_Department_Assignments WHERE date_assigned_to  <  (SELECT max(date_assigned_to) FROM Staff_Department_Assignments WHERE job_title_code  =  'Clerical Staff')