Guides
Piping DuckDB Queries to stdout
By combining the [`COPY...TO`](#docs:sql:statements:copy::copy-to) function with a CSV output file, data can be read from any format supported by DuckDB and piped to YouPlot. There are three important steps to doing this.  
1. As an example, this is how to read all data from `input.json`:  
```bash
duckdb -s "SELECT * FROM read_json_auto('input.json')"
```  
2. To prepare the data for YouPlot, write a simple aggregate:  
```bash
duckdb -s "SELECT date, sum(purchases) AS total_purchases FROM read_json_auto('input.json') GROUP BY 1 ORDER BY 2 DESC LIMIT 10"
```  
3. Finally, wrap the `SELECT` in the `COPY ... TO` function with an output location of `/dev/stdout`.  
The syntax looks like this:  
```sql
COPY (⟨query⟩) TO '/dev/stdout' WITH (FORMAT 'csv', HEADER);
```  
The full DuckDB command below outputs the query in CSV format with a header:  
```bash
duckdb -s "COPY (SELECT date, sum(purchases) AS total_purchases FROM read_json_auto('input.json') GROUP BY 1 ORDER BY 2 DESC LIMIT 10) TO '/dev/stdout' WITH (FORMAT 'csv', HEADER)"
```