-- Get APOGEE_IDs and SDSS/BOSS plate, mjd, fiberid for all stars that have both APOGEE and SEGUE spectra:
-- A small number of objects have been observed both in the optical with
-- the SDSS and/or BOSS spectrographs and in the infrared with the APOGEE
-- spectrograph. The examples below finds all matches between primary
-- SDSS/BOSS spectra and APOGEE stars with a 3 arcsec tolerance for such
-- cases (note that there are some cases where an entry in one catalog
-- matches multiple entries in the other).

SELECT TOP 50
    specobj.plate as specobj_plate, specobj.mjd as specobj_mjd,
    specobj.fiberid as specobj_fiberid, 
    specobj.ra as specobj_ra, specobj.dec as specobj_dec, 
    star.apstar_id, star.ra as star_ra, star.dec as star_dec
FROM apogeeStar AS star 
    CROSS APPLY dbo.fGetNearestSpecObjEq( star.ra, star.dec, 0.05) AS near 
    JOIN specobj ON near.specobjid=specobj.specobjid 
