W3. SQL Count Flashcards

1
Q

Q: What does the SQL COUNT() function do?

A

A: It returns the number of rows that match a specified criterion.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
2
Q

Q: Write a query to find the total number of rows in the “Products” table.

A
SELECT COUNT(*) 
FROM Products;
How well did you know this?
1
Not at all
2
3
4
5
Perfectly
3
Q

Q: What is the difference between COUNT(*) and COUNT(column_name)?

A

A: COUNT(*) counts all rows, including NULL values, while COUNT(column_name) only counts non-NULL values in the specified column.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
4
Q

Q: Write a query to find the number of products where ProductName is not NULL.

A
SELECT COUNT(ProductName) 
FROM Products;
How well did you know this?
1
Not at all
2
3
4
5
Perfectly
5
Q

Q: How can you use the WHERE clause with COUNT()?

A

A: Add a condition in WHERE to specify the rows to count, e.g., WHERE Price > 20.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
6
Q

Q: Write a query to count the products with a Price greater than 20.

A
SELECT COUNT(ProductID) 
FROM Products 
WHERE Price > 20;
How well did you know this?
1
Not at all
2
3
4
5
Perfectly
7
Q

Q: How do you ignore duplicates in COUNT()?

A

A: Use COUNT(DISTINCT column_name) to count only unique values.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
8
Q

Q: Write a query to count the number of different prices in the “Products” table.

A
SELECT COUNT(DISTINCT Price) 
FROM Products;
How well did you know this?
1
Not at all
2
3
4
5
Perfectly
9
Q

Q: How can you give the counted column a descriptive name?

A

A: Use the AS keyword to set an alias, e.g., AS [Number of records].

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
10
Q

Q: Write a query to count all records in the “Products” table and label the count as “Number of records.”

A
SELECT COUNT(*) AS [Number of records] 
FROM Products;
How well did you know this?
1
Not at all
2
3
4
5
Perfectly
11
Q

Q: How do you use COUNT() with GROUP BY?

A

A: Combine COUNT() with GROUP BY to return the count for each group, e.g., GROUP BY CategoryID.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
12
Q

Q: Write a query to count the number of records for each category in the “Products” table.

A
SELECT COUNT(*) AS [Number of records], CategoryID 
FROM Products 
GROUP BY CategoryID;
How well did you know this?
1
Not at all
2
3
4
5
Perfectly