Understanding COUNT(*) vs COUNT(column) in SQL
📖 Scenario: You work at a small bookstore. You have a table called sales that records each book sale. Some sales have a recorded discount_code, but some do not.You want to learn how to count total sales and how to count only sales where a discount code was used.
🎯 Goal: Build SQL queries to count total sales and count sales with discount codes, so you understand the difference between COUNT(*) and COUNT(discount_code).
📋 What You'll Learn
Create a table called
sales with columns sale_id (integer) and discount_code (text, nullable).Insert 5 rows into
sales with some discount_code values NULL and some with text.Write a query using
COUNT(*) to count all sales.Write a query using
COUNT(discount_code) to count only sales with a discount code.💡 Why This Matters
🌍 Real World
Counting total records and filtering counts based on column values is common in sales, inventory, and user data analysis.
💼 Career
Understanding COUNT(*) vs COUNT(column) helps in writing accurate SQL queries for reports and data insights in many data-related jobs.
Progress0 / 4 steps