Most Asked SQL Interview Question - Solved Using 3 Methods

preview_player
Показать описание
In this video, we will learn solving a most asked SQL interview question, which was asked in Google, Amazon and Uber interview. We will solve this question using 3 different methods.

If you liked this video, then please hit LIKE button and SUBSCRIBE this channel for more videos related to Data Science- skills and jobs

Timestamp:
00:00 Intro
00:17 SQL Interview Question
01:30 Method 1- SQL Query Solution
06:51 Method 2- SQL Query Solution
11:28 Method 3- SQL Query Solution
14:26 Outro

➖➖➖➖➖➖➖➖➖➖➖➖➖
Related Videos
Introduction to SQL - What Is SQL + Database | SQL Tutorial In Hindi 1

Data Types, Primary-Foreign Keys & Constraints in SQL | SQL Tutorial In Hindi 2

Create Table In SQL & Create Database | SQL Tutorial In Hindi 3

INSERT UPDATE, DELETE & ALTER Table in SQL With Example | SQL Tutorial in Hindi 4

SELECT Statement & WHERE Clause with Example | Operators in SQL | SQL Tutorial in Hindi 5

How To Import Excel File (CSV) to SQL | Import Data From File | SQL Tutorial in Hindi 6

String Functions in SQL | Functions in SQL | SQL Tutorial in Hindi 7

Aggregate Functions in SQL - COUNT, SUM, AVG, MAX, MIN | SQL Tutorial in Hindi 8

Group By and Having Clause in SQL | SQL Tutorial in Hindi 9

TimeStamp and Extract Function | Date Time Function | SQL Tutorial in Hindi 10

Complete SQL JOINS For Beginners | SQL JOIN Queries with Examples | SQL Tutorial in Hindi 11

SQL SELF JOIN | UNION & UNION ALL in SQL | SQL Tutorial in Hindi 12

Complete SQL Subquery Using Comparison and Logical Operators | SQL Tutorial in Hindi 13

Useful videos
How to Become Data Analyst in 2022 | Data Analyst Roadmap

Data Analyst skills, jobs and Roadmap

Excel Tutorial for Beginners

➖➖➖➖➖➖➖➖➖➖➖➖➖
Connect on other Social Media handles:

➖➖➖➖➖➖➖➖➖➖➖➖➖

If this is the first ever video you're watching allow me to introduce myself, Hey I'm Rishabh Mishra and currently working as Senior Data Analyst at Bangalore. On this channel I like to guide and help my juniors and data science aspirants regarding Data Science jobs and useful tips for college students.
Have a great day buddy!

#sqlinterviewquestions #sqlinterviewquestionsandanswers
Рекомендации по теме
Комментарии
Автор

create table travel (
source varchar(50),
destination varchar(50),
distance int);

insert into travel (source, destination, distance)
values
('Mumbai', 'Bangalore', 500),
('Bangalore', 'Mumbai', 500),
('Delhi', 'Mathura', 150),
( 'Mathura', 'Delhi', 150),
('Nagpur', 'Pune', 500),
( 'Pune', 'Nagpur', 500);

NidaHasan-hn
Автор

In the first approach without doing group by and all, we can just apply distinct after greatest and and least things

subhradipbarik
Автор

In method 1 using Greatest() and Least(), in our output (6:00) 3rd column we are getting Pune -> Nagpur but we are required to show(0:21) Nagpur -> Pune.

alenb
Автор

Mishra ji 🙏, you are wonderful teacher and it is reflected in your videos. 🙂 It is also evident from your prof. experience.

shivendusharma
Автор

We can also use greatest destination and apply distinct

JALAL-bs
Автор

Using greatest and least method

select greatest(source, destination), least(source, destination), max(distance)
from travel
group by greatest(source, destination), least(source, destination);

shraddhadhakad
Автор

Bhaiya direct bhi ho skta h na ye to
Select * from travel Where source>deatination;
Aise

NiteshKumar-yskq
Автор

with cte as(
select 1 as id, source, destination, distance
from travel
union all
select 2 as id, destination, source, distance
from travel)
select * from cte
where source>destination and id=1

ishika
Автор

Hi Rishabh, I was trying with multiple methods and i found if we use the query like
select *
from Travel
where source > destination;
the answer would be the same and this is the shortest method. though i m not sure if there is any discrepancy with this query, Please clarify or suggest.

floyedmoras
Автор

using cte :

with cte1 as
(select source, row_number() over(order by (select null)) as rn_s
from travel
)

select t1.source, t1.destination, t1.distance from travel as t1
join
(
select c1.source as source, c2.source as destination from cte1 c1
join cte1 c2
where c1.source <> c2.source and c1.rn_s < c2.rn_s
) as t2
on t1.source = t2.source and t1.destination = t2.destination ;

rahulnegi
Автор

Please find the code to create the mentioned table in this tutorial:

create table travel (
source varchar(50),
destination varchar(50),
distance varchar(50));

insert into travel (source, destination, distance)
values
('Mumbai', 'Bangalore', 500),
('Bangalore', 'Mumbai', 500),
('Delhi', 'Mathura', 150),
( 'Mathura', 'Delhi', 150),
('Nagpur', 'Pune', 500),
( 'Pune', 'Nagpur', 500);

govindshah
Автор

hi, love your teaching technique, please make a video on advance power bi interview Q&A. it will really appreciated.
thankyou

vinaygupta
Автор

I go with inner join. Method 2.

Method 3 slightly difficult.

hassamulhaq
Автор

Hello Rishabh, Thanks for creating such simple explanation for complex problems, Your approach was really easy to understand. I have seen the same question on other youtube channels but it was quite difficult for me to understand the same problem.

proud_indian
Автор

My personal two methods,
M1:
select distance, GROUP_CONCAT(source) from (select distinct * from table) as sub_query
GROUP BY distance; (:: "select distinct * from table" important if you have original duplicates cities pair)
M2:
select DISTINCT greatest(source, destination) as "from", least(source, destination) as "to", distance from table;

venuamrutham
Автор

please comeup with more interview questions (sql) that would be helpful. Thank you

pavanigoud
Автор

Sir, I enjoyed this session. Great explanation.
I learnt SQL from a course recommended by you in one of your videos. SQL Go from Zero to hero by Jose Portilla. Few concepts were not covered in that course, such as windows functions, greatest, least etc. Can you please tell where can I also learn such concepts?

ashwanikumarkaushik
Автор

Sir, u have given 3 method bt we can simply write it as follows -
Select * from travel
Where source IN ( Mumbai, mathura, nagpur);

Plz tell me if it is correct?

Shubhangisingh
Автор

here we can use mod function mod(id, 2)=0 actully we can solve many methods but sir you solved very easy way great

OmkarShinde-bzoy
Автор

Hello, could you please help on min vs least, while doing least are we need partion on table ?

SatyaMishra-sv