Search code examples
databasesql-optimizationdatabase-optimization

How to reduce this multiple and- and or- conditions in sql-statement?


I´ve got an optimizing question about SQL.

Is there a way to reduce this sql syntax:

Select * from TABLE 
where 
COLUMN != 'abc' and 
COLUMN != 'def' and 
COLUMN != 'xau' and 
COLUMN != 'def' and 
COLUMN != 'eag' and 
COLUMN != 'dff' and 
COLUMN != 'www' 

and second sql syntax

Select * from TABLE 
where 
COLUMN = 'abc' or 
COLUMN = 'def' or 
COLUMN = 'xau' or 
COLUMN = 'def' or 
COLUMN = 'eag' or 
COLUMN = 'dff' or 
COLUMN = 'www' 

Solution

  • Try this:

    Multiple OR condition can be re-written using:

    Select * from TABLE 
    where 
    COLUMN not in('abc','def','xau','def','eag','dff','www')
    

    Multiple AND condition, can you enlighten me when its possible that same column have more than 1 value?