SQL_4GRADED.docx - Mary Doolin SQL_4 Homework COP4710...

Info icon This preview shows pages 1–5. Sign up to view the full content.

View Full Document Right Arrow Icon
Mary Doolin SQL_4 Homework COP4710 8.25/10 ********************************** Ex 4-1 ************************************ Display the COUNT of rows in each of the tables: Grade_report, Student, and Section. How many rows would you expect in the Cartesian product of all three tables? Display the COUNT (not the resulting rows) of the Cartesian product of all three and verify your result. Use SELECT COUNT(*) ... SQL> SELECT COUNT(*) 2 FROM GR; COUNT(*) ---------- 209 SQL> SELECT COUNT(*) 2 FROM Stu; COUNT(*) ---------- 48 SQL> SELECT COUNT (*) FROM rearp.Section; COUNT(*) ---------- 32 Cartesian Product of all three tables: grade_report * student * section 209 * 48 * 32 = 321024 SQL> SELECT COUNT(*) FROM Stu, GR, rearp.Section; COUNT(*) ---------- 321024 1
Image of page 1

Info iconThis preview has intentionally blurred sections. Sign up to view the full version.

View Full Document Right Arrow Icon
Mary Doolin SQL_4 Homework COP4710 ********************************** Ex 4-2 ************************************ Display the COUNT of section-ids from the Section table, and then the COUNT of DISTINCT section-ids from the Grade_report table. What does this information tell you? Hint: section_id is the primary key of the Section table. SQL> SELECT COUNT(SECTION_ID) 2 FROM sec; COUNT(SECTION_ID) ----------------- 32 SQL> SELECT COUNT(SECTION_ID) 2 FROM GR; COUNT(SECTION_ID) ----------------- 209 SQL>SELECT COUNT(DISTINCT(SECTION_ID)) 2 FROM Grade_report; COUNT(DISTINCT(SECTION_ID)) ----------------- 30 The Cartesian is highest number count for each table multiplied with other table. For section row count would be 32 and Grade_report would be 209. 2
Image of page 2
Mary Doolin SQL_4 Homework COP4710 ********************************** Ex 4-3a ************************************ Get the statement to work as a COUNT of a join of the three tables, Student, Grade_report, 105 and Section. Use table aliases in the join condition (remember to use /* join conditions */). Note that a join of n tables requires (n-1) join conditions, so here you have to have two join conditions—one to join the Student and Grade_report tables, and one to join the Grade_report and Section tables. Note the number of rows that you get (expect no more rows than is in the Grade_report table.) Why? SQL> SELECT COUNT(*) 2 FROM Stu, gr, sec 3 WHERE stu.stno = gr.student_number 4 --join student to grade_report 5 AND gr.section_id = sec.section_id 6 --join grade_report to section; COUNT(*) ---------- 209 ********************************** Ex 4-3b ************************************ Modify the query and put the accounting (ACCT) condition in the WHERE clause. Note the number of rows in the result—it should be a good bit less than in (a). SQL> SELECT COUNT(*) 2 FROM stu, gr, sec 3 WHERE stu.stno = gr.student_number 4 --join student to grade_report 5 AND gr.section_id = sec.section_id 6 --join grade_report to section 7 AND stu.major = 'ACCT'; --course_num, not major COUNT(*) ---------- 27 3
Image of page 3

Info iconThis preview has intentionally blurred sections. Sign up to view the full version.

View Full Document Right Arrow Icon
Mary Doolin SQL_4 Homework COP4710 ********************************** Ex 4-3c ************************************ Again, modify the query and add the grade constraints. The number of rows should decrease again. Note that if you have WHERE x and y or z, parentheses are optional, but
Image of page 4
Image of page 5
This is the end of the preview. Sign up to access the rest of the document.

{[ snackBarMessage ]}

What students are saying

  • Left Quote Icon

    As a current student on this bumpy collegiate pathway, I stumbled upon Course Hero, where I can find study resources for nearly all my courses, get online help from tutors 24/7, and even share my old projects, papers, and lecture notes with other students.

    Student Picture

    Kiran Temple University Fox School of Business ‘17, Course Hero Intern

  • Left Quote Icon

    I cannot even describe how much Course Hero helped me this summer. It’s truly become something I can always rely on and help me. In the end, I was not only able to survive summer classes, but I was able to thrive thanks to Course Hero.

    Student Picture

    Dana University of Pennsylvania ‘17, Course Hero Intern

  • Left Quote Icon

    The ability to access any university’s resources through Course Hero proved invaluable in my case. I was behind on Tulane coursework and actually used UCLA’s materials to help me move forward and get everything together on time.

    Student Picture

    Jill Tulane University ‘16, Course Hero Intern