Thursday-solution_-1139956641

PDF

School

University of Toronto *

*We aren’t endorsed by this school

Course

343

Subject

Computer Science

Date

Jan 9, 2024

Type

PDF

Pages

9

Uploaded by ProfBuffalo3824

Report
Fall 2023 Midterm CSC 343 H1F Duration: 90 min. Question 1. [11 marks] Part (a) [1 mark] Using bag semantics, what is {8, 4, 4, 2, 10, 10, 10, 1} ∩ {1, 1, 4, 5, 10, 10}? {4, 10, 10, 1} Part (b) [4 marks] Consider this schema: Ocean(wave, surf, dolphin) Beach(sand, sun) Ocean[surf] ⊆ Beach[sand] Suppose we know that relation Beach has 15 tuples. 1. What is the fewest tuples that Ocean could have? 0 2. What is the most tuples that Ocean could have? (write “no limit" if there is no limit) no limit Suppose we know instead that relation Ocean has 21 tuples. 3. What is the fewest tuples that Beach could have? 1 4. What is the most tuples that Beach could have? (write “no limit" if there is no limit) no limit Part (c) [3 marks] Consider this schema with two relations and no other constraints: R1(alpha, bravo, charlie ) R2(charlie, echo , foxtrot) Suppose we know that R2 has 5 tuples (but nothing about what’s in them), and that the contents of R1 a alpha bravo charlie 1 2 3 4 2 5 4 6 5 1. What is the most tuples possible from this query? ( 𝜎 𝑎𝑙𝑝 𝑎 = foxtrot ( 1 ⋈ 2)) 𝑅 𝑅 10 2. What is the most tuples possible from this query? Π 𝑏𝑟 𝑎𝑣𝑜 , ℎ 𝑐 𝑎𝑟𝑙𝑖𝑒 ( 𝜎 𝑎𝑙𝑝 𝑎 = foxtrot ( 1 ⋈ 2)) 𝑅 𝑅 3 3. What is the most rows possible from the SQL query below? 10 Note: the reference to charlie in this query was ambiguous; it should have said R1.charlie, below.Many students didn’t notice. We told many who asked that it should be R1.charlie. We also accepted the answer 0 if there was an explanation saying that the query doesn’t run. SELECT bravo, R1.charlie FROM R1, R2 WHERE alpha = foxtrot; Part (d) [3 marks] Suppose we have tables Pony with 12 rows, and Cow with 8 rows, and there are no NULL values in t Assume each of the queries below runs without error. This is all you know. For each of the queries below, what is the fewest possible rows it could yield? 1. SELECT * FROM Pony RIGHT JOIN Cow ON hoof = ear; 8 2. SELECT * FROM Pony LEFT JOIN Cow ON hoof = ear; 12 3. SELECT * FROM Pony NATURAL JOIN Cow; 0
Fall 2023 Midterm CSC 343 H1F Duration: 90 min. This space is for any rough work you might like to do. It will not be marked.
Fall 2023 Midterm CSC 343 H1F Duration: 90 min. Question 2. [6 marks] Recall this schema which you saw in a prep exercise: Relations Employee(eid, name, salary, dept) Department(did, name, division) Sales(eid, day, amount) The employee with this eid had sales valued at this amount on this day. Manages(manager, junior) Employee“manager” manages employee “junior”. Integrity constraints Employee[dept] ⊆ Department[did] Sales[eid] ⊆ Employee[eid] Manages[manager] ⊆ Employee[eid] Manages[junior] ⊆ Employee[eid] We want to find the eid of every employee who has had a day with sales valued at over 20, but has neve with sales valued at over 50. For each query below circle one answer to indicate whether the query is invalid (cannot be evaluated): in this case, explain why it is invalid. valid (can be evaluated) but incorrect: in this case, give a valid instance of relation Sales that dem this, as well as the expected query result and the actual query result. The expected result must be non-empty, unless that is impossible. or correct : in this case, no explanation or instance is required. 1. Π 𝑒𝑖𝑑 ( 𝜎 𝑎𝑚𝑜𝑢𝑛𝑡 650 ( 𝜎 𝑎𝑚𝑜𝑢𝑛𝑡 >20 𝑆𝑎𝑙𝑒𝑠 )) Invalid Valid but incorrect Correct Explanation below: Instance below: No explanation or instance required The inner sigma keeps only tuples with an amount over 20. It keeps entire tuples with all 3 attributes. Of these, the next sigma keeps the tuples that have an amount at most 50. The net effect of both sigmas is to keep tuples with an amount in the range (20, 50] . The project keeps the eids from these rows, so we end up with the eid of anyone who had a day of sales valued in the range (20, 50] . This does not guarantee that they’ve never had a day over 50! Any instance that has a person with a day in the range (20, 50] AND a day over 50 demonstrates the bug. The question also requires a non-empty expected result, so we must inc someone who has had a day over 20 but never had a day over 50. (NB: This explanation was NOT required on the test. It’s just for your learning.) Input instance: eid day amount 123 A 23 123 B 51 456 C 48 Expected result: eid 456 Actual result: eid 123 456
Your preview ends here
Eager to read complete document? Join bartleby learn and gain access to the full version
  • Access to all documents
  • Unlimited textbook solutions
  • 24/7 expert homework help
Fall 2023 Midterm CSC 343 H1F Duration: 90 min. 2. 𝑒𝑖𝑑 𝜎 𝑎𝑚𝑜𝑢𝑛𝑡 >20 𝑆𝑎𝑙𝑒𝑠 ) − (Π 𝑒𝑖𝑑 𝜎 𝑎𝑚𝑜𝑢𝑛𝑡 >50 𝑆𝑎𝑙𝑒𝑠 ) Invalid Valid but incorrect Correct Explanation below: Instance below: No explanation or instance required No explanation or instance required. 3. 𝑒𝑖𝑑 ( 𝜎 𝑎𝑚𝑜𝑢𝑛𝑡 >20 𝑆𝑎𝑙𝑒𝑠 )] ∩ [Π 𝑒𝑖𝑑 ( 𝜎 𝑎𝑚𝑜𝑢𝑛𝑡 650 𝑆𝑎𝑙𝑒𝑠 )] Invalid Valid but incorrect Correct Explanation below: Instance below: No explanation or instance required This expression intersects the eids of people who’ve had a day over 20 with the eids of people w a day of at most 50. As in part (1), we end up with the eid of anyone who had a day of sales valued range(20, 50] , which does not guarantee that they’ve never had a day over 50! The same instance (1), or any instance that has a person with a day in the range (20, 50] AND a day over 50, demonstrates the bug. Input instance: eid day amount 123 A 23 123 B 51 456 C 48 Expected result: eid 456 Actual result: eid 123 456
Fall 2023 Midterm CSC 343 H1F Duration: 90 min. Question 3. [13 marks] Below is the University schema that we used extensively in class. Relations Student(sID, surName, firstName, campus, email, cgpa) Course(dept, cNum, name, breadth) Offering(oID, dept, cNum, term, instructor) Took(sID, oID, grade) Integrity constraints Offering[dept, cNum] ⊆ Course[dept, cNum] Took[sID] ⊆ Student[sID] Took[oID] ⊆ Offering[oID] Part (a) [3 marks] Which of the instances of Took below violate this integrity constraint? Circle one answer for each insta 𝑇𝑒𝑚𝑝 ( ) ∶= Π 𝑜𝑖𝑑 𝑇 1. 𝑜𝑖𝑑 𝜎 𝑇 1. = 2. 𝑜𝑖𝑑 𝑇 𝑜𝑖𝑑 𝑇 1. 2. 𝑠𝑖𝑑 𝑇 𝑠𝑖𝑑 [ ( 𝜌 𝑇 1 𝑇𝑜𝑜𝑘 ) × ( 𝜌 𝑇 2 𝑇𝑜𝑜𝑘 ) ] ( 𝑇𝑒𝑚𝑝 ) ( Π 𝑜𝑖𝑑 𝜎 𝑔𝑟𝑎𝑑𝑒 60 𝑇𝑜𝑜𝑘 ) = ∅ Violates? yes no sID oID grade 922 1 52 555 1 40 123 1 58 444 2 68 319 2 61 Violates? yes no sID oID grade 922 1 40 555 5 50 444 2 58 319 6 52 Violates? yes no sID oID grade 922 3 82 444 3 45 555 3 40 123 3 85 319 3 91 Part (b) [2 marks] Use plain English to explain what this constraint expresses. Your answer should be precise and concise. Do not make any reference to the algebra. Solution: There are many ways to say this, including: If an offering has two or more students, they can’t all get a grade below 60. OR If an offering has two or. more grades, they can’t all be below 60. OR If an offering has two or more students, at least one student has to get 60 or higher.
Fall 2023 Midterm CSC 343 H1F Duration: 90 min. Part (c) [8 marks] The university is interested in knowing who started teaching during covid and has broad teaching ex Write a query in relational algebra to find all instructors who meet these conditions: (a) their earliest (firs of any course was in term 20201, and (b) they have taught in at least two depts or they have taught a the dept is "JSC". You must name the attributes on the LHS of each assignment statement you use. Solution: – This instructor offered a course in this term. Offered ( , ) ∶= Π 𝑖𝑛𝑠𝑡𝑟 𝑢𝑐𝑡𝑜𝑟 𝑡𝑒𝑟 𝑚 𝑖𝑛𝑠𝑡𝑟 𝑢𝑐𝑡𝑜𝑟 , 𝑡𝑒𝑟 𝑚 Offering – This is not the first term when this instructor offered something. 𝑁 𝑜𝑡𝐹 𝑖𝑟 𝑠𝑡 ( , ) ∶= Π 𝑖𝑛𝑠𝑡𝑟 𝑢𝑐𝑡𝑜𝑟 𝑡𝑒𝑟 𝑚 𝑂 1. , 1. 𝑖𝑛𝑠𝑡𝑟 𝑢𝑐𝑡𝑜𝑟 𝑂 𝑡𝑒𝑟 𝑚 ( 𝜎 𝑂 1. = 2. 𝑖𝑛𝑠𝑡𝑟 𝑢𝑐𝑡𝑜𝑟 𝑂 𝑖𝑛𝑠𝑡𝑟 𝑢𝑐𝑡𝑜𝑟 𝑂 1. > 2. 𝑡𝑒𝑟𝑚 𝑂 𝑡𝑒𝑟𝑚 ( 𝜌 𝑂 1 Offering × 𝜌 𝑂 1 Offering )) – This is the first term when this instructor offered something. 𝐹 𝑖𝑟 𝑠𝑡 ( , ) ∶= 𝑖𝑛𝑠𝑡𝑟 𝑢𝑐𝑡𝑜𝑟 𝑡𝑒𝑟 𝑚 Offered 𝑁 𝑜𝑡𝐹 𝑖𝑟 𝑠𝑡 – The instructor had their earliest offering of any course in term 20201. 𝐽 𝑢𝑛𝑖𝑜𝑟 ( ) ∶= Π 𝑖𝑛𝑠𝑡𝑟 𝑢𝑐𝑡𝑜𝑟 𝑖𝑛𝑠𝑡𝑟 𝑢𝑐𝑡𝑜𝑟 𝜎 𝑡𝑒𝑟𝑚 =20201 𝐹 𝑖𝑟 𝑠𝑡 – This instructor taught in at least two departments. 𝑇𝑎𝑢𝑔 𝑡𝐼 𝑛𝑇𝑤𝑜 ( ) ∶= 𝑖𝑛𝑠𝑡𝑟 𝑢𝑐𝑡𝑜𝑟 𝜎 𝑂 1. = 2. 1. 2. 𝑖𝑛𝑠𝑡𝑟 𝑢𝑐𝑡𝑜𝑟 𝑂 𝑖𝑛𝑠𝑡𝑟 𝑢𝑐𝑡𝑜𝑟 𝑂 𝑑𝑒𝑝𝑡 𝑂 𝑑𝑒𝑝𝑡 ( 𝜌 𝑂 1 Offering × 𝜌 𝑂 1 Offering ) – This instructor has taught a JSC course. 𝑇𝑎𝑢𝑔 𝑡𝐽 𝑆𝐶 ( ) ∶= Π 𝑖𝑛𝑠𝑡𝑟 𝑢𝑐𝑡𝑜𝑟 𝑖𝑛𝑠𝑡𝑟 𝑢𝑐𝑡𝑜𝑟 𝜎 𝑑𝑒𝑝𝑡 = 𝐽 𝑆𝐶 Offering – This instructor meets all the criteria for inclusion in the answer. 𝐴𝑛𝑠𝑤𝑒𝑟 ( ) ∶= ∩ ( ) 𝑖𝑛𝑠𝑡𝑟 𝑢𝑐𝑡𝑜𝑟 𝐽 𝑢𝑛𝑖𝑜𝑟 𝑇𝑎𝑢𝑔 𝑡𝐼 𝑛𝑇𝑤𝑜 𝑇𝑎𝑢𝑔 𝑡𝐽 𝑆𝐶 There is a simpler way to find the junior instructors, that doesn’t require finding everyone’s firs term: – This instructor taught in 20201, and never before that. 𝐽 𝑢𝑛𝑖𝑜𝑟 ( ) ∶= (Π 𝑖𝑛𝑠𝑡𝑟 𝑢𝑐𝑡𝑜𝑟 𝑖𝑛𝑠𝑡𝑟 𝑢𝑐𝑡𝑜𝑟 𝜎 𝑡𝑒𝑟𝑚 =20201 Offering ) − (Π 𝑖𝑛𝑠𝑡𝑟 𝑢𝑐𝑡𝑜𝑟 𝜎 𝑡𝑒𝑟𝑚 <20201 Offering )
Your preview ends here
Eager to read complete document? Join bartleby learn and gain access to the full version
  • Access to all documents
  • Unlimited textbook solutions
  • 24/7 expert homework help
Fall 2023 Midterm CSC 343 H1F Duration: 90 min. Question 4. [8 marks] This question continues with the University database. Assume that it has been defined in SQL. Relations Student(sID, surName, firstName, campus, email, cgpa) Course(dept, cNum, name, breadth) Offering(oID, dept, cNum, term, instructor) Took(sID, oID, grade) Integrity constraints Offering[dept, cNum] ⊆ Course[dept, cN Took[sID] ⊆ Student[sID] Took[oID] ⊆ Offering[oID] Define a SQL query to find departments that have had fewer than 1000 different students take the 200 level or higher. Report the department, the number of different courses at the 400 level, a average grade given in this department (across all offerings of courses at all levels). Assume that every department has at least one course, every course has at least one off every offering has at least one grade recorded. You may create intermediate views.
Fall 2023 Midterm CSC 343 H1F Duration: 90 min. Continue your answer on this page if needed. Solution : -- This dept has had fewer than 1000 different students take a course at -- the 200 level or higher. create view targetDepartment as select dept from Took join Offering on Took.oID = Offering.oID where cNum >= 200 group by dept having count(distinct SID) < 1000; -- This dept has this many different courses at the 400 level. -- It’s okay to use Offering in this step because, although we didn’t specify that -- the course had to be offered in order to contribute to the department’s count -- of 400-level courses, we did say that every course has an offering. But if we -- use Offering here, we must use count(DISTINCT cnum)! -- If we instead using Course in this step, as is done below, DISTINCT is not -- needed, but does no harm. create view SeniorCourses as select dept, count(cnum) as numSenior from Course where cnum >= 400 and cnum < 500 group by dept; -- This dept has this average grade (across all offerings of courses at all levels) create view deptAverage as select dept, avg(grade) as avgGrade from Took join Offering on Took.oID = Offering.oID group by dept; -- Need a left join to bring in SeniorCourses because a target dept may not -- have any 400-level courses. -- Don’t need left join with deptAverage due to assumptions; they guarantee that -- every dept has an average grade. select td.dept, numSenior, avgGrade from targetDepartment td left join SeniorCourses sc on td.dept = sc.dept join deptAverage da on td.dept = da.dept;
Fall 2023 Midterm CSC 343 H1F Duration: 90 min. Question 5. [6 marks] Below are four similar SQL queries on the same schema about sales from Question 3: -- Query 1: select manager as successful from Manages, Sales s1, Sales s2 where junior = s1.eid and manager = s2.eid; -- Query 2: select manager as successful from Manages, Sales where manager = eid and junior in (select eid from sales); -- Query 3: select manager as successful from Manages, Sales where junior = eid and manager in (select distinct eid from sales); -- Query 4: select manager as successful from Manages where junior in (select distinct eid from sales) and manager in (select distinct eid from sales); Consider a manager (let’s call her Sonia) who meets the following conditions: She manages ≥ 1 𝑘 juniors. 𝑗 ≥ 1 of her juniors have had sales (that is, appear in the Sales table). In total, her juniors appear 𝑚 times in the Sales table. Sonia herself appears ≥ 1 𝑛 times in the Sales table. For each question, circle one answer and provide the required additional information below. 1. Will Query 1 run successfully? Yes No If yes, Sonia will appear in the result this many times: 𝑚 × 𝑛 If no, this is why there is an error: 2. Will Query 2 run successfully? Yes No If yes, Sonia will appear in the result this many times: 𝑗 × 𝑛 If no, this is why there is an error: 3. Will Query 3 run successfully? Yes No If yes, Sonia will appear in the result this many times: 𝑚 If no, this is why there is an error: 4. Will Query 4 run successfully? Yes No If yes, Sonia will appear in the result this many times: 𝑗 If no, this is why there is an error:
Your preview ends here
Eager to read complete document? Join bartleby learn and gain access to the full version
  • Access to all documents
  • Unlimited textbook solutions
  • 24/7 expert homework help