Database: customers_and_addresses

Question: Which city is post code 255 located in?
SQL: SELECT city FROM addresses WHERE zip_postcode  =  255

Question: What are the state and country of all the cities that have post codes starting with 4.\
SQL: SELECT state_province_county ,  country FROM addresses WHERE zip_postcode LIKE "4%"

Question: For which countries are there more than four distinct addresses listed?
SQL: SELECT country FROM addresses GROUP BY country HAVING count(address_id)  >  4

Question: Which contact channel codes were used less than 5 times?
SQL: SELECT channel_code FROM customer_contact_channels GROUP BY channel_code HAVING count(customer_id)  <  5

Question: Find the contact channel code that was used by the customer named "Tillman Ernser".
SQL: SELECT DISTINCT channel_code FROM customers AS t1 JOIN customer_contact_channels AS t2 ON t1.customer_id  =  t2.customer_id WHERE t1.customer_name  =  "Tillman Ernser"

Question: Return the the "active to date" of the latest contact channel used by the customer named "Tillman Ernser".
SQL: SELECT max(t2.active_to_date) FROM customers AS t1 JOIN customer_contact_channels AS t2 ON t1.customer_id  =  t2.customer_id WHERE t1.customer_name  =  "Tillman Ernser"

Question: Compute the average active time span of contact channels.
SQL: SELECT avg(active_to_date - active_from_date) FROM customer_contact_channels

Question: Return the channel code and contact number of the customer contact channel whose active duration was the longest.
SQL: SELECT channel_code ,  contact_number FROM customer_contact_channels WHERE active_to_date - active_from_date  =  (SELECT active_to_date - active_from_date FROM customer_contact_channels ORDER BY (active_to_date - active_from_date) DESC LIMIT 1)

Question: What are the name and active date of the customers whose contact channel code is email?
SQL: SELECT t1.customer_name ,  t2.active_from_date FROM customers AS t1 JOIN customer_contact_channels AS t2 ON t1.customer_id  =  t2.customer_id WHERE t2.channel_code  =  'Email'

Question: Find the name of the customer who made the order of the largest amount of goods.
SQL: SELECT t1.customer_name FROM customers AS t1 JOIN customer_orders AS t2 ON t1.customer_id  =  t2.customer_id JOIN order_items AS t3 ON t2.order_id  =  t3.order_id WHERE t3.order_quantity  =  ( SELECT max(order_quantity) FROM order_items)

Question: Give me the name of the customer who ordered the most items in total.
SQL: SELECT t1.customer_name FROM customers AS t1 JOIN customer_orders AS t2 ON t1.customer_id  =  t2.customer_id JOIN order_items AS t3 ON t2.order_id  =  t3.order_id GROUP BY t1.customer_name ORDER BY sum(t3.order_quantity) DESC LIMIT 1

Question: Tell me the payment method used by the customer who ordered the least amount of goods in total.
SQL: SELECT t1.payment_method FROM customers AS t1 JOIN customer_orders AS t2 ON t1.customer_id  =  t2.customer_id JOIN order_items AS t3 ON t2.order_id  =  t3.order_id GROUP BY t1.customer_name ORDER BY sum(t3.order_quantity) LIMIT 1