max() — SQL Technical Interview Questions & Answers

max()

max()

  • MAX(marks) highest value. Topper query: ORDER BY marks DESC LIMIT 1 or WHERE marks = (SELECT MAX(marks) FROM students). The subquery form keeps ties.
  • LIMIT 1 hides ties. Say that in the interview.
  • Trap: MAX without mentioning ties.
  • On the example next to this theory: max() — aggregate functions collapse many rows into one summary row.

Technical Interview Tip: MAX(marks). Ties: WHERE marks = (SELECT MAX(marks) FROM students).

Code Examples for max()

max() — 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;

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

How to Explain max() to an Interviewer

All toppers, not one?

WHERE marks = (SELECT MAX(marks) FROM students)