Recursive SQL Queries Tutorial | Learn to write SQL Queries using Recursion
ฝัง
- เผยแพร่เมื่อ 6 ก.พ. 2025
- Visit brilliant.org/t... to get started learning STEM for free, and the first 200 people will get 20% off their annual premium subscription.
All the scripts and dataset used in this video can be downloaded for free from my discord. Link to join my discord below:
Discord link: / discord
In this video, let's learn how to write Recursive SQL Queries. Learn to write SQL Queries using Recursion by solving different SQL queries. This video will explain the syntax to be followed to write recursive sql queries and we shall look at the difference in syntax in PostgreSQL, Oracle, MySQL and MSSQL.
I will also explain the process SQL follows to execute recursive sql query. Then we shall solve 3 different SQL queries using recursion. I will execute the queries in all the major RDBMS such as PostgreSQL, Oracle, MySQL and Microsoft SQL Server.
My recommended platform to practice SQL queries: www.stratascra...
My recommended platform for SQL Course: learnsql.com/?...
Consider SUBSCRIBING to my channel if you wish to learn SQL, Python and Data Analytics concepts.
FTC disclaimer: This video was sponsored by Brilliant.
🔴 WATCH MORE VIDEOS HERE 👇
✅ SQL Tutorial - Basic concepts:
• SQL Tutorial - Basic c...
✅ SQL Tutorial - Intermediate concepts:
• SQL Tutorial - Interme...
✅ SQL Tutorial - Advance concepts:
• SQL Tutorial - Advance...
✅ Practice Solving Basic SQL Queries:
• Practice Solving BASIC...
✅ Practice Solving Intermediate SQL Queries:
• Practice Solving INTER...
✅ Practice Solving Complex SQL Queries:
• Practice Solving COMPL...
✅ Data Analytics Career guidance:
• Data Analytics career ...
✅ SQL Course, SQL Training Platform Recommendations:
• SQL Course / Training
✅ Python Tutorial:
• Python Tutorial
THANK YOU,
Thoufiq
Visit brilliant.org/techTFQ/ to get started learning STEM for free, and the first 200 people will get 20% off their annual premium subscription.
Hi @techTFQ I have been following you since very long time and I got better in SQL just because of you! Thanks a lot. Now I have a problem and not able to solve it since 2 days! Could you please help me with it!
SELECT * FROM YourTable
WHERE '123' IN ('Col1', 'Col2','Col3'...'Coln')
But the Where condition isn't working for 2 values example: WHERE ('123', '456') IN ('Col1', 'Col2','Col3'...'Coln') , Is there any way to search for 2 or more values existing in different columns??
Thank you Navya ..
You can use OR clause for this case..
WHERE ( Col1 in (123, 456)
OR Col2 in (123, 456)
OR Col3 in (123, 456)
OR Col4 in (123, 456) )
@@techTFQ Thanks for the response, Yes I did use it but I don't want to write multiple OR's because 123, 456 is not constant and these might go upto 100 or 200, so I have to paste them in every single OR condition every time! So is there any way to solve this issue? I want to give the values only once and it should check in all the columns for it's existence.
Can you share the entire query and datasets with me so I can check . U can email me
@@techTFQ sure thanks.
Hi Thoufiq. I've been working with SQL (and teaching) for more than 2 decades and never found someone addressing SQL problems like you. The strongest aspects are problem-solving orientation (instead of theoretical) and ANSI standard queries (as much as possible). Congratulations! I'm recommending your videos to my students and workmates.
Hi Leandro,
Thank you so much 🙏🏼
It’s very special to receive such amazing feedback from an experienced SQL professional like yourself.
Thank you for sharing this feedback, means a lot 🙏🏼
And thank you too for recommending my contents..
At last a clear, detailed and impressive explanation with real examples of what is, how it works and how to write a WITH RECURSIVE CTE.
BRAVO!!!
I've looked at dozens of RECURSIVE CTE tutorials but until this one, I couldn't find an explanation of the JOIN condition. Thank you ... exactly what I needed!
Every single person who is working in SQL must have this channel in youtube subscribe list. I have recommended this channel to 3 colleague's & I am extremely happy to say they all liked this channel. And all 3 are thanking me 😍
I've started my data science course and I thought that recursive cte wasn't meant for me but thanks to you i'm finally able to understand the logic behind it. Thanks Thoufiq.
Thanks, glad it helped
Thank you so much for the detailed tutorial! I’ve been a data analyst for three years but never came across this technique until I received an interview from another company. Hope I can ace that SQL interview🤞
I did both these topics (CTE & recursive CTE) from LinkedIn learning; they made me feel like I can't learn but you make SQL so easy. Thanks for your content:-)
I'm at my first step as data analyst; looking forward to have help from you.
I am glad to hear that Aakriti
Hey Thoufiq, I have become a big fan of your content off late. I think that I have subscribed and started watching your videos for a month or 2. However, I have completed watching every single video that you have uploaded so far. The way of teaching is excellent and the examples that you provide are the backbone for the concepts that you are explaining. Great work! Thanks for the amazing knowledge share!
Thank you so very much Karthikeyan :)
I am so glad you have liked my contents. Thanks for the feedback too
You’ve taught me more in one week, than I learnt in one semester. GOAT STUFF!
Thanks!
Hi Thoufiq,I am a beginner learning Data Science course. Your teaching skills are very good and anyone can understand the way you explain. I really thank you for the SQL videos which are very useful and also I recommend to my friends to watch your videos.
Literally I would say god level explanation, I was struggling with recursive query recently i had use case 👍
Very well explained. I remember using the same concept when I had to generate the Voucher Code for one promotion feature. The voucher code should be AlphaNumeric and it should not already be present in the existing VoucherCodes table. It was very interesting and this concept had helped a lot.
A detailed explanation of confusing recursive query. simply Awsome
I have watched so many videos regarding the same topic but really you made me understand very easily...thank you very much. keep teaching us.
Glad to hear that Santhosh ☺️
Excellent explanation, Thofique! I like how when you ran the second example in multiple DBMSs, you went in an order that made you do an ever-increasing number of changes. :) I'll add another layer: if you were using Db2, you would have to 1. remove the RECURSIVE keywork, 2. use UNION ALL instead of UNION (I generally discourage using UNION by itself anyway), 3. specify column aliases as part of your CTE syntax (so far all the same changes you need for Oracle)...AND you also can't do an INNER JOIN in your recursive CTE! Not a problem for Example 1 (output integers 1 through 10), but for Example 2 it means you would have to go back in time to a time before INNER JOIN was invented (pre-1992!) and change your (INNER) JOIN to a Cartesian join (using the old-school comma-separated list of tablenames...not even the CROSS JOIN syntax!) followed by the appropriate WHERE clause to ensure you only get back the same rows that you would have with an INNER JOIN. Weird, right?
Hi @techTFQ,
I wanted to express my sincere gratitude for the invaluable knowledge I've gained through your videos. As an experienced data engineer, I have found your content to be exceptionally insightful and well-presented. Your selfless efforts and dedication to teaching have not gone unnoticed, and I truly appreciate the time you invest in sharing your expertise.
The clarity with which you explain complex concepts and the practical insights you provide on data analysis have been immensely beneficial to my learning journey. Your commitment to excellence in education is evident, and I wanted to take a moment to acknowledge and thank you for the positive impact you've had on my understanding of the subject.
I have enthusiastically recommended your channel to many of my colleagues who, like me, greatly appreciate the quality of your content. Please continue your outstanding work, as it is making a significant difference in the learning experiences of individuals in the data engineering community.
Thank you once again for your dedication and exceptional teaching.
Regards
Fantastic in my 10 years of IT i have not seen this in-depth tutorial. Amazing ✋
I love every single lessons you teach. Great work!
Thanks
Thank you so much for the video Taufiq. When I'm struggling to SQL problems, your videos are my first to go.
I am glad to hear that bro
Your Amazing sir . Even a person with Zero Sql knowledge can easily understand the concept .Thank you for your efforts to make us to understand the concept :-)
Finally a video that can help me understand recursive sql query. Salute to you sir
Glad this helped
Hey @techTFQ, i have been struggling with recursive query from long time. You gave me a crystal clear understanding. Thanks
Your SQL videos are awesome & extraordinarily understoodable. Seems like taking a delicious chocolate everytime. Once again another SQL (recursive this time) is now clear like distilled water! Hats off bro!
Great explanation. How to show the hierarchy up and additionally if a manager has another employee, besides the searched one, show him and hierarchy down, if searching down found employee has 2nd manager then show him too. So we show all hierarchy of the company.
Definitely one of the better examples and succinct explanations. I liked how you reviewed the syntax first and came back to it when implementing.
Keep it up.
You explain complex topics in a very intuitive manner. Awesome content!! I was having a hard time understanding recursive CTEs, but after watching this - everything seems crystal clear about recursive CTEs :)
Hi toufiq, i have been go through many sql videos but your videos are very descriptive i love the way you elaborate small small details while solving queries,it would be great if you help out what is going on in industrial work
Love from india
Cant thank you enough for your content and explaination....lots of admiration from banaglore
This is definitely an amazing lesson helping me to resolve the issues in my work. Thanks Thoufiq, you are an amazing tutor.
Man, you are simply brilliant! You were able to make me to understand this (I couldn't even do that at university lol). I just have one suggestion: could you explain not so fast? Thank you so much bro! Greetings from Paraguay!
Your welcome buddy.. Glad you liked it and noted on the suggestion 👍
i really respect you so much you are a professional in my opinion i have learned a lot from you . greetings from egypt
You are great I must be plain...more grace to your elbow...
I am your big fan 🤗
Thank you so much bro 😀
Appreciate your support
Thank you very much🙏😊 It helped me a lot. Watching only one tutorial, made me follow and watch all of your videos. It is a great way to explain complex topics like this with examples. Could you please share your database for each of your tutorials so can pactise with it? Of course I could create it myself but its easier if you would share it.
Thank you bro .. glad you liked it..
As for dataset, I always share it either in my blog or in my discord.
For this video, you can get it from my discord
+1
Thank you bro
Simply good 👍 You are helping so many ppl in their journey of learning.. keep it up. God bless✌
Thank you 😍
finally I understood this concept thanks to this video
Thank you! This tutorial helped me understand recursive sql statement. Thanks very much!
Brilliant Thoufiq! great way of putting things! TY!
Glad you enjoyed it Hadi
Awesome explanation. Incredibly helpful
Ethical Vlogging !! kudos. Disclosing sponsorships @ very beginning. Hard to find such practice these days.
Thanks a lot... CTE is great...we can further optimise by creating cte_employee to store employees _details in single time...and use the same cte in recursive
Really fantastic explanation.... Keep it on..
Thanks Thoufiq for precise and clear explanation
Glad this helped Shaik :)
Awesome! Thank you and keep posting more SQL tutorials.
Your welcome buddy and will do
Thoufiq Sir , you are best in the business for sure
thanks buddy
Omg i was planning how to manage categories with multi level , and this will perfectly work for me.
And most importantly i would able to all parents category and when user click on any of that i can get all which belongs to that specific category.
My problem solved
Excellent info... Have always been thinking of handling it through code. Thanks bro.
Glad this helped Parag :)
This is awesome! I have been looking to achieve this in SQL for sometime now. Thanks for sharing!
so clearly explained Taufiq...your videos really do magic...thank you so much and keep posting such learning videos
Hi Thoufiq, Thanks for your great job. I got a clear understanding of how to analyze a problem and its implementation. Looking forward to more such videos.Thank you
Glad it helped MuthuMari
Looking at your queries and way of explanation I wonder why I wasted my money on some SQL course. God why didn't I find your playlist earlier.
Love the way you explained. Very easy to understand
Thank you Rohan 🙏🏼
Awesome got a clear picture on recursion.
Please make videos related to dynamic queries in PostgreSQL.
One humble request I need clear cut guidance on Service Broker.
Thanks....
Thank you buddy and noted on the request
Awesome video, very well explained. Thank you. Just one little thing with the last example. I wished you would have changed the level number backwards somehow.
Mind blowing explanation ..awesome😍😍😍.. Wonderful lectures .. I admired the way to explain the each and evry concepts from scratch to advancced level.
Beautiful explanation. The way you explain concepts is crystal clear. Thanks for the amazing videos.
Thank you so much ☺️
Wooah.. you deserve many subscription. Many thanks, you save my week..
One question, is this work well for a big data row, or better using another method?
Great video. Helped made a complex topic easy to understand and follow
Another awesome video. Very well explained and easy to understand. Thanks.
Thank you so much ☺️
Fantastic Tutorial. Thank you for actually explaining what the code is doing!
Very well explained! Thank you!
Glad it was helpful!
Very informative and nice explanation
Thank you Tafiq for such a wonderful job. You are an excellent teacher, and I have learned SQL from your channel. Keep it up, and be good.
Great work! Do you have any videos on union n union all complex ones
That was a great explanation
Thank u so much . Could you please make a video on database indexing in detail like what is it, how to do indexing and its usage? Thank you in advance
Sure Prajwal , will consider doing it
Please make videos on procedures and functions. Btw you are doing fabulous job.♥️
Thank you Ajay and sure will do
thank you. you explained beautifully. please give some examples to practice apart from what you have solved - to check learning on these concepts, if possible. thanks a lot .
Thank you Ashutosh :)
will consider you request.
your explanation on SQL are sky level. can you please suggest me best sites for python learning.
10 out of 10 Video🤝
Thanks a lot for this wonderful explanation.
Your welcome 🙏🏼
Thanks so much! very clear and details, love your tutorials!
Thank you :)
Hi Thoufiq, Thank you for this video.
Your welcome buddy
Supereb content 👌👌👌
Thank you 🙏🏼
Thanks a Lot Thoufiq for Making great content😀 .Request:: Please make video on UDF
Superb explanation! 👏
Thank you 🙂
Such a Masterpiece!!!
Thank you bro
Question - in the first example, do you have a table in your database called 'numbers'? If yes, I understand the example. If not, what is 'FROM numbers' referencing?
could you please tell
what is the difference between connect by clause and recursive ..same series or hierarchy data we can also print using connect by clause
Excellent explanation! Thank you very much.
Superb explanation and Excellent Content
Very detail explanation
Nice video ..really appreciate..can you please suggest aany place where I can solve and learn advanced SQL queries
Great work to bring in all concepts 👍
Thanks a lot 😊
Well explained and i find it useful. Thank you.
Excellent, I really love your all videos.
Fanstastic explanation. You have gift of explaining concepts.
Thank you! This explained things so clearly.
Best explanation , thank you ;)
Hi toufiq one question the records will repeat yes because epm details will make joins on all records in heirchy table
please make a video on triggers , stored procedures and pragma as well , Thank you.
Sure will do
Very useful video. Thank you!
Glad it helped
Hi, pls try to post complete vedio on stored procedures and thanks for this vedio
Sure Nisha will do it soon
@@techTFQ Thanks you🙂
Tq for sharing valuable information, sir.
excellent video sir
Many many thanks bro
Amazing as always.
Thank you :)
Great concept and I love your videos
This video helped me a lot, Thanks !
You're welcome