Learn how to declare and use an array in a SQL WHERE condition. In SQL, working with data can sometimes require checking whether values exist in a set or a list. One common question that arises is how to declare an array and use it in a WHERE condition.
Imagine you have a scenario where you need to find users with specific IDs or products with particular categories — and instead of writing multiple OR conditions, it’s much cleaner to use an array-like structure. This approach helps to keep things tidy and improves readability.
In this guide, I’ll walk you through how to declare an array in SQL and use it within a WHERE condition, with examples to help you understand how to implement it in your queries.
What Does It Mean to Use an Array in SQL?
Before we dive into the syntax and examples, let’s talk about why you’d want to use an array in your SQL queries.
Normally, in SQL, when you want to filter results based on multiple values, you would write something like this:
SELECT * FROM users
WHERE user_id = 1
OR user_id = 2
OR user_id = 3;
As you can see, this is perfectly fine but a little repetitive. Wouldn’t it be great if you could use an array instead, something like:
SELECT * FROM users
WHERE user_id IN (1, 2, 3);
This is much cleaner, right? You can achieve similar behavior in many SQL databases by using IN with a set of values. But what if you need to declare that set of values dynamically, like from a list or array, instead of hard-coding them directly into the query?
Declare and Use an Array in a SQL WHERE Condition
There are a couple of ways to achieve array-like functionality in SQL. Depending on the database system you’re using (MySQL, PostgreSQL, SQL Server, etc.), the syntax and features vary slightly.
1. Using IN with a List of Values
Let’s start with a simple example that uses a static list in the WHERE clause.
Here’s how you can declare a list of values directly in your query:
SELECT * FROM users
WHERE user_id IN (1, 2, 3, 5, 7);
This query finds all users with the user_id values 1, 2, 3, 5, and 7. It’s super simple and effective.
But in some cases, you may need to declare the array in your SQL code dynamically. This is where you’d need to use more advanced techniques depending on the database system you’re working with.
2. Using Arrays in PostgreSQL
PostgreSQL is pretty flexible when it comes to working with arrays. You can declare an actual array and use it in your WHERE clause to filter data. Here’s an example:
-- Declare an array of user IDs
SELECT * FROM users
WHERE user_id = ANY (ARRAY[1, 2, 3, 5, 7]);
In this case, the ARRAY[] syntax allows you to define a set of values that can be used with the ANY keyword in the WHERE clause.
This is really useful when you need to pass an array of values dynamically, for example, from a programming language like Python or Java.
3. Using VALUES with SQL Server
In SQL Server, you can use the VALUES keyword to work with multiple rows, which is a great way to emulate array-like behavior:
-- Declare values to check
SELECT *
FROM users
WHERE user_id IN (SELECT value FROM (VALUES (1), (2), (3), (5), (7)) AS t(value));
This query uses a derived table with the VALUES keyword to simulate an array of user IDs. It’s a neat trick when you want to pass multiple values to the IN clause dynamically.
4. Dynamic Array Handling in MySQL
MySQL doesn’t support true arrays, but you can use a combination of variables or even JSON arrays to simulate similar behavior. For example, in MySQL, you can handle multiple values dynamically with the help of prepared statements.
If you’re working with dynamic values in MySQL, you might prepare an SQL statement like so:
SET @user_ids = '1,2,3,5,7';
PREPARE stmt FROM 'SELECT * FROM users WHERE user_id IN (?)';
EXECUTE stmt USING @user_ids;
DEALLOCATE PREPARE stmt;
While this approach is more complex, it allows you to work with dynamically generated arrays in your SQL queries.
Practical Example: Using Arrays in SQL with Programming Languages
Let’s say you’re building an application where the list of values might change dynamically, and you want to pass those values into your SQL query. Here’s an example of how you might handle that using Python and SQLite.
import sqlite3
# Example list of user IDs
user_ids = [1, 2, 3, 5, 7]
# Connect to SQLite database
conn = sqlite3.connect('example.db')
cursor = conn.cursor()
# Create the SQL query dynamically using placeholders
placeholders = ', '.join('?' for _ in user_ids)
query = f"SELECT * FROM users WHERE user_id IN ({placeholders})"
# Execute the query
cursor.execute(query, user_ids)
# Fetch and print results
rows = cursor.fetchall()
for row in rows:
print(row)
# Close the connection
conn.close()
Here, the user_ids array is passed dynamically into the SQL query using placeholders (?) for security (preventing SQL injection) and clarity. This way, the list of values is flexible and can be modified at runtime.
Conclusion
In SQL, working with arrays (or lists) can greatly simplify your queries and make them more efficient. Whether you’re using PostgreSQL’s built-in ARRAY functionality, SQL Server’s VALUES keyword, or dynamic lists in MySQL and SQLite, there are plenty of ways to achieve this. The main takeaway is that using arrays or lists in WHERE clauses keeps your queries cleaner and easier to manage — especially when working with large sets of values.
Now, next time you need to check for multiple values in a column, you’ll be ready to use arrays in SQL for more elegant and readable queries. Happy querying!







