Data Science SQL Interview Question Walkthrough (real interview style) | SQL Sundays #8

Tina Huang · Advanced ·📄 Research Papers Explained ·5y ago

Key Takeaways

This video demonstrates how to solve a data science SQL interview question, specifically finding the email activity percentile for each user, using window functions such as ROW_NUMBER and NTILE.

Full Transcript

hey guys this is sql sunday number eight let's jump into it all right so today's question is from google called activity percentile find the email activity percentile for each user email activity percentiles defined by the total number of emails set the user with the highest number of emails sent will have percent help one and so on i'll put the user total emails and their activity percentile and order records by the total emails in descending order okay let's take a look table so google gmail emails id from user to user as well as the day okay and the expected output here is going to look like user total emails and activity percentile okay let's take a look at the table here okay so it seems pretty standard this is pretty much what i expected so it's zero from user to user and we have the day over here so we want to get the user the number of emails that they sent and as well what percentile it is that they rank cool so let's look at the expected output okay so this is a little unexpected on my part um when they said activity percentile i assume that the end tile here would be you know what percentage or what percent how that person is in terms of their total emails but actually seems like it's just the ranking in terms of total emails um let's see here okay it looks like it's just total ranking and also the ranking so you have 1919 but ranked one two so it's you know ranked like where you want each number and just because they have the total number of emails is not ranked the same cool all right so it's assumptions up front uh i kind of stated it already it's the fact that if you have total number of emails then you would actually be ranked different ones as we can see over here um i don't think i don't i don't think i have any other assumptions to make um let's see yeah looks about right all right let us write our approach so we're gonna say here we're going to group by from user and then we're going to count number of emails so that would just be count and then after that we will get the rank so it says activity percentile here but we can actually get the row number window so we're going to use a little nifty little window function here all right so let's actually approach this question so from google gmail emails so i had our select statement from user i want to do the count oops count as total emails and for our function a window function so i'm just going to write this down i'll explain it a little bit as well what this means so row number over order by um count here and we want it in descending order and we'll call that and tiles okay okay so row number here actually means um we're ranking it we're ordering it by the count which is the total number of emails by descending because we want the total emails that are the highest to show up first and we're getting the row number of that so it's just ranking it from one all the way up to however many people that there are and because we want it so that everybody gets a different number despite the fact they have the same total number of emails that's why i want to use row number as opposed to rank functions all right cool and we have to group by one that should work so let's see uh shall we check it i think this looks right to me okay cool yep yep yep this all looks good to me cool looks like we got it right um so in terms of anything i can do to actually make it more efficient oh actually let's check out their solution first [Music] okay so they did a sub query from user count and then grouped it over here and they used n tile so n tile also works it's just another function um that you can use to rank it in this case so yeah you should get the same results using end tile both would work cool so it looks pretty much the same here all right yeah so in terms of efficiency um i think that my approach might be a little bit more efficient because i don't do a sub query but except for that i think it's pretty similar i also don't really see how we can improve it that much more as always let me know in the comments below what your approach is especially since we've never never seen a window function before so i would love to see how you guys think about what you guys think about that um and if you you also use the window function if you didn't use a window function i'm also really interested to see how you did it then okay so in terms of improvements to make um i don't in terms of like what we can do after this so since we're actually looking at the ranking i think it would actually be useful to go ahead and also find the actual percentiles of these people and grouping it together so what percentile each person is and what they belong to so last homework question for you guys why don't you also modify this function so that we can see the percentiles for each of these um users in terms of their total emails let me know what your solution would be in the comments below all right good luck everyone on their sequel data science interviews if you've been following the series all the way from number one to number eight here if you follow through and approach the questions like i suggested i am very confident that you'll do super super well on your actual interview alright see you guys in the next video

Original Description

Hi friends! Welcome back to SQL Sundays! A weekly SQL data science interview questions and answers walkthrough. This is another data science SQL interview question and answer done from beginning to end in a real interview style. 🔗Affiliates ======================== My SQL for data science interviews course (10 full interviews): https://365datascience.com/learn-sql-for-data-science-interviews/ 365 Data Science: https://365datascience.pxf.io/WD0za3 (link for 57% discount for their complete data science training) Check out StrataScratch for data science interview prep: https://stratascratch.com/?via=tina 📲Socials ======================== instagram: https://www.instagram.com/hellotinah/ linkedin: https://www.linkedin.com/in/tinaw-h/ discord: https://discord.gg/5mMAtprshX 🤯Study with Tina ======================== Study with Tina channel: https://www.youtube.com/channel/UCI8JpGrDmtggrryhml8kFGw How to make a studying scoreboard: https://www.youtube.com/watch?v=KAVw910mIrI Scoreboard website: scoreboardswithtina.com livestreaming google calendar: https://bit.ly/3wvPzHB 🎥Other videos you might be interested in ======================== SQL Sundays Playlist: https://www.youtube.com/playlist?list=PLVD3APpfd1tuXrXBWAntLx4tNaONro5dA How I learned SQL from Scratch in 11 Days to Pass my FANNG SQL Interview: https://www.youtube.com/watch?v=vaD3ZFFNwhM How I consistently study with a full-time job: https://www.youtube.com/watch?v=INymz5VwLmk How I would learn to code (if I could start over): https://www.youtube.com/watch?v=MHPGeQD8TvI&t=84s 🐈‍⬛🐈‍⬛About me ======================== Hi, my name is Tina and I'm a data scientist at a FAANG company. I was pre-med studying pharmacology at the University of Toronto until I finally accepted that I would make a terrible doctor. I didn't know what to do with myself so I worked for a year as a research assistant for a bioinformatics lab where I learned how to code and became interested in data science. I then di
Watch on YouTube ↗ (saves to browser)
Sign in to unlock AI tutor explanation · ⚡30

Playlist

Uploads from Tina Huang · Tina Huang · 24 of 60

1 How to choose between software engineering and data science | 5 Key Considerations
How to choose between software engineering and data science | 5 Key Considerations
Tina Huang
2 How I got Software Engineering and Data Science Internships | Computer Science Job Search Part 1
How I got Software Engineering and Data Science Internships | Computer Science Job Search Part 1
Tina Huang
3 How I Became a Data Scientist | Computer Science Job Search Part 2
How I Became a Data Scientist | Computer Science Job Search Part 2
Tina Huang
4 3rd Year Statistics,  Data Science, Computer Science Resume | Reviewing Your Resumes Ep. 1
3rd Year Statistics, Data Science, Computer Science Resume | Reviewing Your Resumes Ep. 1
Tina Huang
5 How to learn SQL for data science interview (the minimize effort maximize outcome way)
How to learn SQL for data science interview (the minimize effort maximize outcome way)
Tina Huang
6 3rd Year CS Resume (and asian drink) Review | Reviewing Your Resumes Ep. 2
3rd Year CS Resume (and asian drink) Review | Reviewing Your Resumes Ep. 2
Tina Huang
7 Are you a student? If yes, this is the best data science project for you!
Are you a student? If yes, this is the best data science project for you!
Tina Huang
8 The Ultimate Guide to Land a Data Science Internship | schedule and time management survival guide
The Ultimate Guide to Land a Data Science Internship | schedule and time management survival guide
Tina Huang
9 Upenn MCIT Program Details and Real Student Experiences - Dr. Arvind Bhusnurmath
Upenn MCIT Program Details and Real Student Experiences - Dr. Arvind Bhusnurmath
Tina Huang
10 Real Data Science SQL Interview Questions and Answers # 1 | Data Science Interview Questions
Real Data Science SQL Interview Questions and Answers # 1 | Data Science Interview Questions
Tina Huang
11 3 More Unique and Impactful Projects to get a Data Science Job
3 More Unique and Impactful Projects to get a Data Science Job
Tina Huang
12 Real Data Science SQL Interview Questions and Answers # 2 | Data Science Interview Questions
Real Data Science SQL Interview Questions and Answers # 2 | Data Science Interview Questions
Tina Huang
13 THANK YOU FOR 1000! | Proper intro | Random facts about myself
THANK YOU FOR 1000! | Proper intro | Random facts about myself
Tina Huang
14 A day in the life of a data scientist (FAANG data scientist remote)
A day in the life of a data scientist (FAANG data scientist remote)
Tina Huang
15 SQL Data Science Interview Questions and Answers (interview style walk-through) | Question 3
SQL Data Science Interview Questions and Answers (interview style walk-through) | Question 3
Tina Huang
16 Biology to Data Science (data professor's tips on how to get a data science research position)
Biology to Data Science (data professor's tips on how to get a data science research position)
Tina Huang
17 SQL Data Science Interview Questions and Answers (interview style walk-through) | SQL Sundays #4
SQL Data Science Interview Questions and Answers (interview style walk-through) | SQL Sundays #4
Tina Huang
18 Data Science SQL Interview Question Walkthrough | SQL Sundays #5
Data Science SQL Interview Question Walkthrough | SQL Sundays #5
Tina Huang
19 Data Science Resume Round-Up With @KenJee_ds  - Episode 2
Data Science Resume Round-Up With @KenJee_ds - Episode 2
Tina Huang
20 SQL Data Science Interview Question Walkthrough | SQL Sundays #6
SQL Data Science Interview Question Walkthrough | SQL Sundays #6
Tina Huang
21 Data Science vs Software Engineering Interview | 3 Key Differences
Data Science vs Software Engineering Interview | 3 Key Differences
Tina Huang
22 Data Science SQL Interview Question Walkthrough (real interview style) | SQL Sundays #7
Data Science SQL Interview Question Walkthrough (real interview style) | SQL Sundays #7
Tina Huang
23 The data science resume that got me my FAANG (MANGA?) job (entry level data scientist)
The data science resume that got me my FAANG (MANGA?) job (entry level data scientist)
Tina Huang
Data Science SQL Interview Question Walkthrough (real interview style) | SQL Sundays #8
Data Science SQL Interview Question Walkthrough (real interview style) | SQL Sundays #8
Tina Huang
25 Interview with a quant trader
Interview with a quant trader
Tina Huang
26 How I chose my masters degree (as an international student)
How I chose my masters degree (as an international student)
Tina Huang
27 The software engineering resume that got me into FAANG and Goldman Sachs (internship)
The software engineering resume that got me into FAANG and Goldman Sachs (internship)
Tina Huang
28 3 tips to avoid debt for a masters #SHORTS
3 tips to avoid debt for a masters #SHORTS
Tina Huang
29 A hard work day (ft. new NLP project) | FAANG data science isn't chill | vlog 1
A hard work day (ft. new NLP project) | FAANG data science isn't chill | vlog 1
Tina Huang
30 The comments sections are WILD | YouTube sentiment analysis - Data science project for beginners
The comments sections are WILD | YouTube sentiment analysis - Data science project for beginners
Tina Huang
31 Do you have what it takes to be a great data scientist?
Do you have what it takes to be a great data scientist?
Tina Huang
32 How to learn data science in 2022 (the minimize effort maximize outcome way)
How to learn data science in 2022 (the minimize effort maximize outcome way)
Tina Huang
33 A productive day as a data scientist | day in the life of a data scientist vlog #2
A productive day as a data scientist | day in the life of a data scientist vlog #2
Tina Huang
34 How to learn math for data science (the minimize effort maximize outcome way)
How to learn math for data science (the minimize effort maximize outcome way)
Tina Huang
35 Internship that made me rethink my career...(technology summer analyst at Goldman Sachs)
Internship that made me rethink my career...(technology summer analyst at Goldman Sachs)
Tina Huang
36 How to get a data science job
How to get a data science job
Tina Huang
37 cake and big sister advice 🖤
cake and big sister advice 🖤
Tina Huang
38 the most underrated data job in 2021
the most underrated data job in 2021
Tina Huang
39 My career changing computer science masters degree in 15 minutes (Upenn MCIT)
My career changing computer science masters degree in 15 minutes (Upenn MCIT)
Tina Huang
40 Data science interview tips (product and technical interviews)
Data science interview tips (product and technical interviews)
Tina Huang
41 Needed to learn javascript in 3 hours - would not recommend
Needed to learn javascript in 3 hours - would not recommend
Tina Huang
42 from management consultant to software engineer | Humans of MCIT
from management consultant to software engineer | Humans of MCIT
Tina Huang
43 Overview, Review and Study Tips - Google Data Analytics Professional Certificate
Overview, Review and Study Tips - Google Data Analytics Professional Certificate
Tina Huang
44 Overview, Review and Study Tips - Google Data Analytics Professional Certificate (condensed version)
Overview, Review and Study Tips - Google Data Analytics Professional Certificate (condensed version)
Tina Huang
45 Watch this video before applying to Georgia Tech OMSCS
Watch this video before applying to Georgia Tech OMSCS
Tina Huang
46 How to self study technical things
How to self study technical things
Tina Huang
47 What's the best certificate for data analysts? Google vs IBM Data Analyst Certificate
What's the best certificate for data analysts? Google vs IBM Data Analyst Certificate
Tina Huang
48 FAANG Data scientist reviews: Datacamp, Dataquest, 365 Data Science
FAANG Data scientist reviews: Datacamp, Dataquest, 365 Data Science
Tina Huang
49 How I would learn to code (if I could start over)
How I would learn to code (if I could start over)
Tina Huang
50 The quant trader interview guide
The quant trader interview guide
Tina Huang
51 We code a trading bot live! @jacobamaral
We code a trading bot live! @jacobamaral
Tina Huang
52 Why you should have a portfolio website
Why you should have a portfolio website
Tina Huang
53 60k cooking and Q&A (ft. Uncle Roger fried rice)
60k cooking and Q&A (ft. Uncle Roger fried rice)
Tina Huang
54 How to self study using MOOCS (Udemy, Coursera, and other online courses) | self study online
How to self study using MOOCS (Udemy, Coursera, and other online courses) | self study online
Tina Huang
55 Data Science SQL Interview Question Walkthrough | SQL Sundays #9
Data Science SQL Interview Question Walkthrough | SQL Sundays #9
Tina Huang
56 Watch me build my portfolio! | How to create a portfolio website
Watch me build my portfolio! | How to create a portfolio website
Tina Huang
57 How I stop myself from burning out
How I stop myself from burning out
Tina Huang
58 How I take notes - Tips for efficient note taking that speeds up learning
How I take notes - Tips for efficient note taking that speeds up learning
Tina Huang
59 How I design effective study plans for ANY SUBJECT (and stick with them) | trading, coding etc.
How I design effective study plans for ANY SUBJECT (and stick with them) | trading, coding etc.
Tina Huang
60 How I'm learning to trade (forex swing trading)
How I'm learning to trade (forex swing trading)
Tina Huang

This video teaches how to solve a data science SQL interview question using window functions to rank and percentile user email activity.

Key Takeaways
  1. Understand the problem and identify the required output
  2. Write a SQL query to group by user and count the number of emails
  3. Use a window function such as ROW_NUMBER to rank the users by email count
  4. Use the NTILE function as an alternative to ROW_NUMBER
  5. Optimize the query for efficiency
💡 Window functions such as ROW_NUMBER and NTILE can be used to rank and percentile data in SQL queries.

Related Reads

📰
First time ARR users - some questions [D]
Learn how to navigate the ARR review process for machine learning papers and understand reviewer feedback
Reddit r/MachineLearning
📰
Follow-up: The ArxivLens Protocol: Transforming Research Nois
Learn how to apply the ArxivLens Protocol to create dynamic grant-allocation pools that rebalance based on citation-impact signals, transforming research noise into actionable insights
Dev.to AI
📰
On July 1, 2026, arXiv will spin out from Cornell University, its home for the past 25 years, to become an independent nonprofit organization. Major funding support from Simons Foundation and Schmidt Sciences. Ditching the red for their website. [N]
arXiv is becoming an independent nonprofit organization after 25 years at Cornell University, backed by major funding, which will impact the future of research and academia
Reddit r/MachineLearning
📰
CS-NRRM™ Official Publications: Paper 1 and Paper 2 Are Now Available
Learn about the CS-NRRM's official publications on a 12-year longitudinal human observation archive and its significance in research and development
Medium · Data Science
Up next
UPTET July 2026 Paper Analysis/Review by Himanshi Singh. #himanshisingh #letslearn #uptet2026
Let's LEARN
Watch →