-- Find galaxies that are blended with a star.
-- Output the deblended galaxy magnitudes. 
-- This query introduces the use of multiple tables or views. You can assign
-- nicknames to tables as in the FROM clause below. Since you are using
-- multiple tables, you ,ust specify which table each quantity in the SELECT
-- clause comes from. Finally, the last line in the query is the join between
-- the two tables, which is achieved by requiring that a quantity present in
-- both tables be equal.

-- NOTE: This query times out (takes too long) without the "TOP 1000".

SELECT top 1000 G.ObjID, G.u, G.g, G.r, G.i, G.z     --get the ObjID and final mags
into mydb.sampletest
FROM  Galaxy G, Star S           -- use two Views, Galaxy and Star, as a convenient mechanism to compare objects
WHERE G.parentID > 0             -- galaxy has a "parent", which tells us this object was deblended
  and G.parentID = S.parentID    -- and the star has the same parent
