Home › Interview Questions › Is there also an aggregate function you can use? F…

Is there also an aggregate function you can use? For example, if L is 1900 and Dave is 90,000, how would you handle salary ordering?

🟡 Medium Conceptual Mid level
1Times asked
Oct 2026Last seen
Oct 2026First seen

💡 Model Answer

Aggregate functions such as SUM, AVG, MAX, MIN, and COUNT operate on a set of rows and return a single value. They are often used to compute summary statistics for reporting or data quality checks. When you need to combine aggregates with ordering, you can use window functions or subqueries. For example, to find the maximum salary per department and also list each employee with that maximum salary, you could write: SELECT dept, employee, salary FROM employees e JOIN (SELECT dept, MAX(salary) AS max_sal FROM employees GROUP BY dept) m ON e.dept = m.dept AND e.salary = m.max_sal; This joins the aggregate result back to the original table. Alternatively, you can use a window function: SELECT dept, employee, salary, MAX(salary) OVER (PARTITION BY dept) AS max_sal FROM employees; Then filter where salary = max_sal. Ordering can be applied in the SELECT clause with ORDER BY salary DESC to list employees from highest to lowest. Combining aggregates and ordering allows you to compute summary values while preserving row-level detail, which is useful for dashboards, trend analysis, and anomaly detection. Performance considerations include ensuring indexes on the columns used in GROUP BY, JOIN, and ORDER BY clauses, and avoiding unnecessary subqueries that can lead to full table scans.

This answer was generated by AI for study purposes. Use it as a starting point — personalize it with your own experience.

🎤 Get questions like this answered in real-time

Assisting AI listens to your interview, captures questions live, and gives you instant AI-powered answers on a discreet on-screen overlay.

Get Assisting AI — Starts at ₹500