HW2 DBFall 2023 Eclass
HW2 DBFall 2023 Eclass
Homework 2
Issued Sunday 22 October. Due as electronic copy (PDF only!) into eCLASS by Monday 30 October
2023 at noon (12:00, system’s clock). Hardcopy: none (upon request only for specific cases).
IMPORTANT: Multiple submission by members of the same team are penalized as follows:
For each team member who submits the document already submitted, scoring (of related Quiz
2) is reduced by 5/100 points for all team members. Example: A team of three, three members
submit: score of Quiz 2 reduced by 10 points (over 100 points) for all team members.
4.1 The name of the submitted PDF file (or, in the case you used LATEX, the name of the zipped archive
containing the two required files: .tex and .pdf) is required to be in the following format:
00X-ID (example: 001-U2110123), where:
- 00X is the section of the team’s member, say S, who submits the homework, and
- ID is the ID number of S.
5. IMPORTANT: Homework that containts evident elements of plagiarism or cheating, either among
teams or from existing material (e.g., from the Internet, textbook’s Instructor Manual if available online,
etc.) implies a mark 0/100 for all teammates and all future homework assignments. (Equivalently,
you will not be allowed to enter any of future in-class quizzes.)
5.1 IMPORTANT: Sharing of homework’s file(s) among teams is never permitted at whatever stage of
work in progress and it results in a main source of assigned zeros. You can share ideas, not files!
Database (SOC-3020), Fall 2023 – Dr. Alessandro Agostini Homework 2 2
PARTICULAR SETTING:
To proceed, you need to install M Y SQL S ERVER and the graphical interface/console phpMyAdmin for
administering and browsing MySQL (or any compatibly other) database server.
IMPORTANT: You are not allowed to use any other *interface* than phpMyAdmin. Please
read the slides, ask to a classmate, or contact the Instructor or TA if you need help at this stage.
By using phpMyAdmin, create a database (give the name you like; below we use “University DB”)
by importing the ‘DDL for MySQL’ and ‘smallRelationsInsertFile.sql’ files available from textbook’s
website (https://www.db-book.com/db6/, 6th edition); cf. slides for Week 2 (see Appendix) for instal-
lation guidelines. The (partial) logical schema of such database is showed in Figure 1 (page 5).
NOTE: If you submitted Homework #1, simply use the database you created previously.
1. For each SQL query, say Q, in the slides discussed in class, exactly from slide 14 (out of 34) in
slide-set of Week 5 to slide 43 (out of 92) in slide-set of Week 6 (both slide-sets are available in
eClass), do:
(a) Rewrite Q into your document by using plain SQL as written in the slides. Next to the
query write the slide-set (example: W3 for slides of Week 3; W4 for slides of Week 4,...)
and the slide’s page number (you find it bottom-right corner of each slide) of the slide where
Q is written. Example: if Q is the SQL query written in the slide with page number 52 of
Week 3, you could write something like W3-p52 next to Q.
NOTE: We expect that each SQL query in the range of slides as required is processed.
We evaluate this part of the homework with particular attention in ordrr to measure
the quantity of work done by the team.
(b) By using phpMyAdmin’s Console, rewrite Q and screen-shot it. Then embed the screen-
shot in your document just below the query written in plain SQL (cf. previous point).
NOTE: You can screen-shot it together with the result of its execution, see (d) below.
(c) Execute the query over the database “University DB”.
(d) Screen-shot the result of execution (table’ schema and instance). Two cases arise:
Case 1. Execution leads to result. Then embed the screen-shot of such result in your
document just below the screen-shot of the query you embedded (cf. point (b) above).
Case 2. Execution leads to “syntax error” or any other execution error. Then:
- debug your query (refer to M Y SQL’s manual, if necessary) and execute it again (and
again, if necessary) until you managed to solve the error and correctly execute the query;
- screen-shot both the query you finally executed and its result (table’ schema and in-
stance); embed it into your document just below the screen-shot of the query you embedded
in (b) above (i.e., the original query Q);
- explain in short what kind of issue you had to correctly execute the query. Be brief yet
precise to identify the issue(s) you faced.
Database (SOC-3020), Fall 2023 – Dr. Alessandro Agostini Homework 2 3
NB: If, after several debugging cycles, you could not execute the query, explain the reasons
you could not execute the query even if after careful debugging.
2. For each of the queries in English listed in “List of queries” below, do:
(a) Write an equivalent SQL query (according to definition below) by using plain SQL (i.e.,
SQL statements and structure discussed in class and in the slides).
Definition: Two queries (in whatever language you express each of them) on a (re-
lational) database DB are equivalent if they produce (represent, compute, process)
exactly (a) the same structure (schema) and (b) the same content (instance) over DB’
schema and data instance.
(b) By using the Console of phpMyAdmin, execute the query on the “University DB” created
in the section “Particular Setting” above. If a syntax error is promped by the system, un-
derstand the error, rewrite the query and execute it again. Iterate the process, if necessary.
Finally:
1 - write in plain text the SQL query as modified to run without errors on phpMyAdmin;
2 - embed in the document the screenshot of the query’s result (table’ schema and instance).
TECHNICAL NOTE: You might need to insert extra data in some tables to show the
effects of a query’s execution. In these cases, you have to write not only the query, but
also the SQL INSERT... statement(s) you used to add the required extra data.
List of queries:
i. By using an appropraite subquery in the appropriate SQL clause, find all sections that
had the maximum enrollment and display each of then by section id and enrollment.
ii. Find all students who have two or more F grades as per the takes relation, and list
them along with the F grades. Display the identifier (primary key) of each student.
iii. Find all rooms that have been assigned to more than one section at the same time.
Display the rooms in descending order of capacity along with the assigned sections.
iv. Find the maximum and minimum enrollment across all sections, considering only
sections that had some enrollment. (Don’t worry about those that had no students
taking that section.)
Database (SOC-3020), Fall 2023 – Dr. Alessandro Agostini Homework 2 4
v. Find all sections that had the maximum enrollment (along with the enrollment), using
a subquery. Display the identifier (primary key) of each section.
vi. As in query iv. above but now also include sections with no students taking them; the
enrollment for such sections should be treated as 0.
In particular, represent the English query by proposing two different SQL queries
(and create required data for testing, cf. TECHNICAL NOTE above), namely:
a. Query 1, where you are asked to use a scalar subquery.
b. Query 2, where you are asked to use aggregation on a left outer join (use the SQL
natural left outer join syntax).
vii. Find all courses whose identifier (i.e., primary key) starts with the string “CS-”. Dis-
play identifier and title of each course.
viii. Find instructors who have taught all the above courses by using the “NOT EXISTS
... EXCEPT ...” combination of SQL statements.
ix. Insert each instructor as a student, with tot_creds = 0, in the same department.
x. Delete all the newly added “students” above (note: already existing students who
happened to have tot_creds = 0 have not get deleted!).
(a) Write a SQL query Q on the “University DB” where the clause is used.
Note: Be creative. Be interesting. You can write any SQL query you like, provided that
(a) it containts the clause you have to use or its negation (use of NOT, if appropriate w.r.t.
syntax), (b) your query is reasonably different from any of the SQL queries contained in the
slide-sets from Week 3 to Week 5, and (c) your query correctly executes on the “University
DB” via phpMyAdmin’s Console (cf. point (c) below).
IMPORTANT: You might need to refer to your version of M Y SQL’s manual for
the syntax and correct usage of each clause. Note also that some clauses could be
used in different contexts and with different purposes. You decide the context and
purpose. Explain your decision, if a decision must be taken.
(b) Rewrite Q by using plain English. In other word, explain by using English the expected
result of your query.
(c) By using phpMyAdmin’s Console, execute the query over the database “University DB”.
(d) Screen-shot the SQL query and the result of execution (table’ schema and instance). Then,
embed the screen-shot in your document just below the query written in plain English (cf.
previous point (b)).
End of Homework 2