Database: customer_complaints

Question: Which city has the least number of customers whose type code is "Good Credit Rating"?
SQL: SELECT town_city FROM customers WHERE customer_type_code  =  "Good Credit Rating" GROUP BY town_city ORDER BY count(*) LIMIT 1

Question: List the name of all products along with the number of complaints that they have received.
SQL: SELECT t1.product_name ,  count(*) FROM products AS t1 JOIN complaints AS t2 ON t1.product_id  =  t2.product_id GROUP BY t1.product_name

Question: Find the email of the customer who has filed the most complaints.
SQL: SELECT t1.email_address FROM customers AS t1 JOIN complaints AS t2 ON t1.customer_id  =  t2.customer_id GROUP BY t1.customer_id ORDER BY count(*) DESC LIMIT 1

Question: Which products has been complained by the customer who has filed least amount of complaints?
SQL: SELECT DISTINCT t1.product_name FROM products AS t1 JOIN complaints AS t2 ON t1.product_id  =  t2.product_id JOIN customers AS t3 GROUP BY t3.customer_id ORDER BY count(*) LIMIT 1

Question: What is the phone number of the customer who has filed the most recent complaint?
SQL: SELECT t1.phone_number FROM customers AS t1 JOIN complaints AS t2 ON t1.customer_id  =  t2.customer_id ORDER BY t2.date_complaint_raised DESC LIMIT 1

Question: Find the email and phone number of the customers who have never filed a complaint before.
SQL: SELECT email_address ,  phone_number FROM customers WHERE customer_id NOT IN (SELECT customer_id FROM complaints)

Question: Find the phone number of all the customers and staff.
SQL: SELECT phone_number FROM customers UNION SELECT phone_number FROM staff

Question: Find the name and category of the most expensive product.
SQL: SELECT product_name ,  product_category_code FROM products ORDER BY product_price DESC LIMIT 1

Question: Find the prices of products which has never received a single complaint.
SQL: SELECT product_price FROM products WHERE product_id NOT IN (SELECT product_id FROM complaints)

Question: Find the last names of the staff members who processed complaints about the cheapest product.
SQL: SELECT last_name from staff where staff_id in (SELECT staff_id from complaints WHERE product_id in (SELECT product_id FROM products ORDER BY product_price ASC LIMIT 1))

Question: Which complaint status has more than 3 records on file?
SQL: SELECT complaint_status_code FROM complaints GROUP BY complaint_status_code HAVING count(*)  >  3

Question: Find the last name of the staff whose email address contains "wrau".
SQL: SELECT last_name FROM staff WHERE email_address LIKE "%wrau%"

Question: How many customers are there in the customer type with the most customers?
SQL: SELECT count(*) FROM customers GROUP BY customer_type_code ORDER BY count(*) DESC LIMIT 1

Question: What is the last name of the staff who has handled the first ever complaint?
SQL: SELECT t1.last_name FROM staff AS t1 JOIN complaints AS t2 ON t1.staff_id  =  t2.staff_id ORDER BY t2.date_complaint_raised LIMIT 1