Counting data

Introduction to SQL with AI

Jasmin Ludolf

Senior Data Science Content Developer

The COUNT function

Prompt: How many products do we have?

SELECT COUNT(*) AS total_count
FROM products;
|total_count|
|-----------|
|50         |
Introduction to SQL with AI

The COUNT function

Prompt: How many products do we have?

SELECT COUNT(*)
FROM products;
|?count?|
|-------|
|50     |
Introduction to SQL with AI

The COUNT function

Prompt: Count the number of rows in the products table

SELECT COUNT(*) AS total_count
FROM products;
|total_count|
|-----------|
|50         |
Introduction to SQL with AI

Counting records or values

Prompt: How many products do we have?

SELECT COUNT(*) AS total_count
FROM products;
|total_count|
|-----------|
|50         |
  • Counts every record in the table

Prompt: How many product names do we have?

SELECT COUNT(product_name) AS product_count
FROM products;
|product_count|
|-------------|
|49           |
  • Counts only records with a value
Introduction to SQL with AI

Counting unique values

Prompt: How many different categories do we sell?

SELECT COUNT(DISTINCT category) AS unique_categories
FROM products;
|unique_categories|
|-----------------|
|9                |
Introduction to SQL with AI

To recap

$$

  • To get COUNT(*):
    • "How many records" or "how many rows"
  • To get COUNT(field):
    • "How many product names" or "how many customers"
  • Adding "different," "unique," or "distinct" adds DISTINCT

Person sitting at a table with a laptop

Introduction to SQL with AI

Let's practice!

Introduction to SQL with AI

Preparing Video For Download...