lec10.365.worksheet

# lec10.365.worksheet - Fall 2007 CPE/CSC 365 Introduction to...

This preview shows pages 1–3. Sign up to view the full content.

. . Fall 2007 CPE/CSC 365: Introduction to Database Systems Alexander Dekhtyar . . SQL: Structured Query Language Grouping Queries: Example How Grouping works Consider two relations R(A,B,C) and S(B,D) (all attributes are integer). Con- sider the following instances of tables R and S : R A B C 1 2 2 1 4 4 2 2 5 3 4 8 2 4 1 3 4 12 S B D 2 1 4 7 Consider the following SQL query SELECT R.A, SUM(R.B), AVG(R.C), COUNT(*), MIN(S.D) FROM R, S WHERE R.B = S.B and R.C < 10 GROUP BY R.A HAVING COUNT(*) > 1; This query is evaluated as follows. Step 1. FROM Clause: Cartesian Product. First, the FROM clause is evalu- ated. It is convenient to view thist step as creation of a cartesian product of all tables referenced in the FROM clause. Here, this leads to computation of R × S : 1

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

View Full Document
R × S R.A R.B R.C S.B S.D 1 2 2 2 1 1 4 4 2 1 2 2 5 2 1 3 4 8 2 1 2 4 1 2 1 3 4 12 2 1 1 2 2 4 7 1 4 4 4 7 2 2 5 4 7 3 4 8 4 7 2 4 1 4 7 3 4 12 4 7 Step 2: WHERE Clause: Join and selection. On this step, each tuple in the cartesian product constructed on the previous step is evaluated against the conditions specified in the WHERE clause.
This is the end of the preview. Sign up to access the rest of the document.

{[ snackBarMessage ]}

### What students are saying

• 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.

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

• 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.

Dana University of Pennsylvania ‘17, Course Hero Intern

• 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.

Jill Tulane University ‘16, Course Hero Intern