-- Broadest line of each spectrum with its ID.
-- (or more generally, all the columns for
-- the spectral line with the highest/lowest something)? The line width is
-- stored in the column sigma, the identification is in the column restwave.
-- (Sebastian Jester	)

SELECT 
  L.specObjId, 
  L.specLineID,  
  sMax.Maxwidth  -- get the spectroscopic object ID, the line ID, and the width

FROM specLine L, 
    (SELECT specObjID, 
     Max(waveMax-waveMin) as MaxWidth
  
-- Define MaxWidth as the largest difference in the min and max wavelength of
-- an identified line, using the built-in Max function of SQL

    FROM  SpecLine
    GROUP BY specObjID) as sMax 

-- sMax contains a single specObjID and MaxWidth. Remember that for each line
-- identified in a given object's spectrum, there is an entry in
-- SpecLine. This means that each specObjID appears many times in SpecLine,
-- once for each line found, and they must be aggregated before performing any
-- operation such as Max. The GROUP BY is thus NECESSARY.

-- The upper FROM clause is telling us we will use the table SpecLine and this
-- new output called sMax, which contains one entry with the SpecObjID and
-- MaxWidth. If we used some other function instead of 'Max', such as 'top
-- 10', sMax would have more than row.

WHERE 
    L.specObjID = sMax.specObjID   
    and (L.waveMax-L.waveMin) = sMax.MaxWidth

-- 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 MaxWidth in sMax.
