-- Extremely red galaxies. 
-- from James Annis.
-- Similar to the diameter limited sample query.

SELECT 
  g.run,
  g.camCol,
  g.rerun,
  g.field,
  g.objID,
  g.ra, g.dec
FROM Field f, Galaxy g
WHERE
  g.fieldID = f.fieldID
  and ( g.flags & (dbo.fPhotoFlags('BINNED1')
                   + dbo.fPhotoFlags('BINNED2')
                   + dbo.fPhotoFlags('BINNED4')) ) > 0
  and ( g.flags & (dbo.fPhotoFlags('BLENDED')
                   + dbo.fPhotoFlags('NODEBLEND')
                   + dbo.fPhotoFlags('CHILD')) ) != dbo.fPhotoFlags('BLENDED')
  and ( g.flags & (dbo.fPhotoFlags('COSMIC_RAY') 
                   + dbo.fPhotoFlags('INTERP')) ) = 0
  and f.psfWidth_r < 1.5
  and (g.i - g.z > 1.0 )     -- i and z in primary tables DO NOT include extinction
