DEV Community

Cover image for SQL Interview Questions Asked in Solverines Technology Solutions – Technical Round
Arul .A
Arul .A

Posted on

SQL Interview Questions Asked in Solverines Technology Solutions – Technical Round

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)
);
Enter fullscreen mode Exit fullscreen mode

To retrieve the last record:

SELECT *
FROM Employee
ORDER BY emp_id DESC
LIMIT 1;
Enter fullscreen mode Exit fullscreen mode

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;
Enter fullscreen mode Exit fullscreen mode

Formula:

LIMIT 1 OFFSET (N - 1);
Enter fullscreen mode Exit fullscreen mode

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;
Enter fullscreen mode Exit fullscreen mode

Formula:

ORDER BY emp_id DESC
LIMIT 1 OFFSET (N - 1);
Enter fullscreen mode Exit fullscreen mode

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);
Enter fullscreen mode Exit fullscreen mode

General Formula:

rn = (2 * N - 1)
Enter fullscreen mode Exit fullscreen mode

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);
Enter fullscreen mode Exit fullscreen mode

General Formula:

rn = (2 * N)
Enter fullscreen mode Exit fullscreen mode

Retrieve All Odd Rows

SELECT *
FROM (
    SELECT *,
           ROW_NUMBER() OVER(ORDER BY emp_id) AS rn
    FROM Employee
) t
WHERE rn % 2 = 1;
Enter fullscreen mode Exit fullscreen mode

Retrieve All Even Rows

SELECT *
FROM (
    SELECT *,
           ROW_NUMBER() OVER(ORDER BY emp_id) AS rn
    FROM Employee
) t
WHERE rn % 2 = 0;
Enter fullscreen mode Exit fullscreen mode

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
Enter fullscreen mode Exit fullscreen mode

Initial State:

0 0 0 0 0
Enter fullscreen mode Exit fullscreen mode

After Operation 1:

0 1 1 1 0
Enter fullscreen mode Exit fullscreen mode

After Operation 2:

0 1 0 0 1
Enter fullscreen mode Exit fullscreen mode

Final Output:

0 1 0 0 1
Enter fullscreen mode Exit fullscreen mode

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 + " ");
        }
    }
}
Enter fullscreen mode Exit fullscreen mode

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:

  1. Retrieving the last record from a table.
  2. Finding the Nth row from the beginning and end.
  3. Retrieving Nth even and odd rows using ROW_NUMBER().
  4. Understanding row numbering concepts.
  5. 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)