SQL Questions and Answers – SQL Count Function

This set of SQL Multiple Choice Questions & Answers (MCQs) focuses on “SQL Count Function”.

1. Which function is used to get the number of rows that matches specified criteria?
a) Number( )
b) Count( )
c) Total( )
d) Calculate( )
View Answer

Answer: b
Explanation: COUNT( ) is an aggregate function. This function returns the number of rows in a table satisfying the criteria specified in the WHERE clause.

2. The SQL COUNT function is used to count the number of rows returned in which of the following statement?
a) Select
b) Where
c) Update
d) Delete
View Answer

Answer: a
Explanation: The SQL COUNT function is used to count the number of records, which are returned by a SELECT statement.

3. Which of the following queries returns the number of records in a table named hospital?
a) Select * count( ) From hospital;
b) Select count(1) From hospital;
c) Select count( ) 1 From hospital;
d) Select count( ) all From hospital;
View Answer

Answer: b
Explanation:

advertisement
advertisement
SELECT COUNT(column_name) FROM TABLE_NAME;

This is the basic syntax using COUNT( ) function. To count all the records * is used in place of column_name. COUNT(1) has the same effect as COUNT(*).

4. SELECT COUNT(*) returns the total of all records returned in the result set ______
a) Excluding the tuples with all NULL values
b) Regardless of tuples with all NULL values
c) Excluding the tuples with more than one NULL value
d) Excluding the tuples with no NULL values
View Answer

Answer: b
Explanation: The COUNT function returns the total number of rows returned in a result set. Using SELECT COUNT(*) or SELECT COUNT(1) will return the total of all records returned in the result set regardless of NULL values.

5. Using SELECT COUNT(column_name), we can count the number of non-NULL items in the specified column. (column_name is not *)
a) True
b) False
View Answer

Answer: a
Explanation: SELECT COUNT(*) returns total number of records in the result set regardless of NULL values, whereas SELECT COUNT(column_name) returns number of non-NULL items in the specified column.
advertisement

6. It is not possible to have any other columns in addition to COUNT function when it is used in SELECT statement.
a) True
b) False
View Answer

Answer: b
Explanation: It is possible to have one or more columns in addition to the COUNT function in the SELECT statement. But these columns need to be a part of GROUP BY clause also.

7. If any other columns are written along with COUNT in a SELECT statement, then _____
a) Those columns need to be a part of having clause
b) Those columns need to be a part of where clause
c) Those columns need to be a part of group by clause
d) Those columns need not to be a part of any other clauses
View Answer

Answer: c
Explanation: If any other columns are written along with COUNT function in a SELECT statement, these columns need to be part of the GROUP BY clause as well. Otherwise the query results in wrong outputs.
advertisement

8. Which of the following clauses when used with COUNT function calculates the number of unique values of a specified column?
a) Unique
b) Different
c) Specific
d) Distinct
View Answer

Answer: d
Explanation: COUNT is combined with DISTINCT to calculate the number of unique values. Even if DISTINCT treats NULL as unique value, COUNT function doesn’t count those NULL values.

9. SQL COUNT( ) function with DISTINCT clause _________ the repetitive appearance of the same data.
a) Modifies
b) Eliminates
c) Counts
d) Updates
View Answer

Answer: b
Explanation: SQL COUNT( ) function with DISTINCT clause eliminates the repeated data and counts the number of distinct values of the specified column.

10. With which of the following clause(s), COUNT( ) function cannot be used?
a) Having
b) Distinct
c) Group By
d) Order By
View Answer

Answer: d
Explanation: COUNT( ) function when combined with DISTINCT calculates the number of unique values. HAVING and GROUP BY clauses can also be used with COUNT function depending on the conditions, whereas order by cannot use any function or clause other than ASC and DESC.

Sanfoundry Global Education & Learning Series – SQL.

To practice all areas of SQL, here is complete set of 1000+ Multiple Choice Questions and Answers.

advertisement
advertisement
Subscribe to our Newsletters (Subject-wise). Participate in the Sanfoundry Certification contest to get free Certificate of Merit. Join our social networks below and stay updated with latest contests, videos, internships and jobs!

Youtube | Telegram | LinkedIn | Instagram | Facebook | Twitter | Pinterest
Manish Bhojasia - Founder & CTO at Sanfoundry
Manish Bhojasia, a technology veteran with 20+ years @ Cisco & Wipro, is Founder and CTO at Sanfoundry. He lives in Bangalore, and focuses on development of Linux Kernel, SAN Technologies, Advanced C, Data Structures & Alogrithms. Stay connected with him at LinkedIn.

Subscribe to his free Masterclasses at Youtube & discussions at Telegram SanfoundryClasses.