Informatics Practices
Anjali writes the following commands with respect to a table employee having fields, empno, name, department, commission :
Command1: Select count(*) from employee;
Command2: Select count(commission) from employee;
She gets the output as 4 for the first command but gets an output 3 for the second command. Explain the output with justification.
SQL Queries
1 Like
Answer
The difference in output is due to how COUNT()
function behaves with different types of data in the specified column.
Command1: Select count(*) from employee;
: This command counts all rows in the employee
table, regardless of whether any column contains NULL values or not. When we specify an asterisk (*) with COUNT()
, it returns the total number of rows in the table, including duplicates and NULL values. So, it returns the total number of rows in the table, which is 4.
Command2: Select count(commission) from employee;
: If we specify argument (expr) in COUNT()
function, it returns rows where expr is not null. This command counts the number of non-NULL values in the commission
column. Therefore, it returns a count of 3 instead of 4 due to the presence of a NULL value in that column in one of the rows.
Answered By
3 Likes
Related Questions
Assertion. Both WHERE and HAVING clauses are used to specify conditions.
Reason. The WHERE and HAVING clauses are interchangeable.
Assertion. Both WHERE and HAVING clauses are used for specifying conditions.
Reason. The WHERE condition is applicable on individual rows and HAVING condition is applicable on a group of rows.
- Both A and R are true and R is the correct explanation of A.
- Both A and R are true but R is not the correct explanation of A.
- A is true but R is false.
- A is false but R is true.
What is the use of ORDER BY clause ?
What is the default sort order of ORDER BY clause ?