Lesson 1

Mastering the COUNT Function


Hello and welcome to our journey through Mastering SQL Functions with Taylor Swift! In this course, we're going to embark on an engaging journey, honing our SQL skills while navigating the world of Taylor Swift's discography.

In today's lesson, we'll delve into the basics of SQL functions, with a particular focus on the COUNT function. This function serves as a convenient tool when conducting quantitative analysis of our data set, such as determining the total number of records in a table. So, let's get started!

Dataset Introduction

In case you haven't done any of our other courses using this dataset, here is a quick overview for you. Below are some sample rows to give you an idea of the structure starting from the simplest table:

Albums Table

22Speak Now (Deluxe Edition)2010-10-25
23Speak Now2010-10-25
24Fearless Platinum Edition2008-11-11

Albums table contains all of Taylor's albums including release dates.

Songs Table
0vvt4IZOMkRug195S4MUq022If This Was A Movie16spotify:track:0vvt4IZOMkRug195S4MUq046234546
0wavGRldH0AWyu2zvTz8zb6Sweet Nothing12spotify:track:0wavGRldH0AWyu2zvTz8zb73188496

This table lists Taylor Swift's songs, linking them to their albums, and includes details about track numbers, Spotify URIs, popularity ratings, and song lengths in milliseconds.

SongFeatures Table

The SongFeatures table breaks down the musical traits of each song, like its rhythm, energy, loudness, and mood, among others. It helps us understand what makes each song unique beyond just the lyrics and tune.

Understanding the COUNT Function

The COUNT function in SQL is an aggregate function that returns the number of rows matching a specified criterion. It is especially useful for counting the number of records in a table or the number of rows that meet a particular condition.

Here's the general syntax to use the COUNT function:

1SELECT COUNT(column_name) FROM table_name WHERE condition;

If you want to count all rows in a table, regardless of any NULL values in other columns, use:


On the other hand, when you specify a column name:


This counts the number of rows where the specified column has a non-NULL value. This distinction is important for understanding how to best use the COUNT function depending on your data analysis needs.

Let's now see it in action with our songs data.

Executing the First COUNT Query

Within the context of our Taylor Swift discography, we might be interested to know the total number of songs across all albums.

Here's the SQL query we would execute to determine that:


Upon running this query, you would receive an output reflecting the total number of songs present in our Songs table.

Expanding the Use of COUNT

Let's suppose we want to ascertain how many songs belong to a specific album. We could employ the COUNT function in tandem with a WHERE clause to tally the songs associated with a specific AlbumID:


This query will display the count of songs from album 1.

Remember, the COUNT function can prove to be a handy aid for quantitative analyses in numerous scenarios. It's a powerful tool to have in your SQL arsenal!

Wrapping Up and Looking Ahead

Congratulations on completing your first lesson in this course! You've made the initial stride in understanding SQL aggregate functions with a focus on COUNT.

In this lesson, we covered what the COUNT function is, when to use it, and even ran our own COUNT queries on the Songs table.

In the ensuing practice exercises, you'll be encouraged to implement what you've learned by composing your own COUNT queries. Don't hesitate to experiment and try distinct approaches - it's the best way to learn!

Ready to probe further? Let's forge ahead!

Enjoy this lesson? Now it's time to practice with Cosmo!

Practice is how you turn knowledge into actual skills.