SQL recipes
A library of real, runnable SQL queries for common questions on CSV and JSON data — grouping, filtering, ranking, window functions, and more.
- How do I preview the first rows of a CSV file with SQL?
- How do I count the number of rows in a CSV file?
- How do I sum a numeric column across all rows?
- How do I total a column for each category with SQL GROUP BY?
- How do I calculate an average per group in SQL?
- How do I get the top N rows by value with SQL?
- How do I filter rows with a numeric condition in SQL?
- How do I search for a text pattern in a column with SQL LIKE?
- How do I list the unique values in a column with SQL?
- How do I count unique values in a column with SQL?
- How do I filter grouped results in SQL with HAVING?
- How do I sort by more than one column in SQL?
- How do I filter rows between two dates in SQL?
- How do I bucket rows into categories with SQL CASE WHEN?
- How do I rank rows within each group using a SQL window function?
- How do I total revenue for each category with SQL?
- How do I total revenue for each customer with SQL?
- How do I compute the average unit price per country in SQL?
- How do I compute the average unit price per customer in SQL?
- How do I count rows per country with SQL GROUP BY?
- How do I count rows per category with SQL GROUP BY?
- How do I count rows per customer with SQL GROUP BY?
- How do I find the minimum and maximum unit price per country in SQL?
- How do I find the minimum and maximum unit price per category in SQL?
- How do I find the minimum and maximum unit price per customer in SQL?
- How do I get the top 3 rows by value with SQL LIMIT 3?
- How do I get the top 5 rows by value with SQL LIMIT 5?
- How do I get the top 15 rows by value with SQL LIMIT 15?
- How do I get the top 20 rows by value with SQL LIMIT 20?
- How do I filter rows where quantity is at least 1 in SQL?
- How do I filter rows where quantity is at least 2 in SQL?
- How do I filter rows where quantity is at least 4 in SQL?
- How do I filter rows where quantity is at least 5 in SQL?
- How do I filter rows where unit price is above 20 in SQL?
- How do I filter rows where unit price is above 50 in SQL?
- How do I filter rows where unit price is above 100 in SQL?
- How do I filter rows where unit price is above 200 in SQL?
- How do I find rows where country starts with the letter U in SQL?
- How do I find rows where country starts with the letter F in SQL?
- How do I find rows where country starts with the letter S in SQL?
- How do I list the distinct country values in SQL?
- How do I list the distinct customer values in SQL?
- How do I count distinct country values with SQL?
- How do I count distinct category values with SQL?
- How do I keep only groups whose total exceeds 150 using SQL HAVING?
- How do I keep only groups whose total exceeds 100 using SQL HAVING?
- How do I keep only groups whose total exceeds 300 using SQL HAVING?
- How do I sort by category and then by value (ASC) in SQL?
- How do I sort by customer and then by value (ASC) in SQL?
- How do I sort by country and then by value (DESC) in SQL?
- How do I filter rows between 2025-01-01 and 2025-01-15 with SQL?
- How do I filter rows between 2025-01-15 and 2025-01-31 with SQL?
- How do I filter rows between 2025-02-01 and 2025-02-28 with SQL?
- How do I bucket orders as bulk vs single at a quantity threshold of 2 in SQL?
- How do I bucket orders as bulk vs single at a quantity threshold of 3 in SQL?
- How do I bucket orders as bulk vs single at a quantity threshold of 4 in SQL?
- How do I rank rows within each category using a SQL window function?
- How do I rank rows within each customer using a SQL window function?
- How do I compute a running total over time with SQL?
- How do I number each customer’s orders in date order with SQL?