How do you find the Second Highest Salary from an Employee table without using dialect-specific features like LIMIT or TOP?
This subquery approach is universal. The inner subquery finds the absolute maximum salary, and the outer query finds the maximum salary that is strictly less than that absolute maximum, which is logically the second highest.
Which query correctly finds the Nth Highest Salary using a generalized correlated subquery approach?
Both A and B are mathematically valid using correlated subqueries. Option B checks for exactly N-1 unique salaries greater than the current row's salary. If N-1 salaries are higher, the current row must be the N-th highest.
How do you select the Highest Salary Department-Wise along with the corresponding department identifier?
To calculate an extreme value (like a maximum) partitioned by a specific group field, you must combine the MAX() aggregate function with a matching GROUP BY clause.
How do you find Duplicate Records based on a specific column (e.g., Email) in a table?
Filtering based on the result of an aggregate function (like COUNT) cannot be done in a WHERE clause because it executes before rows are grouped. You must use a GROUP BY clause followed by a HAVING clause.
Which query correctly finds Employees without Managers in a standard employee table where ManagerID references the employee's boss?
In relational databases, the structural absence of a value or relationship is explicitly represented by NULL. Therefore, testing for the top-tier entity in a hierarchy requires checking WHERE ManagerID IS NULL.
How do you find the Top 3 Salary earners within each department using window functions?
Window functions cannot be evaluated directly inside a WHERE clause because filtering occurs logically before window partitions are computed. You must compute the rank inside a subquery or CTE first, then filter its output.
If you want to find the Bottom 3 Salary earners in a company using window functions, how should your window clause look?
Sorting by Salary ASC (Ascending) assigns the lowest values the top ranks (1, 2, 3...). This allows you to filter out the lowest earners by selecting ranks less than or equal to 3.
How can you dynamically Generate Numbers from 1 to 100 on-the-fly without pulling from an existing physical data table?
Both A and B are valid engineering approaches. A recursive CTE functions across almost all SQL dialects, whereas engines like PostgreSQL offer native utility set-returning functions like generate_series().
Which query correctly finds Employees earning more than the average salary in the company?
Aggregate functions like AVG() cannot be used directly in a WHERE clause. The average salary must first be computed using a subquery, and then each employee's salary is compared against that result.
How do you find Employees earning more than their respective Managers?
Since managers are stored in the same Employee table, this requires a self join. The employee is joined with their manager using ManagerID, and then their salaries are compared.
Which query correctly finds Customers who have never placed an Order?
A LEFT JOIN keeps every customer, even if no matching order exists. Customers without orders will have NULL values for the Order table columns, making IS NULL the correct filter.
How do you find the Second Highest Salary within each Department?
Partitioning by DepartmentID creates independent rankings inside each department. Filtering for rank = 2 returns the second highest salary in every department.
Which approach correctly removes Duplicate Rows while keeping only one copy?
ROW_NUMBER() assigns a sequence number to duplicate records. Keeping RowNum = 1 and deleting rows where RowNum > 1 safely removes duplicates.
How do you find the Department having the Highest Average Salary?
The average salary must first be calculated for each department using GROUP BY. Sorting the averages in descending order and selecting the top department returns the correct result.
Which query correctly counts Employees department-wise?
GROUP BY creates one group per department, and COUNT(*) counts the employees within each group.
How do you identify values that appear in three consecutive rows (for example, three consecutive logins by the same user)?
Consecutive-row problems depend on row order rather than grouped values. Window functions such as LAG() and LEAD() allow comparison with adjacent rows to detect consecutive occurrences.
Which SQL feature is commonly used to calculate a Running Total (Cumulative Sum)?
A window aggregate computes cumulative totals without collapsing rows. As each row is processed in order, the running total grows progressively.
How do you retrieve the Latest Order placed by each Customer?
ROW_NUMBER() ranks each customer's orders independently. Ordering by OrderDate DESC ensures the newest order receives rank 1, making it easy to retrieve the latest order for every customer.
Premium Content
Unlock SQL Coding Challenges and all premium lessons with a subscription.
From ₹199.99/year — See plans