# First Impressions

> Just the first three characters. That is all.

Canonical URL: <https://datadriven.io/problems/first_impressions>

Domain: SQL · Difficulty: easy · Seniority: L3

## Problem

Show each product's ID next to just the first three characters of its name, ordered by product ID.

## Worked solution and explanation

### What this really tests

Let's be honest: there is no algorithmic trap here. This is a one-line string extraction wearing a product-analytics costume, and anyone who knows `SUBSTR` can write it. What an interviewer actually clocks on a warm-up like this is whether you get the argument order right on the first pass and whether you remember that SQL string indexing starts at 1, not 0. Reach for a zero or a two as the start position and you hand back the wrong characters; drop `product_id` from the SELECT and the shape is wrong. On an easy question, a clean first attempt is the entire signal.

---

### Break it down

#### Step 1: Extract the first three characters

`SUBSTR(product_name, 1, 3)` takes three characters starting at position 1. The 1 is the part people fluff: SQL is 1-indexed, so 1 gives you the first character, not the second. Alias it to `name_prefix` so the output column matches what was asked.

#### Step 2: Order by `product_id`

`ORDER BY product_id` returns the rows in ascending ID order. It is the only ordering requirement, and forgetting it leaves the row order at the engine's discretion, which will not match the expected output.

---

### The solution

**Product ID with its three-character name prefix**

```sql
SELECT product_id, SUBSTR(product_name, 1, 3) AS name_prefix
FROM products
ORDER BY product_id
```

> **Where the cost goes**
>
> With 8,000 rows this is a full table scan followed by a sort on `product_id`, and the scan dominates: the per-row SUBSTR is cheap and touches only `product_name`. There is nothing to index for the extraction itself; the only cost worth a sentence at real scale is the sort, which `product_id` being the primary key already keeps in physical order.

> **What interviewers watch for**
>
> On a trivial question the tell is speed and precision: correct SUBSTR argument order, 1-based start index, `product_id` present in the SELECT, and the alias spelled exactly as requested. A candidate who writes it once, aliases cleanly, and moves on reads as senior; one who debugs off-by-one indexing on a warm-up raises a flag.

> **Common pitfall**
>
> The classic slip is the start index: writing `SUBSTR(product_name, 0, 3)` or `SUBSTR(product_name, 2, 3)` because you assumed zero-based indexing. The other is dropping `product_id` from the SELECT list or forgetting the `name_prefix` alias, so the output shape no longer matches. Neither is conceptually hard; both cost you the question if you do not re-read your own output.

---

## Common follow-up questions

- What does your query return for a product whose name is shorter than three characters, and how would you pad it to a fixed width if the business required exactly three? _(Tests whether the candidate knows SUBSTR degrades gracefully rather than erroring, and can reason about padding.)_
- How would you change the query to take the last three characters of the name instead of the first three? _(Tests flexibility with SUBSTR positioning, including negative or computed start positions.)_
- If product names followed a 'brand-model' convention, how would you extract just the segment after the hyphen? _(Tests combining string functions to locate and slice on a delimiter.)_

## Related

- [All practice problems](https://datadriven.io/problems)
- [Mock interview mode](https://datadriven.io/interview/first_impressions)
- [SQL Interview Questions](https://datadriven.io/sql-interview-questions)
- [Data Engineering Interview Prep Guide](https://datadriven.io/data-engineer-interview-prep)
- [Daily Challenge](https://datadriven.io/daily)

---

Source: DataDriven (https://datadriven.io). DataDriven is the data engineering interview community. Live code execution in SQL, Python, and Spark sandboxes. Every feature is open to every member.