Advanced SQL and Database Interview Assessment

Sets 4–6: Window Functions, Query Optimization, Transactions and Real Interview Problems

This article continues the Backend Developer SQL series and focuses on the topics typically asked in 3–10 years experienced backend developer interviews.

Topics covered:

✅ Window Functions
✅ ROW_NUMBER, RANK, DENSE_RANK
✅ Common Table Expressions (CTE)
✅ Query Optimization
✅ Indexes
✅ Execution Plans
✅ Transactions
✅ Locking
✅ Isolation Levels
✅ Deadlocks
✅ Real SQL Interview Problems


Additional Sample Table

EMPLOYEE

EMP_IDNAMEDEPTSALARY
1JohnIT5000
2MaryHR7000
3SteveIT6000
4DavidHR7000
5TomIT8000

SET 4 – Window Functions


Q1. What does ROW_NUMBER() do?

A. Returns duplicate rows
B. Assigns unique sequence numbers
C. Counts rows
D. Groups rows


Q2.

SELECT NAME,
       ROW_NUMBER() OVER(
           ORDER BY SALARY DESC)
FROM EMPLOYEE;

What happens?

A. Rows numbered by salary order
B. Compilation error
C. Duplicate numbers possible
D. Groups employees


Q3. Which function gives same rank for ties?

A. ROW_NUMBER
B. RANK
C. COUNT
D. AVG


Q4.

Employees with salaries:

7000
7000
6000

Using RANK().

Result?

A. 1,2,3
B. 1,1,2
C. 1,1,3
D. 1,2,2


Q5.

Same data using DENSE_RANK().

A. 1,1,3
B. 1,2,3
C. 1,1,2
D. 1,2,2


Q6.

SELECT NAME,
       SALARY,
       RANK() OVER(
          ORDER BY SALARY DESC)
FROM EMPLOYEE;

Purpose?

A. Aggregate rows
B. Rank employees by salary
C. Remove duplicates
D. Sort only


Q7. Which clause is mandatory in window functions?

A. WHERE
B. OVER
C. GROUP BY
D. HAVING


Q8. Exception scenario.

What happens?

SELECT ROW_NUMBER()
FROM EMPLOYEE;

A. Works
B. Compilation error
C. Returns NULL
D. Runtime error


Q9. Which SQL feature allows temporary named result sets?

A. Cursor
B. View
C. CTE
D. Index


Q10. Which function skips ranks?

A. ROW_NUMBER
B. DENSE_RANK
C. RANK
D. COUNT


Answers – SET 4

QAnswerExplanation
1BROW_NUMBER assigns unique sequential numbers.
2AEmployees sorted and numbered.
3BRANK gives same rank to ties.
4CRanks 1,1,3 because rank 2 is skipped.
5CDENSE_RANK removes gaps.
6BRanks employees by salary.
7BOVER clause is mandatory.
8BOVER clause missing.
9CCTE creates temporary named results.
10CRANK skips numbers.

SET 5 – Transactions and Locking


Q1. Which ACID property prevents partial transactions?

A. Isolation
B. Consistency
C. Atomicity
D. Durability


Q2.

UPDATE ACCOUNT
SET BALANCE = BALANCE - 100;

COMMIT;

Purpose?

A. Undo changes
B. Save changes permanently
C. Lock rows
D. Release index


Q3.

UPDATE ACCOUNT
SET BALANCE = BALANCE - 100;

ROLLBACK;

Result?

A. Changes saved
B. Changes reverted
C. Table deleted
D. Lock remains


Q4. Which isolation level prevents dirty reads?

A. Read Uncommitted
B. Read Committed
C. Serializable
D. Snapshot


Q5. What is a deadlock?

A. Slow query
B. Two transactions waiting for each other
C. Full table scan
D. Index corruption


Q6. Which isolation level provides maximum consistency?

A. Read Uncommitted
B. Read Committed
C. Repeatable Read
D. Serializable


Q7.

Transaction A locks row 1.

Transaction B locks row 2.

A waits for row 2 and B waits for row 1.

This is:

A. Blocking
B. Deadlock
C. Timeout
D. Rollback


Q8. Which command permanently saves changes?

A. SAVEPOINT
B. COMMIT
C. ROLLBACK
D. BEGIN


Q9. Oracle automatically handles deadlocks by:

A. Restarting DB
B. Killing one transaction
C. Dropping tables
D. Rebuilding indexes


Q10. Which statement creates savepoints?

A. SAVEPOINT
B. COMMIT
C. LOCK
D. RELEASE


Answers – SET 5

QAnswerExplanation
1CAtomicity ensures all-or-nothing execution.
2BCOMMIT permanently saves changes.
3BROLLBACK undoes changes.
4BRead Committed prevents dirty reads.
5BTransactions wait for each other.
6DSerializable provides strongest isolation.
7BClassic deadlock scenario.
8BCOMMIT saves transaction.
9BOracle selects a victim transaction.
10ASAVEPOINT creates checkpoints.

SET 6 – Query Optimization and Indexes


Q1. What is an index?

A. Duplicate table
B. Search structure
C. Backup table
D. Temporary table


Q2. Which operation benefits most from indexes?

A. INSERT
B. UPDATE
C. DELETE
D. SELECT


Q3. Which index is automatically created?

A. Foreign Key
B. Check Constraint
C. Primary Key
D. View


Q4.

SELECT *
FROM EMPLOYEE
WHERE EMP_ID = 100;

Best index?

A. Salary index
B. EMP_ID index
C. Name index
D. Composite index


Q5. Full table scan means:

A. Index lookup
B. Entire table read
C. Locking table
D. Corrupted table


Q6. Which SQL statement displays execution plans in Oracle?

A. ANALYZE
B. EXPLAIN PLAN
C. SHOW PLAN
D. PLAN


Q7. Composite index contains:

A. Multiple rows
B. Multiple tables
C. Multiple columns
D. Multiple databases


Q8.

SELECT *
FROM EMPLOYEE
WHERE NAME LIKE '%JOHN%';

Index usage?

A. Always used
B. Usually not used efficiently
C. Mandatory
D. Compilation error


Q9. Which SQL clause is processed last?

A. FROM
B. WHERE
C. ORDER BY
D. GROUP BY


Q10. Which operation generally becomes slower after adding indexes?

A. SELECT
B. INSERT
C. Lookup
D. Search


Answers – SET 6

QAnswerExplanation
1BIndexes improve search performance.
2DSELECT benefits most.
3CPrimary keys create indexes automatically.
4BSearching by EMP_ID needs EMP_ID index.
5BEntire table must be scanned.
6BOracle uses EXPLAIN PLAN.
7CComposite indexes contain multiple columns.
8BLeading wildcard limits index usage.
9CORDER BY executes last.
10BIndexes increase INSERT overhead.

Real SQL Interview Problems

Problem 1: Second Highest Salary

SELECT MAX(SALARY)
FROM EMPLOYEE
WHERE SALARY <
(
    SELECT MAX(SALARY)
    FROM EMPLOYEE
);

Problem 2: Department Wise Highest Salary

SELECT DEPT,
       MAX(SALARY)
FROM EMPLOYEE
GROUP BY DEPT;

Problem 3: Duplicate Records

SELECT EMP_ID,
       COUNT(*)
FROM EMPLOYEE
GROUP BY EMP_ID
HAVING COUNT(*) > 1;

Problem 4: Employees Without Departments

SELECT E.NAME
FROM EMPLOYEE E
LEFT JOIN DEPARTMENT D
ON E.DEPT_ID = D.DEPT_ID
WHERE D.DEPT_ID IS NULL;

Advanced Backend Evaluation

ScoreAssessment
27–30Outstanding SQL Developer
24–26Strong Database Knowledge
20–23Good Intermediate Skills
15–19Average
Below 15Needs Improvement

Topics Covered

✅ Window Functions
✅ ROW_NUMBER
✅ RANK
✅ DENSE_RANK
✅ CTE
✅ Transactions
✅ Locking
✅ Isolation Levels
✅ Deadlocks
✅ Indexes
✅ Query Optimization
✅ Execution Plans
✅ Full Table Scan
✅ Oracle Concepts
✅ Real SQL Problems

Candidates scoring above 80% across all six sets generally possess strong backend database skills expected from experienced Java and Spring Boot developers.

Leave a Reply

Your email address will not be published. Required fields are marked *