Public dataset · Web, code and analytics
Stack Overflow in BigQuery
The stackoverflow dataset is a snapshot of Stack Overflow questions, answers, comments, users, votes and tags, last updated in November 2022 and no longer refreshed. The questions table is 37 GiB, mostly the body column, so selecting only titles, tags and scores cuts a scan to about 2.5 GiB.
bigquery-public-data.stackoverflow contains the public content of Stack Overflow. The dataset description gives 2022-11-25 as the last data update and says the dataset is no longer actively updated, so nothing after November 2022 is in it.
Questions and answers live in separate tables: posts_questions and posts_answers. An answer points to its question through parent_id, and a question points to its accepted answer through accepted_answer_id. comments, votes, badges, post_links and post_history (every revision of every post, the largest table at 113 GiB) complete the activity data, and users holds profiles with reputation. Tags on a question are one string separated by |, for example python|pandas; the tags table has one row per tag with its question count.
stackoverflow_posts is an older combined table last modified in 2017. Its description says not to use it and to use the posts_* tables instead. No table is partitioned or clustered.
Tables and sizes
| Table | Rows | Size | Full scan | Layout |
|---|---|---|---|---|
post_historyEvery revision of every post. | 152,435,941 | 113 GiB | $0.69 | None |
posts_questionsQuestions, with title, body, tags and scores. | 23,020,127 | 37.2 GiB | $0.23 | None |
stackoverflow_postsOld combined posts table from 2017. Its description says to use the posts_* tables instead. | 31,017,889 | 29.4 GiB | $0.18 | None |
posts_answersAnswers. `parent_id` is the question id. | 34,024,119 | 28.6 GiB | $0.17 | None |
comments | 86,754,111 | 16.0 GiB | $0.10 | None |
votes | 236,452,885 | 7.0 GiB | $0.04 | None |
users | 18,712,212 | 3.1 GiB | $0.02 | None |
badges | 46,135,386 | 2.0 GiB | $0.01 | None |
post_linksLinks between posts, such as duplicates. | 8,395,210 | 320 MiB | under $0.01 | None |
posts_tag_wiki | 55,113 | 36.0 MiB | under $0.01 | None |
posts_tag_wiki_excerpt | 55,115 | 10.9 MiB | under $0.01 | None |
tagsOne row per tag with the number of questions. | 63,653 | 2.5 MiB | under $0.01 | None |
posts_moderator_nomination | 342 | 470 KiB | under $0.01 | None |
posts_orphaned_tag_wiki | 167 | 52.1 KiB | under $0.01 | None |
posts_wiki_placeholder | 5 | 5.0 KiB | under $0.01 | None |
posts_privilege_wiki | 2 | 3.4 KiB | under $0.01 | None |
Example queries
Most used tags
The tags table already holds the count per tag, so this reads about 1 MiB instead of splitting the tags string of 23 million questions.
SELECT tag_name, count AS questions
FROM `bigquery-public-data.stackoverflow.tags`
ORDER BY count DESC
LIMIT 20;BigQuery questions per year, and how many went unanswered
SPLIT turns the |-separated tag string into an array, so IN UNNEST matches the exact tag and not tags that merely contain the word.
SELECT
EXTRACT(YEAR FROM creation_date) AS year,
COUNT(*) AS questions,
COUNTIF(answer_count = 0) AS unanswered
FROM `bigquery-public-data.stackoverflow.posts_questions`
WHERE 'google-bigquery' IN UNNEST(SPLIT(tags, '|'))
GROUP BY year
ORDER BY year;Top-voted BigQuery questions with SELECT * (expensive)
SELECT * pulls the body column, which holds the full HTML of every question. The filter does not help, because the table is not partitioned or clustered.
SELECT *
FROM `bigquery-public-data.stackoverflow.posts_questions`
WHERE 'google-bigquery' IN UNNEST(SPLIT(tags, '|'))
ORDER BY score DESC
LIMIT 20;The same question, selecting only the columns you need
Same rows, about 15 times fewer bytes. Fetch the body later for the handful of ids you care about.
SELECT id, title, score, view_count, answer_count
FROM `bigquery-public-data.stackoverflow.posts_questions`
WHERE 'google-bigquery' IN UNNEST(SPLIT(tags, '|'))
ORDER BY score DESC
LIMIT 20;Fastest first answers to BigQuery questions in 2022
Joins answers to questions on parent_id and keeps the earliest answer per question. Neither body column is read.
SELECT
q.id,
q.title,
TIMESTAMP_DIFF(MIN(a.creation_date), q.creation_date, MINUTE) AS minutes_to_first_answer
FROM `bigquery-public-data.stackoverflow.posts_questions` AS q
JOIN `bigquery-public-data.stackoverflow.posts_answers` AS a
ON a.parent_id = q.id
WHERE 'google-bigquery' IN UNNEST(SPLIT(q.tags, '|'))
AND EXTRACT(YEAR FROM q.creation_date) = 2022
GROUP BY q.id, q.title, q.creation_date
ORDER BY minutes_to_first_answer
LIMIT 20;Querying it cheaply
- The
bodycolumns ofposts_questionsandposts_answershold most of their bytes.SELECT *on questions reads 37 GiB, while id, title, scores and tags read about 2.5 GiB. - No table is partitioned or clustered. A filter on
creation_dateor on a tag returns fewer rows but reads the same bytes. - Skip
post_history(113 GiB) unless you need edit history, and skipstackoverflow_posts, which is outdated. - For per-tag counts, read the 2.5 MiB
tagstable instead of splitting the tag string of every question. - The data stopped changing in 2022, so query results are cached for repeated identical queries, and a slice you use often can be saved to your own table once with CREATE TABLE AS SELECT.
FAQ
How much does it cost to query the Stack Overflow dataset?
Most questions about tags, scores and dates read 1 to 3 GiB, far inside the free 1 TiB a month. SELECT * on posts_questions reads 37 GiB, and the whole dataset is about 237 GiB. At $6.25 per TiB on-demand, even a full scan of posts_questions costs about $0.23.
Is the Stack Overflow BigQuery dataset free?
Yes for storage: the table sits in the bigquery-public-data project and Google pays to store it. You pay for the queries you run from your own project. On-demand, the first 1 TiB scanned each month is free, then you pay per TiB scanned (see BigQuery pricing).
Is the Stack Overflow dataset still updated?
No. Its description says it is no longer actively updated, and the last data update is 2022-11-25. Questions and answers posted after that date are not in it.
How do I filter Stack Overflow questions by tag in BigQuery?
Tags are stored as one string such as python|pandas. Use 'pandas' IN UNNEST(SPLIT(tags, '|')) for an exact tag match. tags LIKE '%pandas%' also matches tags like pandas-groupby.
Related datasets
All public datasets · BigQuery cost queries · Pricing calculator
