SQL how-to: answers to everyday tasks
Short answers to the tasks people search for, each with the query first, how it works line by line, and the result from both PostgreSQL and SQLite.
How to find duplicate rows in SQL
Find the values, or whole rows, that appear more than once in a table.
How to delete duplicate rows in SQL
Remove duplicate rows from a table and keep exactly one copy of each.
How to find the second highest (or Nth) value in SQL
Find the second highest value in a column, or more generally the Nth.
How to get the top N rows per group in SQL
Get the top N rows within each group, for example the two heaviest pets of each species.
How to get the first or latest row per group in SQL
Keep one row per group: the first or the most recent, such as each customer's latest order.
How to calculate a running total in SQL
Add up a value row by row, so each row shows the total so far.
How to calculate a percentage of the total in SQL
Show each group's share of the total, as a percentage.
How to group by month in SQL (PostgreSQL and SQLite)
Count or add up rows per month, or per week or year.
How to calculate the median in SQL (PostgreSQL and SQLite)
Find the middle value of a column, the median, overall or per group.
How to pivot rows into columns in SQL
Turn values from rows into columns, for example one column per device with a count in each.
How to count rows that match a condition in SQL
Count how many rows meet a condition, several conditions side by side in one query.
Date difference in SQL: PostgreSQL and SQLite
Find how many days (or months) lie between two dates.
How to calculate age from a birth date in SQL
Work out someone's age in whole years from their date of birth.
How to get the last day of the month in SQL
For any date, find the first and the last day of its month.
How to calculate month-over-month growth in SQL
Compare each month with the one before: the change and the percent change.
How to calculate a moving average in SQL
Smooth a daily series by averaging each day with the days around it.
How to find consecutive days (streaks) in SQL
Find runs of consecutive days, such as the longest stretch a user listened every day.
How to find rows that aren't in another table in SQL
Find the rows in one table that have no matching row in another, such as pets nobody has asked about.
How to compare two tables in SQL
Find what differs between two versions of a table: rows added, removed or changed.
How to concatenate rows into one string in SQL
Combine values from several rows into one string per group, like every item in a category.
How to split a string in SQL (PostgreSQL and SQLite)
Split text at a separator, such as an email into its name and domain, or a list into rows.
How to generate a series of dates in SQL
Produce one row per day in a range, so days with no data still show up.
How to avoid division by zero in SQL
Divide one number by another without an error when the divisor is 0, and without losing the decimals.
How to rank rows within each group in SQL
Show every row with its position inside its own group, such as each item's price rank within its category.