LeetCode Interview SQL Question with Detailed Explanation | Practice SQL | LeetCode 613

preview_player
Показать описание
Previous Video: LeetCode 610 Triangle Judgement

In this video I solve and explain a leetcode SQL question using MySQL query. This question has been asked in Apple, Facebook, Amazon, Google, Adobe, Microsoft, Adobe interviews or what we popularly call FAANG interviews.
I explain the related concept as well. This question is about finding the shortest distance between points and also includes points to keep in mind to develop SQL queries. You will also learn about CROSS JOIN and INNER JOIN and OPTIMIZING SQL queries.

LeetCode is the best platform to help you enhance your skills, expand your knowledge and prepare for technical interviews.

If you found this helpful, Like and Subscribe to the channel for more content.

#LeetCodeSQL #FAANG #SQLinterviewQuestions
Рекомендации по теме
Комментарии
Автор

which will be more costly in ascendin case, making a subquery in which each number has a row number and we join only x.rownumber+1=y.rownumber or x<y inner join

mickyman
Автор

select min(abs(p2.x-p1.x)) as shortest
from Point p1
inner join Point p2
where p1.x<p2.x

pankajchandel
Автор

It can be solved using Window Function too i guess
select MIN(abs( next_pos - x )) as Distance FROM
(select
x, Lead(x) over( order by x) as next_pos
from point
order by x) a

nikhilvyas
Автор

Today Interviewer asked me same question

abhishekasane
Автор

We can do it by window function . Or we can create a CTE with rank of the rows. Then join the rank with the next rank+ 1.

anuragkumawat
Автор

for ascending order if we just create a new row with lead() return the min(abs diff between them) it will work right

abhishekkesharwani
Автор

Cross join or windows_function?
which one has minimum run_time?

Just using an windows function here....

select min(distance)
from
(
select x,
lead (x) over (order by x) as next_pos,
lead (x) over (order by x) - next_pos as distance
from points
)x

meenayegan
Автор

As you told cross join doesn't require 'on' statement, does it goes same for inner join ..as i tried to use 'on' statement " on p1.x = p2.x" but it is giving null value as shortest value.

prnvsgr
Автор

select (ax-bx) as shortest
from
(select x as ax
from
distance d1) a
join
(select x as bx
from
distance d1) b
where (a.ax-b.bx)=1

imdeepu
Автор

Can you explain how it works for duplicate records

raulkishore
Автор

Can this also be done through lead and lag? If table is arranged in ascending order

yashwani
Автор

Its a Premium Q/A bro Help me to access it on leetcode

sukumar-mt
Автор

BUT sir, hum kuch aisa nhi kar sakte hai ki ....
Kyunki data ascending order main hai. hence humein to uske next row ke sath compare karna chasiye

AmanAshutoshBCS
visit shbcf.ru