We use cookies (including Google cookies) to personalize ads and analyze traffic. By continuing to use our site, you accept our Privacy Policy.

Select Data

Number: 3074

Difficulty: Easy

Paid? No

Companies: N/A


Problem Description

Given a table named students with columns student_id, name, and age, write a SQL query to select the name and age of the student whose student_id is 101.


Key Insights

  • The query only requires two columns: name and age.
  • A condition (WHERE clause) is used to filter records by student_id.
  • This is a straightforward SELECT query with a simple filtering condition.

Space and Time Complexity

Time Complexity: O(1) – The query retrieves a specific row using a filtering condition, assuming an indexed student_id. Space Complexity: O(1) – Only a fixed amount of data (two columns for one row) is returned.


Solution

The solution uses a basic SQL SELECT statement with a WHERE clause. The WHERE clause ensures that only the row with student_id = 101 is selected. This is the simplest and most direct solution to the problem. The data structure involved is the relational table itself, and the algorithmic approach is simply filtering based on a condition.


Code Solutions

# Define the SQL query to select name and age where student_id is 101
query = "SELECT name, age FROM students WHERE student_id = 101;"
# Print the query (in an actual application, you would execute this query on a database)
print(query)
← Back to All Questions