Practice Complex SQL Queries | SQL Query to fetch N consecutive records

preview_player
Показать описание
Practice Complex SQL Queries | SQL Query to fetch N consecutive records
In this video, I explain how to write a complex SQL query which is to fetch N consecutive records from a table based on some condition. We see how to write a generic SQL Query to fetch any number of consecutive records from a table.
We see 3 different variations of writing this query:
1) How to write this query when the table has a primary key column
2) How to write this query when the table DOES NOT have a primary key column
3) How to write this query based on Date field value.

All the queries and scripts mentioned in this video are posted on my blog. You can download them for free from below link:

Timestamp:
00:00 Intro
01:08 SQL Query to fetch N consecutive records from a table which has a primary key
14:52 SQL Query to fetch N consecutive records from a table which does not have a primary key
18:46 SQL Query to fetch N consecutive records from a table based on date field

🔴 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:

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

Amongst so many tutorials out there on youtube, hands down your's are the best. Will eagerly wait for your upcoming uploads . thanks

arihantdhadiwal
Автор

Have been doing SQL for years, but your clips are still very informative. Thanks.

brianligat
Автор

No doubt your videos are more elaborate and better than institutes charging so much. One request, please create a series for SQL to cover end to end course

bhuwanthapliyal
Автор

this is a great presentation to explain how to pull N consecutive records! Thank you

racheldong
Автор

This was brilliant! A perfect blend of math and SQL. Keep these videos coming!

sonalivv
Автор

I am very happy and excited to see more videos from you.. YOUR VIDEOS ARE AWESOME.. Great explainations

KrishnaYadav-gere
Автор

That is a very clever approach imho! Well done!

flwi
Автор

Great way of explaining complex concepts so that everyone can understand it clearly, you are a life saver.

abhilashs
Автор

Perfect zoom for phone user, perfect mic, and perfect execution to approach a problem! Make more videos on companies SQL questions and your channel will definitely grow.

aashishmalhotra
Автор

You are one of the best SQL instructor on YouTube. I have been using SQL for more than 2 years but still I have learnt so much from you. Please keep uploading the videos. Thank you!

mananshah
Автор

All of your SQL videos are really helpful to understand the advanced concepts. Thanks for making them.

vikranttyagi
Автор

Clear explanation ! Easy way to understand!

karthikd
Автор

The best explanation for consecutive record fetching I ever get. Thank you

arindamnath
Автор

you have touth a very important problem in awesome way... brillliant sir

pavangsk
Автор

Thanks TFQ for your videos.
Just wanted tweak the last part of the query to make it more generic.

With t1 as
(
select id, city, temperature, day,
row_number() over (order by id) as diff,
id - row_number() over (order by id) as conse
from weather
where temperature <0
),
t2 as
(
select id, city, temperature, day, count(*) over (partition by conse) as record
from t1
group by id, city, temperature, day, conse
)

select id, city, temperature, day from t2
where record = (select max(record) from t2)

guchhusworld
Автор

thank you for the tutorial, super helpful!. for the last problem, I think we can increase the complexity by having multiple order records within a day. by that way, each orderID is no longer a unique indicator of a day for us to calculate the difference. I think there will be just one extra step that is to get to the current table structure like we have in this video by creating a new table grouped by order_date and generating a new id column using row_number.

hungphutr
Автор

midway on the video, i immediately clicked on subscribe. this is a gem!

tsukuruuu
Автор

Very helpful. I used to struggle with this, but this explanation is worth watching :)

swatisinha
Автор

Thank you so much for the amazing content! Are you planning to do more videos like this or where you solve stratascratch problems? Those would also be super helpful to see how you approach problems

nhbgpov
Автор

Mashallah! You are really an SQL genius... 👍

ashabhumza