Join egghead, unlock knowledge.

Want more egghead?

This lesson is for members. Join us? Get access to all 3,000+ tutorials + a community with expert developers around the world.

Unlock This Lesson
1×
Become a member
to unlock all features

Level Up!

Access all courses & lessons on egghead today and lock-in your price for life.

Autoplay

    Select Grouped and Aggregated Data with SQL

    Tyler ClarkTyler Clark
    postgresPostgreSQL

    Another powerful SQL skill is understanding how to group rows together by column values. Once the data is grouped together, it can be difficult to understand how to actually work with the groupings. In this lesson, we will use the group by clause, as well as the count, sum, avg, min, and max aggregate functions.

    Counts with create date and first name example:

    select u.total, u.create_date, first_name from Users us inner join (select count(create_date) as total, create_date from Users group by create_date) u on u.create_date = us.create_date;

    Min create date with first name example:

    select create_date, first_name from Users where create_date = (select min(create_date) from Users);

    Aggregate functions:

    https://www.postgresql.org/docs/9.5/functions-aggregate.html https://dev.mysql.com/doc/refman/8.0/en/group-by-functions.html https://docs.microsoft.com/en-us/sql/t-sql/functions/aggregate-functions-transact-sql https://docs.oracle.com/database/121/SQLRF/functions003.htm

    Code

    Code

    Become a Member to view code

    You must be a Member to view code

    Access all courses and lessons, track your progress, gain confidence and expertise.

    Become a Member
    and unlock code for this lesson
    Discuss

    Discuss

    Transcript

    Transcript

    Instructor: Our users table currently has four rows of data. We've got a Tyler, Debbie, Mary, and Patrick. They each have unique user handles, but two of them share the same create date, 2019 January 10th. One has one in 2019 and the other is February 1st of 2019.

    When we select out data, I want to be able to group common column values together. For instance, I want to get a count of shared create dates. The group by clause does exactly what you would assume. It combines the rows returned from the select statement into groups.

    This is telling us that there are three groups of rows. Even though there are four rows in our users table, since we're grouping by create date, and two rows share the same value, we get three groups. Just to show both sides, when none of the rows share, let's say, a user handle, we'll get those four rows.

    Just seeing the count isn't really helpful, because we don't know which create dates are actually shared. Let's pull that out as well inside of our select statement. Now we know that the January 10th date is the one that has two rows grouped together. Let's also pull out the first name column values as well.

    As you can see, we're getting an error. This is telling us that the first name column needs to be added to the group by in order to pull this out. Remember, our rows are being grouped together by create date.

    Because more than one row can be in a group, as in our January 10th example, our database doesn't know which first name value to pull out of the grouping, which is why we're getting this error.

    If we add first name to the group by, we no longer get this error, but instead get all four rows back. This is because our group by is now looking to create groups that share create date and first name. Because none of our rows share these two columns, we get all four rows out.

    Keeping this mentality of grouping in mind, most SQL databases have built-in aggregate functions. For example, if we wanted to find what the lowest value create date was within our table, we'd use the min function.

    Aggregate functions are closely related to grouping operations like group by, which is why we'll get the same error as we did before when trying to pull out our first name column with the min function.

    As a side note, if you're curious on how to accomplish the task I talked about in this lesson, I'll post those two queries in the notes to this video. We need to do a little bit more than just group by and aggregate the create date values.

    Some other common aggregate functions are max, which does the opposite of min, there's sum, which sums up values, and average. Be sure to check the notes to this video for links to popular SQL databases that implement these, as well as their own functions.