Back to Code Snippets

Ryan Boyd

@ryan-1


Select a sample of rows from a table

DuckDB allows sampling of data in your tables using a several different statistical techniques, usually to increase performance. The default sampling method is used in this case- this is a bernoulli variant where each vector has a specified chance of being included in the result set.

Execute this SQL

-- uses system sampling to select 10% of the people rows
SELECT * FROM people USING SAMPLE 10%;

Copy code

Ryan Boyd

Expand

Share link


Select all columns except a few

Editor's note: tired of copying/pasting many column names to select all columns except a handful? The EXCLUDE() function allows you to exclude specific columns from the result set. Together with COLUMNS() and REPLACE() it provides an easy way to specific the data you want returned.

Execute this SQL

SELECT * EXCLUDE (column1, colum2) FROM ducks;

Copy code

Ryan Boyd

Expand

Share link


Download CSV and convert to ParquetSQL

Execute this SQL

COPY (SELECT * FROM read_csv_auto(
'http://raw.githubusercontent.com/fivethirtyeight/data/master/bechdel/movies.csv'))
TO 'movies.parquet' (FORMAT 'parquet');

Copy code

Ryan Boyd

Copy code

Expand

Share link