SQL WITH Clause | How to write SQL Queries using WITH Clause | SQL CTE (Common Table Expression)
Vložit
- čas přidán 2. 07. 2024
- SQL WITH Clause | How to write SQL Queries using WITH Clause | SQL CTE (Common Table Expression)
In this video, we shall understand how to write SQL Queries using WITH Clause. We will go through the syntax of using WITH Clause and I will also explain how SQL treats and executes SQL Queries containing With Clause.
SQL WITH Clause is also referred to as CTE which stands for Common Table Expression and can also be referred to as Subquery Refactoring.
This video will also provide what are the advantages of using With Clause while writing SQL Queries and also mention few scenarios when you should be using With Clause while writing SQL Queries.
Download all the scripts and SQL Queries used in this video from my blog. Link below:
techtfq.com/blog/sql-with-cla...
Timeline:
00:00 Intro
00:34 Query 1: Simple example of writing SQL Query using With clause
06:25 Query 2: Ideal Scenario for writing SQL Query using With clause
20:29 Advantages of using WITH Clause in SQL
22:19 When to use With Clause in SQL
If you are new to SQL then you might have assumed that SQL Queries must always start with SELECT clause but that's not true because we can also start SQL Queries using WITH Clause. Understanding With clause can really help you to solve some of the tricky problems in SQL.
In this video, we shall first see a simple example of using with clause within SQL query. The query no 2 explained in this video will be most suitable scenario for using WITH clause in SQL Query.
🔴 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
✅ Git and GitHub Tutorial:
• Git and GitHub
✅ Data Analytics Projects:
• Data Analytics Projects
THANK YOU,
Thoufiq
Please note the COST column used in this table represents the total cost of purchase hence we do not need to multiple quantity with cost to get the total cost. I designed the dataset with an assumption that COST is the total purchase cost.
then it becomes easy...
What if we have to calculate quantity × price first? Does Sum(quantity*price) work?
Yeah your videos are really helping me to understand this course
Hello, please help me reconstruct this using SELECT. The with clause isn’t supported
WITH seq(n) AS
(SELECT 0 UNION ALL SELECT n + 1 FROM seq
WHERE n < DATEDIFF(DAY, '20210101', (DATEADD(DAY, -1, DATEADD(YEAR, 30, '20210101'))))),
d(d) AS
(SELECT DATEADD(DAY, n, '20210101') FROM seq)
SELECT d, COUNT (*)
ON a.dogma_registereddate = d OR a.dogma_removeddate IS NULL)
WHERE a.dogma_charitynumber != 'SC000036'
GROUP BY d
ORDER BY d
OPTION (MAXRECURSION 0);
Sir can you please explain why did you give X as alias and also again avg_sales as alias but confused
You're the best resource I have found! I've taken classes on Udemy and everything but it's nothing like this. I graduate from college in December and I'm pursuing a career in Data Analytics. Your videos are definitely going to help me with my upcoming interviews. God bless you brother! You have helped me to understand SQL on a whole another level!
I am so glad to hear this bro :)
Happy to know my videos have helped you to understand SQL.
Thank you for the amazing feedback :)
That's correct @ItsReflexes, he is awesome 🙂
Thank you once again Akshay 🙏🏼
UPDATE : I signed a job offer with a great compensation range. In addition, I had 3+ companies to choose from. Hard work does pay off!
Congratulations buddy 💐
I am so happy for your success 👌
All the best 👍
I have been working two and a half years as a Data Management Officer at icddr,b Bangladesh. Now, I am learning SQL to become a data analyst. I found your video classes very useful, effective and eloquently explained. Thanks techTFQ👌.
alot of people just show how to make the query, what I call "watch me type". You actually teach the how and whys. i learned alot with your channel. thanks
Another big advantage is when using CTE's for inserting/updating records in multiple rows (One-to-one, or one-to-mamy), you can use the returned id in the other queries.
Mutiple modifications in a single query reduce overall latency from a code perspective.
Moreover, a CTE is executed as a transaction, so you won't need to start/commit a transaction in your code.
This video on the with clause gave me a solution to a query I have been trying to write for a month. I can't thank you enough. Now the query works as it should and is readable! I had tried various unions, exists, partition statements to isolate some data. It did not work, this did! 😀
Greetings from Vancouver :) Big thank you for this video. As a fintec product manager who uses metabase and its wisiwig (scratch for sql), I was limited severely in what I could do (I know some sql but didn't know this). Your video here finally explains how to create variables, I mean, cte or datasets to work with. I'm binge watching your channel now! Cheers and happy new year to you and yours.
You are the best instructor who explains tough things in a very simple way.
Appreciate your work..!!!
from zero to super hero with you man, massive thanks to do it easier.
i have never seen someone like you to be that perfect in sql ever. hats off taufiq ,god bless you SIR♥
Such a wonderful explanation. I was always afraid of using WITH clause but after watching this video, I will definitely try to implement the same wherever it's possible. Thanks a lot brother.
Glad to hear that buddy
Most underrated CZcams channel i have seen, you have rocked all the sql videos man
I am obsessed with your channel, I have been working with SQL for so long and I used to run away from window functions but you explain it so nice and clear. I learned by working on it so was never clear on concepts but thank god for your Chanel !
Thank you Kamaljeet 🙏🏼
I am glad to read this comment and happy to know my contents have helped you
This is the best video on CTE. Thanks a lot for explaining in a simple way. 🙌
I have watched many SQL videos and I hardly comment. Thank you and appreciate your videos, I found them very beneficial and what makes real difference in your videos is you explain the rational of why we need to use and its pros and cons. They have unexpectedly answered some of the question that I didn't even exist in my mind. Keep up and great work!
Thank you so very much for the amazing feedback :)
I am so glad you took the time to comment and so glad you like my contents 🙏
Thanks for the generous sharing! Your explanation makes it much easier to grasp than the one i took on Udemy.
Learning from you each day, my friend. Great content, not only in this video but other SQL videos as well. Keep up the good work!
Great Teacher.i never seen a person who explain complex stuff in a easy and simple way.
thank you so much man i was just blindly copy pasting the cte queries without understanding how it actually works and I am glad to find this channel, bro explained the concept like we explain everything to 1 year old babies
Awesome. I use with all the time. As a data analyst I write for single use code so I often don’t write very efficiently. Using a with clause to get the initial dataset to just look a little bit more like the target dataset and then using it again until I incrementally achieve the target dataset is pretty much my solution. Almost every time.
You're the best SQL instructor in CZcams. Hoping for the best and keep up this mighty work.
Im grateful to you beyond words could express Mr.Thoufiq. Big Salute to you
Undoubtly your course is the best resource not only in youtube but also whatever available in internet that to be free.
Thank you. You explained very clearly and also in detail at the same time. Glad I found this video.
Bro You're an Amazing Teacher. Nobody ever could make learning SQL so simple!.
Thank You!
I had to go through this 3x to really understand the with clause concept. Thanks a whole bunch 🎉.
Exllent Learning Platform for Advanced SQL
you explained in very simple way so it is best video so far I ever seen on SQL.
Was always feeling quite hard to get hold of this With clause. Thanks a lot for making it so easy to understand✌️ As usual loved the way you explained the whole thing🙌🏻
Thank you bro ❤️❤️❤️
Best video!!!! 100x better than all univ professors combined! Such a clear cut explanation!
Glad you liked it
What I mainly like about you is, the project based explanations you provide. Its now making alot of sense to me after watching your videos. Thanks. Please more and more of project based explanation of SQL.
Thank you for the feedback 🙏🏼
So glad to read this ☺️
Really an excellent way to explain with clause. Thank you for this!
Just like the way you explain the concept so that it is more easier to understand. Thanks again for your video. I have learnt a lot.
It's great to start the SQL learning career with your video's.
Thank you Prashanth.. glad you think so
Thanks for explaining the concepts from a very basic. It really helped me to understand easily.
Thanks a bunch for putting this out - this was super duper helpful!
Excellent.. using cte also get rid of temp tables or reference tables..also bear in mind that memory should be enough to hold data .. since it's being referenced later.
Thank you so much.
God is so good as you are a blessing with your lessons.
Really useful video. Spared me a lot of web research and let me understand the principle that enabled me to execute at a way more complex query.
Thank you for this video, I was using CTE's only for deleting duplicates and I was so confused when I should use these and what they are for. You explained it so well. I'm glad I finally understand CTE.
glad this helped
I love the way you are explaining the complex things...please make some more videos on advance sql
GRATITUDE for all the videos in sql complex queries.
GREAT EXPLANATION AND IS BETTER THAN OTHER CHARGEABLE INSTITUTIONS
5 minutes in and I already understand more than any other video on this topic. Thanks man
Awesome.. glad this helped
Yea. Best I have watched so far. Thanks for how u convey this information. U crafted!
Damn, best and underrated channel I have seen till now. Crisp, to the point and just useful information...
Thank you Sagar once again for all the support and always liking my content 🙏🏼
Am so glad, I am able to add some value through these videos 🙂
I was getting hopeless with my sql learning. This channel gave me a new direction. This is the first video that I watched. Now I am going to practice from your sql practice playlist.
Awesome Sahil 👌
Very happy to read this comment.. I am glad this helped you
Really Helpful and Very Good to understand SQL especially for Beginners
Thanks for the video, needed to understand this for my job. Very well explained and the way you give the lecture is excellent!
Thank you so much 🙏🏼
Glad you liked it..
Your SQL YT videos has helped me a lot, thank you
Randomly I was scrolling CZcams ... saw your channel and saw this video it's amazing! the way of explanation is perfect..glad to see your videos Macha Tqu and do more videos on dynamic queries, SP debugging 😊
Simple but elegant explanation about WITH clause, thanks for creating these videos and sharing your knowledge .....
Glad you liked it 🙏🏼
CLEAR AND SIMPLE. LOVELY EXPLANATION
Thank you for your brilliant examples and explanations, you are doing an excellent job.
Thank you so much 🙏🏼
Happy to know this helped you
what i felt about the video ? your a god dude ❤, thats what i felt
What an amazing explaination..it really helps me to understand SQL well ☺Thank you so much sir🌺🌸
YOU ARE THE BEST TUTOR.
Helps a Lot!!! thanks from Brazil
By far the best material I ever met. Thank you!
Thank you Coma. glad you liked it :)
THIS IS THE WAY ONE SHOULD TEACH... TOO GOOD
Thank you very much Vikram 🙏🏼
Appreciate your feedback ☺️
All your contents are straight to the point. No nonsense . I checked your blog it was very useful. Thanks keep up the good work
Thank you Manoj 🙏🏼
It’s so nice to see such positive feedback 🙂
So glad you liked the content 🙂
Thank you for providing the helpful sql video.
Explain everything so clearly with all of the explanation that helped a lot to understand the concepts Better. Thanks a lot for such valuable videos.👍
Your welcome Asma
Glad this helped
Never used it. Fantastic proof of concept. Will be using it as a cursor in future.
Thank you Sudhanshu 🙏🏼
Yes it’s a very very useful concept
super bro, got clear idea on CTE and its applications, Thanks a ton !!
Your way of teaching and explaining is too good and helpful. keep it up
Please continue doing this ! Great tutorial Thank you so much !!
Your welcome Ashish
Glad this helped
Best One to show how really CTEs work and the difference between them and subqueries. Nice Job
thank you
What an explanation 🔥 This is the most simplest explanation I have ever seen in my career. Thank You so much ❤️
Please make a video on table indexes!
Thank you Akash 🙏🏼
Glad you liked it ..
Will consider a video on index in the near future
@@techTFQ yes please...
You have made it so easy to understand the with clause. Thank You!
Your welcome 🙏🏼
Glad this helped
ThankYou Sir , Your tecahing skills are awesome.
Hi Tawfeeq @techTFQ really grateful to have found your videos' on SQL, no one could have explained better and so much in depth. Do you also conduct online bootcamps on SQL / Python? Would like to learn from you.
you are a gem sir, as soon as I crack my data analyst, I am gonna donate good part of my first month salary to channel, #gratitude
Thank you Saurav :)
Appreciate your generosity. And wishing you all the best
You are the best no one explained with clause better than you.
thank you so much :)
So clear. Thanks for sharing. This was very useful.
Excellent video, I am learning lot of things from you. If you can also provide some practice questions as well like one you solve in your videos. Thank you so much for such very well explained conceptual SQL videos.
You're a saviour ❤🙂
Please make more SQL videos on how to solve and write complex queries
Thank you Ramya!
Will do and glad this helped..
This is soooo helpful!! Thank you Thoufiq, you are a God sent :D
Thank you very much for the explanation, everything was very clear, I have seen it before, but it was very interesting watching this video and getting a detailed example to get a better understanding.
Glad it was helpful!
Simple and clear explanation for usage of CTE. Thanks
Glad you liked it
Quality content 🔥We need these kind of videos regularly...❤️
Thank you Roy 🙏🏼
Thank you for sharing, your explanations help me a lot to solve a test case =)
you explained this so well, and made it feel easy, thanks a ton TFQ !!
Your welcome bro
Your videos are next level. I couldn't find this quality even in paid courses on udemy
Thank you very much Anas 🙏🏼
thx for the videos, they really help to understand how sql querries could be written!
Thank you 🙏🏼
Glad this helped
Thank you very much for this simple and informative course. it helped me solve part of my problem with a database. I am still trying to complete my query of statistics.
I am glad to hear that bro
I use CTE in almost every production query I write. After the first sub query, I always format the alias name with leading comma as
, as (
Select ….
)
, as (
--
The reason for this is when revisiting the query a year later, I just need to comment out any alias name row and highlight from top to and including the query with the alias commented out, and examine any intermediate results. Excellent technique when a business person wants to see how something is being generated. Use white space above and below these lines. My tip? Format for functionality, use leading commas.
Crystal Clear explanation on CTE and Subqueries. Can you please create one video in which we can convert/replace correlated subquery with CTE. Thanks.
Thanks techTFQ and team for your contributions to the tech community. can you make a dedicated video for dynamic sql queries.
Thank you so much for providing such a fabulous content.. I am totally obsessed with your channel.
Thank you Surbhi 🙏🏼
I am glad you liked it
sir u are great,ur learning procedure awasome
I liked all your sql video. The way you explain simplifies the concept. I would request you to make a video on Explain Plan and Execution Plan.
Hi @techTFQ you are a very good teacher. your videos are best for learning SQL. Please make a video on coalesce and is null command also.
Great explanation, one of the best I have seen. Thank you sir!
Glad you liked it
thanx techTFQ for your simplicity.
Thank you bro 🙏🏼
Glad you liked it 🙂
believe me.. Now i can able to solve complex queries after following techTFQ. i know still longway to go but i have confidence to solve any kind of questions
Very happy to know that ☺️
Thank you 🙏🏼
Best video i have sen for this concept!
exactly what i was looking for . Great work!
Thank you 🙏🏼
it was really amazing explanation! since i started fallowing you're videos i can easily say that i have better understnading of how sql works, thank you so much Bro for you're efforts!!!
Glad to hear that Anand ☺️
It's crisp and clear ❤