"In" Relational Operator in SQL

"In" is used to find out if the value of a field is included in a specified list of values.

In the following statement we use "in" to find out if the value of the author field is included in the specified list of values ​​(in this case, 2 strings).


Until now, to recover books whose author is 'Paenza' or 'Borges' we used 2 conditions:


 select * from books

  where author = 'Borges' or author = 'Paenza';


We can use "in" and simplify the query:


 select * from books

  where author in ('Borges', 'Paenza');

To recover books whose author is not 'Paenza' or 'Borges' we used:


 select * from books

  where author <> 'Borges' and

  author <> 'Paenza';


We can also use "in" by prepending "not":


 select * from books

  where author not in ('Borges', 'Paenza');


Using "in" we find out if the value of the field is included in the specified list of values; with "not" preceding the condition, we invert the result, that is, we retrieve the values ​​that are not found (do not match) with the list of values.

Values ​​"null" are not considered.

Comments

Popular posts from this blog

Data Consistency and Inconsistency - What is Difference

Cardinality in DBMS – max/minimum cardinality & examples

Relational Model in DBMS