DataFrame: employees
+-------------+--------+
| Column Name | Type |
+-------------+--------+
| employee_id | int |
| name | object |
| department | object |
| salary | int |
+-------------+--------+
Write a solution to display the first 3 rows of this DataFrame.
Example 1:
Input: DataFrame employees +-------------+-----------+-----------------------+--------+ | employee_id | name | department | salary | +-------------+-----------+-----------------------+--------+ | 3 | Bob | Operations | 48675 | | 90 | Alice | Sales | 11096 | | 9 | Tatiana | Engineering | 33805 | | 60 | Annabelle | InformationTechnology | 37678 | | 49 | Jonathan | HumanResources | 23793 | | 43 | Khaled | Administration | 40454 | +-------------+-----------+-----------------------+--------+ Output: +-------------+---------+-------------+--------+ | employee_id | name | department | salary | +-------------+---------+-------------+--------+ | 3 | Bob | Operations | 48675 | | 90 | Alice | Sales | 11096 | | 9 | Tatiana | Engineering | 33805 | +-------------+---------+-------------+--------+ Explanation: Only the first 3 rows are displayed.
The goal of #2879 Display the First Three Rows is to return only the first three records from a table. Since databases often contain large datasets, SQL provides built-in clauses that allow us to limit the number of rows returned by a query.
The most common approach is to use row-limiting keywords such as LIMIT, TOP, or FETCH FIRST, depending on the SQL dialect. These clauses instruct the database engine to stop returning rows once the specified count is reached. For example, using LIMIT 3 ensures that only the first three rows from the result set are retrieved.
This approach is efficient because the database does not need to process or return the entire dataset—only the required rows. In most implementations, the database engine stops scanning once the requested number of rows is collected, making it highly performant for simple retrieval tasks.
| Approach | Time Complexity | Space Complexity |
|---|---|---|
| Row limiting using LIMIT / TOP / FETCH FIRST | O(1) for returned rows (database stops after first three) | O(1) |
You Data And AI
Use these hints if you're stuck. Try solving on your own first.
Consider using a built-in function in pandas library to retrieve the initial rows.
Most data manipulation libraries provide a direct method to access the top few rows of a DataFrame. This approach utilizes the built-in method to fetch the first three rows efficiently.
Time Complexity: O(1), since it directly accesses the specified rows.
Space Complexity: O(1), no additional space usage beyond the output.
1function getFirstThreeRows(employees) {
2 return employees.slice(0, 3);
3}In JavaScript, if you have a 2D array representing the DataFrame, the slice method helps to extract the first three elements efficiently.
This approach involves manually iterating over the DataFrame and extracting the first three rows. It's useful in languages or environments where built-in functionality might not exist.
Time Complexity: O(1), since we only loop through a fixed number of rows.
Space Complexity: O(1), as we only store a constant number of rows.
1#include <iostream>
2#include <vector>
3#include <string>
4
5using namespace std;
6
typedef vector<vector<string>> DataFrame;
DataFrame getFirstThreeRows(const DataFrame &employees) {
DataFrame result;
for (int i = 0; i < 3 && i < employees.size(); ++i) {
result.push_back(employees[i]);
}
return result;
}Watch expert explanations and walkthroughs
Jot down your thoughts, approach, and key learnings
Direct SQL questions like returning the first N rows are common in technical interviews, especially for data engineering and analytics roles. They test basic SQL knowledge and familiarity with query result limiting.
The optimal approach is to use a SQL row-limiting clause such as LIMIT, TOP, or FETCH FIRST depending on the database system. These clauses restrict the output to the first three rows without processing the entire dataset.
The most common clause is LIMIT 3 in databases like MySQL and PostgreSQL. In SQL Server you can use TOP 3, while Oracle and some ANSI-compliant systems use FETCH FIRST 3 ROWS ONLY.
From a user perspective, SQL queries operate on tables, which are relational structures managed by the database engine. Internally, the database may use indexes or sequential scans to retrieve rows efficiently before applying the row limit.
This C++ solution manually iterates through the rows of a DataFrame using a for loop. We ensure to only access rows as long as they exist, preventing out-of-bound errors.