ADQL and PostgreSQL
Start with the Astronomical Data Query Language (ADQL) when querying Gaia@AIP. It is the default in the Query interface and the standard query language for TAP, so you can reuse the same query syntax across archives. Query interface
Getting started with ADQL
Preview a table
A basic ADQL query selects columns from a table. You can also limit how many rows it returns.
SELECT TOP 100 source_id, ra, dec, phot_g_mean_mag
FROM gaiadr4.gaia_source
SELECT lists the columns to return. Here they contain the source ID, right ascension, declination, and G magnitude. FROM identifies the gaiadr4.gaia_source table. TOP 100 limits the result to 100 rows. Without TOP, a query can return up to 5 million rows, the Gaia@AIP service limit.
Filter and sort results
ADQL lets you narrow the result by specifying a condition that the rows must meet. You can also choose the order in which the matching rows appear.
SELECT TOP 100 source_id, ra, dec, phot_g_mean_mag
FROM gaiadr4.gaia_source
WHERE phot_g_mean_mag < 15
ORDER BY phot_g_mean_mag ASC
WHERE phot_g_mean_mag < 15 selects sources brighter than G = 15. Filtering on an indexed column can help the archive find matches faster.
ORDER BY phot_g_mean_mag ASC sorts the result from brightest to faintest before TOP 100 limits it. ASC means ascending order and is the default. Use DESC for descending order.
Calculate a column
An expression in SELECT can calculate a value for each row and return it as a new column.
SELECT TOP 100 source_id, ra, dec, phot_g_mean_mag,
POWER(10.0, -0.4 * phot_g_mean_mag) AS relative_g_flux
FROM gaiadr4.gaia_source
WHERE phot_g_mean_mag < 15
ORDER BY phot_g_mean_mag ASC
POWER() converts G magnitude to flux relative to the G-band zero point. AS names the calculated column relative_g_flux in the result.
Search around a sky position
DISTANCE() measures the angular separation between two positions on the sky. A limit on that separation defines the region to search.
SELECT TOP 100 source_id, ra, dec, phot_g_mean_mag,
POWER(10.0, -0.4 * phot_g_mean_mag) AS relative_g_flux
FROM gaiadr4.gaia_source
WHERE phot_g_mean_mag < 15
AND DISTANCE(ra, dec, 245.8962, -26.5222) <= 0.5
ORDER BY phot_g_mean_mag ASC
DISTANCE(ra, dec, 245.8962, -26.5222) gives the angular separation of a source from the centre of M4 in degrees. The comparison with 0.5 sets the search radius. Estimating physical separation also requires the distances to the source and the cluster.
Adding the angular distance condition with AND keeps the earlier magnitude cut and restricts the search to sources inside the cone. Use OR when meeting at least one of the conditions is enough.
Join a second table
A join lets you use columns from different tables by matching rows on a shared identifier.
SELECT TOP 100 g.source_id, g.ra, g.dec, g.phot_g_mean_mag,
POWER(10.0, -0.4 * g.phot_g_mean_mag) AS relative_g_flux,
f.has_rvs
FROM gaiadr4.gaia_source AS g
JOIN gaiadr4.all_source_flags AS f ON g.source_id = f.source_id
WHERE g.phot_g_mean_mag < 15
AND DISTANCE(g.ra, g.dec, 245.8962, -26.5222) <= 0.5
AND f.has_rvs > 0
ORDER BY g.phot_g_mean_mag ASC
JOIN ... ON matches rows in gaia_source and all_source_flags by source_id. The aliases g and f identify which table each column comes from. f.has_rvs > 0 keeps sources with an available mean Radial Velocity Spectrometer (RVS) spectrum.
More ADQL examples
ADQL resources
- Look up syntax and functions in the IVOA ADQL 2.1 specification.
- Practise writing queries with GAVO's A Short Course on ADQL.
PostgreSQL
Gaia@AIP also accepts a subset of PostgreSQL, including pgSphere functions. You can select PostgreSQL in the Query interface or set it as the query language in TAP. Scripted access