Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

PostgreSQL How to query String array


I am trying to write a query to check if an element is within an array of Strings.

Here is my simple select query along with the output

select languages from person limit 3;
{CSS,HTML,Java,JavaScript,Python}
{JavaScript,Python,TensorFlow}
{C++,Python}

How do I write a query to find all people who have "Java" as a listed language they know?
I tried following the syntax but it isn't working.

select languages from person where languages @> ARRAY['Java']::varchar[];
like image 303
Sandy Avatar asked May 12 '26 17:05

Sandy


1 Answers

You need to use a string constant on the left side, and the ANY operator on the array column:

select languages 
from person 
where 'Java' = any(languages);

This assumes languages is defined as text[] or varchar[] as your sample output indicates


Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!