47% of all Data Analyst roles require SQL.
This makes it the most in-demand technical skill in the market.
(Source: datanerd.tech)
If you’ve interviewed for a data analyst position that requires SQL, chances are you've had to face a technical interview. These interviews can feel intimidating, scary, and flat-out difficult at times.
Well, I’d like to help you prepare for this scary scenario.
To do so, I’m going to share with you my 4-step framework to answer any SQL interview question.
I call them the 4 C’s. Here they are:
Clarify
Confirm
Construct
Code
Let’s break down each on using an example.
Here’s the prompt:
“You've been tasked with identifying the job titles of the highest-paid employees. Your output should list the job title or titles with the highest salary, considering the possibility of multiple titles sharing the same salary.”
And here are our tables:
Now, let’s dig into each of the 4 steps.
1. Clarify
This is your opportunity to ask questions about the prompt.
The goal is to make sure you understand the data before diving into the prompt.
You could ask things like:
“What does this column or table mean?”
“Is the worker_id column the primary key of this table?”
The goal is to make sure you understand the data before diving into the prompt.
Once you’ve clarified that you understand all of the data provided in the prompt, you can move on to the next step.
2. Confirm
This step is reframing the question in your own words to confirm you understand it correctly.
Here’s an example:
Original question: “You've been tasked with identifying the job titles of the highest-paid employees. Your output should list the job title or titles with the highest salary, considering the possibility of multiple titles sharing the same salary.”
Reframed question: "It sounds like we need to identify the job title or titles associated with the highest salary in the company. This means if multiple job titles share the same highest salary, they should all be included in the output. Essentially, I'm looking for the maximum salary and then finding all job titles that match that salary. Does that sound correct?”
This shows you understand the question, but also makes sure you’re on the same page with the interviewer.
Another good thing to do is to provide an example output of the prompt.
Something like this:
You can type that out and ask, “Should my answer look something like this?” It’s another great way to make sure you’re on the right track.
3. Construct
Next you want to outline your approach and break down the problem into steps.
Confirm your approach with the interviewer before writing the actual query.
Here’s how you could do that for our example:
Step 1: Determine the maximum salary of all titles
Step 2: Filter to the job title or titles with that salary
Confirm by asking, “How does that sound?”
If you get the green light, then you can move on to the final step.
If they tell you that’s not it, revisit steps 1 and 2 to make sure you understand the prompt fully.
4. Code
Now it’s time for the fun part, writing the actual code.
Since we’ve outline our approach, we can finally write our query.
Tackle the steps you outlined earlier one at a time. Be sure to talk through what you’re doing as you write the code (to the best of your ability).
Example:
Step 1: “To get the titles of workers with the highest salaries, we need to use a join since the columns we need are in different tables.”
SELECT
t.worker_title AS best_paid_title
FROM title t
INNER JOIN worker w
ON
t.worker_ref_id = w.worker_id
Step 2: “Now we need to write a subquery to retrieve the maximum salary value, then incorporate it into a WHERE clause.”
SELECT
t.worker_title AS best_paid_title
FROM title t
INNER JOIN worker w
ON
t.worker_ref_id = w.worker_id
WHERE w.salary = (SELECT MAX(salary) FROM worker);
You also want to make sure you review the query for any mistakes before locking it in.
The answer, best paid titles, for this example would be Manager and Assistant Manager (which was the same as our example output above).
You could also potentially explain how the query could be improved. This shows that even though you got it correct, you’re someone who thinks through how they could optimize a query.
Recap
So to recap, we first clarify our understanding of the data.
Then we confirm our understanding of the prompt.
Next we construct by outlining our approach to answering the prompt.
And finally, we code to solve the problem.
Boom.
Practice solving SQL problems using this framework to get used to it. If you do it enough times, it becomes second nature.
A great place to do that is Dataford. This also happens to be where I got question for this example from.
Hope this was helpful! Let me know what you think in the comments.
Until next time ✌️
2 more ways I can help you
Book a 1:1 call with me: In 60-90 minutes, we’ll develop a personally tailored roadmap to help you land your next job in data and answer any questions you have. I’ll send you a detailed Notion doc following the call outlining your plan.
Hire me as your personal coach: 4 pre-outlined calls to supercharge your data job hunt, plus unlimited video call check-ins until you land your next role. Insane value in this offer. This is for serious job seekers only.
This took me back, I last used SQL back in Uni, 26 years ago! It seems like a lot has changed in computing since then but SQL doesn't really seem that different!
I actually had an interview where I had to write code by hand with a pen back then, feels like it was the stone age!