How to use same list twice in WHERE clause?

Scarabee

My SQL query contains a WHERE clause looking like this:

WHERE 
      name1 in ('Emily', 'Jack', 'James', 'Chloe') 
   OR name2 in ('Emily', 'Jack', 'James', 'Chloe')

Note that the same list appears twice, which is quite unsatisfying (and my real list is actually longer).

What would be a better way to write this query?

klin

You can use arrays and overlap operator &&, e.g.:

with my_table(name1, name2) as (
values ('Emily', 'Bob'), ('Ben', 'Jack'), ('Bob', 'Ben')
)

select *
from my_table
where array[name1, name2] && array['Emily', 'Jack', 'James', 'Chloe'];

 name1 | name2 
-------+-------
 Emily | Bob
 Ben   | Jack
(2 rows)

Collected from the Internet

Please contact [email protected] to delete if infringement.

edited at
0

Comments

0 comments
Login to comment

Related

PL/SQL - Use "List" Variable in Where In Clause

How do I use multiple values from the same column in the WHERE clause?

How to use a macro list twice in the same "call stack"?

How can I use a list of values with the IN operator in a WHERE clause?

How to use Max in the where clause

How to use WITH subqueries as a list of options in WHERE clause in standard SQL

How to use a "where not" clause with searchkick

'xinput list' shows same device twice & device IDs change: how to use 'set-prop' in a script?

SQL: Use a predefined list in the where clause

How to use WHERE clause with a PIVOT

How to use CASE in WHERE clause

How to use OR & AND in WHERE clause in mysql

How to use NVL in where clause

Using WHERE clause twice on same column?

How to use where clause in Pods

How to use a WITH clause in conjuction with a WHERE clause

How to use OR in this WHERE clause?

How to use the AND in a variable in the WHERE clause

How to use Multiple OR on WHERE Clause?

How to use aliases in WHERE clause?

How to use a LINQ where clause on a list of Func delegates

How to use same psql subquery in from and where clause?

how to use WHERE clause and WITH in Laravel

how to use the same label twice

How to ORDER a list where the same value doesn't appear twice in a row?

How to use a JQuery function twice in the same page?

How to use function result twice in the same select

BigQuery: How to iterate through list and use variable in where clause

How can I use a named CASE or IF result in the same select or in the where clause