SQL Problem Solving
SQL Problem Solving
Overview
SQL - Writing “any” query just won’t cut it. At Amazon, we need queries that are efficient and can scale for our
massive data volume. Ability to understand the data, grain, entity relationships, set operators and wisely deciding on
usage of aggregate functions, analytical functions or a combination of both is essential. Writing hierarchical queries
with insights to handle huge volumes of data and performance considerations is also preferred. This was a relatively
long list of topics to review and might seem somewhat overwhelming. Your interviewers won’t be evaluating your
ability to memorize all of the details about each of these topics. What they will be looking for is your ability to apply
what you know to solve problems efficiently and effectively.
Topics
● Partition key, Primary key, Foreign key
● Type 1 vs. Type 2 dimension/table
● UNION vs. UNION ALL
● Indexing
● View and Materialized View
● IN and Exists clause
● External table
● Having vs. Where clauses
● Inner, Left, Outer, Full join
● Column alias
● Condition filter: HAVING
● Hash join vs Nested join loop
Format
● You can expect some scenario-based questions on the above topics
● The interviewer may inquire about your previous experiences in projects
● The interviewer may ask you a few SQL Problem Solving questions where they give you an order table and
ask you to write a query to return xyz
● You may be given a JSON and be asked to extract data based on the JSON
● Given an order table, write the SQL queries for the desired output. EX. Find the maximum frequency of a
name in a group
Expectations
● Be able to write advanced SQL fluently and think about edge cases
● Be able to use SQL to manipulate and transform data comfortably
● Be familiar with ways of simplifying a complex query and optimizing performance
● Evaluate multiple approaches in this process and mostly select the optimized solutions in all scenarios
● Address outliers and anomalies in the data
● Ask clarification questions like the size of dataset, etc. so that you can design the appropriate, optimized
logic
● Assessing your speed and efficiency in solving coding problems, and your approach towards the problem
● Should be able to use SQL to manipulate and transform data comfortably
● Joins are written correctly (outer, inner, left, self)
● Good query structure and syntax
● Pivoting out in the expected format
● Case statements
● Correct string filters
● Various Windows functions
● Order and Rank function
● Aggregation (Group By, SUM, Count)
● DISTINCT
● Data Type conversion - Int to Float