← Amazon Interview Insights

Amazon·Data Scientist·Technical Phone Screen·Intermediate

Intermediate
Jul 2026

Summary

Amazon data scientist interview that was heavier on SQL internals than I expected. Most of the session was about cross-database compatibility and query semantics, the kind of stuff you think you know until someone asks you to explain it out loud.

Questions Asked (5)

Q1

A MySQL query using a column alias inside HAVING works fine, but the same query breaks on MS SQL Server. Why does this happen, and how would you rewrite it to work on both databases?

Technical Trade-offsData Modeling
Author's notes

This one stung a little because I actually use MySQL day to day and never thought about why the alias worked.

Create a free account to read the full note

AI HintsAI Generated

Suggested Approach

Explain that the difference stems from when each database resolves column aliases in the query execution order, with MySQL allowing aliases in HAVING and SQL Server not. Then propose rewriting the query to avoid aliases in HAVING by either repeating the expression or using a subquery/CTE.

Pro tip: Mention that using a subquery or CTE not only ensures portability but also improves readability and maintainability, which is crucial in collaborative data science environments.

1. Identify the root cause

Explain that SQL Server does not allow column aliases in the HAVING clause because aliases are resolved after HAVING, while MySQL allows it as an extension.

2. Demonstrate the issue

Provide a simple example query with an alias in HAVING that works in MySQL but fails in SQL Server.

3. Propose a portable rewrite

Show how to rewrite the query by repeating the aggregate expression in HAVING or by using a subquery/CTE to compute the alias first.

4. Discuss trade-offs

Compare the approaches: repeating the expression may be less readable, while subqueries/CTEs add complexity but improve portability and clarity.

5. Recommend best practice

Advise using subqueries or CTEs for complex queries to ensure cross-database compatibility and maintainability.

Key Points to Mention

  • SQL Server's logical query processing order: HAVING is evaluated before SELECT, so aliases are not available.
  • MySQL's extension allows aliases in HAVING, GROUP BY, and ORDER BY for convenience.
  • Portable rewrite: repeat the aggregate expression in HAVING.
  • Alternative rewrite: use a subquery or CTE to compute the alias and filter in an outer query.
  • Trade-offs: repeating expressions can lead to errors if not updated consistently; subqueries may impact performance but improve readability.
  • Best practice: avoid aliases in HAVING for cross-database compatibility.

AI-generated suggestions, not part of the candidate's original notes. May be inaccurate — verify before relying on them.

Q2

What does SELECT * FROM A B; do, and why is it valid SQL?

Technical Trade-offs
Author's notes

Blanked for a second.

Create a free account to read the full note

AI HintsAI Generated

Suggested Approach

First, clarify that the query selects all columns from table A and aliases it as B, making B a correlation name for A. Then explain that this is valid SQL because the alias is optional and does not change the query's semantics, and discuss why such syntax might appear in practice.

Pro tip: Mention that while the query is syntactically valid, it is semantically equivalent to SELECT * FROM A, and in a data science context, unnecessary aliases can reduce readability and may hint at auto-generated code or a misunderstanding of SQL aliasing.

1. Parse the query

Identify the components: SELECT * (all columns), FROM A (table A), and B (alias for A). Explain that B is a correlation name or alias for table A.

2. Explain the semantics

State that the query returns all rows and columns from table A, and the alias B does not affect the result set. It is equivalent to SELECT * FROM A.

3. Justify validity

Explain that SQL syntax allows an optional alias after a table name, even if the alias is not used elsewhere in the query. This is part of the SQL standard and supported by most databases.

4. Discuss practical implications

Mention that while valid, such aliases are often unnecessary and can be confusing. In a data science workflow, they might appear in generated code or when adapting queries for joins.

5. Connect to trade-offs

Highlight the trade-off between syntactic flexibility and code clarity. Emphasize that understanding such nuances helps in debugging and writing efficient SQL.

Key Points to Mention

  • Table alias (correlation name) syntax in SQL
  • Optional nature of aliases when not referenced
  • Semantic equivalence to SELECT * FROM A
  • SQL standard compliance and database support
  • Potential for confusion or reduced readability
  • Contexts where such queries arise (e.g., auto-generated code, join preparation)

AI-generated suggestions, not part of the candidate's original notes. May be inaccurate — verify before relying on them.

Q3

Among ORDER BY id, ORDER BY id ASC, and ORDER BY id DESC, which produces a different result from the other two and why?

Technical Trade-offs
Author's notes

Easy one.

Create a free account to read the full note

AI HintsAI Generated

Suggested Approach

Start by clarifying that ORDER BY id and ORDER BY id ASC are equivalent because ASC is the default sort order, so they produce identical results. Then explain that ORDER BY id DESC reverses the order, producing a different result. Emphasize that the difference is purely in the sort direction, not in the set of rows returned.

Pro tip: Mention that while the rows are the same, the order can affect downstream operations like LIMIT, window functions, or pagination, so understanding default sort direction is crucial in production queries.

1. Identify the default sort order

State that in SQL, ORDER BY id defaults to ascending order (ASC), so ORDER BY id and ORDER BY id ASC are functionally identical.

2. Compare with DESC

Explain that ORDER BY id DESC sorts in descending order, which reverses the sequence of rows compared to the other two.

3. Confirm the result difference

Conclude that ORDER BY id DESC produces a different result from the other two, which produce the same result.

4. Highlight practical implications

Discuss how this matters in real queries, such as when using LIMIT or when the order affects data processing.

Key Points to Mention

  • ASC is the default sort order in SQL, so it can be omitted.
  • ORDER BY id and ORDER BY id ASC return rows in ascending order of id.
  • ORDER BY id DESC returns rows in descending order of id.
  • The set of rows returned is identical; only the order differs.
  • The difference becomes significant when combined with LIMIT, OFFSET, or window functions.
  • Understanding default behavior helps write concise and correct SQL.

AI-generated suggestions, not part of the candidate's original notes. May be inaccurate — verify before relying on them.

Q4

Write the correct skeleton for a query that defines two CTEs and then selects from both of them.

Data Modeling
Author's notes

I wrote WITH A AS (...), B AS (...) SELECT ...

Create a free account to read the full note

AI HintsAI Generated

Suggested Approach

Start by writing the WITH keyword followed by two CTE definitions, each with a unique name and a SELECT statement in parentheses. Then write the main SELECT query that references both CTE names, joining or selecting from them as needed. Keep the syntax clean and ensure proper comma separation between CTEs.

Pro tip: Mention that CTEs improve readability and can be reused, but be aware that in some databases they may be materialized, affecting performance. At Amazon, where data volumes are huge, it's wise to consider whether a CTE or a subquery is more efficient.

1. Start with WITH clause

Begin the query with the WITH keyword to define common table expressions.

2. Define first CTE

Give the first CTE a descriptive name, followed by AS and a SELECT statement in parentheses.

3. Define second CTE

Add a comma after the first CTE's closing parenthesis, then define the second CTE similarly.

4. Write main SELECT

After the CTEs, write the main SELECT statement that references both CTE names, using JOIN or other operations.

5. Review syntax

Ensure proper commas, parentheses, and aliases; verify that the main query correctly uses both CTEs.

Key Points to Mention

  • WITH keyword introduces CTEs
  • Each CTE has a name and a SELECT statement
  • CTEs are separated by commas
  • Main query can reference CTEs like tables
  • CTEs can be used to simplify complex joins or aggregations
  • Consider performance implications of CTEs in large-scale systems

AI-generated suggestions, not part of the candidate's original notes. May be inaccurate — verify before relying on them.

Q5

For a column containing only uppercase letters, will these three predicates return identical rows: col IN ('A','B','C'), col BETWEEN 'A' AND 'C', and col >= 'A' AND col <= 'C'? Justify your answer.

Technical Trade-offsData Modeling
Author's notes

This is where I got tripped up.

Create a free account to read the full note

AI HintsAI Generated

Suggested Approach

First, clarify that the answer depends on the collation and character set of the column. Then, explain that under binary or case-sensitive collations with standard ASCII ordering, the three predicates are equivalent for uppercase letters, but under case-insensitive collations or with special characters, they may differ. Finally, emphasize the importance of testing with the actual database settings.

Pro tip: Mention that BETWEEN is inclusive on both ends, and IN is a set membership test, so they are logically equivalent only if the range is contiguous and the collation orders characters as expected. Also, note that performance can vary: IN might use a hash or index, while BETWEEN and range comparisons can use indexes efficiently.

1. Clarify assumptions

State that the answer depends on the collation and character set. Assume a case-sensitive collation with standard ASCII ordering for the initial analysis.

2. Analyze logical equivalence

Under the assumed collation, 'A' < 'B' < 'C', so BETWEEN 'A' AND 'C' includes A, B, C. IN ('A','B','C') also includes exactly those. The range condition col >= 'A' AND col <= 'C' is identical to BETWEEN. Thus all three return the same rows.

3. Consider edge cases

Discuss how case-insensitive collations (e.g., utf8_general_ci) might treat 'a' as equal to 'A', but since the column contains only uppercase letters, this doesn't affect the result. However, if the collation orders characters differently (e.g., 'A' > 'B'), the predicates could differ. Also, if there are characters between 'A' and 'C' that are not in the IN list, the results diverge.

4. Address performance implications

Mention that IN may be optimized as a set lookup, while BETWEEN and range comparisons can leverage indexes. In practice, all can use indexes, but the optimizer might treat them differently.

5. Conclude with recommendation

Recommend verifying with the specific database's collation and testing with EXPLAIN to understand performance. For correctness, ensure the collation matches expectations.

Key Points to Mention

  • Collation and character set determine ordering and equality.
  • BETWEEN is inclusive on both ends.
  • IN is a set membership test; equivalence holds if the set is exactly the contiguous range.
  • Case-insensitive collations can cause unexpected matches if data is not strictly uppercase.
  • Performance: IN vs BETWEEN vs range conditions may have different execution plans.
  • Always test with the actual database settings and data.

AI-generated suggestions, not part of the candidate's original notes. May be inaccurate — verify before relying on them.