|
The following is a selection of actual queries submitted by SDSS users, and some are in response to scientific questions posed by users. The queries are listed in increasing order of difficulty/complexity. Where applicable, query execution times for the latest SDSS data releases are noted. NOTE: Please also read the Optimizing Queries and Bookmark Lookup Bug sections of the SQL Intro page to learn how to run faster queries, and the Query Limits page to see the timeouts and row limits on queries. Click on the name of the query from the list below to go directly to that sample query. The queries are roughly in order of increasing complexity. You can cut and paste queries from here into your favorite search tool.
Basic SELECT-FROM-WHERE
| |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
-- Returns 5261 objects in DR2 (5278 in DR1) in a few sec. -- Find objects in a particular field. -- A basic SELECT - FROM - WHERE query. | |
| SELECT objID, | -- Get the unique object ID, |
| | -- the field number, and coordinates |
| FROM PhotoObj | -- From the photometric data |
| WHERE run=1336 and field = 11 | -- that matches our criteria |
| | |
|
-- Returns 1000 objects in a few sec. -- Find all galaxies brighter than r magnitude 22, where the local -- extinction is > 0.175. This is a simple query that uses a WHERE clause, -- but now two conditions that must be met simultaneously. However, this -- query returns a lot of galaxies (29 Million in DR2!), so it will take a -- long time to get the results back. The sample therefore includes a -- "TOP 1000" restriction to make it run quickly. -- This query also introduces the Galaxy view, which contains the -- photometric parameters (no redshifts or spectra) for unresolved objects. | |
| SELECT TOP 1000 objID | |
| FROM Galaxy | |
| WHERE | |
| | -- r IS NOT deredenned |
| | -- extinction more than 0.175 |
|
-- Find all objects with unclassified spectra. -- A simple SELECT-FROM-WHERE query, using a function | |
| SELECT specObjID | |
| FROM SpecObj | |
| WHERE SpecClass = dbo.fSpecClass('UNKNOWN') | |
|
-- Find all galaxies with blue surface brightness between 23 and 25 -- mag per square arcseconds, and -10 < supergalactic latitude (sgb) < 10, and -- declination less than zero. Currently, we have to live with ra/dec until we -- get galactic coordinates. To calculate surface brightness per sq. arcsec, -- we use (g + rho), where g is the blue magnitude, and rho= 5*log(r). This -- query now has three requirements, one involving simple math. | |
| SELECT objID | |
| FROM Galaxy | |
| WHERE ra between 250 and 270 | |
| | |
| | -- g is blue magnitude, and rho= 5*log(r) |
| | |
|
-- Find galaxies in a given area of the sky, using a coordinate cut -- in the unit vector cx,cy,cz that corresponds to RA beteen 40 and 100. -- Another simple query that uses math in the WHERE clause. | |
| SELECT colc_g, colc_r | |
| FROM Galaxy | |
| WHERE (-0.642788 * cx + 0.766044 * cy>=0) | |
| | |
| | |
|
-- Search for Cataclysmic Variables and pre-CVs with White Dwarfs and -- very late secondaries. Just uses some simple color cuts from Paula Szkody. -- Another simple query that uses math in the WHERE clause | |
| SELECT run, | |
| | |
| | |
| | |
| | |
| | |
| | -- Just get some basic quantities |
| FROM PhotoPrimary | -- From all primary detections, regardless of class |
| WHERE u - g < 0.4 | |
| | |
| | |
| | -- that meet the color criteria |
| | |
|
-- Give me the colours of a random 1% sample of objects from all fields -- which are 'survey quality' so that I could plot up colour-colour diagrams -- and play around with more sophisticated cuts. From Karl Glazebrook. Uses -- the HTM spatial index ID to apply the cut against. Replace the last '1' by -- a different number if you want to sample a different percentage of objects. | |
| SELECT u, g, r, i, z FROM Galaxy | |
| WHERE htmid*37 & 0x000000000000FFFF < (650 * 1) | |
| | |
|
-- Low-z QSO candidates using the color cuts from Gordon Richards. -- Also a simple query with a long WHERE clause. | |
| SELECT | |
| | |
| | |
| | |
| | |
| | |
| | |
| FROM | |
| | |
| WHERE ( (g <= 22) | |
| | |
| | |
| | |
| | |
| | |
| | |
| | |
| | |
| | |
|
-- Get object velocities and errors. This is also a simple query that uses a WHERE clause. -- However, we perform a more complex mathematical operation, using 'power' to -- exponentiate. (From Robert Lupton). -- NOTE: This query takes a long time to run without the "TOP 1000". | |
| SELECT TOP 1000 | |
| | |
| | |
| | |
| | |
| | |
| | |
| | |
| | |
| FROM PhotoPrimary | |
| WHERE | |
| | |
| | |
| | |
| | |
|
-- Find galaxies with an isophotal surface brightness (SB) larger -- than 24 in the red band, and with an ellipticity > 0.5, and with the major -- axis of the ellipse having a declination between 30" and 60" arc seconds. -- This is also a simple query that uses a WHERE clause with three conditions -- that must be met. We introduce the syntax 'between' to do a range search. | |
| SELECT TOP 10 objID, r, rho, isoA_r | |
| FROM Galaxy | |
| WHERE | |
| | -- red surface brightness more than |
| -- 24 mag/sq-arcsec | |
| | -- major axis between 30" and 60" |
| | -- (SDSS pixels = 0.4 arcsec) |
| | -- square of ellipticity > 0.5 squared |
| | |
|
-- Provide a list of moving objects consistent with an asteroid. -- Also a simple query, but we introduce the 'as' syntax, which allows us to -- name derived quantities in the result file. | |
| SELECT | |
| | |
| | |
| FROM PhotoObj | |
| WHERE | |
| | |
| | |
| | |
|
-- Find quasars as specified by Xiaohui Fan et.al. -- A rather straightforward query, just with many conditions. It also introduces -- the Star view, which contains the photometric parameters for all primary point-like -- objects (including quasars). | |
| SELECT run, | |
| | |
| | |
| | |
| | |
| | |
| | |
| FROM Star | -- or Galaxy |
| WHERE ( u - g > 2.0 or u > 22.3 ) | |
| | |
| | |
| | |
| | |
| | |
| | |
|
-- Using object counting and logic in a query. -- Find all objects similar to the colors of a quasar at 5.5 | ||||||||||
| SELECT count(*) as 'total', | ||||||||||
| | | FROM PhotoPrimary -- for each object
| WHERE (( u - g > 2.0) or (u > 22.3) ) -- apply the quasar color cut.
| | | | | | | |
|
-- Find galaxies that are blended with a star, and output the -- deblended galaxy magnitudes. -- This query introduces the use of multiple tables or views with a table JOIN clause. -- You can assign nicknames to tables as in the FROM clause below. Since you are using -- multiple tables, you must specify which table each quantity in the SELECT clause -- comes from. The "ON -- condition between the two tables, which is achieved by requiring that a quantity -- present in both tables be equal. -- NOTE: This query takes a long time to run without the "TOP 10". | |
| SELECT TOP 10 G.ObjID, G.u, G.g, G.r, G.i, G.z | -- get the ObjID and final mags |
| FROM Galaxy AS G | -- use two Views, Galaxy and Star, as a |
| | -- convenient way to compare objects |
| | -- JOIN condition: star has same parent |
| WHERE G.parentID > 0 | -- galaxy has a "parent", which tells us this |
| -- object was deblended | |
| | |
|
-- Give me the PSF colors of all stars brighter than g=20 that have PSP_STATUS = 2. -- Another simple multi-table query with a JOIN. | |
| SELECT | |
| | -- or whatever you want from each object |
| | |
| | |
| | |
| | |
| FROM Star AS s | |
| | |
| WHERE s.psfMag_g < 20 | |
| | |
| | |
|
-- Find the parameters for all objects in fields with desired PSF width and range -- of columns. Now we are using three tables, but it is still a simple query. | |
| SELECT | |
| | |
| | |
| | |
| | |
| | |
| | |
| | |
| | |
| | |
| | |
| | |
| | |
| | |
| | |
| | |
| | |
| | |
| | |
| | |
| | |
| | |
| | |
| FROM | |
| | |
| | |
| | |
| WHERE | |
| | |
| | |
| | |
| | |
| | |
|
-- This is a query from Robert Lupton that finds selected neighbors in a given run and -- camera column. It contains a nested query containing a join, and a join with the -- results of the nested query to select only those neighbors from the list that meet -- certain criteria. The nested queries are required because the Neighbors table does -- not contain all the parameters for the neighbor objects. This query also contains -- examples of setting the output precision of columns with STR. SELECT FROM WHERE ORDER BY obj.run, obj.camCol, obj.field |
|
-- Find quasars with 2.5 < redshift < 2.7. This will use the spectro tables,with a simple -- multi-constraint WHERE clause. We introduce the use of a function, in this case -- dbo.fSpecClass, to select objects by named types instead of using the bitwise flags. | |
| SELECT specObjID, | -- get the spectroscopic object id |
| | -- redshift, redshift confidence |
| | -- and spectral classification |
| FROM SpecObj | -- from the spectroscopic objects |
| WHERE | |
| | |
| | |
| | |
| | |
| | |
| | |
| | |
| | |
|
-- Find all objects within 30 arcseconds of one another -- that have very similar colors: that is where the color ratios -- u-g, g-r, r-I are less than 0.05m. | |
| SELECT TOP 10 P.ObjID | -- distinct cases |
| FROM PhotoPrimary AS P | -- P is the primary object |
| | -- N is the neighbor link |
| | |
| -- L is the lens candidate of P | |
| WHERE | |
| | -- avoid duplicates |
| | -- L and P have similar spectra. |
| | |
| | |
| | |
| | |
|
-- Another useful query is to see if the errors on moving (or -- apparently moving) objects are correct. For example, it used to be that -- some known QSOs were being flagged as moving objects. One way to look for -- such objects is to compare the velocity to the error in velocity and see if -- the "OBJECT1_MOVED" or "OBJECT2_BAD_MOVING_FIT" is set. -- This query introduces bitwise logic for flags, and uses the 'as' syntax to -- make the query more readable. Note that if a flag is not set, the value -- will be zero. If you want to ensure multiple flags are not set, you can -- either check that each individually is zero, or their sum is zero. -- (From Gordon Richards) -- NOTE: This query takes a long time to run without the "TOP 1000". | |
| SELECT TOP 1000 | |
| | |
| | |
| | |
| | |
| | |
| | |
| | |
| | |
| | |
| | |
| | |
| FROM Galaxy | |
| WHERE | |
| | |
| | |
| | |
| | |
|
-- Find all galaxies with a deVaucouleours profile and the -- photometric colors consistent with an elliptical galaxy. NOTE THAT THE -- NAMES AND VALUES OF THE LIKELIHOODS HAVE CHANGED SINCE THE EDR; they are -- now log likelihoods, and named accordingly (lDev is now lnlDev, etc.) to -- indicate these are log likelihoods. This query has many conditions, and -- also has the use of bitwise logic necessary for dealing with flags. | |
| SELECT ObjID | |
| FROM Galaxy as G | |
| WHERE | |
| | |
| | |
| | |
| | |
| | |
| | |
| | |
| | |
| | |
| | |
| | |
| | |
| | |
| | |
| | |
| | |
| | |
| | |
| | |
| | |
| | |
| | |
| | |
| | |
| | |
| | |
| | |
| | |
|
-- Galaxies with bluer centers, by Michael Strauss. For all galaxies with r_Petro < 18, -- not saturated, not bright, and not edge, give me those with centers appreciably bluer -- than their outer parts, i.e., define the center color as: u_psf - g_psf and define -- the outer color as: u_model - g_model; give me all objs which have -- (u_model - g_model) - (u_psf - g_psf) < -0.4 -- -- Another flags-based query. -- NOTE: This query takes a long time to run without the "TOP 1000". | |
| SELECT TOP 1000 | |
| | |
| FROM Galaxy | |
| WHERE | |
| | |
| | |
| | |
| | |
| | |
| | |
|
-- Diameter-limited sample of galaxies from James Annis. -- Another query showing the use of flags, now using the bitwise '|' (or). | |
| SELECT | |
| | |
| | |
| | |
| | |
| | |
| | |
| | |
| FROM Galaxy | |
| WHERE ( flags & (dbo.fPhotoFlags('BINNED1') | |
| | |
| | |
| | |
| | |
| | |
| | |
| | |
| | |
| | |
| | |
| | |
| | |
| | |
| | |
|
-- Extremely red galaxies (from James Annis). -- Similar to the previous query. | |
| SELECT | |
| | |
| | |
| | |
| | |
| | |
| | |
| FROM Field f | |
| | |
| WHERE | |
| | |
| | |
| | |
| | |
| | |
| | |
| | |
| | |
| | |
| | |
| | |
|
-- A version of the LRG sample, by James Annis. -- Another query with many conditions and flag tests. | |
| SELECT | |
| | |
| | |
| | |
| | |
| | |
| | |
| | |
| FROM Galaxy | |
| WHERE ( ( flags & (dbo.fPhotoFlags('BINNED1') | |
| | |
| | |
| | |
| | |
| | |
| | |
| | |
| | |
| | |
| | |
| | |
| | |
| | |
| | |
| | |
| | |
| | |
| | |
| | |
| | |
| | |
| | |
| | |
| | |
| | |
| | |
|
-- The query below was originally written by Andy Connolly to find the brightness of -- the closest source within 0.5arcmin. It involves a 3-way join of the PhotoObjAll table -- with itself and the Neighbors table. This is a huge join because the PhotoObjAll table -- is the largest table in the DB, and the Neighbors table has over a billion entries -- (although it is a thin table). The two versions of the query shown below illustrate -- how we can speed up the query a lot by using the (much thinner) PhotoTag table -- instead of the PhotoObjAll table. See also the Optimizing Queries section of the -- SQL Intro page for more on using the PhotoTag table. The query also illustrates the -- LEFT JOIN construct and the use of nested joins. -- The first (original) version of the query uses the PhotoObjAll table twice in the 3-way -- join because we need some of the columns that are only in the PhotoObjall table. -- Since this version literally takes days to run on the entire DR2 database, a TOP 100 -- has been inserted into the SELECT to prevent the query from being submitted as is. SELECT TOP 100 o.ra,o.dec,o.flags, o.type,o.objid, FROM PhotoObjAll as o WHERE -- The second version of this query demonstrates the advantage of using the PhotoTag -- table over the PhotoObjAll table. One of the PhotoObjAll joins in the main 3-way -- join is replaced with PhotoTag, and the nested PhotoObjAll join with Neighbors is -- also replaced with PhotoTag. This version runs in about 2-3 hours on the DR2 DB. -- Note that when you replace PhotoObjAll or its views by PhotoTag, you have to also -- replace any references to the shorthand (simplified) magnitudes (u,g,r,i,z) and errors -- by their full names (modelMag_u and modelMagErr_u etc.). SELECT o.ra,o.dec,o.flags, o.type,o.objid, FROM PhotoObjAll as o WHERE | |
|
-- Two versions of a query to find galaxies with particular spectral lines. -- Version 1: Find galaxies with spectra that have an equivalent width in -- H_alpha > 40 Angstroms. We want object ID's from the photometry (Galaxy) -- but constraints from spectroscopy. The line widths and IDs are stored in -- SpecLine. This is a simple query, but now we are using three tables. The -- spectroscopy tables of measured lines are arranged non-intuitively, and we -- urge users to read about them on the DAS help pages. -- IMPORTANT NOTE: -- Each spectroscopic object now has a match to at least two photometric -- objects, one in Target and one in Best. Therefore, when performing a join -- between spectroscopic photometric objects, you must specify either -- PhotoObj.ObjID=SpecObj.bestObjID OR PhotoObj.ObjID = SpecObj.targetObjID. -- Normally, the default photometric database is BEST, so you will want to use -- SpecObj.bestObjID | |
| SELECT G.objId | -- we want the photometric objID |
| FROM Galaxy as G | |
| JOIN SpecObj as S ON G.objId=S.bestObjId | -- this galaxy has a spectrum, and |
| JOIN SpecLine as L ON S.specObjId=L.specObjId | -- line L is detected in spectrum |
| WHERE | |
| -- you could add a constraint that the spectral type is galaxy | |
| L.LineId = 6565 | -- and line L is the H alpha line |
| and L.ew > 40 | -- and is > 40 angstrom wide |
| -- Second version of this query finds galaxies with more specific spectra. -- This version also requires weak Hbeta line (Halpha/Hbeta > 20.) | |
| SELECT G.ObjID | -- return qualifying galaxies |
| FROM Galaxy as G | -- G is the galaxy |
| JOIN SpecObj as S ON G.ObjID = S.BestObjID | -- S is the spectra of galaxy G |
| JOIN SpecLine as L1 ON S.SpecObjID = L1.SpecObjID | -- L1 is a line of S |
| JOIN SpecLine as L2 ON S.SpecObjID = L2.SpecObjID | -- L2 is a second line of S |
| JOIN SpecLineNames as LN1 ON L1.LineId = LN1. value | -- the names of the lines (Halpha) |
| JOIN SpecLineNames as LN2 ON L2.LineId = LN2.value | -- the names of the lines (Hbeta) |
| WHERE LN1.name = 'Ha_6565' | -- L1 is H-alpha |
| | -- L2 is H-beta |
| | -- BIG Halpha |
| | -- significant Hbeta emission line |
| | -- Hbeta is comparatively small |
| | |
|
-- This query demonstrates the use of the photometry flags to select clean -- photometry for star and galaxy objects. Note that using these flag combinations -- may invoke the bookmark lookup bug if your query is searching a large fraction -- of the database. In that case, use the prescribed workaround for it as described on -- the SQL intro page. -- For queries on star objects, when you use PSF mags, use only PRIMARY objects -- and the flag combinations indicated below. If you use the Star view as below, you -- will get only primary objects, otherwise you will need to add a "mode=1" constraint. -- NOTE: The symbolic flag values are purposely replaced in the following examples by -- the hex values for the flag masks. This is for efficiency (see the Using dbo -- functions section of the SQL Intro page). -- For example, if you are interested in r-band magnitudes of objects, perform the -- following checks (add analogous checks with AND for other bands if you are -- interested in multiple magnitudes or colors): | |||
|
SELECT TOP 10 u,g,r,i,z,ra,dec, flags_r FROM Star WHERE -- For galaxies (i.e. not using PSF mags): Again use only PRIMARY objects. Other -- cuts are nearly the same, but remove the cut on EDGE. Possibly also remove -- the cut on INTERP flags. SELECT TOP 10 u,g,r,i,z,ra,dec, flags_r FROM Galaxy WHERE | |
|
-- Find binary stars with specific colors. -- At least one of them should have the colors of a white dwarf. | |
| SELECT TOP 100 s1.objID as s1, s2.objID as s2 | |
| FROM Star AS S1 | -- S1 is the white dwarf |
| JOIN Neighbors AS N ON S1.objID = N.objID | -- N is the precomputed neighbors lists |
| JOIN Star AS S2 ON S2.objID = N.NeighborObjID | -- S2 is the second star |
| WHERE | |
| | -- and S2 is a star |
| | -- the 3 arcsecond test |
| | -- and S1 meets Paul Szkodys color cut for |
| | -- white dwarfs. |
| | |
| | |
| | |
|
-- Find quasars with a broad absorption line and a nearby galaxy within 10arcsec. -- Return both the quasars and the galaxies. | |
| SELECT Q.BestObjID as Quasar_candidate_ID , | |
| | |
| FROM SpecObj as Q | -- Q is the specObj of the quasar candidate |
| JOIN Neighbors as N ON Q.BestObjID = N.ObjID | -- N is the Neighbors list of Q |
| JOIN Galaxy as G ON G.ObjID = N.NeighborObjID | -- G is the nearby galaxy |
| JOIN SpecClass as SC ON Q.SpecClass =SC.value | -- Spec Class |
| JOIN SpecLine as L ON Q.SpecObjID = L.SpecObjID | -- the broad line we are looking for |
| JOIN SpecLineNames as LN ON L.LineID = LN.value | -- Line Name |
| WHERE | |
| | -- Spectrum says "QSO" |
| | -- and isn't not identified |
| | -- but its a prominent absorption line |
| | -- and it is within 10 arcseconds of the Q. |
| | |
|
-- Find galaxies without saturated pixels within 1' of a given point (ra=185.0, dec=-0.5). -- This query uses a function fGetNearbyObjEq,which takes 3 arguments (ra,dec, -- distance in arcmin); this function uses the Neighbors table. The Neighbors and Galaxy -- tables have in common the objID, so we have to select objects from both where the -- objIDs are the same. The output of the function is a table with the Galaxy Object ID -- and distance in arcmin from the input. This query introduces the use of a JOIN to -- combine table contents. We also use the 'ORDER BY' syntax, which sorts the output. | ||||
| SELECT TOP 100 G.objID, GN.distance | ||||
| FROM Galaxy as G | ||||
| JOIN dbo.fGetNearbyObjEq(185.,-0.5, 1) AS GN -- this function outputs a table, so we have to do a join | ||||
| WHERE (G.flags & dbo.fPhotoFlags('saturated')) = 0 -- and the object is not saturated. f.PhotoFlags is a function that interprets the flag bits.
| ORDER BY distance -- sort these by distance
| | | |
|
-- Find all elliptical galaxies with spectra that have an anomalous emission line. -- This query introduces the SQL syntax DISTINCT, which will return only one instance -- of the requested parameter (ObjID, in this case), because our query may return the -- same object more than once. This is also the first nested query, where we use one -- SELECT (the inner one) to get a group of objects we are not interested in. The outer -- SELECT includes the new syntax 'not in', which is used to perform the exclusion. | |
| SELECT DISTINCT G.ObjID | |
| FROM | |
| JOIN Galaxy as G | |
| JOIN SpecObj as S ON G.ObjID = S.bestObjID | -- the galaxy has a spectrum |
| JOIN SpecLine as L ON S.SpecObjID = L.SpecObjID | -- L is a spectral line |
| JOIN XCRedshift as XC ON S.SpecObjID = XC.SpecObjID | -- cross-correlation redshift |
| WHERE | |
| | -- template used is "elliptical" |
| | -- any line type is found |
| | -- and the line is prominent by some |
| -- definition; in this case, equivalent | |
| -- width is over 10 Angstroms | |
| | -- insist that there are no other lines |
| | -- This is the chosen line. |
| | -- L1 is another line |
| | -- for this object |
| | -- at nearly the same wavelength |
| | -- but with unknown line type |
| | |
| | |
|
-- What's the SQL to get the broadest line of each spectrum, together with its -- identification (or more generally, all the columns for the spectral line with the -- highest/lowest something)? (Sebastian Jester) -- Note: The previous version of this query was corrected by Gordon Richards. | |
| SELECT top 100 | |
| | |
| | |
| | |
| -- get the spectroscopic object ID, the line ID, and the max width (in velocity) | |
| FROM SpecLine AS sl | |
| JOIN (SELECT | |
| | |
| | |
| | |
| | |
| | |
| | |
| | |
| | |
| | |
| | |
| | |
| | |
| | |
| | |
| | |
| | |
| | |
| | |
| | |
| | |
| | |
| | |
| | |
| | |
| | |
| | |
| | |
| WHERE | |
| | |
| -- Just as with the specObjID, each specLineID appears many times in specLine | |
| -- This final WHERE clause makes sure we get the one specLineID from SpecObj | |
| -- which matches the unique combination of specObjID and lineID in sMax. | |
| | |
|
-- Gridded galaxy counts and masks. Actually consists of TWO queries: -- 1) Create a gridded count of galaxies with u-g > 1 and r < 21.5 over -1 < dec < 1, -- and 179 < R.A. < 181, on a grid, and create a map of masks over the same grid. -- Scan the table for galaxies and group them in cells 2 arc-minuteson a side. Provide -- predicates for the color restrictions on u-g and r and to limit the search to the -- portion of the sky defined by the right ascension and declination conditions. Return -- the count of qualifying galaxies in each cell. -- 2) Run another query with the same grouping, but with a predicate to include only -- objects such as satellites, planets, and airplanes that obscure the cell. The second -- query returns a list of cell coordinates that serve as a mask for the first query. --- First find the gridded galaxy count (with the color cut) --- In local tangent plane, ra/cos(dec) is a linear degree. | |||||||||||||||||||||||||||
| SELECT cast((ra/cos(cast(dec*30 as int)/30.0))*30 as int)/30.0 as raCosDec, | |||||||||||||||||||||||||||
| | FROM Galaxy as G
| | | WHERE ra between 179 and 181
| | | | GROUP BY cast((ra/cos(cast(dec*30 as int)/30.0))*30 as int)/30.0,
| |
| -- now build mask grid. -- This is a separate query if no temp tables can be made SELECT cast((ra/cos(cast(dec*30 as int)/30.0))*30 as int)/30.0 as raCosDec,
| | | FROM PhotoObj as PO
| | | | WHERE
| | | | GROUP BY cast((ra/cos(cast(dec*30 as int)/30.0))*30 as int)/30.0,
| | | | ||
|
-- Create a count of galaxies for each of the HTM triangles. Galaxies should satisfy a -- certain color cut, like 0.7u-0.5g-0.2i<1.25 && r<21.75, output it in a form adequate -- for visualization. | |||||||||
| SELECT (htmID / power(2,24)) as htm_8 , | |||||||||
| | | | FROM Galaxy -- only look at galaxies
| WHERE (0.7*u - 0.5*g - 0.2*i) < 1.25 -- meeting this color cut
| | group by (htmID /power(2,24)) -- group into 8-deep HTM buckets.
| | | |
|
-- Find stars with multiple measurements with magnitude variations > 0.1. Note that -- this runs very slowly without the "TOP 100", so please see the Optimizing queries -- section of the SQL help page to learn how to speed up this query. | |
| SELECT TOP 100 | |
| | -- select object IDs of star and its pair |
| FROM Neighbors as N | -- the neighbor record |
| | -- the primary star |
| | -- the second observation of the star |
| | |
| | |
| WHERE | |
| | -- distance is 0.5 arc second or less |
| | -- observations are two different runs |
| | -- S2 is indeed a star |
| | -- S1 magnitudes are reasonable |
| | |
| | |
| | |
| | |
| | -- S2 magnitudes are reasonable. |
| | |
| | |
| | |
| | |
| | -- and one of the colors is different. |
| | |
| | |
| | |
| | |
| | |
| | |
| | |
|
-- Select white dwarf candidates, returning the necessary photometric parameters, -- proper motion, spectroscopic information, and the distance to the nearest neighbor -- brighter than g=21. (From Jeff Munn) | |
|
SELECT
FROM ( ) AS o LEFT OUTER JOIN ( ) AS nbor ON o.objID = nbor.objID |
|
-- Here is a query to get object IDs and field MJDs (Modified Julian Dates) for quasars with secondary -- matches. (From Jordan Raddick) SELECT top 100 FROM PhotoTag AS p WHERE ORDER BY p.modelmag_g |
|
-- Some more useful quasar queries (from Sebastian Jester). -- Getting magnitudes for spectroscopic quasars - retrieves BEST photometry. -- This query introduces the SpecPhoto view of the SpecPhotoAll table, which is a pre-computed join -- of the important fields in the SpecObjAll and PhotoObjAll tables. It is very convenient and much -- faster to use this when you can instead of doing the join yourself. SELECT ra,dec,psfmag_i-extinction_i AS mag_i,psfmag_r-extinction_r AS mag_r,z FROM SpecPhoto WHERE zconf > 0.35 -- Getting TARGET photometry for spectra SELECT sp.ra,sp.dec,sp.z, FROM specphoto AS sp INNER JOIN TARGDR2..photoprimary AS p ON sp.targetobjid = p.objid WHERE sp.zconf > 0.35 -- Getting FIRST data for spectroscopic quasars - returns only those quasars that match SELECT sp.ra,sp.dec,sp.z, FROM SpecPhoto AS sp WHERE sp.zconf > 0.35 -- Surface density of quasar targets and FIRST matches to them on a field-by-field basis -- restricted to some part of the sky. SELECT f.run,f.rerun,f.camcol,f.field,ra_avg,dec_avg, FROM ( ) AS f INNER JOIN ( ) AS p ON f.fieldid = p.fieldid ORDER BY ra_avg,dec_avg |
|
-- This query from Sebastian Jester demonstrates the use of the LEFT OUTER JOIN -- construct in order to include even rows that do not meet the JOIN condition. The -- query also gets the sky brighness and turns it into a flux, which illustrates the use of -- the POWER() function and CAST to change the string representation into floating -- point. The First table contains matches between SDSS and FIRST survey objects. select fld.run, fld.avg_sky_muJy, fld.runarea as area, isnull(fp.nfirstmatch,0) from ( --first part: for each run, get total area and average sky brightness ) as fld left outer join ( -- second part: for each run,get total number of FIRST matches. To get the run number -- for each FIRST match, need to join FIRST with PHOTOPRIMARY. Some runs may have -- 0 FIRST matches, so these runs will not appear in the result set of this subquery. -- But we want to keep all runs from the first query in the final result, hence -- we need a LEFT OUTER JOIN between the first and the second query. -- The LEFT OUTER JOIN returns all the rows from the first subquery and matches -- with the corresponding rows from the second query. Where the second query -- has no corresponding row, a NULL is returned. The ISNULL() function in the -- SELECT above converts this NULL into a 0 to say "0 FIRST matches in this run". ) as fp on fld.run=fp.run order by fld.run |
|
-- This query from Gordon Richards demonstrates the use of multiple OUTER JOINs -- It does take a few hours to run, hence the TOP 10 is added if you want to try it. SELECT TOP 10 FROM BESTDR3..SpecObjAll AS p WITH (index(0)) WHERE ( |
|
-- A query from Tomo Goto that looks for several spec lines at once. | |
| SELECT TOP 100 | |
| FROM SpecPhoto AS S | -- S is the spectra of galaxy G |
| -- L is a line of S | |
| -- L2 is a line of S | |
| -- L_OII is a line of S | |
| -- L_Hd is a line of S | |
| WHERE | |
| -- L is the H alpha line | |
| -- L2 is the H beta line | |
| -- L_OII is the O-II line | |
| -- L_Hd is the H delta line | |
|
-- A query from Jon Loveday to count galaxies in the North.
-- Galaxy number counts for northern Galactic hemisphere, ie. stripe < 50. -- -- 262158 is the sum of the SATURATED, BLENDED, BRIGHT and EDGE flags, -- obtained with the query: -- -- SELECT top 1 (dbo.fPhotoFlags('SATURATED') -- + dbo.fPhotoFlags('BLENDED') -- + dbo.fPhotoFlags('BRIGHT') -- + dbo.fPhotoFlags('EDGE')) from PhotoTag SELECT cast(2*(g.petroMag_r - g.extinction_r + 0.25) as int)/2.0 as r, 2*count(*) as N FROM galaxy g WHERE GROUP BY cast(2*(g.petroMag_r - g.extinction_r + 0.25) as int)/2.0 ORDER BY cast(2*(g.petroMag_r - g.extinction_r + 0.25) as int)/2.0 |
|
-- List the number of each type of object observed by each -- special program. SELECT plate.programname, dbo.fSpecClassN(specClass) AS Class, FROM SpecObjAll WHERE plate.programtype > 0 GROUP BY plate.programname, specClass ORDER BY plate.programname, specClass |
|
-- A query to list the primary and special plates that have objects in common -- Returns the pairs of special and primary plates, the total number of nights -- on which the objects they have in common have been observed, the progam to -- which the special plate belongs, and the number of objects the plates -- have in common. SELECT first.plate, other.plate, FROM SpecObjAll first WHERE first.scienceprimary = 1 AND other.scienceprimary = 0 GROUP BY first.plate, other.plate, otherPlate.programname ORDER BY nightsObserved DESC, otherPlate.programname, |
|
-- A query to list the spec IDs and classifications of the primary -- targets of a special program, in this case fstar51. -- -- Note that the flag may be different for other special programs SELECT specObjId, dbo.fSpecClassN(specClass) AS Class FROM SpecObjAll WHERE plate.programName LIKE 'fstar51' AND |
|
-- Find redshifts and types of all galaxies -- in the lowz special program with z < 0.01 SELECT specObjID, z, zErr, zConf, dbo.fSpecClassN(specClass) FROM SpecObjAll |