SQL Interview Questions Asked in Solverines Technology Solutions – Technical Round
Recently, I attended a technical interview at Solverines Technology Solutions Private Limited. During the SQL round, I was asked several interesting queries related to retrieving records and row numbering. I was also asked a logical programming problem involving bulb state toggling.
In this blog, I will explain the questions along with their solutions.
1. Write a Query to Access the Last Record from a Table
Assume we have an Employee table:
CREATE TABLE Employee(
emp_id INT,
emp_name VARCHAR(50)
);
To retrieve the last record:
SELECT *
FROM Employee
ORDER BY emp_id DESC
LIMIT 1;
Explanation:
- ORDER BY emp_id DESC sorts the records in descending order.
- LIMIT 1 returns only the first record after sorting, which is the last record in the table.
2. Access the First Nth Row and Last Nth Row in a Table
Nth Row from the Beginning
Example: Retrieve the 3rd row.
SELECT *
FROM Employee
ORDER BY emp_id
LIMIT 1 OFFSET 2;
Formula:
LIMIT 1 OFFSET (N - 1);
Nth Row from the End
Example: Retrieve the 3rd row from the end.
SELECT *
FROM Employee
ORDER BY emp_id DESC
LIMIT 1 OFFSET 2;
Formula:
ORDER BY emp_id DESC
LIMIT 1 OFFSET (N - 1);
3. Retrieve Only the Nth Even and Odd Row in a Table
For this problem, we use ROW_NUMBER().
Nth Odd Row
Example: Retrieve the 2nd odd row.
SELECT *
FROM (
SELECT *,
ROW_NUMBER() OVER(ORDER BY emp_id) AS rn
FROM Employee
) t
WHERE rn = (2 * 2 - 1);
General Formula:
rn = (2 * N - 1)
Nth Even Row
Example: Retrieve the 3rd even row.
SELECT *
FROM (
SELECT *,
ROW_NUMBER() OVER(ORDER BY emp_id) AS rn
FROM Employee
) t
WHERE rn = (2 * 3);
General Formula:
rn = (2 * N)
Retrieve All Odd Rows
SELECT *
FROM (
SELECT *,
ROW_NUMBER() OVER(ORDER BY emp_id) AS rn
FROM Employee
) t
WHERE rn % 2 = 1;
Retrieve All Even Rows
SELECT *
FROM (
SELECT *,
ROW_NUMBER() OVER(ORDER BY emp_id) AS rn
FROM Employee
) t
WHERE rn % 2 = 0;
Logical Programming Question
Bulb Toggle Problem
Problem Statement
There are N bulbs arranged in a row.
Initially, all bulbs are OFF (0).
Perform K operations.
In each operation, toggle all bulbs from index i to j (inclusive).
- 0 becomes 1
- 1 becomes 0
Finally, print the state of all bulbs.
Example
Input:
N = 5
K = 2
Operation 1: 1 3
Operation 2: 2 4
Initial State:
0 0 0 0 0
After Operation 1:
0 1 1 1 0
After Operation 2:
0 1 0 0 1
Final Output:
0 1 0 0 1
Java Solution
import java.util.Scanner;
public class BulbToggle {
public static void main(String[] args) {
Scanner sc = new Scanner(System.in);
int n = sc.nextInt();
int k = sc.nextInt();
int[] bulbs = new int[n];
for(int op = 0; op < k; op++) {
int i = sc.nextInt();
int j = sc.nextInt();
for(int index = i; index <= j; index++) {
bulbs[index] = 1 - bulbs[index];
}
}
for(int bulb : bulbs) {
System.out.print(bulb + " ");
}
}
}
Time Complexity
- SQL queries using ROW_NUMBER(): O(N)
- Bulb Toggle Problem: O(K × N) in the basic approach
Conclusion
The technical round focused on SQL fundamentals and logical problem-solving skills. The main SQL topics covered were:
- Retrieving the last record from a table.
- Finding the Nth row from the beginning and end.
- Retrieving Nth even and odd rows using ROW_NUMBER().
- Understanding row numbering concepts.
- Solving a bulb state toggling problem using arrays.
These are common interview questions for freshers and junior software developers. Practicing these concepts can significantly improve performance in technical interviews.
Top comments (0)