[Schema (values) (types)]: | cre_Doc_Control_Systems |  Ref_Document_Types : document_type_code (text) , document_type_description (text) | Roles : role_code (text) , role_description (text) | Addresses : address_id (text) , address_details (text) | Ref_Document_Status : document_status_code (text) , document_status_description (text) | Ref_Shipping_Agents : shipping_agent_code (text) , shipping_agent_name (text) , shipping_agent_description (text) | Documents : document_id (text) , document_status_code (text) , document_type_code (text) , shipping_agent_code (text) , receipt_date (text) , receipt_number (number) , other_details (text) | Employees : employee_id (text) , role_code (text) , employee_name (text) , other_details (text) | Document_Drafts : document_id (text) , draft_number (text) , draft_details (text) | Draft_Copies : document_id (text) , draft_number (text) , copy_number (text) | Circulation_History : document_id (text) , draft_number (text) , copy_number (text) , employee_id (text) | Documents_Mailed : document_id (text) , mailed_to_address_id (text) , mailing_date (text);
[Primary Keys]: ref_document_types : document_type_code, roles : role_code, addresses : address_id, ref_document_status : document_status_code, ref_shipping_agents : shipping_agent_code, documents : document_id, employees : employee_id, document_drafts : document_id, draft_copies : document_id, circulation_history : document_id, documents_mailed : document_id
[Foreign Keys]: documents : shipping_agent_code = ref_shipping_agents : shipping_agent_code | documents : document_status_code = ref_document_status : document_status_code | documents : document_type_code = ref_document_types : document_type_code | employees : role_code = roles : role_code | document_drafts : document_id = documents : document_id | draft_copies : document_id = document_drafts : document_id | draft_copies : draft_number = document_drafts : draft_number | circulation_history : employee_id = employees : employee_id | circulation_history : document_id = draft_copies : document_id | circulation_history : draft_number = draft_copies : draft_number | circulation_history : copy_number = draft_copies : copy_number | documents_mailed : mailed_to_address_id = addresses : address_id | documents_mailed : document_id = documents : document_id

Question: What document status codes do we have?
SQL: SELECT document_status_code FROM Ref_Document_Status;

Question: What is the description of document status code 'working'?
SQL: SELECT document_status_description FROM Ref_Document_Status WHERE document_status_code = "working";

Question: What document type codes do we have?
SQL: SELECT document_type_code FROM Ref_Document_Types;

Question: What is the description of document type 'Paper'?
SQL: SELECT document_type_description FROM Ref_Document_Types WHERE document_type_code = "Paper";

Question: What are the shipping agent names?
SQL: SELECT shipping_agent_name FROM Ref_Shipping_Agents;

Question: What is the shipping agent code of shipping agent UPS?
SQL: SELECT shipping_agent_code FROM Ref_Shipping_Agents WHERE shipping_agent_name = "UPS";

Question: What are all role codes?
SQL: SELECT role_code FROM ROLES;

Question: What is the description of role code ED?
SQL: SELECT role_description FROM ROLES WHERE role_code = "ED";

Question: How many employees do we have?
SQL: SELECT count(*) FROM Employees;

Question: What is the role of the employee named Koby?
SQL: SELECT T1.role_description FROM ROLES AS T1 JOIN Employees AS T2 ON T1.role_code = T2.role_code WHERE T2.employee_name = "Koby";

Question: List all document ids and receipt dates of documents.
SQL: SELECT document_id ,  receipt_date FROM Documents;

Question: How many employees does each role have? List role description, id and number of employees.
SQL: SELECT T1.role_description ,  T2.role_code ,  count(*) FROM ROLES AS T1 JOIN Employees AS T2 ON T1.role_code = T2.role_code GROUP BY T2.role_code;

Question: List roles that have more than one employee. List the role description and number of employees.
SQL: SELECT Roles.role_description , count(Employees.employee_id) FROM ROLES JOIN Employees ON Employees.role_code = Roles.role_code GROUP BY Employees.role_code HAVING count(Employees.employee_id)  >  1;

Question: What is the document status description of the document with id 1?
SQL: SELECT Ref_Document_Status.document_status_description FROM Ref_Document_Status JOIN Documents ON Documents.document_status_code = Ref_Document_Status.document_status_code WHERE Documents.document_id = 1;

Question: How many documents have the status code done?
SQL: SELECT count(*) FROM Documents WHERE document_status_code = "done";

Question: List the document type code for the document with the id 2.
SQL: SELECT document_type_code FROM Documents WHERE document_id = 2;

Question: List the document ids for any documents with the status code done and the type code paper.
SQL: SELECT document_id FROM Documents WHERE document_status_code = "done" AND document_type_code = "Paper";

Question: What is the name of the shipping agent of the document with id 2?
SQL: SELECT Ref_Shipping_Agents.shipping_agent_name FROM Ref_Shipping_Agents JOIN Documents ON Documents.shipping_agent_code = Ref_Shipping_Agents.shipping_agent_code WHERE Documents.document_id = 2;

Question: How many documents were shipped by USPS?
SQL: SELECT count(*) FROM Ref_Shipping_Agents JOIN Documents ON Documents.shipping_agent_code = Ref_Shipping_Agents.shipping_agent_code WHERE Ref_Shipping_Agents.shipping_agent_name = "USPS";

Question: Which shipping agent shipped the most documents? List the shipping agent name and the number of documents.
SQL: SELECT Ref_Shipping_Agents.shipping_agent_name , count(Documents.document_id) FROM Ref_Shipping_Agents JOIN Documents ON Documents.shipping_agent_code = Ref_Shipping_Agents.shipping_agent_code GROUP BY Ref_Shipping_Agents.shipping_agent_code ORDER BY count(Documents.document_id) DESC LIMIT 1;

Question: What is the receipt date of the document with id 3?
SQL: SELECT receipt_date FROM Documents WHERE document_id = 3;

Question: What address was the document with id 4 mailed to?
SQL: SELECT Addresses.address_details FROM Addresses JOIN Documents_Mailed ON Documents_Mailed.mailed_to_address_id = Addresses.address_id WHERE document_id = 4;

Question: What is the mail date of the document with id 7?
SQL: SELECT mailing_date FROM Documents_Mailed WHERE document_id = 7;

Question: List the document ids of documents with the status done and type Paper, which not shipped by the shipping agent named USPS.
SQL: SELECT document_id FROM Documents WHERE document_status_code  =  "done" AND document_type_code = "Paper" EXCEPT SELECT document_id FROM Documents JOIN Ref_Shipping_Agents ON Documents.shipping_agent_code = Ref_Shipping_Agents.shipping_agent_code WHERE Ref_Shipping_Agents.shipping_agent_name = "USPS";

Question: List document id of documents status is done and document type is Paper and the document is shipped by shipping agent named USPS.
SQL: SELECT document_id FROM Documents WHERE document_status_code  =  "done" AND document_type_code = "Paper" INTERSECT SELECT document_id FROM Documents JOIN Ref_Shipping_Agents ON Documents.shipping_agent_code = Ref_Shipping_Agents.shipping_agent_code WHERE Ref_Shipping_Agents.shipping_agent_name = "USPS";

Question: What is draft detail of the document with id 7?
SQL: SELECT draft_details FROM Document_Drafts WHERE document_id = 7;

Question: How many draft copies does the document with id 2 have?
SQL: SELECT count(*) FROM Draft_Copies WHERE document_id = 2;

Question: Which document has the most draft copies? List its document id and number of draft copies.
SQL: SELECT document_id , count(copy_number) FROM Draft_Copies GROUP BY document_id ORDER BY count(copy_number) DESC LIMIT 1;

Question: Which documents have more than 1 draft copies? List document id and number of draft copies.
SQL: SELECT document_id , count(*) FROM Draft_Copies GROUP BY document_id HAVING count(*)  >  1;

Question: List all employees in the circulation history of the document with id 1. List the employee's name.
SQL: SELECT Employees.employee_name FROM Employees JOIN Circulation_History ON Circulation_History.employee_id = Employees.employee_id WHERE Circulation_History.document_id = 1;

Question: List the employees who have not showed up in any circulation history of documents. List the employee's name.
SQL: SELECT employee_name FROM Employees EXCEPT SELECT Employees.employee_name FROM Employees JOIN Circulation_History ON Circulation_History.employee_id = Employees.employee_id

Question: Which employee has showed up in most circulation history documents. List the employee's name and the number of drafts and copies.
SQL: SELECT Employees.employee_name , count(*) FROM Employees JOIN Circulation_History ON Circulation_History.employee_id = Employees.employee_id GROUP BY Circulation_History.document_id , Circulation_History.draft_number , Circulation_History.copy_number ORDER BY count(*) DESC LIMIT 1;

Question: For each document, list the number of employees who have showed up in the circulation history of that document. List the document ids and number of employees.
SQL: SELECT document_id ,  count(DISTINCT employee_id) FROM Circulation_History GROUP BY document_id;