Test your knowledge of advanced SQL concepts with this easy-level quiz. Ideal for developers preparing for SQL interviews, these questions cover subqueries, joins, group functions, aggregate operations, and more.
Which SQL clause is commonly used to find the maximum value of a column, such as the highest salary in each department?
Explanation: The MAX function returns the highest value in a column, such as the maximum salary in each department. MIN gives the lowest value, COUNT returns the total number of rows, and SUM adds up all the values. Only MAX directly determines the greatest amount.
If you want to calculate the average salary for each department in SQL, which aggregate function should you use?
Explanation: AVG calculates the average of a numeric column, perfect for finding average salaries. SUM totals values, COUNT counts rows, and STDEV computes standard deviation. Only AVG gives the arithmetic mean required here.
What SQL clause allows you to apply aggregate functions like SUM or AVG for groups of rows, such as by department?
Explanation: GROUP BY collects rows into groups for aggregate calculations like MAX or AVG per category. ORDER BY sorts results, WHERE filters them, and DISTINCT removes duplicates. The only clause enabling grouped aggregation is GROUP BY.
Which SQL statement calculates each employee's salary difference from their department's average?
Explanation: The first statement joins employee data to department averages and calculates the difference. The second lacks a join and doesn't compute a difference. The third uses SUM instead of AVG. The fourth only doubles salary, not calculating a difference from a group average.
When wanting to use the result of a group operation to filter rows in the outer query, which SQL technique is commonly employed?
Explanation: A subquery is used to calculate a value, like a department average, and join or compare it in the outer query. ORDER BY sorts but doesn't filter. UNION merges result sets. HAVING filters grouped results but is not always used for embedding aggregations as new columns.
What is the main purpose of using a JOIN clause in SQL queries involving employees and departments?
Explanation: JOIN combines data from tables using related columns, such as department_id. HAVING filters after grouping, DISTINCT removes duplicates, and aliases (not JOIN) rename columns. Only JOIN creates rows combining information from two tables.
How would you write a query to list employees who have the highest salary within their department?
Explanation: A subquery per department identifying the max salary and joining to employee data returns top earners. Simply filtering above a threshold doesn't ensure they're department leaders. Grouping by name isn't useful here, and counting doesn't help find salary leaders.
Which clause in SQL is used to filter groups after aggregation, unlike WHERE which filters rows before aggregation?
Explanation: HAVING applies conditions to groups after aggregation, such as filtering departments by average salary. WHERE filters rows before aggregation. ORDER BY sorts results, and DISTINCT removes duplicates; neither filters after aggregation.
What does SELECT * do in an SQL statement such as SELECT e.* FROM Employee e?
Explanation: SELECT * returns every column for the table or alias specified. It does not limit the selection to the name, remove duplicates, or result in zero columns. The asterisk is a wildcard meaning 'all columns.'
If you want to find the department_id of an employee, which SQL clause should you use to specify which employee you need?
Explanation: WHERE is used to filter rows and find specific entries, such as department_id for a given employee. ORDER BY is for sorting, GROUP BY is for aggregation, and DISTINCT removes duplicates, none of which help in directly filtering for one employee.
Which statement will return the highest salary for each department in a table called Employee?
Explanation: The correct query groups by department and selects the maximum salary in each group. The second only gives one overall maximum. The third lists salaries above zero, and the fourth is syntactically incorrect.
Why are table aliases like 'e' and 'd' used in SQL queries with joins?
Explanation: Aliases such as 'e' and 'd' help shorten queries and clarify references, especially with self-joins or complex statements. They do not sort, enforce uniqueness, or inherently improve speed.
How can you calculate a new column value like salary difference in the SELECT clause?
Explanation: You can calculate new column values directly in SELECT using expressions like (salary - avg_salary). GROUP BY organizes data but doesn’t calculate new columns by itself. Writing salary = avg_salary checks equality, not difference, and omitting aggregates ignores the group context.
What is the main use of aggregate functions like SUM, AVG, and MAX in SQL?
Explanation: Aggregate functions summarize data, such as totals or averages, from multiple rows. They do not filter, concatenate, or convert data types, but are central to grouped calculations.
Why might a join use more than one condition, such as 'ON e.department_id = d.department_id AND e.salary = d.max_salary'?
Explanation: Using multiple conditions in a join restricts rows to exact matches, like ensuring employees match both department and salary. Sorting, null removal, and table creation require different syntax, not join conditions.
What is the benefit of using a derived table (subquery in the FROM clause) in an SQL query?
Explanation: Derived tables let you reference subquery results like any table, making complex logic possible. They don't stop grouping, create permanent tables, or always remove duplicates unless you use DISTINCT.