1 / 9

CS122 Using Relational Databases and SQL Aggregations

CS122 Using Relational Databases and SQL Aggregations. Chengyu Sun California State University, Los Angeles. Aggregation Functions. Returns a single results based on multiple rows COUNT SUM AVG MIN, MAX FIRST, LAST. Aggregation Function Examples. Number of movies released by SONY

susanlutz
Download Presentation

CS122 Using Relational Databases and SQL Aggregations

An Image/Link below is provided (as is) to download presentation Download Policy: Content on the Website is provided to you AS IS for your information and personal use and may not be sold / licensed / shared on other websites without getting consent from its author. Content is provided to you AS IS for your information and personal use only. Download presentation by click this link. While downloading, if for some reason you are not able to download a presentation, the publisher may have deleted the file from their server. During download, if you can't get a presentation, the file might be deleted by the publisher.

E N D

Presentation Transcript


  1. CS122 Using Relational Databases and SQLAggregations Chengyu Sun California State University, Los Angeles

  2. Aggregation Functions • Returns a single results based on multiple rows • COUNT • SUM • AVG • MIN, MAX • FIRST, LAST

  3. Aggregation Function Examples • Number of movies released by SONY • Total weekend grosses of the movies released by SONY • Average weekend grosses of the movies released by SONY • The movie released by SONY with the highest (lowest) weekend gross

  4. Aggregation Queries • How many consoles are sold in the first quarter at each store location? • How many consoles are sold in the Alhambra store each quarter? • What’s the annual sales of each console?

  5. GROUP BY • The number of movies released by each distributor SELECT dist, count(title) FROM Box_office GROUP BY dist;

  6. GROUP BY Multiple Columns SELECT location, sum(sales) FROM Sales_report GROUP BY location; SELECT location, quarter, sum(sales) FROM Sales_report GROUP BY location; SELECT location, quarter, sum(sales) FROM Sales_report GROUP BY location, quarter; SELECT location, quarter, month, sum(sales) FROM Sales_report GROUP BY location, quarter;

  7. Observations About GROUP BY • Aggregate on distinct value(s) of the group-by attribute(s) • Group-by attribute(s) must be the same as the non-aggregate attributes in the SELECT clause

  8. HAVING • The number of movies released by each distributor. Only list those who released at least 2 movies. SELECT dist, count(title) FROM Box_office GROUP BY dist HAVING count(title) >= 2;

  9. Conditional vs. Final Filter • HAVING – final filter • WHERE – conditional filter SELECT class, COUNT(*) FROM Student GROUP BY class HAVING class=3; SELECT class, COUNT(*) FROM Student WHERE class=3 GROUP BY class;

More Related