CREATE TABLE Roles (
    role_code TEXT PRIMARY KEY,
    role_description TEXT
);
CREATE TABLE Users (
    user_id NUMBER PRIMARY KEY,
    role_code TEXT,
    user_name TEXT,
    user_login TEXT,
    password TEXT,
    FOREIGN KEY ("role_code") REFERENCES Roles("role_code")
);
CREATE TABLE Document_Structures (
    document_structure_code TEXT PRIMARY KEY,
    parent_document_structure_code TEXT,
    document_structure_description TEXT
);
CREATE TABLE Functional_Areas (
    functional_area_code TEXT PRIMARY KEY,
    parent_functional_area_code TEXT,
    functional_area_description TEXT
);
CREATE TABLE Images (
    image_id NUMBER PRIMARY KEY,
    image_alt_text TEXT,
    image_name TEXT,
    image_url TEXT
);
CREATE TABLE Documents (
    document_code TEXT PRIMARY KEY,
    document_structure_code TEXT,
    document_type_code TEXT,
    access_count NUMBER,
    document_name TEXT,
    FOREIGN KEY ("document_structure_code") REFERENCES Document_Structures("document_structure_code")
);
CREATE TABLE Document_Functional_Areas (
    document_code TEXT,
    functional_area_code TEXT,
    FOREIGN KEY ("functional_area_code") REFERENCES Functional_Areas("functional_area_code"),
    FOREIGN KEY ("document_code") REFERENCES Documents("document_code")
);
CREATE TABLE Document_Sections (
    section_id NUMBER PRIMARY KEY,
    document_code TEXT,
    section_sequence NUMBER,
    section_code TEXT,
    section_title TEXT,
    FOREIGN KEY ("document_code") REFERENCES Documents("document_code")
);
CREATE TABLE Document_Sections_Images (
    section_id NUMBER PRIMARY KEY,
    image_id NUMBER,
    FOREIGN KEY ("image_id") REFERENCES Images("image_id"),
    FOREIGN KEY ("section_id") REFERENCES Document_Sections("section_id")
);

Question: Find the name and access counts of all documents, in alphabetic order of the document name.
SQL: SELECT document_name ,  access_count FROM documents ORDER BY document_name

Question: What are the names of all the documents, as well as the access counts of each, ordered alphabetically?
SQL: SELECT document_name ,  access_count FROM documents ORDER BY document_name

Question: Find the name of the document that has been accessed the greatest number of times, as well as the count of how many times it has been accessed?
SQL: SELECT document_name ,  access_count FROM documents ORDER BY access_count DESC LIMIT 1

Question: What is the name of the document which has been accessed the most times, as well as the number of times it has been accessed?
SQL: SELECT document_name ,  access_count FROM documents ORDER BY access_count DESC LIMIT 1

Question: Find the types of documents with more than 4 documents.
SQL: SELECT document_type_code FROM documents GROUP BY document_type_code HAVING count(*)  >  4

Question: What are the codes of types of documents of which there are for or more?
SQL: SELECT document_type_code FROM documents GROUP BY document_type_code HAVING count(*)  >  4

Question: Find the total access count of all documents in the most popular document type.
SQL: SELECT sum(access_count) FROM documents GROUP BY document_type_code ORDER BY count(*) DESC LIMIT 1

Question: What is the total access count of documents that are of the most common document type?
SQL: SELECT sum(access_count) FROM documents GROUP BY document_type_code ORDER BY count(*) DESC LIMIT 1

Question: What is the average access count of documents?
SQL: SELECT avg(access_count) FROM documents

Question: Find the average access count across all documents?
SQL: SELECT avg(access_count) FROM documents

Question: What is the structure of the document with the least number of accesses?
SQL: SELECT t2.document_structure_description FROM documents AS t1 JOIN document_structures AS t2 ON t1.document_structure_code  =  t2.document_structure_code GROUP BY t1.document_structure_code ORDER BY count(*) DESC LIMIT 1

Question: Return the structure description of the document that has been accessed the fewest number of times.
SQL: SELECT t2.document_structure_description FROM documents AS t1 JOIN document_structures AS t2 ON t1.document_structure_code  =  t2.document_structure_code GROUP BY t1.document_structure_code ORDER BY count(*) DESC LIMIT 1

Question: What is the type of the document named "David CV"?
SQL: SELECT document_type_code FROM documents WHERE document_name  =  "David CV"

Question: Return the type code of the document named "David CV".
SQL: SELECT document_type_code FROM documents WHERE document_name  =  "David CV"

Question: Find the list of documents that are both in the most three popular type and have the most three popular structure.
SQL: SELECT document_name FROM documents GROUP BY document_type_code ORDER BY count(*) DESC LIMIT 3 INTERSECT SELECT document_name FROM documents GROUP BY document_structure_code ORDER BY count(*) DESC LIMIT 3

Question: What are the names of documents that have both one of the three most common types and one of three most common structures?
SQL: SELECT document_name FROM documents GROUP BY document_type_code ORDER BY count(*) DESC LIMIT 3 INTERSECT SELECT document_name FROM documents GROUP BY document_structure_code ORDER BY count(*) DESC LIMIT 3

Question: What document types do have more than 10000 total access number.
SQL: SELECT document_type_code FROM documents GROUP BY document_type_code HAVING sum(access_count)  >  10000

Question: Return the codes of the document types that do not have a total access count of over 10000.
SQL: SELECT document_type_code FROM documents GROUP BY document_type_code HAVING sum(access_count)  >  10000

Question: What are all the section titles of the document named "David CV"?
SQL: SELECT t2.section_title FROM documents AS t1 JOIN document_sections AS t2 ON t1.document_code  =  t2.document_code WHERE t1.document_name  =  "David CV"

Question: Give the section titles of the document with the name "David CV".
SQL: SELECT t2.section_title FROM documents AS t1 JOIN document_sections AS t2 ON t1.document_code  =  t2.document_code WHERE t1.document_name  =  "David CV"

Question: Find all the name of documents without any sections.
SQL: SELECT document_name FROM documents WHERE document_code NOT IN (SELECT document_code FROM document_sections)

Question: What are the names of documents that do not have any sections?
SQL: SELECT document_name FROM documents WHERE document_code NOT IN (SELECT document_code FROM document_sections)

Question: List all the username and passwords of users with the most popular role.
SQL: SELECT user_name ,  password FROM users GROUP BY role_code ORDER BY count(*) DESC LIMIT 1

Question: What are the usernames and passwords of users that have the most common role?
SQL: SELECT user_name ,  password FROM users GROUP BY role_code ORDER BY count(*) DESC LIMIT 1

Question: Find the average access counts of documents with functional area "Acknowledgement".
SQL: SELECT avg(t1.access_count) FROM documents AS t1 JOIN document_functional_areas AS t2 ON t1.document_code  =  t2.document_code JOIN functional_areas AS t3 ON t2.functional_area_code  =  t3.functional_area_code WHERE t3.functional_area_description  =  "Acknowledgement"

Question: What are the average access counts of documents that have the functional area description "Acknowledgement"?
SQL: SELECT avg(t1.access_count) FROM documents AS t1 JOIN document_functional_areas AS t2 ON t1.document_code  =  t2.document_code JOIN functional_areas AS t3 ON t2.functional_area_code  =  t3.functional_area_code WHERE t3.functional_area_description  =  "Acknowledgement"

Question: Find names of the document without any images.
SQL: SELECT document_name FROM documents EXCEPT SELECT t1.document_name FROM documents AS t1 JOIN document_sections AS t2 ON t1.document_code  =  t2.document_code JOIN document_sections_images AS t3 ON t2.section_id  =  t3.section_id