Resolved: difference between 2 codes
Select count(salary)
from salaries
where salary >= 100000
is similar to
Select count(*)
from salaries
where salary >= 100000
?
Hi Fatma!
Thanks for reaching out.
Yes, both codes give the same result set. Since there are no NULL
values, it does not matter what column you will provide to the COUNT()
function.
Hope this helps.
Best,
Tsvetelin
First query in the solution seems wrong . It does not account for contract period while question clearly asks for annual contracts. Right query should be something like this. Thanks
SELECT count(*)
from salaries
where datediff(to_date,from_date) =365 and salary>=100000;
^ wrong
Here's the breakdown of the differences between the two SQL code snippets and why they'll generally yield the same result:
Code 1:
SQL
Select count(salary)
from salaries
where salary >= 100000
What it does: Counts the number of non-NULL salary values in the salaries table that are greater than or equal to 100000.
Why it focuses on non-NULL: The COUNT(salary) function specifically ignores any rows where the salary column has a NULL (empty) value.
Code 2:
SQL
Select count(*)
from salaries
where salary >= 100000
What it does:
Counts all rows in the salaries table where the salary is greater than or equal to 100000, regardless of NULL values.
Why it counts all rows:
The COUNT(*) function counts every single row that matches the condition.
When the Results are the Same
In most cases, these codes will produce the same result. This is because it's fairly uncommon to have NULL values in a salary column.
When the Results Could Differ
The results could be different if your salaries table contains rows where the salary column is NULL. In this case:
Code 1 would ignore those rows with NULL salaries.
Code 2 would include those rows in its count.
HTML
<p><strong>Code 1:</strong></p>
```sql
Select count(salary)
from salaries
where salary >= 100000
Use code with caution.
<ul>
<li><strong>What it does:</strong> Counts the number of <strong>non-NULL</strong> salary values in the salaries table that are greater than or equal to 100000.</li>
<li><strong>Why it focuses on non-NULL:</strong> The COUNT(salary) function specifically ignores any rows where the salary column has a NULL (empty) value.</li>
</ul>
<p><strong>Code 2:</strong></p>
SQL
Select count(*)
from salaries
where salary >= 100000
Use code with caution.
<ul>
<li><strong>What it does:</strong> Counts <strong>all rows</strong> in the salaries table where the salary is greater than or equal to 100000, regardless of NULL values.</li>
<li><strong>Why it counts all rows:</strong> The COUNT(*) function counts every single row that matches the condition.</li>
</ul>
<p><strong>When the Results are the Same:</strong></p>
<p>In most cases, these codes will produce the same result, as it's uncommon to have NULL values in a salary column.</p>
<p><strong>When the Results Could Differ:</strong></p>
<p>The results could differ if your salaries table contains rows where the salary column is NULL. In this case:</p>
<ul>
<li>Code 1 would ignore those rows with NULL salaries.</li>
<li>Code 2 would include those rows in its count.</li>
</ul>