Why CTEs are the most misunderstood SQL language | Nick Holt posted on the topic | LinkedIn (2025)

Nick Holt

Transform Your SQL Skills with Unique Video Training & Mentorship Program | Online Tuition + Weekly Q&A Support

  • Report this post

I think CTEs are the most misunderstood element of SQL.Recent posts I've seen here on this platform demonstrate a frightening lack of awareness about their function, affect on performance and overall usefulness.Thereโ€™s an awful lot of rubbish spoken about CTEs both in LinkedIn and in articles.Some key headlines about CTEs:๐Ÿ“ŒThey exist to aid readability๐Ÿ“ŒYou can split up a long and complex query into small bitesize chunks๐Ÿ“ŒThereโ€™s no practical limit to the number of CTEs you can have in a query๐Ÿ“ŒCTEs enable easier troubleshooting of a query.๐Ÿ“ŒThey CAN help improve performance (as advised by Brent Ozar), but in most cases thereโ€™s little or no performance benefit.๐Ÿ“ŒCTEs last only as long as the current query execution. ๐Ÿ“ŒYou can nest CTEs using a technique called โ€˜daisy-chainingโ€™ where you refer to one CTE inside another. I wouldnโ€™t recommend this personally.--------------------------------------------------------------------About me:I help individuals learn SQL to advance their own careersI help businesses get their data/BI teams skilled up in SQLBook an appointment to speak to me about how I can help you--------------------------------------------------------------------#sql#thebischool#learnsql#sqluk#CTEs

43

6 Comments

Like Comment

Richard Bennett

Data Leader

9mo

  • Report this comment

And most excitingly they can use recursion to solve some interesting challenges.

Like Reply

1Reaction 2Reactions

To view or add a comment, sign in

More Relevant Posts

  • Nick Holt

    Transform Your SQL Skills with Unique Video Training & Mentorship Program | Online Tuition + Weekly Q&A Support

    • Report this post

    I think CTEs are the most misunderstood element of SQL.Recent posts I've seen here on this platform demonstrate a frightening lack of awareness about their function, affect on performance and overall usefulness.Thereโ€™s an awful lot of rubbish spoken about CTEs both in LinkedIn and in articles.Some key headlines about CTEs:๐Ÿ“ŒThey exist to aid readability๐Ÿ“ŒYou can split up a long and complex query into small bitesize chunks๐Ÿ“ŒThereโ€™s no practical limit to the number of CTEs you can have in a query๐Ÿ“ŒCTEs enable easier troubleshooting of a query.๐Ÿ“ŒThey CAN help improve performance (as advised by Brent Ozar), but in most cases thereโ€™s little or no performance benefit.๐Ÿ“ŒCTEs last only as long as the current query execution. ๐Ÿ“ŒYou can nest CTEs using a technique called โ€˜daisy-chainingโ€™ where you refer to one CTE inside another. I wouldnโ€™t recommend this personally.---------------------------------------------------------------------------About me:I help individuals learn SQL to advance their own careersI help businesses get their data/BI teams skilled up in SQLVisit my website and there you can book an appointment to speak to me about how I can help you---------------------------------------------------------------------------#sql#thebischool#learnsql#sqluk#CTEs

    34

    4 Comments

    Like Comment

    To view or add a comment, sign in

    • Report this post

    ๐Ÿš€ Unlocking the Power of SQL with CTEs! ๐Ÿš€I recently used a Common Table Expression (CTE) to solve a tricky data analysis problem and wanted to share how helpful they can be. Here's a snippet of the query:-----------------------------------------------------------------------------------with cte as (select user_id,sum(case when activity_type = 'open' then time_spent else 0 end) as ot,sum(case when activity_type = 'send' then time_spent else 0 end) as stfrom activitiesgroup by user_id)select a.age_bucket , (c.st/(c.st+c.ot))*100.0 as send_perc, (c.ot/(c.st+c.ot))*100.0as open_percfrom cte cRight join age_breakdown aon c.user_id = a.user_id-------------------------------------------------------------------------------------๐Ÿ” Why CTEs Rock:- Clarity: Breaks complex queries into manageable chunks.- Reusability: Reference CTEs multiple times in your query.- Organization: Keeps your main query clean and focused.CTEs have made my SQL queries so much cleaner and easier to understand. If you haven't used them yet, give them a try!How have you used CTEs in your work? Share your experiences below!#DataScience #SQL #CTE #DataAnalytics #TechTips

    4

    1 Comment

    Like Comment

    To view or add a comment, sign in

  • Gayatri Gattani

    MSDS student | Python | SQL | Machine Learning | Data Science

    • Report this post

    ๐ŸŒŸ ๐—˜๐—ป๐—ต๐—ฎ๐—ป๐—ฐ๐—ฒ ๐—ฌ๐—ผ๐˜‚๐—ฟ ๐—ฆ๐—ค๐—Ÿ ๐—ฆ๐—ธ๐—ถ๐—น๐—น๐˜€ ๐˜„๐—ถ๐˜๐—ต ๐—ฎ ๐——๐—”๐—ง๐—˜-๐—ณ๐—ผ๐—ฐ๐˜‚๐˜€๐—ฒ๐—ฑ ๐—–๐—ต๐—ฎ๐—น๐—น๐—ฒ๐—ป๐—ด๐—ฒ! ๐ŸŒŸIn the world of data, SQL stands as a cornerstone, enabling us to navigate through complex databases with ease. Today, I want to spotlight a unique aspect of SQL that often presents intriguing challenges: the ๐——๐—”๐—ง๐—˜ ๐—ฑ๐—ฎ๐˜๐—ฎ๐˜๐˜†๐—ฝ๐—ฒ.Handling dates in SQL can be tricky, yet it's crucial for generating insightful analyses and reports. To help you sharpen your skills in this area, I'm sharing ๐˜๐—ต๐—ฟ๐—ฒ๐—ฒ ๐—Ÿ๐—ฒ๐—ฒ๐˜๐—–๐—ผ๐—ฑ๐—ฒ ๐—ฐ๐—ต๐—ฎ๐—น๐—น๐—ฒ๐—ป๐—ด๐—ฒ๐˜€ that are perfect for anyone looking to delve deeper into SQL's capabilities with dates:๐— ๐—ผ๐—ป๐˜๐—ต๐—น๐˜† ๐—ง๐—ฟ๐—ฎ๐—ป๐˜€๐—ฎ๐—ฐ๐˜๐—ถ๐—ผ๐—ป๐˜€ ๐—œ - A fantastic starting point to practice aggregating data over time. Check it out here : https://lnkd.in/gXhfZUEd๐—œ๐—บ๐—บ๐—ฒ๐—ฑ๐—ถ๐—ฎ๐˜๐—ฒ ๐—™๐—ผ๐—ผ๐—ฑ ๐——๐—ฒ๐—น๐—ถ๐˜ƒ๐—ฒ๐—ฟ๐˜† ๐—œ๐—œ - This challenge takes you a step further, asking you to calculate rates and percentages over specific time periods. Dive in here: https://lnkd.in/gtd9K2Ug๐—š๐—ฎ๐—บ๐—ฒ ๐—ฃ๐—น๐—ฎ๐˜† ๐—”๐—ป๐—ฎ๐—น๐˜†๐˜€๐—ถ๐˜€ ๐—œ๐—ฉ - A more advanced challenge that tests your ability to perform complex analyses over different time frames. Give it a go here: https://lnkd.in/gdGWUE_pWhy not take this opportunity to challenge yourself and perhaps even a colleague? Let's share our solutions and learning experiences. Drop your thoughts, tips, or any questions you might have in the comments below. Let's grow together in our SQL journey!๐—›๐—ฎ๐—ฝ๐—ฝ๐˜† ๐—ค๐˜‚๐—ฒ๐—ฟ๐˜†๐—ถ๐—ป๐—ด! ๐Ÿš€#SQL #DataAnalysis #CodingChallenge #LeetCode #ProfessionalDevelopment #ContinuousLearning

    42

    3 Comments

    Like Comment

    To view or add a comment, sign in

  • Venkata Naga Sai Kumar Bysani

    Data Scientist | 70K LinkedIn | BCBS Of South Carolina | SQL | Python | AWS | ML | Featured on Times Square, Favikon, Fox, NBC | MS in Data Science at UConn | Proven record in driving insights and predictive analytics |

    • Report this post

    Once you've learned/mastered the fundamentals of SQL, try learning these:- ๐‰๐Ž๐ˆ๐๐ฌ: LEFT, RIGHT, INNER, OUTER joins.- ๐€๐ ๐ ๐ซ๐ž๐ ๐š๐ญ๐ž ๐…๐ฎ๐ง๐œ๐ญ๐ข๐จ๐ง๐ฌ: Utilize SUM, COUNT, AVG, and others for efficient data summarization.- ๐‚๐€๐’๐„ ๐–๐‡๐„๐ ๐’๐ญ๐š๐ญ๐ž๐ฆ๐ž๐ง๐ญ๐ฌ: Use conditional logic to tailor query results.- ๐ƒ๐š๐ญ๐ž ๐“๐ข๐ฆ๐ž ๐…๐ฎ๐ง๐œ๐ญ๐ข๐จ๐ง๐ฌ: Master manipulating dates and times for precise analysis.Next, explore advanced methods to structure and reuse SQL code effectively:- ๐‚๐จ๐ฆ๐ฆ๐จ๐ง ๐“๐š๐›๐ฅ๐ž ๐„๐ฑ๐ฉ๐ซ๐ž๐ฌ๐ฌ๐ข๐จ๐ง๐ฌ (๐‚๐“๐„๐ฌ): Simplify complex queries into manageable parts to increase the readability.- ๐’๐ฎ๐›๐ช๐ฎ๐ž๐ซ๐ข๐ž๐ฌ: Nest queries for more granular data retrieval.- ๐“๐ž๐ฆ๐ฉ๐จ๐ซ๐š๐ซ๐ฒ ๐“๐š๐›๐ฅ๐ž๐ฌ: Create and manipulate temporary data sets for specific tasks.Then, move on to advanced ones:- ๐–๐ข๐ง๐๐จ๐ฐ ๐…๐ฎ๐ง๐œ๐ญ๐ข๐จ๐ง๐ฌ: Perform advanced calculations over sets of rows with ease.- ๐’๐ญ๐จ๐ซ๐ž๐ ๐๐ซ๐จ๐œ๐ž๐๐ฎ๐ซ๐ž๐ฌ: Create reusable SQL routines for streamlined operations.- ๐“๐ซ๐ข๐ ๐ ๐ž๐ซ๐ฌ: Automate database actions based on specific events.- ๐‘๐ž๐œ๐ฎ๐ซ๐ฌ๐ข๐ฏ๐ž ๐‚๐“๐„๐ฌ: Solve complex problems using recursive queries.- ๐Ž๐ฉ๐ญ๐ข๐ฆ๐ข๐ณ๐š๐ญ๐ข๐จ๐ง ๐จ๐Ÿ ๐๐ฎ๐ž๐ซ๐ข๐ž๐ฌ: Techniques to enhance performance and efficiency.What other essential SQL skills would you add to this learning journey? ๐Ž๐ง๐œ๐ž ๐ฒ๐จ๐ฎ ๐ฅ๐ž๐š๐ซ๐ง ๐จ๐ซ ๐š๐ฌ ๐ฒ๐จ๐ฎ ๐ฅ๐ž๐š๐ซ๐ง, ๐ฉ๐ซ๐š๐œ๐ญ๐ข๐œ๐ž ๐’๐๐‹ ๐ก๐ž๐ซ๐ž:1. Dataford - https://lnkd.in/dau5UwmN2. InterviewQuery - https://lnkd.in/dzJET9aC3. LeetCode - https://lnkd.in/gkCpv7NA4. HackerRank - https://lnkd.in/exe3kXzD#sql #dataanalytics #interviewprep

    325

    54 Comments

    Like Comment

    To view or add a comment, sign in

  • Md Ashraf

    MIS Executive @Sita Fashions Pvt. Ltd. |||| Advanced Excel || SQL || Power BI || Python || VBA || Alike Mentor||Curious to learn||

    • Report this post

    Hey everyone! After a break from SQL, I've recently jumped back into the world of data, and itโ€™s like coming home!But, as with any return, Iโ€™ve stumbled upon a few bumps along the way.Here are some common mistakes Iโ€™ve caught myself making: โŒ Syntax Errors: Missing semicolons or using wrong keywords.โŒ JOIN Mistakes: Mixing up INNER JOIN and OUTER JOIN or joining on incorrect columns.โŒ GROUP BY Problems: Not using aggregate functions properly. โŒ NULL Handling: Forgetting to use IS NULL or IS NOT NULL. โŒ SELECT * Overuse: Slowing down queries by selecting unnecessary data.โŒ Ignoring Indexes: Not adding indexes for faster querying.Itโ€™s been a learning curve, but every mistake is a chance to improve.When I first returned to SQL, I found that while I remembered the core concepts, some syntax and details had slipped my mind.I also had to readdress my tendency to overuse SELECT *, which was impacting query performance.To get back on track, hereโ€™s what helped me:Revisit the Basics: Refreshing my memory with tutorials and documentation.Practice Regularly: Working on small exercises and sample databases.Use a Cheat Sheet: Keeping a reference guide handy for quick reminders.Write and Review Code: Practicing queries and reviewing them to spot mistakes.Itโ€™s all about patience and persistence. Each mistake is a stepping stone towards mastery.How do you handle jumping back into skills after a break? Letโ€™s share tips and support each other!#SQL #SQLMistakes #Linkedin

    • Why CTEs are the most misunderstood SQL language | Nick Holt posted on the topic | LinkedIn (20)

    26

    2 Comments

    Like Comment

    To view or add a comment, sign in

  • John Pauler

    John Pauler is an Influencer

    Learn data skills @ Maven Analytics

    • Report this post

    Here's how you can create dynamic projections in your reporting. We'll use SQL for the example, but the same logic can be applied to whatever tool your using.Here's the code from the video so you can copy it down and follow along on your own: -- number of days in current monthSELECT DAY(LAST_DAY(NOW()))-- ,NOW()-- ,LAST_DAY(NOW());-- first day of current month SELECT DATE_FORMAT(NOW(),'%Y-%m-01') AS first_day;-- seconds into this month so farSELECT TIMESTAMPDIFF(SECOND, DATE_FORMAT(NOW(),'%Y-%m-01'), NOW()) AS secs;-- total seconds in this month SELECT DAY(LAST_DAY(NOW())) * 86400; SELECT DAY(LAST_DAY(NOW())) * 86400 / TIMESTAMPDIFF(SECOND, DATE_FORMAT(NOW(),'%Y-%m-01'), NOW())AS total_to_current_ratio , TIMESTAMPDIFF(SECOND, DATE_FORMAT(NOW(),'%Y-%m-01'), NOW()) / (DAY(LAST_DAY(NOW())) * 86400)AS pct_through_monthFor folks who are interesting in improving their SQL skills, check out our MySQL Specialist Path or our individual courses. MYSQL SPECIALIST LEARNING PATH: https://lnkd.in/eTpUrqwZ#learning #sql #data #analysis #datascience #businessintelligence #careers

    44

    2 Comments

    Like Comment

    To view or add a comment, sign in

  • John Pauler

    John Pauler is an Influencer

    Learn data skills @ Maven Analytics

    • Report this post

    Here's how you can create dynamic projections in your reporting. We'll use SQL for the example, but the same logic can be applied to whatever tool your using.Here's the code from the video so you can copy it down and follow along on your own: -- number of days in current monthSELECT DAY(LAST_DAY(NOW()))-- ,NOW()-- ,LAST_DAY(NOW());-- first day of current month SELECT DATE_FORMAT(NOW(),'%Y-%m-01') AS first_day;-- seconds into this month so farSELECT TIMESTAMPDIFF(SECOND, DATE_FORMAT(NOW(),'%Y-%m-01'), NOW()) AS secs;-- total seconds in this month SELECT DAY(LAST_DAY(NOW())) * 86400; SELECT DAY(LAST_DAY(NOW())) * 86400 / TIMESTAMPDIFF(SECOND, DATE_FORMAT(NOW(),'%Y-%m-01'), NOW())AS total_to_current_ratio , TIMESTAMPDIFF(SECOND, DATE_FORMAT(NOW(),'%Y-%m-01'), NOW()) / (DAY(LAST_DAY(NOW())) * 86400)AS pct_through_monthFor folks who are interesting in improving their SQL skills, check out our MySQL Specialist Path or our individual courses. MYSQL SPECIALIST LEARNING PATH: https://lnkd.in/eTpUrqwZ#learning #sql #data #analysis #datascience #businessintelligence #careers

    79

    4 Comments

    Like Comment

    To view or add a comment, sign in

  • Anjusha Rajan, M.Eng

    Google Certified Business Intelligence and Data Analyst | Strategizing Scalable AI Solutions for Tomorrow's Challenges | AI/ML | SQL |Azure |Data Visualization

    • Report this post

    ๐Ÿ’ก Heres a tip by John Pauler if you would love to learn 'how to add dynamic projections to your reporting ' . โšก Dynamic reporting dynamically updates reports in response to changes in underlying data sources. โšก We can discover how to calculate an estimated projection for the end of the month, like May, dynamically for any Key Performance Indicator (KPI).#dataanalyst #reporting #sqlqueries

    12

    2 Comments

    Like Comment

    To view or add a comment, sign in

  • Kirolos Daniel

    Data Engineer | Data Analyst | Azure Certified | Freelancer | Financial Analyst @EvaPharma

    • Report this post

    While working on a SQL problem this morning, I reflected on how my problem-solving approach has significantly evolved over time. Itโ€™s fascinating to observe how practice has refined my thinking process compared to when I first began.Now, when faced with a SQL challenge, Iโ€™ve discovered that taking a thoughtful and deliberate approach yields far better results than rushing to a solution. I start by thoroughly analyzing the dataโ€”reviewing its attributes and key information to gain a comprehensive understanding of its structure.Once I have a clear grasp of the data, I carefully read the problem statement to ensure I fully comprehend what is being asked. Before diving into writing queries, I take a moment to draft a step-by-step plan. This strategy is particularly useful for tackling complex problems that involve subqueries, joins, or regular expressions.By investing time in planning my solution, I find that I can approach problems more efficiently and accurately.How do you approach SQL challenges? comment below ๐Ÿ‘‡

    1

    Like Comment

    To view or add a comment, sign in

  • Prerna S.

    Campus Ambassador @ InternsElite | Empowering Students with Career Opportunities | Leadership & Networking Advocate

    • Report this post

    ๐Ÿš€ Day 7 of My SQL Learning Journey: Unlocking the Power of LIKE Queries! ๐Ÿš€Today, I explored the versatility of LIKE queries in SQL, which are essential for pattern matching and flexible data retrieval. ๐Ÿ”๐Ÿ”‘Hereโ€™s what I accomplished on Day 7:- ๐Ÿ“š Learned how to use the LIKE keyword to search for patterns within text data.- ๐Ÿงฉ Practiced using wildcards (%) and (_) to create flexible search conditions.- ๐Ÿ› ๏ธ Combined LIKE with SELECT, WHERE, and other SQL clauses for dynamic queries.- ๐ŸŒŸ Explored practical examples, such as finding all records with a certain prefix, suffix, or substring.LIKE queries are incredibly powerful for filtering and extracting data based on specific patterns, making them indispensable for data analysis and reporting.Iโ€™m excited to continue my SQL journey and apply these techniques to real-world scenarios. Your support and insights have been invaluable!If you have any tips or interesting use cases for LIKE queries, Iโ€™d love to hear them. Letโ€™s keep pushing the boundaries of what we can do with SQL! ๐Ÿš€#SQL #DataManagement #LearningJourney #ProfessionalDevelopment #Day7 #DataAnalytics #LikeQueries

    • Why CTEs are the most misunderstood SQL language | Nick Holt posted on the topic | LinkedIn (37)

    16

    Like Comment

    To view or add a comment, sign in

Why CTEs are the most misunderstood SQL language | Nick Holt posted on the topic | LinkedIn (39)

Why CTEs are the most misunderstood SQL language | Nick Holt posted on the topic | LinkedIn (40)

18,526 followers

  • 736 Posts

View Profile

Follow

Explore topics

  • Sales
  • Marketing
  • IT Services
  • Business Administration
  • HR Management
  • Engineering
  • Soft Skills
  • See All
Why CTEs are the most misunderstood SQL language | Nick Holt posted on the topic | LinkedIn (2025)
Top Articles
Latest Posts
Recommended Articles
Article information

Author: Horacio Brakus JD

Last Updated:

Views: 5327

Rating: 4 / 5 (51 voted)

Reviews: 82% of readers found this page helpful

Author information

Name: Horacio Brakus JD

Birthday: 1999-08-21

Address: Apt. 524 43384 Minnie Prairie, South Edda, MA 62804

Phone: +5931039998219

Job: Sales Strategist

Hobby: Sculling, Kitesurfing, Orienteering, Painting, Computer programming, Creative writing, Scuba diving

Introduction: My name is Horacio Brakus JD, I am a lively, splendid, jolly, vivacious, vast, cheerful, agreeable person who loves writing and wants to share my knowledge and understanding with you.