Git Product home page Git Product logo

sql-aggregate-functions-readme's Introduction

SQL Aggregate Functions

Overview

We'll cover basic aggregate functions and how to write SQL queries that utilize them.

Objectives

  1. Define aggregate functions and when to use them
  2. Use the following aggregate functions in SQL queries: average, sum, count, minimum, maximum

Operating on Data

Imagine writing an application for a restaurant owner to track her customers and transactions, or an app that an e-commerce company uses to store users, transactions and shopping behaviors. Think about creating a social networking application whose administrators want to keep track of the number of times users log on to identify who their most frequent users are. It's easy to see that storing or persisting information in an application or program is about more than just keeping track of static data. We can imagine any number of situations in which we want to operate on or analyze the data we store. Our restaurant owner will want to discover who her biggest spenders are or what they make on average over a busy weekend. Our e-commerce company wants to know who their most frequent buyers are and how much they spend on average on a given item, and so on.

We can do so using the aggregate functions that SQL makes available to us. With these functions we can sum and average column data, request minimum and maximum values, and more. SQL also includes keywords that allow us to group aggregated data by various categories and narrow our search criteria based on various conditions.

Aggregate Functions

Aggregate functions perform a calculation on specified values, queried from a database table. We will cover the following aggregators here:

  • AVG
  • SUM
  • COUNT
  • MIN
  • MAX

We'll craft queries that select a desired set of values from a table and then aggregate that data using the above aggregators, in addition to clauses that will group and/or order the returned data based on various conditions.

For this walk-through, we'll be utilizing a database of pets and owners.

Setting up the Database

Some cats are very famous and, accordingly, very wealthy. Our pets database will have a cats table in which each cat has a name, age, breed, and net worth.

Creating the Database:

Create the database in your terminal with the following:

sqlite3 pets_database.db

Creating the table:

In the sqlite> prompt in your terminal:

CREATE TABLE cats (
  name TEXT,
  age INTEGER,
  breed TEXT,
  net_worth INTEGER
);

Inserting the values:

INSERT INTO cats (name, age, breed, net_worth) VALUES ("Maru", 3, "Scottish Fold", 1000000);
INSERT INTO cats (name, age, breed, net_worth) VALUES ("Hana", 1, "Tabby", 21000);
INSERT INTO cats (name, age, breed, net_worth) VALUES ("Grumpy Cat", 4, "Persian", 181800);
INSERT INTO cats (name, age, breed, net_worth) VALUES ("Lil' Bub", 2, "Tortoiseshell", 3000000);

Confirming our Data:

SELECT * FROM cats;

should return:

name             age         breed          net_worth
---------------  ----------  -------------  ----------
Maru             3           Scottish Fold  1000000
Hana             1           Tabby          21000
Grumpy Cat       4           Persian        181800
Lil' Bub         2           Tortoiseshell  3000000  

Using Aggregators

Code Along I: AVG()

The average, AVG(), function returns the average value of a column. Here's how it works:

SELECT AVG(column_name) FROM table_name;

Let's write a query to grab the average net worth of our very lucrative cats.

SELECT AVG(net_worth) FROM cats;

This should return:

AVG(net_worth)
---------------
1050700.0

That return value is a little ugly, however. Let's use the AS keyword to rename the column. This is called "aliasing the return value".

SELECT AVG(net_worth) AS average_net_worth FROM cats;

This should return:

average_net_worth
--------------------
1050700.0

Code Along II: SUM()

The sum, SUM(), function returns the sum of all of the values in a particular column.

Here's how it works:

SELECT SUM(column_name) FROM table_name;

Let's try it out by calculating the sum of the net worths of all of our cats:

SELECT SUM(net_worth) FROM cats;

This should return:

SUM(net_worth)
--------------------
4202800

Code Along III: MIN() and MAX()

The minimum and maximum aggregator functions return the minimum and maximum values from a specified column respectively.

Here's how it works:

SELECT MIN(column_name) FROM table_name;
SELECT MAX(column_name) FROM table_name;

Let's try it out:

SELECT MIN(net_worth) FROM cats;

This should return:

MIN(net_worth)
--------------------
21000

Code Along IV: COUNT()

The count function returns the number of rows that meet a certain condition.

Here's how it works:

SELECT COUNT(column_name) FROM table_name;

We can use the COUNT() function to calculate the total number of rows in a table that are not NULL. NULL means empty. All of our cats have a name so we can call COUNT on the name column like this:

SELECT COUNT(name) FROM cats;

This should return:

COUNT(name)
--------------------
4

We have a total of four cats in our Cats table with a name. If we really didn't care about a specific column and we just wanted the total number of rows in our database we can call COUNT(*). * means everything. Sometimes it's called the "wildcard." This COUNT(*) will count the rows where at least one column has data in it.

We can also use COUNT() to count the total number of rows in a table that meet a certain condition. Let's use this aggregator to count the number of cats whose net worth is greater than one million:

SELECT COUNT(*) FROM cats WHERE net_worth > 1000000;

This should return:

COUNT(*)
--------------------
1

Because only Lil' Bub is that rich.

sql-aggregate-functions-readme's People

Contributors

annjohn avatar deniznida avatar drakeltheryuujin avatar eaud avatar franknowinski avatar gj avatar ihollander avatar jenmyers avatar jmburges avatar kevinmirc avatar lcorr8 avatar lizbur10 avatar maxcell avatar maxwellbenton avatar onyoo avatar sophiedebenedetto avatar

Watchers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

sql-aggregate-functions-readme's Issues

Issue with Data Presented in Cats Table

The instruction states that for Lil' Bub you should make net_worth = 2,000,000
INSERT INTO cats (name, age, breed, net_worth) VALUES ("Lil' Bub", 2, "Tortoiseshell", 2000000);

But when confirming table the lesson shows Lil' Bub's network as 20,000,000
name age breed net_worth


Maru 3 Scottish Fold 1000000
Hana 1 Tabby 21800
Grumpy Cat 4 Persian 181600
Lil' Bub 2 Tortoiseshell 20000000

So there is an extra 0 in the net_worth for Lil'Bub, this throws off all calculations for that column

Asked to comment out Test Case because identical Output Not recognized by Test Case

I am attempting to finish a test case for a lab but despite identical
results the test case is giving errors. (test case on line 109 for the
sql lab)

I spoke with Ms. Kenlyn Terai @ 1/22/20 2:20 PM< EST and was asked to
comment out the test case and submit since the output was identical
but the error was still there.

I am enclosing the error along with the src

=begin
#selects_user_names_and_amounts_of_all_pledges_grouped_by_name_then_orders_them_by_the_summed_amount
(FAILED - 1)
#selects_the_category_names_and_pledge_amounts_of_all_pledges_in_the_music_category
#selects_the_category_name_and_the_sum_total_of_the_all_its_pledges_for_the_books_category

Failures:

  1. crowdfunder sql questions
    #selects_user_names_and_amounts_of_all_pledges_grouped_by_name_then_orders_them_by_the_summed_amount
    Failure/Error:
    expect(@db.execute(selects_user_names_and_amounts_of_all_pledges_grouped_by_name_then_orders_them_by_the_summed_amount)).to
    eq([["Iguana", 10],
    ["Swizzle", 12], ["Sirius", 19], ["Alex", 20], ["Marisa", 24],
    ["Voldemort", 34], ["Amanda", 40], ["Bear", 50], ["Hermione", 50],
    ["Rosey", 50], ["Pacha", 60], ["So
    phie", 60], ["Finnebar", 70], ["Franz", 90], ["Ena", 100], ["Whale",
    125.5], ["Katie", 170], ["Squid", 270], ["Albus", 470], ["Victoria",
    1700]])

    expected: [["Iguana", 10], ["Swizzle", 12], ["Sirius", 19],
    ["Alex", 20], ["Marisa", 24], ["Voldemort", 34], ["... ["Ena", 100],
    ["Whale", 125.5], ["Katie",
    170], ["Squid", 270], ["Albus", 470], ["Victoria", 1700]]D
    got: [["Iguana", 10], ["Swizzle", 12], ["Sirius", 19],
    ["Alex", 20], ["Marisa", 24], ["Voldemort", 34], ["... ["Ena", 100],
    ["Whale", 125.5], ["Katie",
    170], ["Squid", 270], ["Albus", 470], ["Victoria", 1700]]

    (compared using ==)

    ./spec/crowdfunder_spec.rb:110:in `block (3 levels) in <top (required)>'

Finished in 0.68723 seconds (files took 1.38 seconds to load)
22 examples, 1 failure

Failed examples:

rspec ./spec/crowdfunder_spec.rb:109 # crowdfunder sql questions
#selects_user_names_and_amounts_of_all_pledges_grouped_by_name_then_orders_them_by_the_summed_amount

=end

SRC:

CREATE TABLE users (
id INTEGER PRIMARY KEY,
name TEXT,
age INTEGER);

CREATE TABLE projects (
id INTEGER PRIMARY KEY,
title TEXT,
category TEXT,
funding_goal NUMERIC,
start_date TEXT,
end_date TEXT);

CREATE TABLE pledges (
id INTEGER PRIMARY KEY,
amount NUMERIC,
user_id INTEGER,
project_id INTEGER);

--

# Write your sql queries in this file in the appropriate method like the example below:
#
# def select_category_from_projects
# "SELECT category FROM projects;"
# end

# Make sure each ruby method returns a string containing a valid SQL statement.

def selects_the_titles_of_all_projects_and_their_pledge_amounts_alphabetized_by_title
=begin
SELECT  projects.title, SUM(amount) FROM projects JOIN pledges ON pledges.project_id=projects.id GROUP BY projects.id ORDER BY projects.title
=end
  "SELECT  projects.title, SUM(amount) FROM projects JOIN pledges ON pledges.project_id=projects.id GROUP BY projects.id ORDER BY projects.title"
end

def selects_the_user_name_age_and_pledge_amount_for_all_pledges_alphabetized_by_name
=begin
SELECT users.name, users.age, SUM(amount) FROM users JOIN pledges ON pledges.user_id= users.id GROUP BY users.id ORDER BY users.name
=end
  "SELECT users.name, users.age, SUM(amount) FROM users JOIN pledges ON pledges.user_id= users.id GROUP BY users.id ORDER BY users.name"
end

def selects_the_titles_and_amount_over_goal_of_all_projects_that_have_met_their_funding_goal
=begin
  SELECT  projects.title,  (SUM(amount) -projects.funding_goal) AS over_goal FROM projects JOIN pledges ON pledges.project_id=projects.id GROUP BY projects.id HAVING SUM(amount)> projects.funding_goal
=end
  "SELECT  projects.title,  (SUM(amount) -projects.funding_goal) AS over_goal FROM projects JOIN pledges ON pledges.project_id=projects.id GROUP BY projects.id HAVING SUM(amount)>= projects.funding_goal"

end

def selects_user_names_and_amounts_of_all_pledges_grouped_by_name_then_orders_them_by_the_summed_amount
=begin
SELECT users.name,  SUM(pledges.amount) AS total FROM users JOIN pledges ON pledges.user_id= users.id GROUP BY users.id ORDER BY total
=end
  "SELECT users.name,  SUM(pledges.amount) FROM users JOIN pledges ON users.id=pledges.user_id  GROUP BY users.id ORDER BY SUM(pledges.amount)"
end

def selects_the_category_names_and_pledge_amounts_of_all_pledges_in_the_music_category
=begin
SELECT projects.category, pledges.amount FROM pledges JOIN projects ON pledges.project_id=projects.id where projects.category="music"
=end
  "SELECT projects.category, pledges.amount FROM pledges JOIN projects ON pledges.project_id=projects.id where projects.category=\"music\""
end

def selects_the_category_name_and_the_sum_total_of_the_all_its_pledges_for_the_books_category
=begin
  SELECT projects.category, SUM(pledges.amount) FROM pledges JOIN projects ON pledges.project_id=projects.id GROUP BY projects.category HAVING projects.category="books"
=end
  "SELECT projects.category, SUM(pledges.amount) FROM pledges JOIN projects ON pledges.project_id=projects.id GROUP BY projects.category HAVING projects.category=\"books\""
end

and/or typo

Near Top of Lesson, under subtitle, "Aggregate Functions", this line is missing the / in the 'and or' .

"We'll craft queries that select a desired set of values from a table and then aggregate that data using the above aggregators, in addition to clauses that will group and or order the returned data based on various conditions."

Spelling Mistake

The second Objective has an extra 'r' in aggregate:
"Use the following aggregrate functions in SQL queries: average, sum, count, minimum, maximum"

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.