ANSI SQL Interview Questions Guide
ANSI SQL Interview Questions Guide
Aggregate functions in SQL, such as AVG(), COUNT(), MAX(), MIN(), and SUM(), are used to perform calculations on a set of values to return a single value. When used with the HAVING clause, they allow filtering of grouped results, unlike the WHERE clause which filters rows before grouping. An example is: SELECT dept_id, AVG(salary) AS avg_salary FROM Employees GROUP BY dept_id HAVING AVG(salary) > 45000;. This filters groups to show only departments with an average salary above 45000 .
To show employees who joined in the last 30 days, SQL uses date functions to calculate the date range. The query is: SELECT name, hire_date FROM Employees WHERE hire_date >= CURRENT_DATE - INTERVAL '30' DAY;. This query utilizes the 'CURRENT_DATE' function to get the current date and subtracts 30 days using 'INTERVAL', illustrating how date functions are critical for filtering data based on time criteria .
The SQL query to list employees who earn more than their department’s average salary is: SELECT name, dept_id, salary FROM Employees e WHERE salary > (SELECT AVG(salary) FROM Employees WHERE dept_id = e.dept_id);. This query utilizes a correlated subquery to compare each employee’s salary to the average salary within their specific department, requiring an understanding of subquery scoping .
A RIGHT JOIN in SQL retrieves all records from the right table and the matched records from the left table. If there is no match, NULL values are returned for columns from the left table. It might be used over other joins when the complete set of records from the right table is required regardless of matching conditions, highlighting its utility when joining tables with potentially missing references in the left table .
To retrieve the second highest salary of employees, the SQL query uses a nested SELECT statement. One way to perform this query is by selecting the maximum salary that is less than the maximum salary of the employees, which is achieved using the query: SELECT MAX(salary) FROM Employees WHERE salary < (SELECT MAX(salary) FROM Employees);. This query demonstrates the use of nested queries to rank aggregate data .
To find employees with no department assigned, the query looks for rows where the 'dept_id' field is NULL. This can be accomplished with the following SQL statement: SELECT name FROM Employees WHERE dept_id IS NULL;. This query leverages the understanding of NULL representation in SQL database systems to filter records that have an undefined or missing department id .
Retrieving the highest salary from each department involves grouping the data by department and then using the MAX() function. The SQL query is: SELECT dept_id, MAX(salary) AS highest_salary FROM Employees GROUP BY dept_id;. Here, 'GROUP BY' is used to organize data into dept_id subsets before aggregation functions are applied, which calculates the maximum salary per department .
Constraints in SQL are rules applied to table columns to ensure data integrity. During table creation, constraints such as PRIMARY KEY, UNIQUE, CHECK, and FOREIGN KEY provide mechanisms to guarantee that data adheres to specific rules. For example, the CHECK constraint ensures numerical data stays within specified bounds and the UNIQUE constraint avoids duplicate entries in a column. These constraints form the backbone of reliable data management by preventing invalid data entry and maintaining relational integrity across tables .
Correlated subqueries differ from simple subqueries as they reference columns from the outer query, which means they are evaluated row by row. In contrast, simple subqueries are standalone and do not rely on the outer query. For example, a correlated subquery is used to find employees with salaries higher than the average salary of their department: SELECT name FROM Employees e WHERE salary > (SELECT AVG(salary) FROM Employees WHERE dept_id = e.dept_id);. Here, the subquery depends on 'dept_id' from the outer query .
To identify duplicate values in a dataset, SQL can use the GROUP BY clause in conjunction with HAVING COUNT() > 1 to filter out groups with duplicate entries: SELECT salary, COUNT(*) FROM Employees GROUP BY salary HAVING COUNT(*) > 1;. Handling duplicates is important to ensure data accuracy and consistency, especially in analysis where duplicates might distort results. Techniques like deduplication, either programmatically or manually, are essential for maintaining data quality .