Data Interview Qs - Tree structures in SQL

InterviewQs

Tree structures in SQL

SQL Database Querying Self-join

Forward this email to a friend or colleague and challenge them to solve it.

Hi, 

Suppose you have a table called 'tree' that contains a column of nodes as well as corresponding parent nodes:

Table: tree

node parent
1 2
2 5
3 5
4 3
5 null

Write a SQL query that will label each node as a 'root' (no parent node), 'leaf' (no child nodes), or 'inner' (contains both parent and child nodes). Given the table above, your query should return the following:

node label
2 Inner
5 Root
3 Inner
1 Leaf
4 Leaf

You can view/query this table in an interactive SQL fiddle here.



 

You’ve accumulated 317 questions since joining our email list. Create a premium account to access detailed solutions to each problem (including this one) + our new Case studies.


Was this email forwarded to you? You can join our list here to receive questions

How did you like today's question?

15411 Boischatel, Montreal, QC, H9H1Y7, Canada
Don't want to get these emails anymore? We'll forgive you if you unsubscribe

Older messages

Simulating a marketing campaign

Friday, July 8, 2022

Python, Data Structures, Arrays, Algorithms, Simulation, Data Visualization, InterviewQs Simulating a marketing campaign Python Data Structures Arrays Algorithms Simulation Data Visualization Forward

Run-length encoding

Wednesday, July 6, 2022

Python, Algorithms, Data Compression, InterviewQs Run-length encoding Python Algorithms Data Compression Forward this email to a friend or colleague and challenge them to solve it. Hi, Run-length

Changing the company's color

Monday, July 4, 2022

Statistics, Hypothesis Testing, InterviewQs Changing the company's color Statistics Hypothesis Testing Forward this email to a friend or colleague and challenge them to solve it. Hi, Suppose you

Employee salary increase with promotion

Friday, July 1, 2022

SQL, Database Querying, Window Function, Lead Function, InterviewQs Employee salary increase with promotion SQL Database Querying Window Function Lead Function Forward this email to a friend or

Simple trading simulation

Wednesday, June 29, 2022

Python, Data Structures, Arrays, Algorithms, Simulation, InterviewQs Simple trading simulation Python Data Structures Arrays Algorithms Simulation Forward this email to a friend or colleague and

You Might Also Like

JSK Daily for Apr 24, 2024

Wednesday, April 24, 2024

JSK Daily for Apr 24, 2024 View this email in your browser A community curated daily e-mail of JavaScript news JSK Weekly - 24th April, 2024 React 19 has introduced many great functionalities and

Daily Coding Problem: Problem #1422 [Hard]

Wednesday, April 24, 2024

Daily Coding Problem Good morning! Here's your coding interview problem for today. This problem was asked by Airbnb. Given a list of integers, write a function that returns the largest sum of non-

Charted | Artificial Intelligence Patents, by Country 🤖

Wednesday, April 24, 2024

This visualization shows which countries have been granted the most AI patents each year, from 2012 to 2022. View Online | Subscribe Presented by: New on VC+: Our Visual Briefing on the IMF's World

Save your seat: 1Password’s 2024 Security report insights webinar

Wednesday, April 24, 2024

Join us April 25th. ͏ ͏ ͏ ͏ ͏ ͏ ͏ ͏ ͏ ͏ ͏ ͏ ͏ ͏ ͏ ͏ ͏ ͏ ͏ ͏ ͏ ͏ ͏ ͏ ͏ ͏ ͏ ͏ ͏ ͏ ͏ ͏ ͏ ͏ ͏ ͏ ͏ ͏ ͏ ͏ ͏ ͏ ͏ ͏ ͏ ͏ ͏ ͏ ͏ ͏ ͏ ͏ ͏ ͏ ͏ ͏ ͏ ͏ ͏ ͏ ͏ ͏ ͏ ͏ ͏ ͏ ͏ ͏ ͏ ͏ ͏ ͏ ͏ ͏ ͏ ͏ ͏ ͏ ͏ ͏ ͏ ͏ ͏ ͏ ͏ ͏ ͏ ͏ ͏ ͏ ͏

Top Tech Deals 📱 LG Flex TV, Google Pixel 7, DJI Mini 3, and More

Wednesday, April 24, 2024

Get yourself a discounted DJI drone, save on the Pixel 7, or score some PC and phone accessories. How-To Geek Logo April 24, 2024 Top Tech Deals: LG Flex TV, Google Pixel 7, DJI Mini 3, and More Find

The Protest Song Wakes Up 🎙️

Wednesday, April 24, 2024

Is this song the future of musical protest? Here's a version for your browser. Hunting for the end of the long tail • April 24, 2024 The Protest Song Wakes Up A buzzy protest song about the

JSK Weekly - 24th April, 2024

Wednesday, April 24, 2024

React 19 has introduced many great functionalities and features, among which the useOptimistic hook stands out. The useOptimistic hook offers a seamless way to manage UI states during asynchronous

The clock’s ticking for TikTok

Wednesday, April 24, 2024

The US Senate has passed a bill that would ban TikTok if its US business is not divested by Bytedance View this email online in your browser By Alex Wilhelm Wednesday, April 24, 2024 Good morning, and

How to block Windows 11 Start menu ads

Wednesday, April 24, 2024

Oura Ring hits Target; 7 iPad Pro features we need; AI hallucinations aren't all bad -- ZDNET ZDNET Tech Today - US April 24, 2024 placeholder Microsoft is now showing ads in Windows 11's Start

The Chilling of TikTok

Wednesday, April 24, 2024

Ban or not, this is the end of TikTok as we know it The Chilling of TikTok By MG Siegler • 24 Apr 2024 View in browser View in browser The tok is tiking... Later today, President Biden will sign a bill