human
Schema:
Staff (staff_id, gender, first_name, last_name, email_address, phone_number)
Customers (customer_id, customer_type_code, address_line_1, address_line_2, town_city, state, email_address, phone_number)
Products (product_id, parent_product_id, product_category_code, date_product_first_available, date_product_discontinued, product_name, product_description, product_price)
Complaints (complaint_id, product_id, customer_id, complaint_outcome_code, complaint_status_code, complaint_type_code, date_complaint_raised, date_complaint_closed, staff_id)
Please answer with SQL.
assistant
Sure, I understand.
human
What is first names of the top 5 staff who have handled the greatest number of complaints?
assistant
SELECT t1.first_name FROM staff AS t1 JOIN complaints AS t2 ON t1.staff_id = t2.staff_id GROUP BY t2.staff_id ORDER BY count(*) LIMIT 5
human
Which products has been complained by the customer who has filed least amount of complaints?
assistant
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
human
List the name of all products along with the number of complaints that they have received.
assistant
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
human
Which complaint status has more than 3 records on file?
assistant
SELECT complaint_status_code FROM complaints GROUP BY complaint_status_code HAVING count(*) > 3
human
Find the number of complaints with Product Failure type for each complaint status.
assistant
SELECT complaint_status_code , count(*) FROM complaints WHERE complaint_type_code = 'Product Failure' GROUP BY complaint_status_code
human
Count the number of different complaint type codes.
assistant
SELECT COUNT( DISTINCT complaint_type_code ) FROM complaints ;