Database: document_management

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: 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: 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: 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 average access count of documents?
SQL: SELECT avg(access_count) FROM documents

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

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: 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: 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: 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: 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

Question: What is the name of the document with the most number of sections?
SQL: SELECT t1.document_name FROM documents AS t1 JOIN document_sections AS t2 ON t1.document_code  =  t2.document_code GROUP BY t1.document_code ORDER BY count(*) DESC LIMIT 1

Question: List all the document names which contains "CV".
SQL: SELECT document_name FROM documents WHERE document_name LIKE "%CV%"

Question: How many users are logged in?
SQL: SELECT count(*) FROM users WHERE user_login  =  1

Question: Find the description of the most popular role among the users that have logged in.
SQL: SELECT role_description FROM ROLES WHERE role_code  =  (SELECT role_code FROM users WHERE user_login  =  1 GROUP BY role_code ORDER BY count(*) DESC LIMIT 1)