SQL Interview Problem asked by Product Based Company | Solving SQL Interview Query

preview_player
Показать описание
In this video, I will solve a real SQL Interview problem asked by a Product Based Company. We shall look at the problem statement and figure out how to come up with a solution to solve this kind of problems.

This is a simple problem with simple solution but if you are new to SQL then you might find it complex.

I have solved this SQL problem in PostgreSQL but the same solution will also work in Microsoft SQL Server. However in my blog I have provided the solution for the same in Oracle and MySQL database also.

Download the Dataset, scripts and SQL Query solution from my blog below:

🔴 My Recommended courses 👇

🔴 WATCH MORE VIDEOS HERE 👇
✅ SQL Tutorial - Basic concepts:

✅ SQL Tutorial - Intermediate concepts:

✅ SQL Tutorial - Advance concepts:

✅ Practice Solving Basic SQL Queries:

✅ Practice Solving Intermediate SQL Queries:

✅ Practice Solving Complex SQL Queries:

✅ Data Analytics Career guidance:

✅ SQL Course, SQL Training Platform Recommendations:

✅ Python Tutorial:

✅ Git and GitHub Tutorial:

✅ Data Analytics Projects:

THANK YOU,
Thoufiq
Рекомендации по теме
Комментарии
Автор

Thanks for your video. I try to solve this problem first and here are my code.
select id_name + ', ' + name2 as result
from (select *,
lead(id_name) over (order by id) as name2
from (select *,
cast(id as varchar) + ' ' + name as id_name
from emp_input) x) y
where id % 2 <> 0

angmai
Автор

Table name is input

SELECT concat(a.id, ' ', a.name, ' ', b.id, ' ', b.name) from input as a
JOIN input as b
On a.id < b.id
WHERE a.id%2 = 1 and b.id%2 = 0 and b.id- a.id = 1

smurfguy
Автор

Loved your solution! It is a superior solution for the problem. I tried solving it before watching your solution and came up with this. If record is odd, combine text from this row and leading row. So much to improve! thanks for everything!
select * from
(select case when id%2<>0 then
id||' '||name||', '||
lead(id) over (order by id) || ' '||
lead(name) over (order by id)
end as res
from input) x
where x.res IS NOT null;

pavanareddy
Автор

Applicable, concise, and practical. Thank you 👏🏽

mijailmariano
Автор

Very Well Explained.

MySql Solution:

SELECT GROUP_CONCAT( CONCAT_WS(', ', CONCAT(id, ' ', name))) as Result FROM
(
SELECT id, name, ntile(20) over(order by name) as grp
FROM `workers`
) as temp
GROUP BY grp;

ayazahamed
Автор

I learn and learned all tricky SQL part from your videos only..request you to make video on "sql Index" with practical examples

LoveIndia
Автор

Great explanation, got to learn about the ntile function today, thanks a lot sir!

avi
Автор

with odd as (
select * from emp_input where id%2<>0
),
even as (

select * from emp_input where id%2=0
)
select concat(odd.id, " ", odd.name, ", ", even.id, " ", even.name)as output from odd, even where even.id = odd.id+1

this is my solution

SwethaNandyala-sflt
Автор

I haven‘t really verified my thoughts but I‘d rather go with the mod function to create buckets. In this case you won’t have to speficy how many buckets you need (you just need to think of the size of each bucket and use the row number as numerator). For this exact problem though, is your way of solving it, just perfect! Thanks for sharing 👍

partymaschine
Автор

Logic is this

with cte1 as (select * from emp_input
where id%2=0),

cte2 as (select * from emp_input
where id%2<>0)

select * from cte1 a
inner join cte2 b on a.id=b.id+1

sowmivinay
Автор

sir, make more videos on solving interview question, best teaching method and the way you explain each and every line is very much understandable. Thank you sir.

putulsaini
Автор

I saw one video of yours and I completely understood Sir I want to learn SQL in a proper manner can please suggest me your playlist which one I need to refer . Hope you will reply 😊

pramodkumar-kwrj
Автор

I have learned a lot from your videos! A lot of things that I thought were complex and that were actually simple to understand. Thank you so much for everything and congratulations for the channel!!

ArthurCittaAguiar
Автор

Your Videos are very helpful, gained more knowledge through your videos and I like the way you are explaining step by step and providing every dataset for practise, Thanks a lot

udhaybhaskarbellamkonda
Автор

Awsome that was easyily done...thanks brother...

shaikusman
Автор

I started watching your videos, very excellent explanation . will wait for more such videos.

rushikeshfargade
Автор

You didn't use the within group order by clause in the string aggregate function. It's possible to get 2 Emp2, 1 Emp1, etc. I know because that is what I got when I left out the order clause

select string_agg (concat (ID, ' ', Name), ', ' ) within group (order by ID) as output
from emp_input
group by round(ID/2.0, 0) /* create buckets */

What about creating one long string and then cutting it into distinct pieces...is that possible with no grouping or window clause?

arturoramirez
Автор

I solved it using self join

with cte as(
select e1.id id1, e2.id id2, e1.name name1, e2.name name2 from emp_input e1 left join emp_input e2
on e1.id+1 = e2.id
where e2.name is not null and e1.id % 2 <>0 )

select concat(id1, name1, ', ', id2, name2) output from cte

vablestory.
Автор

Please upload every week solve SQL projects, also could you please start a tutorial series for SQL 0 to 360 Degree❤

rayees_thurkki
Автор

As an absolute beginner who started learning simple select statements, my solution was:
select concat(id, name, ', ', id+1, ' Emp', id+1) as Output from tbl
where mod(id, 2)<> 0;
of course, this fails miserably if all names are not simply Emp and number. So, if we want to stick to very basic syntax, is it possible to concat outputs from two select statements (one with mod(id, 2) <>0 and the other mod(id, 2) =0 in anyway?
I love this site and I am sure I will be spending a lot of time here.

arunny