laika222
10/31/2016 - 7:23 PM

When declaring a variable, you can plug in a value based on a calculation by nesting in a SELECT statement. In line 1, declares variable @av

When declaring a variable, you can plug in a value based on a calculation by nesting in a SELECT statement. In line 1, declares variable @avg2, which is set equal to the results of the SELECT statement showing the AVG of the Price column in the Orders table). Line 5 shows the alternate method which declares the variable and selects it at the same time using the := symbol.

SET @avg2 = (SELECT AVG(Price) FROM Orders);
SELECT @avg2;


SELECT @avg2 := (SELECT AVG(Price) FROM Orders);