---
title: "How to write SQL QUERY TO JOIN Comma Separated Values?"  
description: "How to write SQL QUERY TO JOIN Comma Separated Values?"  
author: "Sandra Emily"  
published: 2023-07-30  
updated: 2023-07-31  
canonical: https://www.mindstick.com/forum/159360/how-to-write-sql-query-to-join-comma-separated-values  
category: "mssql server"  
tags: ["mssql server", "sql server", "sql"]  
reading_time: 2 minutes  

---

# How to write SQL QUERY TO JOIN Comma Separated Values?

How to write [SQL QUERY](https://www.mindstick.com/forum/529/rename-table-name-and-column-name-using-sql-query) TO [JOIN](https://www.mindstick.com/articles/1487/join-sleep-and-interrupt-methods-in-c-sharp-threading) [Comma Separated](https://www.mindstick.com/forum/33788/how-to-select-comma-separated-list-in-sqlserver) [Values](https://www.mindstick.com/forum/327/sum-textbox-values)?

## Replies

### Reply by Aryan Kumar

Sure, here is how to write a [SQL](https://www.mindstick.com/articles/13115/types-of-keys-in-sql-or-oracle-database) [query](https://www.mindstick.com/blog/202/sub-query-in-sqlserver) to join comma separated values:

SQL

```plaintext
SELECT
  CONCAT_WS(', ', values) AS joined_values
FROM
  (
    SELECT
      value
    FROM
      my_table
    WHERE
      column_name IN ('value1', 'value2', 'value3')
  ) AS t;
```

This query will first select all of the values from the `my_table` table where the `column_name` column is equal to `value1`, `value2`, or `value3`. The `CONCAT_WS()` function will then join the values with commas.

The results of the query will be a single column called `joined_values` that contains the comma separated values.

Here is an explanation of the query:

- The `SELECT` statement specifies the columns that you want to return. In this case, you want to return the `joined_values` column.
- The `CONCAT_WS()` function joins the values with commas. The `CONCAT_WS()` function takes three arguments: the separator, the first value, and the rest of the values.
- The `IN` operator checks if a value is in a list of values. In this case, the `IN` operator is checking if the `column_name` column is equal to `value1`, `value2`, or `value3`.
- The `FROM` clause specifies the table that you want to select the data from. In this case, you want to select the data from the `my_table` table.


---

Original Source: https://www.mindstick.com/forum/159360/how-to-write-sql-query-to-join-comma-separated-values

Copyright © MindStick Software Pvt. Ltd. This Markdown version is provided for developers, AI systems, and offline reading.
