Skip to content

US elections

US federal election results, House apportionment, district maps and FEC campaign finance

Connector software
Free and open source
Service credentials
Optional
Use with an agent

Install the Orchard skill in your project to help your agent find connectors, configure them, and verify SQL results.

npx skills add Query-farm/vgi-skills --skill orchard

Copy this request into your coding agent to get started.

Use https://query.farm/products/orchard/connectors/us-elections/index.md to help me query US elections with DuckDB or Haybarn 2.0.1 and VGI protocol 3.4. Ask only for missing details about the result I need or whether I want local execution, self-hosting or Query Farm hosting. Check the connector's actual hosting availability, credentials and input requirements. Read one format of each needed reference and the exact object declarations before writing SQL. Use a released connector, and distinguish a saved example result from anything you actually execute. If you cannot run the query, tell me what remains to verify.

Delaware's at-large district in the 118th Congress

Example output · captured
Connector release
v0.2.0
Engine
2.0.1
VGI protocol
3.4.0
SELECT congress, state_po, district, start_congress, end_congress
FROM us_elections.main.districts(118, state := 'DE')
ORDER BY district
Result
congress state_po district start_congress end_congress
118 DE 00 115 119

Quick start

Choose where to run US elections, then follow the steps below.

The connector runs alongside DuckDB or Haybarn. No Query Farm account is needed.

1. Install the vgi extension

Run this in your SQL client. On DuckDB, keep FROM query_farm on LOAD in later sessions.

setup.sql
SET allow_extension_repositories = 'allowed';

CREATE EXTENSION REPOSITORY IF NOT EXISTS query_farm
    WITH PREFIX 'https://duckdb-extensions.query.farm';

INSTALL vgi FROM query_farm;
LOAD vgi FROM query_farm;

2. Attach the connector

Attach with uvx

Needs uv. uvx builds the worker from its release tag on first attach and reuses the cached environment after that.

ATTACH 'us_elections' AS us_elections (
  TYPE vgi,
  LOCATION 'uvx --from git+https://github.com/Query-farm/[email protected] vgi-us-elections-worker'
);

3. Add service credentials (optional)

Election results need no key; the FEC campaign-finance tables need a free api.data.gov key. Replace the placeholders with your service settings.

us_elections_fec credentials

api.data.gov API key for the FEC OpenFEC API (free at https://api.data.gov/signup/).

Get these credentials →
secret.sql
CREATE SECRET us_elections_fec (
    TYPE us_elections_fec,
    api_key '<US_ELECTIONS_FEC_API_KEY>'  -- secret
);
Credential fields and requirements →

4. Run a query

query.sql
SELECT congress, state_po, district, start_congress, end_congress
FROM us_elections.main.districts(118, state := 'DE')
ORDER BY district
More example queries →
Technical details
Verified connector release
v0.2.0
Engine
2.0.1
VGI protocol
3.4.0
Verified on
December 1, 2026
Release notes on GitHub
Pin a version for production

When running the connector yourself, keep a specific release until you’re ready to upgrade. Replace the ATTACH statement above with this one and follow the same extension and credential setup.

Attach with uvx

Needs uv. uvx builds the worker from its release tag on first attach and reuses the cached environment after that.

ATTACH 'us_elections' AS us_elections (
  TYPE vgi,
  LOCATION 'uvx --from git+https://github.com/Query-farm/[email protected] vgi-us-elections-worker'
);

Overview

US federal elections as SQL: House, Senate and presidential results since 1976 and county presidential results since 2000 (MIT Election Lab), House apportionment by census, historical and current congressional district boundaries, and FEC campaign finance.

ATTACH options

cache_dir
Directory for downloaded upstream files. Default: the per-user cache directory, e.g. ~/Library/Caches/vgi-us-elections or ~/.cache/vgi-us-elections.VARCHAR
max_age_days
Re-check Harvard Dataverse for a newer MEDSL release once a cached file is this old.BIGINT
offline
Never download; use cached files only (fails for anything not cached).BOOLEAN
dataverse_url
Base URL of the Dataverse installation holding the MEDSL datasets.VARCHAR
lewis_base_url
Base URL of the Lewis et al. boundary repository (raw files at the pinned commit).VARCHAR
census_cd_base_url
Base URL of the Census cartographic boundary shapefiles (GENZ2024/shp).VARCHAR
fec_base_url
Base URL of the OpenFEC API.VARCHAR
guestbook_name
Your name, for the Harvard Dataverse guestbook that gates the House and county files.VARCHAR
guestbook_email
Your email, for the Dataverse guestbook.VARCHAR
guestbook_institution
Your institution, for the Dataverse guestbook.VARCHAR
guestbook_position
Your position, for the Dataverse guestbook.VARCHAR
dataverse_api_token
Optional Harvard Dataverse API token, sent with guestbook downloads.VARCHAR

Catalog

Everything us_elections exposes once attached — read straight from the worker's released build.

us_elections 1 schema
main 6 tables · 9 functions

Election results, apportionment, district boundaries, FEC campaign finance and join-key helpers for US federal elections.

Schema examples (15)
Examples (15)

Delaware's at-large district in the 118th Congress

SELECT congress, state_po, district, start_congress, end_congress
FROM us_elections.main.districts(118, state := 'DE')
ORDER BY district;
Result
congress state_po district start_congress end_congress
118 DE 00 115 119

States with the most districts in the 119th Congress, with their land area

SELECT state_po, count(*) AS districts,
  round(sum(land_area_m2) / 1e6) AS land_km2
FROM us_elections.main.current_districts(resolution := '20m')
WHERE district <> '98'
GROUP BY state_po
ORDER BY districts DESC, state_po
LIMIT 5;

Delaware's 2024 federal candidates who raised money

SELECT candidate_id, name, party, office, district
FROM us_elections.main.fec_candidates(election_year := 2024, state := 'DE')
WHERE has_raised_funds
ORDER BY office, name;

Top fundraisers in Delaware's 2024 federal races

SELECT name, party, office, receipts, disbursements
FROM us_elections.main.fec_candidate_totals(2024, state := 'DE')
ORDER BY receipts DESC NULLS LAST
LIMIT 5;
11 more examples

Kinds of committees registered in Delaware for 2024

SELECT committee_type_full, count(*) AS committees
FROM us_elections.main.fec_committees(cycle := 2024, state := 'DE')
GROUP BY ALL
ORDER BY committees DESC;

Top donor states for Lisa Blunt Rochester's 2024 Senate campaign

SELECT state, total, contributions
FROM us_elections.main.fec_contributions_by_state('S4DE00060', 2024)
ORDER BY total DESC
LIMIT 5;

At-large and single-digit labels

SELECT us_elections.main.normalize_district('AL') AS at_large,
  us_elections.main.normalize_district('7') AS seventh;

The Congress elected in November 2024

SELECT us_elections.main.congress_for_year(2024) AS congress;

List every cacheable upstream file and whether it is present

SELECT source, item, status, files
FROM us_elections.main.cache_info()
ORDER BY source, item;

Winners of Delaware's at-large House general elections since 2016

SELECT year, candidate, party_simplified, round(100 * vote_share, 1) AS pct
FROM us_elections.main.house_results
WHERE state_po = 'DE' AND stage = 'gen' AND year >= 2016
QUALIFY row_number() OVER (PARTITION BY year ORDER BY candidatevotes DESC) = 1
ORDER BY year;

Winner and vote share of every 2024 regular general Senate race

SELECT state_po, candidate, party_simplified,
  round(100 * vote_share, 1) AS pct
FROM us_elections.main.senate_results
WHERE year = 2024 AND stage = 'gen' AND NOT special
QUALIFY row_number() OVER (PARTITION BY state_po ORDER BY candidatevotes DESC) = 1
ORDER BY state_po;

National popular vote by party in 2020 and 2024

SELECT year, party_simplified, sum(candidatevotes) AS votes
FROM us_elections.main.president_results
WHERE year IN (2020, 2024)
GROUP BY ALL
ORDER BY year, votes DESC;

Delaware's three counties in 2024: Democratic share

SELECT county_name, county_fips,
  round(100 * sum(candidatevotes) FILTER (WHERE party = 'DEMOCRAT') / sum(candidatevotes), 1) AS dem_pct
FROM us_elections.main.county_president_results
WHERE year = 2024 AND state_po = 'DE'
GROUP BY ALL
ORDER BY county_fips;

States that gained House seats in the 2020 apportionment

SELECT state_po, seats, seat_change
FROM us_elections.main.apportionment
WHERE census_year = 2020 AND seat_change > 0
ORDER BY seat_change DESC, state_po;

Look up the FIPS code of a USPS abbreviation

SELECT state_fips, state
FROM us_elections.main.states
WHERE state_po = 'DE';
Tables
apportionment 3 examples10 cols

US House seats, population and seats change per state after each census, 1910-2020 (Census Bureau).

Columns
census_yearnot null INTEGER
Decennial census year, 1910 to 2020.
statenot null VARCHAR
State name ('California').
state_ponot null VARCHAR
Two-letter USPS state code ('CA').
state_fipsnot null VARCHAR
Two-digit FIPS state code ('06').
seatsnot null INTEGER
US House seats apportioned to the state from this census. 1920: Congress did not reapportion, so the Census lists the seats in force (from 1910).
seat_change INTEGER
Change in seats from the previous census; NULL for the first year listed.
resident_population BIGINT
Number of residents counted by the census (persons).
population_change_pct DOUBLE
Percent change in resident population since the previous census.
population_density DOUBLE
Residents per square mile of land.
avg_population_per_seat BIGINT
Average apportionment population per representative (the Census's district-size figure).
ConstraintsPRIMARY KEY (census_year, state_po)
Examples (3)

States that gained House seats in the 2020 apportionment

SELECT state_po, seats, seat_change
FROM us_elections.main.apportionment
WHERE census_year = 2020 AND seat_change > 0
ORDER BY seat_change DESC, state_po;

California's House seats after each census since 1950

SELECT census_year, seats
FROM us_elections.main.apportionment
WHERE state_po = 'CA' AND census_year >= 1950
ORDER BY census_year;

Total House seats: 433 after 1910 (Arizona and New Mexico added 2 on admission), then 435

SELECT census_year, sum(seats) AS house_seats
FROM us_elections.main.apportionment
WHERE census_year IN (1910, 1960, 2020)
GROUP BY census_year
ORDER BY census_year;
Link
county_president_results 1 example13 cols

US presidential returns by county, 2000-2024, with five-digit county FIPS codes (MEDSL, CC0).

Columns
yearnot null INTEGER
Election year (INTEGER). Join key with the other tables and with FEC cycles.
state VARCHAR
State name, proper case ('Delaware'), derived from state_po.
state_ponot null VARCHAR
Two-letter USPS state code ('DE'); the same code FEC uses.
state_fips VARCHAR
Two-digit FIPS state code ('06'), from county_fips or state_po.
county_name VARCHAR
County (or county-equivalent) name, upper case as MEDSL publishes it.
county_fips VARCHAR
Five-digit county FIPS code as VARCHAR ('01001'); NULL for non-county reporting units (overseas, federal precincts, statewide write-ins).
candidate VARCHAR
Candidate name as MEDSL publishes it (upper case; 'LAST, FIRST' in older years). NULL for some write-ins.
party VARCHAR
MEDSL's party grouping: DEMOCRAT, REPUBLICAN, LIBERTARIAN, GREEN or OTHER.
mode VARCHAR
Vote mode, lower case: 'total' for most rows; some states report election-day/absentee/early separately. Do not add 'total' to the other modes.
candidatevotes BIGINT
Votes for this candidate in this contest (and mode).
totalvotes BIGINT
Total votes cast in the contest (all candidates); the denominator of vote_share.
vote_share DOUBLE
Derived fraction (0 to 1) of the contest's votes: candidatevotes / totalvotes; NULL when totalvotes is 0.
version VARCHAR
MEDSL's version stamp for the row's source vintage (e.g. '20250203').
Example

Delaware's three counties in 2024: Democratic share

SELECT county_name, county_fips,
  round(100 * sum(candidatevotes) FILTER (WHERE party = 'DEMOCRAT') / sum(candidatevotes), 1) AS dem_pct
FROM us_elections.main.county_president_results
WHERE year = 2024 AND state_po = 'DE'
GROUP BY ALL
ORDER BY county_fips;
Link
house_results 2 examples20 cols

US House returns by district, 1976-2024: one row per candidate (and party line) per contest (MEDSL, CC0).

Columns
yearnot null INTEGER
Election year (INTEGER). Join key with the other tables and with FEC cycles.
congressnot null INTEGER
Derived: the Congress elected at a regular general election in this year, (year - 1786) / 2 (2024 -> 119). Special elections in even years fill a seat in the sitting Congress (one less).
state VARCHAR
State name, proper case ('Delaware'), derived from state_po.
state_ponot null VARCHAR
Two-letter USPS state code ('DE'); the same code FEC uses.
state_fips VARCHAR
Two-digit FIPS state code as VARCHAR with the leading zero ('06').
district VARCHAR
House district as two-digit VARCHAR: '01', '02' ... and '00' for an at-large (single-seat) state. Joins to districts() and current_districts() and to FEC's district.
stage VARCHAR
Election stage, lower case: 'gen' (general), 'pre' (primary) or 'runoff'.
runoff BOOLEAN
TRUE for a runoff contest.
special BOOLEAN
TRUE for a special election (filling a vacancy).
candidate VARCHAR
Candidate name as MEDSL publishes it (upper case; 'LAST, FIRST' in older years). NULL for some write-ins.
party_detailed VARCHAR
Party label exactly as on the ballot or in the official return ('DEMOCRATIC-FARMER-LABOR').
party_simplified VARCHAR
Derived from party_detailed: DEMOCRAT, REPUBLICAN, LIBERTARIAN or OTHER.
writein BOOLEAN
TRUE for write-in candidates (including aggregated 'scattering' rows).
mode VARCHAR
Vote mode, lower case: 'total' for most rows; some states report election-day/absentee/early separately.
candidatevotes BIGINT
Votes for this candidate in this contest (and mode).
totalvotes BIGINT
Total votes cast in the contest (all candidates); the denominator of vote_share.
vote_share DOUBLE
Derived fraction (0 to 1) of the contest's votes: candidatevotes / totalvotes; NULL when totalvotes is 0.
unofficial BOOLEAN
TRUE when MEDSL marked the count unofficial (not yet certified when compiled).
fusion_ticket BOOLEAN
TRUE when the candidate ran on several party lines (New York, Connecticut ...): one row per line; sum the rows for the candidate's total.
version VARCHAR
MEDSL's version stamp for the row's source vintage (e.g. '20250203').
Examples (2)

Winners of Delaware's at-large House general elections since 2016

SELECT year, candidate, party_simplified, round(100 * vote_share, 1) AS pct
FROM us_elections.main.house_results
WHERE state_po = 'DE' AND stage = 'gen' AND year >= 2016
QUALIFY row_number() OVER (PARTITION BY year ORDER BY candidatevotes DESC) = 1
ORDER BY year;

Two-party House popular vote by year

SELECT year,
  sum(candidatevotes) FILTER (WHERE party_simplified = 'DEMOCRAT') AS dem,
  sum(candidatevotes) FILTER (WHERE party_simplified = 'REPUBLICAN') AS rep
FROM us_elections.main.house_results
WHERE stage = 'gen' AND year >= 2016
GROUP BY year
ORDER BY year;
Link
president_results 2 examples13 cols

US presidential returns by state, 1976-2024: one row per candidate per state (MEDSL, CC0).

Columns
yearnot null INTEGER
Election year (INTEGER). Join key with the other tables and with FEC cycles.
state VARCHAR
State name, proper case ('Delaware'), derived from state_po.
state_ponot null VARCHAR
Two-letter USPS state code ('DE'); the same code FEC uses.
state_fips VARCHAR
Two-digit FIPS state code as VARCHAR with the leading zero ('06').
candidate VARCHAR
Candidate name as MEDSL publishes it (upper case; 'LAST, FIRST' in older years). NULL for some write-ins.
party_detailed VARCHAR
Party label exactly as on the ballot or in the official return ('DEMOCRATIC-FARMER-LABOR').
party_simplified VARCHAR
Simplified party: DEMOCRAT, REPUBLICAN, LIBERTARIAN or OTHER.
writein BOOLEAN
TRUE for write-in candidates (including aggregated 'scattering' rows).
candidatevotes BIGINT
Votes for this candidate in this contest (and mode).
totalvotes BIGINT
Total votes cast in the contest (all candidates); the denominator of vote_share.
vote_share DOUBLE
Derived fraction (0 to 1) of the contest's votes: candidatevotes / totalvotes; NULL when totalvotes is 0.
notes VARCHAR
MEDSL's free-text note on the row, usually NULL.
version VARCHAR
MEDSL's version stamp for the row's source vintage (e.g. '20250203').
Examples (2)

National popular vote by party in 2020 and 2024

SELECT year, party_simplified, sum(candidatevotes) AS votes
FROM us_elections.main.president_results
WHERE year IN (2020, 2024)
GROUP BY ALL
ORDER BY year, votes DESC;

Delaware's 2024 presidential result

SELECT candidate, party_simplified, candidatevotes,
  round(100 * vote_share, 1) AS pct
FROM us_elections.main.president_results
WHERE year = 2024 AND state_po = 'DE' AND NOT writein
ORDER BY candidatevotes DESC;
Link
senate_results 2 examples17 cols

US Senate returns by state, 1976-2024: one row per candidate per contest (MEDSL, CC0).

Columns
yearnot null INTEGER
Election year (INTEGER). Join key with the other tables and with FEC cycles.
congressnot null INTEGER
Derived: the Congress elected at a regular general election in this year, (year - 1786) / 2 (2024 -> 119). Special elections in even years fill a seat in the sitting Congress (one less).
state VARCHAR
State name, proper case ('Delaware'), derived from state_po.
state_ponot null VARCHAR
Two-letter USPS state code ('DE'); the same code FEC uses.
state_fips VARCHAR
Two-digit FIPS state code as VARCHAR with the leading zero ('06').
stage VARCHAR
Election stage, lower case: 'gen' (general), 'pre' (primary) or 'runoff'.
special BOOLEAN
TRUE for a special election (filling a vacancy).
candidate VARCHAR
Candidate name as MEDSL publishes it (upper case; 'LAST, FIRST' in older years). NULL for some write-ins.
party_detailed VARCHAR
Party label exactly as on the ballot or in the official return ('DEMOCRATIC-FARMER-LABOR').
party_simplified VARCHAR
Simplified party: DEMOCRAT, REPUBLICAN, LIBERTARIAN or OTHER.
writein BOOLEAN
TRUE for write-in candidates (including aggregated 'scattering' rows).
mode VARCHAR
Vote mode, lower case: 'total' for most rows; some states report election-day/absentee/early separately.
candidatevotes BIGINT
Votes for this candidate in this contest (and mode).
totalvotes BIGINT
Total votes cast in the contest (all candidates); the denominator of vote_share.
vote_share DOUBLE
Derived fraction (0 to 1) of the contest's votes: candidatevotes / totalvotes; NULL when totalvotes is 0.
unofficial BOOLEAN
TRUE when MEDSL marked the count unofficial (not yet certified when compiled).
version VARCHAR
MEDSL's version stamp for the row's source vintage (e.g. '20250203').
Examples (2)

Winner and vote share of every 2024 regular general Senate race

SELECT state_po, candidate, party_simplified,
  round(100 * vote_share, 1) AS pct
FROM us_elections.main.senate_results
WHERE year = 2024 AND stage = 'gen' AND NOT special
QUALIFY row_number() OVER (PARTITION BY state_po ORDER BY candidatevotes DESC) = 1
ORDER BY state_po;

Delaware Senate general-election results in 2024

SELECT candidate, party_detailed, candidatevotes,
  round(100 * vote_share, 2) AS pct
FROM us_elections.main.senate_results
WHERE year = 2024 AND state_po = 'DE' AND stage = 'gen'
ORDER BY candidatevotes DESC;
Link
states 2 examples4 cols

US states, DC and territories with FIPS and USPS codes: the join keys every table here uses.

Columns
state_fipsnot null VARCHAR
Two-digit FIPS state code ('06').
state_ponot null VARCHAR
Two-letter USPS code ('CA').
statenot null VARCHAR
Name ('California').
kindnot null VARCHAR
'state', 'district' (DC) or 'territory'.
ConstraintsPRIMARY KEY (state_fips)
Examples (2)

Look up the FIPS code of a USPS abbreviation

SELECT state_fips, state
FROM us_elections.main.states
WHERE state_po = 'DE';

Count states, DC and territories

SELECT kind, count(*) AS n
FROM us_elections.main.states
GROUP BY kind
ORDER BY kind;
Link
Scalar functions
congress_for_year (year INTEGER) 2 examples

The Congress elected at the regular November election of a year: 2024 -> 119

Arguments
year INTEGER
Election year; NULL in, NULL out.
ReturnsINTEGER
Examples (2)

The Congress elected in November 2024

SELECT us_elections.main.congress_for_year(2024) AS congress;
SELECT y, us_elections.main.congress_for_year(y) AS congress
FROM (VALUES (1788), (2022), (2024)) t(y)
ORDER BY y;
Link
normalize_district (label VARCHAR) 2 examples

Normalize a House district label ('AL', '7', '12.0', 'at-large') to two-digit text ('00' at-large)

Arguments
label VARCHAR
District label in any common spelling; NULL in, NULL out.
ReturnsVARCHAR
Examples (2)

At-large and single-digit labels

SELECT us_elections.main.normalize_district('AL') AS at_large,
  us_elections.main.normalize_district('7') AS seventh;
SELECT d, us_elections.main.normalize_district(d) AS district
FROM (VALUES ('AL'), ('7'), ('12.0'), ('ZZ')) t(d)
ORDER BY d;
Link
Table functions
cache_info () 1 example

Show which upstream election files are cached locally, their versions and paths

No arguments
Returns columns
source VARCHAR
'medsl', 'lewis' (historical districts) or 'census' (119th districts).
item VARCHAR
Dataset key ('house', 'senate' ...), 'geojson files', or the Census zip name.
status VARCHAR
'cached' (downloaded by the worker), 'manual' (a file you placed there) or 'missing'.
path VARCHAR
Absolute cache path; save a manual download here.
files BIGINT
Number of cached files (several for the per-state Lewis files).
size_bytes BIGINT
Bytes on disk; NULL when missing.
dataset_version VARCHAR
Upstream version: Dataverse dataset version or pinned git commit.
sha256 VARCHAR
SHA-256 of the cached file (MEDSL), when recorded.
downloaded_at TIMESTAMP WITH TIME ZONE
When the worker downloaded it.
upstream VARCHAR
Where it comes from (DOI or URL).
license VARCHAR
Upstream data licence.
Example

List every cacheable upstream file and whether it is present

SELECT source, item, status, files
FROM us_elections.main.cache_info()
ORDER BY source, item;
Link
current_districts (resolution := VARCHAR, state := VARCHAR) 2 examples

119th Congress district boundaries from the Census Bureau, with land and water area

Named arguments
resolution := VARCHAR
Census generalisation: '500k' (most detail, ~7 MB download), '5m' or '20m' (~0.4 MB). · default "500k"
state := VARCHAR
Limit to one state or territory: USPS code ('DE'), FIPS code or name. · default null
Returns columns
congress INTEGER
Always 119: the Congress these boundaries are drawn for.
state VARCHAR
State or territory name ('Delaware').
state_po VARCHAR
Two-letter USPS code ('DE').
state_fips VARCHAR
Two-digit FIPS state code ('10').
district VARCHAR
Two-digit district ('01'); '00' at-large; '98' for the non-voting delegate seats (DC, PR, territories).
geoid VARCHAR
Census GEOID: state FIPS + district ('1000').
name VARCHAR
Census name ('Congressional District (at Large)').
land_area_m2 BIGINT
Land area in square metres (Census ALAND).
water_area_m2 BIGINT
Water area in square metres (Census AWATER).
geometry GEOMETRY('OGC:CRS84')
District boundary as GEOMETRY('OGC:CRS84') (lon/lat; NAD83 passed through).
Examples (2)

States with the most districts in the 119th Congress, with their land area

SELECT state_po, count(*) AS districts,
  round(sum(land_area_m2) / 1e6) AS land_km2
FROM us_elections.main.current_districts(resolution := '20m')
WHERE district <> '98'
GROUP BY state_po
ORDER BY districts DESC, state_po
LIMIT 5;

Wyoming's at-large district

SELECT geoid, district, name
FROM us_elections.main.current_districts(state := 'WY', resolution := '20m')
ORDER BY geoid;
Link
districts (congress BIGINT, state := VARCHAR) 2 examples

Congressional district boundaries for any Congress from 1789 to today, as GEOMETRY

Positional arguments
congress BIGINT
Congress number, 1 (1789) to 119 (2025-2027).
Named arguments
state := VARCHAR
Limit to one state: USPS code ('DE'), FIPS code ('10') or name. · default null
Returns columns
congress INTEGER
The Congress requested (1 = 1789-1791 ... 119 = 2025-2027).
state VARCHAR
State name, proper case ('Delaware').
state_po VARCHAR
Two-letter USPS state code ('DE').
state_fips VARCHAR
Two-digit FIPS state code as VARCHAR ('10').
district VARCHAR
District as two-digit VARCHAR ('01'); '00' is an at-large seat (a single-seat state, or a seat elected statewide alongside districts).
start_congress INTEGER
First Congress these boundaries were used for.
end_congress INTEGER
Last Congress these boundaries were used for (inclusive).
district_id VARCHAR
Lewis et al. identifier: state FIPS (3), start, end and district (3 each).
law VARCHAR
Statute or court order that drew the district, when the source records it.
note VARCHAR
Source notes on how the boundary was reconstructed.
geometry GEOMETRY('OGC:CRS84')
District boundary as GEOMETRY('OGC:CRS84') polygons (lon/lat, NAD83 passed through).
Examples (2)

Delaware's at-large district in the 118th Congress

SELECT congress, state_po, district, start_congress, end_congress
FROM us_elections.main.districts(118, state := 'DE')
ORDER BY district;
Result
congress state_po district start_congress end_congress
118 DE 00 115 119

Wyoming's district count in the 100th and 119th Congress

SELECT congress, count(*) AS districts
FROM (SELECT * FROM us_elections.main.districts(100, state := 'WY') UNION ALL SELECT * FROM us_elections.main.districts(119, state := 'WY'))
GROUP BY congress
ORDER BY congress;
Link
fec_candidate_totals (election_year BIGINT, candidate_id := VARCHAR, district := VARCHAR, office := VARCHAR, party := VARCHAR, state := VARCHAR) 1 example

Receipts, spending, cash and debts per FEC candidate for one election year

Positional arguments
election_year BIGINT
Election year, e.g. 2024 (totals cover the whole election period).
Named arguments
candidate_id := VARCHAR
One FEC candidate id. · default null
district := VARCHAR
House district number in any common spelling; normalized like normalize_district. · default null
office := VARCHAR
'H', 'S' or 'P'. · default null
party := VARCHAR
Three-letter FEC party code (DEM for Democratic). · default null
state := VARCHAR
State: USPS code, FIPS code or name. · default null
Returns columns
candidate_id VARCHAR
FEC candidate id.
name VARCHAR
Candidate name as filed.
party VARCHAR
FEC party code.
office VARCHAR
'H', 'S' or 'P'.
state VARCHAR
USPS state code of the race.
district VARCHAR
Two-digit House district ('00' at-large and for Senate/President).
election_year INTEGER
The election these totals are for.
cycle INTEGER
Two-year FEC cycle of the totals record.
incumbent_challenge VARCHAR
'I' incumbent, 'C' challenger, 'O' open seat.
receipts DECIMAL(18,2)
Total receipts in US dollars over the election period.
disbursements DECIMAL(18,2)
Total disbursements in US dollars.
cash_on_hand_end_period DECIMAL(18,2)
Cash on hand at the end of the coverage period.
debts_owed_by_committee DECIMAL(18,2)
Debts owed by the campaign committee.
individual_itemized_contributions DECIMAL(18,2)
Itemized contributions from individuals (over $200).
other_political_committee_contributions DECIMAL(18,2)
Contributions from PACs and other committees.
transfers_from_other_authorized_committee DECIMAL(18,2)
Transfers from the candidate's other committees.
coverage_start_date DATE
First day covered by the reports.
coverage_end_date DATE
Last day covered by the reports.
Example

Top fundraisers in Delaware's 2024 federal races

SELECT name, party, office, receipts, disbursements
FROM us_elections.main.fec_candidate_totals(2024, state := 'DE')
ORDER BY receipts DESC NULLS LAST
LIMIT 5;
Link
fec_candidates (candidate_id := VARCHAR, district := VARCHAR, election_year := BIGINT, name := VARCHAR, office := VARCHAR, party := VARCHAR, state := VARCHAR) 1 example

Candidates registered with the FEC, filtered by year, state, office, district, party or name

Named arguments
candidate_id := VARCHAR
One FEC candidate id. · default null
district := VARCHAR
House district number in any common spelling; normalized like normalize_district. · default null
election_year := BIGINT
Election year, e.g. 2024. · default null
name := VARCHAR
Full-text name search, e.g. 'rochester'. · default null
office := VARCHAR
'H', 'S' or 'P'. · default null
party := VARCHAR
Three-letter FEC party code (DEM for Democratic). · default null
state := VARCHAR
State: USPS code, FIPS code or name. · default null
Returns columns
candidate_id VARCHAR
FEC candidate id ('H4DE01084'); join key across FEC tables.
name VARCHAR
Candidate name as filed ('LAST, FIRST MIDDLE').
party VARCHAR
FEC party code ('DEM', 'REP', 'LIB', 'IND' ...).
party_full VARCHAR
Party name ('DEMOCRATIC PARTY').
office VARCHAR
Office sought: 'H' House, 'S' Senate, 'P' President.
state VARCHAR
USPS state code of the race ('US' for president).
district VARCHAR
Two-digit House district ('00' at-large and for Senate/President).
incumbent_challenge VARCHAR
'I' incumbent, 'C' challenger, 'O' open seat.
candidate_status VARCHAR
'C' statutory candidate, 'F' future, 'N' not yet, 'P' prior.
candidate_inactive BOOLEAN
TRUE when the FEC marks the candidate inactive.
election_years INTEGER[]
Election years the candidate registered for.
cycles INTEGER[]
Two-year cycles with activity.
active_through INTEGER
Last election year the candidate is active through.
has_raised_funds BOOLEAN
TRUE when the candidate's committees reported receipts.
first_file_date DATE
Date of the first filing.
last_file_date DATE
Date of the latest filing.
Example

Delaware's 2024 federal candidates who raised money

SELECT candidate_id, name, party, office, district
FROM us_elections.main.fec_candidates(election_year := 2024, state := 'DE')
WHERE has_raised_funds
ORDER BY office, name;
Link
fec_committees (candidate_id := VARCHAR, committee_type := VARCHAR, cycle := BIGINT, designation := VARCHAR, name := VARCHAR, party := VARCHAR, state := VARCHAR) 1 example

Campaign committees, PACs and super PACs registered with the FEC

Named arguments
candidate_id := VARCHAR
Committees linked to this candidate. · default null
committee_type := VARCHAR
One-letter FEC committee type code, as in the committee_type output column. · default null
cycle := BIGINT
Two-year cycle, e.g. 2024. · default null
designation := VARCHAR
One-letter FEC designation code, as in the designation output column. · default null
name := VARCHAR
Full-text name search. · default null
party := VARCHAR
Three-letter FEC party code (DEM for Democratic). · default null
state := VARCHAR
State: USPS code, FIPS code or name. · default null
Returns columns
committee_id VARCHAR
FEC committee id ('C00884494').
name VARCHAR
Committee name.
committee_type VARCHAR
FEC type code: 'H'/'S'/'P' candidate, 'O' super PAC, 'Q'/'N' PAC ...
committee_type_full VARCHAR
Committee type in words ('Super PAC (Independent Expenditure-Only)').
designation VARCHAR
'P' principal campaign, 'A' authorized, 'J' joint fundraiser, 'U' unauthorized ...
designation_full VARCHAR
Designation in words.
party VARCHAR
Party code, when the committee has one.
state VARCHAR
USPS code of the committee's state.
organization_type VARCHAR
Organization type code (corporation, labor ...), when reported.
filing_frequency VARCHAR
'Q' quarterly, 'M' monthly, 'T' terminated ...
treasurer_name VARCHAR
Treasurer of record.
candidate_ids VARCHAR[]
Candidates the committee is linked to (FEC candidate ids).
cycles INTEGER[]
Two-year cycles with activity.
first_file_date DATE
Date of the first filing.
last_file_date DATE
Date of the latest filing.
Example

Kinds of committees registered in Delaware for 2024

SELECT committee_type_full, count(*) AS committees
FROM us_elections.main.fec_committees(cycle := 2024, state := 'DE')
GROUP BY ALL
ORDER BY committees DESC;
Link
fec_contributions_by_state (candidate_id VARCHAR, cycle BIGINT) 1 example

A candidate's itemized individual contributions summed by contributor state

Arguments
candidate_id VARCHAR
FEC candidate id, e.g. 'S4DE00060'.
cycle BIGINT
Election cycle (even year), e.g. 2024.
Returns columns
candidate_id VARCHAR
FEC candidate id.
cycle INTEGER
Two-year cycle requested (election_full: the whole election period).
state VARCHAR
USPS code of the contributors' state.
state_full VARCHAR
Contributors' state name.
total DECIMAL(18,2)
Itemized individual contributions from that state, US dollars.
contributions BIGINT
Number of itemized contributions from that state.
Example

Top donor states for Lisa Blunt Rochester's 2024 Senate campaign

SELECT state, total, contributions
FROM us_elections.main.fec_contributions_by_state('S4DE00060', 2024)
ORDER BY total DESC
LIMIT 5;
Link

Credentials

The secret type us_elections declares, as reported by the worker itself.

us_elections_fec

How to get these credentials

api.data.gov API key for the FEC OpenFEC API (free at https://api.data.gov/signup/).

secret.sql
CREATE SECRET us_elections_fec (
    TYPE us_elections_fec,
    api_key '<US_ELECTIONS_FEC_API_KEY>'  -- secret
);
Key Type Required Description
api_key secret VARCHAR — api.data.gov key sent as the api_key query parameter.

The worker doesn't mark which keys are required.

Needed by fec_candidatesfec_candidate_totalsfec_committeesfec_contributions_by_state

Create the secret in the same session, after attaching us_elections — the worker registers its secret types at ATTACH. Keys marked secret are masked in duckdb_secrets(); angle-bracket values are placeholders. Read credentials from your environment or secret store in your application and pass them through your SQL client’s parameter bindings. Keep real values out of saved SQL and history. Native DuckDB and Haybarn do not provide a SQL getenv() function.

Try these queries

States with the most districts in the 119th Congress, with their land area

SELECT state_po, count(*) AS districts,
  round(sum(land_area_m2) / 1e6) AS land_km2
FROM us_elections.main.current_districts(resolution := '20m')
WHERE district <> '98'
GROUP BY state_po
ORDER BY districts DESC, state_po
LIMIT 5

Uses current_districts

Delaware's 2024 federal candidates who raised money

SELECT candidate_id, name, party, office, district
FROM us_elections.main.fec_candidates(election_year := 2024, state := 'DE')
WHERE has_raised_funds
ORDER BY office, name

Uses fec_candidates

Top fundraisers in Delaware's 2024 federal races

SELECT name, party, office, receipts, disbursements
FROM us_elections.main.fec_candidate_totals(2024, state := 'DE')
ORDER BY receipts DESC NULLS LAST
LIMIT 5

Uses fec_candidate_totals

Kinds of committees registered in Delaware for 2024

SELECT committee_type_full, count(*) AS committees
FROM us_elections.main.fec_committees(cycle := 2024, state := 'DE')
GROUP BY ALL
ORDER BY committees DESC

Uses fec_committees

Top donor states for Lisa Blunt Rochester's 2024 Senate campaign

SELECT state, total, contributions
FROM us_elections.main.fec_contributions_by_state('S4DE00060', 2024)
ORDER BY total DESC
LIMIT 5

Uses fec_contributions_by_state

At-large and single-digit labels

SELECT us_elections.main.normalize_district('AL') AS at_large,
  us_elections.main.normalize_district('7') AS seventh

Uses normalize_district

18 more examples in the Catalog, each listed under the table or function it uses.

Questions it can answer

Plain-English questions from the worker's agent test tasks. An AI agent with this catalog attached should answer each one with SQL.

  • Using the us_elections worker, which states gained House seats in the 2020 apportionment, and how many did each gain?
  • How many US House seats did California have after each census from 1950 through 2020?
  • Using the us_elections worker, which numbered Congress was elected in the November 2022 elections?
  • Using the us_elections worker, what is Nebraska's two-digit FIPS state code?
  • Using the us_elections worker, how many congressional districts did Wyoming have in the 100th Congress, and what district code does it use?
  • Using the us_elections worker, which 119th Congress district has the largest land area? Give its state code and district.

US elections is an independent open-source connector from Query.Farm. It is not affiliated with, endorsed by or sponsored by the services, projects or organisations it works with. Product names, logos and trademarks belong to their respective owners.