I have been solving problem set 0 from Harvard’s CS50 for SQL online course. There is a question in it, which says:
In 7.sql, write a SQL query to count the number of players who bat (or batted) right-handed and throw (or threw) left-handed, or vice versa.
There are two columns in the players table called bats and throws.
I tried two approaches:
SELECT COUNT(id) FROM players
WHERE (bats = 'R' AND throws = 'L') OR (bats = 'L' AND throws = 'R');
which gives the correct result.
However, I also tried this:
SELECT COUNT(id) FROM players
WHERE throws<>bats AND throws IS NOT NULL AND bats IS NOT NULL;
which (according to me) logically, should give the same answer. Can someone explain what I am missing? Sorry if there are any formatting issues as this is my first question.
Ishan Dubey is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
Check out our Code of Conduct.