Search code examples
sqlarrayspostgresqlwhere-clause

SELECT WHERE column values are in array


Suppose I have a table called 'myTable':

columnA|    columnB|    columnC
   1   |     Yellow|     Apple
   3   |     Red   |     Grape
   8   |     Blue  |     Banana
   6   |     Green |     Orange

The above table is a simplification of an actual table for demo purposes. Imagine actual table is 100K + rows. Now, I want to select only the rows where columnB is in a list/array: ex - ['Red', 'Blue', 'Green']. I am not sure the right syntax to use here.

 SELECT * FROM myTable WHERE columnB IN Array['Red', 'Blue', 'Green']

Whats the proper syntax to achieve this?


Solution

  • Here is the example, I believe its for MS SQL

    SELECT p.FirstName, p.LastName, e.JobTitle  
    FROM Person.Person AS p  
        JOIN HumanResources.Employee AS e  
        ON p.BusinessEntityID = e.BusinessEntityID  
    WHERE e.JobTitle IN ('Design Engineer', 'Tool Designer', 'Marketing Assistant');  
    

    Or in simple words (pay attention to text if its in lower, upper or proper base).

    SELECT * FROM design WHERE Color IN ('Red', 'Green', 'Blue');