Section 9 - The Magic of Aggregate Functions Flashcards
Give a few examples of aggregate data.
- Finding averages
- Sum a bunch of different things
- Grouping things by author, title, genre, sales numbers, class size, etc
- Average page count per author, etc.
What does the COUNT function do?
It does pretty much what you’d expect it to do (it COUNTs things) - at least in its basic form.
What would the following function do?
SELECT COUNT(*) from sports_cars;
This would COUNT all the sports_cars in our database.
What function counts things?
The COUNT function.
What would the following function do?
SELECT COUNT(repair_guides) FROM help_you_fix;
This would COUNT all repair_guides (column) from the help_you_fix database.
What might be incorrect about the following function?
SELECT COUNT(car_manufacturer) FROM cars;
This would count ALL instances of car_manufacturers FROM our cars database.
BUT, what if you had 20, 30, 40+ models from each manufacturer? This COUNT function would could each instance separately, so if you had, say, 20 different Toyota models, it would count Toyota 20 different times.
With this in mind, use the DISTINCT function, as in the following:
SELECT COUNT(DISTINCT car_manufacturer) FROM cars;
Write a function that does the following:
- Finds the number of unique car_models in a database
- The database is named all_things_cars
- Print the total number of unique car models as “This is the total number of cars in the All Things Car database: ____ (
SELECT CONCAT(‘This is the total number of cars in the All Things Car database:’, ‘ ‘, COUNT(DISTINCT car_models)) AS ‘Here’s the Result of Our Cars Query’ FROM all_things_car;
What does GROUP BY do?
It summarizes (or “aggregates”) identical data into single rows.
What are a few examples of functions you could run with GROUP BY?
- GROUP BY authors who released a book in 2017
- GROUP BY classes whose sizes are 30 students
- Group all of our books by genre
- Group all of our tea bags by tea type and then sort them by sales numbers
What do the MIN and MAX functions do?
They help identify minimum and maximum values in a table.
Write a function that finds the oldest (lowest) released_year from a table called mazda_cars.
SELECT MIN(released_year) FROM mazda_cars;
Write a function that finds the longest book (book_pages = column name) from a table named borders. Print back both the title and the page count. Use the MAX function to do so.
Will the below result give you the longest book title?
SELECT MAX(title) AS ‘This will take you awhile to read’ FROM borders;
NO, it will NOT! It will only give you the last title if the titles were in alphabetical order.
One solution is to run a subquery. You can find this below. (This is the long solution, but you’ll learn the more concise solution later. )
SELECT title, book_pages FROM borders WHERE book_pages = (SELECT MAX(book_pages) FROM borders);
Note that in the above query, you need the FROM statement twice.
Another way to find the same result would be as follows:
SELECT title, book_pages FROM borders ORDER BY book_pages DESC LIMIT 1;
Write the following query:
- Find the most recently released car_model from each car_make in the the cars table.
- Print only the most recently released car_model from each car manufacturer/car make. Print car_make, car_model, and car_model_released_year, in that order.
SELECT
car_make, car_model,
MIN(released_year) FROM cars
GROUP BY car_make, car_model;
I think the above is accurate.
Give some examples of things you could do with the SUM function.
- SUM the pages all the books in the database.
- SUM the sales for all yellow yoyos within the database
- SUM the sales totals for all orders under $100 places between January and February 2019.
- SUM together the payroll of all operations personal for a month.
Write a query that sums together all the pages in a book_shop database.
SELECT SUM(pages) FROM book_shop;