user3628240
user3628240

Reputation: 927

SQL Query - Filtering out column values with None

I have a PSQL database called raw and one of the columns is option, with type VARCHAR. The value is either 'Call', 'Put', or None.

I want to select all rows where the value of the option field is not 'Call' or 'Put', but rather None.

However, when I run this query:

curr.execute('SELECT * FROM holdings h WHERE option != \'Call\' AND option != \'Put\';')

nothing gets returned

If I run :

curr.execute('SELECT * FROM holdings h WHERE option != \'Call\';')

I only get those rows with option value = 'Put'

How can I get the rows with None for option, without just doing a set difference. Ideally I want to be able to just select those rows that aren't 'Call' and 'Put' without calling difference from the all the rows and those that have 'Call' and 'Put'

Thank you for any help!

Upvotes: 0

Views: 5270

Answers (2)

Tomasz Jakub Rup
Tomasz Jakub Rup

Reputation: 10680

If You want to select rows with null option, You must indicate this by option IS NULL:

SELECT * FROM holdings WHERE option IS NULL

Instead of option != 'Call' AND option != 'Put' You can use option NOT IN ('Call', 'Put'):

SELECT * FROM holdings WHERE option NOT IN ('Call', 'Put')

And join this together with OR:

SELECT * FROM holdings WHERE option NOT IN ('Call', 'Put') OR option IS NULL

Upvotes: 1

vivek kv
vivek kv

Reputation: 416

SELECT * FROM holdings h WHERE option =''

Upvotes: 0

Related Questions