W3. SQL Avg Flashcards
Q: What does the SQL AVG() function do?
A: It returns the average value of a numeric column.
Q: Write a query to find the average price in the “Price” column of the “Products” table.
SELECT AVG(Price) FROM Products;
Q: Do AVG() calculations include NULL values?
A: No, NULL values are ignored in AVG() calculations.
Q: What is the syntax for using AVG() with a condition?
SELECT AVG(column_name)
FROM table_name
WHERE condition;
Q: Write a query to find the average price of products in category 1.
SELECT AVG(Price) FROM Products WHERE CategoryID = 1;
Q: How can you give a descriptive name to the result column of AVG()?
A: Use the AS keyword to set an alias, e.g., AS [average price].
Q: Write a query to find the average price in the “Products” table and label it as “average price.”
SELECT AVG(Price) AS [average price] FROM Products;
Q: How do you list all records with a value higher than the average?
A: Use a subquery with AVG(), e.g., WHERE Price > (SELECT AVG(Price) FROM Products).
Q: Write a query to return all products with a price higher than the average price.
SELECT * FROM Products WHERE Price > (SELECT AVG(Price) FROM Products);
Q: How do you use AVG() with GROUP BY to find averages for each group?
A: Combine AVG() with GROUP BY, e.g., GROUP BY CategoryID.
Q: Write a query to find the average price for each category in the “Products” table.
SELECT AVG(Price) AS AveragePrice, CategoryID FROM Products GROUP BY CategoryID;