SQL Interview Questions
SQL Interview Questions
Basic Level
SQL Server is a relational database management system (RDBMS) developed by Microsoft. It stores and
manages data in a structured format using tables and provides tools for querying, updating, and managing
data.
Purpose:
o Example: SELECT
Example:
DeptName NVARCHAR(50)
);
EmpName NVARCHAR(50),
DeptID INT,
);
Types:
Types:
RIGHT JOIN: Returns all rows from the right table, and matching rows from the left.
FULL OUTER JOIN: Returns all rows when there is a match in either table.
Advantages:
Enhances reusability.
Example:
FROM Employee E
Example:
FROM Employees
GROUP BY Department
9. What is normalization?
Forms:
Types:
Intermediate Level
Advantages:
Faster execution.
Reusability.
Centralized logic.
Types:
Example:
WITH EmployeeCTE AS (
8. What is a subquery?
Types:
DENSE_RANK: No gaps.
10. What is a cursor?
Advanced Level
2. Partitioning
3. Database Replication
Types:
4. Transaction Log
6. SQL Profiler
Monitors performance.
7. Isolation Levels
Control concurrency.
9. Deadlocks
Reusable logic.
Here are interview questions specifically related to window functions for a Data Analyst role using SQL
Server, along with detailed explanations and examples.
Window functions perform calculations across a set of table rows related to the current row, but they do
not reduce the number of rows returned like aggregate functions.
Example:
FROM Employees;
The OVER clause defines the window (group of rows) to apply the function.
Row Count Returns the same number of rows as the input. Reduces rows based on the aggregation.
Usage Calculates results across a specific "window" of rows. Aggregates values across a group.
Example RANK(), ROW_NUMBER(), SUM() OVER(...). SUM(), AVG(), COUNT() (without OVER).
3. Explain the purpose of the OVER clause.
The OVER clause defines a window or a set of rows for the window function. It can include:
Example:
FROM Employees;
Function Purpose
RANK() Assigns ranks with gaps for ties. Ties: 1, 2, 2, 4 (gap after ties).
DENSE_RANK() Assigns ranks without gaps for ties. Ties: 1, 2, 2, 3 (no gap after ties).
Example:
FROM Employees;
Example:
FROM Employees;
The NTILE(n) function divides rows into n buckets, assigning each row a bucket number.
Example:
FROM Employees;
Function Description
Example:
SELECT EmpID, Salary,
FROM Employees;
Example:
SUM(Salary) OVER (ORDER BY Salary ROWS BETWEEN UNBOUNDED PRECEDING AND CURRENT ROW)
AS RunningTotal
FROM Employees;
ROWS BETWEEN UNBOUNDED PRECEDING AND CURRENT ROW: Includes all rows from the start to
the current row.
10. What is the difference between ROWS and RANGE in a window function?
Definition Refers to specific rows. Refers to rows with the same value in the ORDER BY clause.
Example:
-- Using ROWS
SUM(Salary) OVER (ORDER BY Salary ROWS BETWEEN 1 PRECEDING AND CURRENT ROW)
-- Using RANGE
SUM(Salary) OVER (ORDER BY Salary RANGE BETWEEN 1000 PRECEDING AND CURRENT ROW)
You can apply multiple window functions in a single query by using different OVER clauses.
Example:
SELECT EmpID, Salary,
FROM Employees;
Avoid Unnecessary Calculations: Only include the required columns in the SELECT statement.
Scenario-Based Questions
13. How would you calculate the top 3 salaries per department?
FROM (
FROM Employees
) AS Ranked
14. How would you find consecutive months with revenue growth using window functions?
FROM MonthlyRevenue
15. How would you calculate the percentage of total sales by product?
FROM Sales;
These questions cover both theoretical concepts and practical scenarios to test a candidate's knowledge
and application of window functions in SQL Server. Let me know if you'd like to explore any question
further!
Here are some commonly asked SQL Server interview questions related to joins, tailored for a Data
Analyst role, along with examples and detailed explanations.
A join in SQL combines rows from two or more tables based on a related column between them.
Type Description
LEFT JOIN (OUTER) Returns all rows from the left table, and matching rows from the right table.
RIGHT JOIN (OUTER) Returns all rows from the right table, and matching rows from the left table.
FULL OUTER JOIN Returns all rows when there is a match in either table.
CROSS JOIN Returns the Cartesian product of two tables (all combinations of rows).
Example:
FROM Employees E
ON E.DeptID = D.DeptID;
Returns employees and their department names where there is a match between
Employees.DeptID and Departments.DeptID.
Example:
FROM Employees E
ON E.DeptID = D.DeptID;
A RIGHT JOIN returns all rows from the right table and the matching rows from the left table. If no match is
found, NULL is returned for columns of the left table.
Example:
FROM Employees E
ON E.DeptID = D.DeptID;
A FULL OUTER JOIN returns all rows from both tables, with NULL for unmatched rows on either side.
Example:
FROM Employees E
ON E.DeptID = D.DeptID;
A CROSS JOIN returns the Cartesian product of two tables, meaning every row in the first table is combined
with every row in the second table.
Example:
SELECT E.EmpName, D.DeptName
FROM Employees E
Example:
FROM Employees E1
ON E1.ManagerID = E2.EmpID;
Returns employees and their managers by joining the Employees table to itself.
Matching Returns only matching Returns all rows from one or both tables, with NULL for non-
Rows rows. matching rows.
Excludes unmatched
Result Includes unmatched rows.
rows.
10. How do you join more than two tables in SQL Server?
Example:
FROM Employees E
Example:
FROM Employees E
ON E.DeptID = D.DeptID;
A non-equi join uses operators other than = (like <, >, <=, >=, !=).
Example:
FROM Employees E
JOIN SalaryGrades S
Example:
14. What is the difference between NATURAL JOIN and INNER JOIN?
NATURAL JOIN: Automatically matches columns with the same name and datatype.
Yes, joins can be used with aggregate functions like SUM(), AVG(), etc.
Example:
SELECT D.DeptName, COUNT(E.EmpID) AS EmployeeCount
FROM Departments D
ON D.DeptID = E.DeptID
GROUP BY D.DeptName;
Example:
EmpID INT,
ProjectID INT
);
SELECT E.EmpName, P.ProjectName
FROM Employees E
Duplicate rows: Occurs when joining tables with a one-to-many or many-to-many relationship
without handling duplicates.
How would you retrieve employees who do not belong to any department?
FROM Employees E
ON E.DeptID = D.DeptID
These questions test your understanding of joins, their types, and real-world applications as a Data Analyst.
Let me know if you'd like to dive deeper into any question!