SQL statement not working

SQL statement not working
0

#1

Hi all,

Could someone tell me why the below SQL statement returns the full ‘game’ table instead of only the games where team1 or team2 equals Panthers?

SELECT * FROM game
WHERE (team1 OR team2) = ‘Panthers’;

The following code returns what I want:

SELECT * FROM game
WHERE team1 = ‘Panthers’
OR team2 = ‘Panthers’;

Can someone explain why the first query is not working?

Thank you,


#2

Up for correction -Your first statement is incorrect from the “WHERE” clause, so you may find that it’s running the first part “SELECT * FROM game” and returning everything as expected.

When writing SQL queries. you have “Simple Conditions” and “Compound Conditions”. In order to create a compound condition you need to combine 2 simple conditions as you have done on your second example. Similar to programming, compound conditions can be made up of AND, NOT and OR operators.

If you have written an IF statement in programming with multiple conditions you could compare it to this as you also have to write out each condition in full if you are writing multiple conditions in one statement.


#3

I hadn’t thought about comparing it to normal programming yet but you are right, in conditionals you will indeed use a == 2 || b == 2 and not a || b == 2. This helps. Thank you!