AVG()() — SQL Technical Interview Questions & Answers

AVG()()

AVG()()

  • AVG(marks) = SUM/COUNT of non-NULL marks. Absentees (NULL) don’t pull the average down. If they should count as 0, AVG(IFNULL(marks,0)).
  • ROUND(AVG(marks), 2) for two decimals.
  • Trap: averaging in PHP after SELECT * of 1 lakh rows.
  • On the example next to this theory: AVG()() — aggregate functions collapse many rows into one summary row.

Technical Interview Tip: AVG(marks) ignores NULL. AVG(IFNULL(marks,0)) includes as 0.

Code Examples for AVG()()

AVG()() — sample query

CREATE TABLE scores (id INTEGER PRIMARY KEY, marks INTEGER);
INSERT INTO scores VALUES (1, 80), (2, 90), (3, 70);
SELECT COUNT(*) AS n, AVG(marks) AS avg_marks, MAX(marks) AS top
FROM scores;

AVG()() — aggregate functions collapse many rows into one summary row.

How to Explain AVG()() to an Interviewer

Does AVG count NULL as 0?

No Use IFNULL if you need that