SQL Aggregation Interview Questions
3 questions with answers · SQL Interview Guide
GROUP BY, HAVING, and aggregate functions like COUNT, SUM, AVG, MIN, MAX. Interviewers test whether you can summarize and analyze data.
Explain the purpose of the GROUP BY clause.
GROUP BY collapses rows that share the same value in one or more columns into a single row, allowing you to apply aggregate functions like COUNT, SUM, AVG, MIN, and MAX to each group. Without GROUP BY, aggregate functions operate on the entire result set as one group. A classic use case is calculating total sales per region or counting users per signup date.
SELECT region, SUM(sales_amount) AS total_sales
FROM sales
GROUP BY region
ORDER BY total_sales DESC;What are aggregate and scalar functions
Aggregate functions operate on a set of rows and return a single summary value. Common examples are SUM(), COUNT(), AVG(), MIN(), and MAX(). They are typically used with GROUP BY to compute values per group. Scalar functions, on the other hand, operate on a single value and return a single value. Examples include UPPER(), LEN(), ROUND(), and GETDATE(). The distinction is that aggregate functions collapse multiple rows into one result, while scalar functions transform one input into one output on a row-by-row basis.
-- Aggregate: summarizes multiple rows
SELECT department_id, AVG(salary) AS avg_salary
FROM employees
GROUP BY department_id;
-- Scalar: transforms a single value per row
SELECT UPPER(first_name), ROUND(salary, 2) FROM employees;What does it mean to have QUOTED_IDENTIFIER ON? What are the implications of having it OFF
QUOTED_IDENTIFIER ON tells SQL Server to treat double-quoted strings as object identifiers, such as column or table names, rather than as string literals. With it ON, you must use single quotes for string literals, which is the ANSI standard behavior and required for creating or altering certain objects like computed columns and indexed views. With it OFF, double quotes behave like single quotes for string literals, which can break ANSI compliance and cause issues with any object that was created with QUOTED_IDENTIFIER ON. Most modern development keeps it ON by default, and SQL Server drivers like ODBC and OLE DB set it ON automatically.
Knowing the answers is half the battle
The other half is explaining them clearly under pressure.
Try a free mock interviewarrow_forward