G
Guest
Can someone help me out with SQL query syntax? I don’t know how to properly
construct a join when I’m using an aggregate function, in this case Min().
Here is the data:
TABLE vegetables
veg color
--- -----
Apple red
Apple green
Banana yellow
TABLE colors
color hex
----- ---
red FF0000
green 00FF00
yellow FFFF00
Here is the query that uses the aggregate function:
SELECT vegetables.veg, min(vegetables.color) as minColor FROM vegetables
GROUP BY vegetables.veg
Here is the result:
Veg minColor
--- ------
Apple green
Banana yellow
Great! Now give me a single SQL query, building upon the query above, that
uses joins and/or lookups to the colors table to give me this result:
Veg minColor hex
--- ------- ---
Apple green 00FF00
Banana yellow FFFF00
construct a join when I’m using an aggregate function, in this case Min().
Here is the data:
TABLE vegetables
veg color
--- -----
Apple red
Apple green
Banana yellow
TABLE colors
color hex
----- ---
red FF0000
green 00FF00
yellow FFFF00
Here is the query that uses the aggregate function:
SELECT vegetables.veg, min(vegetables.color) as minColor FROM vegetables
GROUP BY vegetables.veg
Here is the result:
Veg minColor
--- ------
Apple green
Banana yellow
Great! Now give me a single SQL query, building upon the query above, that
uses joins and/or lookups to the colors table to give me this result:
Veg minColor hex
--- ------- ---
Apple green 00FF00
Banana yellow FFFF00