0

I have a table of music tracks with a length field that stores information on how long each individual track is. I'm trying to query to database to find out the maximum track length, minimum track length and I want the return the number of tracks which have a length listed (IS NOT NULL).

Here is the statement:

SELECT MAX(length), MIN(length), COUNT(length)
FROM tracks WHERE length IS NOT NULL;

The problem is the value COUNT() returns is the exact same whether I include the IS NOT NULL operator at the end or not. The table has around 25 entries that are NULL so surely I should be getting a lower result for COUNT() when I add the NOT NULL operator to the end of the statement?

2 Answers 2

1

The documentation says that count will ignore null columns http://dev.mysql.com/doc/refman/5.7/en/counting-rows.html

Including the where clause will not make a difference.

Sign up to request clarification or add additional context in comments.

1 Comment

This is correct. If you want to count all rows matched by WHERE, use COUNT(*) or COUNT(1) (they are equivalent). If you want to count rows where col1 is not null, that's exactly what COUNT(col1) does.
0

BACKTICKS are your friend: length is a function name in MySQL and if you want o use it as field name you must put it in backticks like this:

SELECT MAX(`length`), MIN(`length`), COUNT(`length`)
FROM tracks WHERE `length` IS NOT NULL;

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.