Lessons › SQL › Chapter 3: Summarising
SQL lesson
COUNT(*), COUNT(column), COUNT(DISTINCT)
COUNT(*) counts every row. COUNT(discount) counts only the rows where discount has a value. COUNT(DISTINCT customer_id) counts each customer once, however many orders they placed.
Try this lesson free →Example
Three ways to count
SELECT COUNT(*) AS orders
FROM orders| orders |
|---|
| 5 |
Step by step
- Counts all the rows.
SELECT COUNT(*) AS total_orders, COUNT(discount) AS orders_with_discount, COUNT(DISTINCT customer_id) AS unique_customers FROM orders - Counts only rows where discount has a value.
SELECT COUNT(*) AS total_orders, COUNT(discount) AS orders_with_discount, COUNT(DISTINCT customer_id) AS unique_customers FROM orders - Counts each customer once, however many orders they have.
SELECT COUNT(*) AS total_orders, COUNT(discount) AS orders_with_discount, COUNT(DISTINCT customer_id) AS unique_customers FROM orders
Practice
1. Using orders, write a query to count how many rows are in the table.
SELECT COUNT(*) FROM orders2. Using products, write a query to count how many rows are in the table.
SELECT COUNT(*) FROM productsLearn it by doing. This lesson is a short hands-on level on Bampaa: see it work, then build and write it yourself, with instant feedback.
Try this lesson free →