Most Asked SQL Interview Question - Solved Using 3 Methods

Most Asked SQL Interview Question - Solved Using 3 Methods

Rishabh Mishra

1 год назад

67,269 Просмотров

Ссылки и html тэги не поддерживаются


Комментарии:

Xavier
Xavier - 16.11.2023 17:04

platform to practise advance sql??

Ответить
Vinay Gupta
Vinay Gupta - 03.11.2023 18:55

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

Ответить
pavanigoud 1998
pavanigoud 1998 - 02.11.2023 08:17

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

Ответить
Spoken English World
Spoken English World - 25.10.2023 06:56

Sir, Could u pls tell me in Sub query what is the use of NOT EXISTS Function.....what will this not exists function suppose to do here?pls help

Ответить
Gudia Toka
Gudia Toka - 21.10.2023 11:35

select t1.* from dbo.tdistance t1
inner join dbo.tdistance t2
on t1.source=t2.destination and t1.destination=t2.source and t1.destination>t2.destination

Ответить
Cctns Helpdesk
Cctns Helpdesk - 19.10.2023 14:27

using sub query ,, inside query is giving the same results, then why we need to use outer not exists

Ответить
SAYALI DIGE
SAYALI DIGE - 03.10.2023 19:26

Hi sir, i am not getting the exact output

Ответить
putul saini
putul saini - 19.09.2023 19:42

Sir I am using 1st and 2nd method in Microsoft SQL server and it is showing error. In 2nd method it is saying 'the row_number must have an over clause with order by'. And in 1st method it is showing ' greatest ' is not a recognised built-in function name. Please help me

Ответить
Haidy
Haidy - 15.09.2023 14:06

I came up with a very simple one 😹:

SELECT *
FROM travel
WHERE source > destination
(Though it won't work if we change the question a little by adding a single entry with no reversed entry, Method 2 will also fail in that situation.)
------------------------------------

We can use DISTINCT in Method 1:
SELECT DISTINCT GREATEST(source, destination), LEAST(source, destination), distance FROM travel

Method 3 is nice and creative, took me a couple of minutes to understand.

Learning a lot from you, keep doing it bro. ❤❤❤

Ответить
Saurav Labade
Saurav Labade - 17.08.2023 11:03

If you give the link of the tables in description which you used to solved the problems in this video then it will be better rishabh

Ответить
Venu Amrutham
Venu Amrutham - 08.08.2023 11:58

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;

Ответить
SUBHRADIP BARIK
SUBHRADIP BARIK - 05.08.2023 15:06

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

Ответить
Rahul Singh
Rahul Singh - 03.08.2023 12:17

I m failed to do also understand

Ответить
Nitesh Kumar
Nitesh Kumar - 28.07.2023 21:29

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

Ответить
Shubhangi Singh
Shubhangi Singh - 27.07.2023 12:52

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?

Ответить
Hassam Ul Haq
Hassam Ul Haq - 21.07.2023 07:15

I go with inner join. Method 2.

Method 3 slightly difficult.

Ответить
RAHUL NEGI
RAHUL NEGI - 19.07.2023 10:51

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 ;

Ответить
Arpit Gupta
Arpit Gupta - 11.07.2023 19:21

sirji third method mein agar dono city same alphabet se start hogi to appka favorite method fail kr jayega (iss table mein it worked) lol!

Ответить
Shardha Dhaked
Shardha Dhaked - 08.07.2023 10:57

Using greatest and least method

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

Ответить
Omkar Shinde
Omkar Shinde - 08.07.2023 08:09

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

Ответить
Lalit Kumar
Lalit Kumar - 05.07.2023 22:35

Can't understand why the max function is used with distance? We are not using distance in the group by then why is it throwing in an error to add an aggregate function. Is it necessary to add all the called variables while using the group by on any one of them?🤔 Also, why the where not exists is added in third method? ThanQ in advance. @rishabh mishra gr8 going...🖖

Ответить
Anuradha Varadha
Anuradha Varadha - 05.07.2023 14:24

Do you have in English version?

Ответить
piyush gupta
piyush gupta - 03.07.2023 12:19

great and excellence sir

Ответить
Shivendu Sharma
Shivendu Sharma - 29.06.2023 09:33

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

Ответить
Dharmendra Kumar
Dharmendra Kumar - 27.06.2023 07:22

first method was the easiest...other 2 I did not understand😅

Ответить
Irfan Ansari
Irfan Ansari - 22.06.2023 14:02

sir group by distance kr denge to ho jaega

Ответить
Srikant Pal
Srikant Pal - 15.06.2023 21:23

SELECT source,destination,distance FROM travel WHERE source LIKE 'M%' OR source LIKE 'N%';

Ответить
sub
sub - 14.06.2023 06:10

Being from non IT background, If i learn SQL and other required things how i get job ???

Ответить
Sayed Reyaz
Sayed Reyaz - 16.05.2023 13:22

I am sorry but I must say beginners will find it hard to understand your Queries

In your 3rd method beginners will gona ask why is there NOT EXISTS clause.

Even after watching your whole SQL playlist 😅

Ответить
20UEC017 Sayandeep Banik
20UEC017 Sayandeep Banik - 15.05.2023 06:47

i found out the easiest one
select source, destination, distance FROM travel
WHERE length(source) between 6 and 7

Ответить
Ashwani kumar kaushik
Ashwani kumar kaushik - 06.05.2023 15:50

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?

Ответить
Shubham Varude
Shubham Varude - 01.05.2023 12:53

table download link description mai nhii diya sir?

Ответить
amit sakhare
amit sakhare - 10.04.2023 09:08

Sir in using self join I am getting only 2 rows of output but as you doing in your slide it's having 3 rows of data

Ответить
TAJ NOOR
TAJ NOOR - 05.04.2023 13:52

Why u use not exist in subquery method?

Ответить
Nothing to worry
Nothing to worry - 14.03.2023 07:48

I don't understand the leaste and greatest concept of text not number only text how it work?

Ответить
Brick n Key Properties
Brick n Key Properties - 14.03.2023 03:44

code created below.

create table Travel
(
source varchar(20),
destination varchar(20),
distance int8
);

insert into travel
(source,destination,distance)
values
('Mumbai','Bnagalore',500),
('bangalore','Mumbai',500),
('delhi','mathura',150),
('mathura','delhi',150),
('nagpur','pune',500),
('pune','nagpur',500);

Ответить
Vishnu Datt
Vishnu Datt - 26.02.2023 20:10

select * from travel where source>destination;

is this query right or wrong it show the same result can anyone please explain this query how this is working ????

Ответить
govind shah
govind shah - 25.01.2023 23:59

Method 2 Will not work

If there is duplicate value in either source or destination please have a thought on that

e.g You can add a new record like ('Mumbai', 'Goa', 700) & ('Goa', 'Mumbai', 700) then try this method-2 and it will not work

Ответить
govind shah
govind shah - 25.01.2023 23:21

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);

Ответить
Dharmendra Kumar
Dharmendra Kumar - 27.12.2022 00:37

I solved it this way:

SELECT * FROM travel
WHERE source IN ('Mumbai','Mathura','Nagpur')


is it right ??...coz it is giving the same results

Ответить
option trading school
option trading school - 18.12.2022 09:42

Hi, sir aap data ka file post kr diya kijiye so practice kr sake hmlog

Ответить
Amalia Mcclane
Amalia Mcclane - 15.11.2022 00:27

𝖕𝖗𝖔𝖒𝖔𝖘𝖒 ☹️

Ответить
Parag Sharma
Parag Sharma - 11.11.2022 14:58

Sir, Is there any chance of connecting with you over a call?

Ответить