CREATE TABLE Ref_Document_Types (
    document_type_code TEXT PRIMARY KEY,
    document_type_description TEXT
);
CREATE TABLE Roles (
    role_code TEXT PRIMARY KEY,
    role_description TEXT
);
CREATE TABLE Addresses (
    address_id NUMBER PRIMARY KEY,
    address_details TEXT
);
CREATE TABLE Ref_Document_Status (
    document_status_code TEXT PRIMARY KEY,
    document_status_description TEXT
);
CREATE TABLE Ref_Shipping_Agents (
    shipping_agent_code TEXT PRIMARY KEY,
    shipping_agent_name TEXT,
    shipping_agent_description TEXT
);
CREATE TABLE Documents (
    document_id NUMBER PRIMARY KEY,
    document_status_code TEXT,
    document_type_code TEXT,
    shipping_agent_code TEXT,
    receipt_date TIME,
    receipt_number TEXT,
    other_details TEXT,
    FOREIGN KEY ("shipping_agent_code") REFERENCES Ref_Shipping_Agents("shipping_agent_code"),
    FOREIGN KEY ("document_status_code") REFERENCES Ref_Document_Status("document_status_code"),
    FOREIGN KEY ("document_type_code") REFERENCES Ref_Document_Types("document_type_code")
);
CREATE TABLE Employees (
    employee_id NUMBER PRIMARY KEY,
    role_code TEXT,
    employee_name TEXT,
    other_details TEXT,
    FOREIGN KEY ("role_code") REFERENCES Roles("role_code")
);
CREATE TABLE Document_Drafts (
    document_id NUMBER PRIMARY KEY,
    draft_number NUMBER,
    draft_details TEXT,
    FOREIGN KEY ("document_id") REFERENCES Documents("document_id")
);
CREATE TABLE Draft_Copies (
    document_id NUMBER PRIMARY KEY,
    draft_number NUMBER,
    copy_number NUMBER,
    FOREIGN KEY ("document_id") REFERENCES Document_Drafts("document_id"),
    FOREIGN KEY ("draft_number") REFERENCES Document_Drafts("draft_number")
);
CREATE TABLE Circulation_History (
    document_id NUMBER PRIMARY KEY,
    draft_number NUMBER,
    copy_number NUMBER,
    employee_id NUMBER,
    FOREIGN KEY ("employee_id") REFERENCES Employees("employee_id"),
    FOREIGN KEY ("document_id") REFERENCES Draft_Copies("document_id"),
    FOREIGN KEY ("draft_number") REFERENCES Draft_Copies("draft_number"),
    FOREIGN KEY ("copy_number") REFERENCES Draft_Copies("copy_number")
);
CREATE TABLE Documents_Mailed (
    document_id NUMBER PRIMARY KEY,
    mailed_to_address_id NUMBER,
    mailing_date TIME,
    FOREIGN KEY ("mailed_to_address_id") REFERENCES Addresses("address_id"),
    FOREIGN KEY ("document_id") REFERENCES 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;