🧠 Don’t miss out! Get FREE access to my Skool community — packed with resources, tools, and support to help you with Data, Machine Learning, and AI Automations! 📈 https://www.skool.com/data-and-ai-aut...
Today we are going to take a look at temporary tables within SQL. Using temp tables within your code can help speed up queries and write more efficient code.
Everything is coded within MSSQL and inside SQL Server Management Studio.
🚀 Hire me for Data Work: https://ryanandmattdatascience.com/da...
👨💻 Mentorships: https://ryanandmattdatascience.com/me...
📧 Email: [email protected]
🌐 Website & Blog: https://ryanandmattdatascience.com/
🖥️ Discord: / discord
📚 *Practice SQL & Python Interview Questions: https://stratascratch.com/?via=ryan
📖 *SQL and Python Courses: https://datacamp.pxf.io/XYD7Qg
🍿 WATCH NEXT
SQL Tutorials Playlist: • SQL Tutorials for Data Analysts
SQL String Functions: • 12 Essential SQL String Functions You Must...
SQL Merge: • SQL MERGE for Beginners: A Full Tutorial w...
SQL Cross Joins: • SQL Cross Joins Made Easy: 2 Essential Exa...
In this comprehensive tutorial, I walk you through everything you need to know about temporary tables in SQL using real-world examples from baseball statistics. Temporary tables are powerful tools that can significantly speed up your queries when you need to run them multiple times, but they come with one important caveat—once your session ends, the table disappears.
We start by building a temporary table from scratch using Nolan Ryan's career stats, covering how to properly declare table columns, insert data, and query results. I demonstrate the exact syntax you need, including how to create temporary tables, insert values manually, and filter data based on specific criteria. Then we move through a hands-on practice exercise with Sandy Koufax's statistics where you'll create a filtered temporary table showing only seasons with over 200 strikeouts.
The video also covers more advanced concepts like dropping temporary tables using DROP TABLE IF EXISTS statements, which is especially useful in stored procedures. I explain how to combine temporary tables with Common Table Expressions (CTEs), including the correct syntax placement that often trips people up. Finally, I show you exactly where these temporary tables are stored in your SQL Server database within the system databases TempDB folder.
By the end of this tutorial, you'll have practical, working knowledge of temporary tables and be able to implement them confidently in your own SQL projects to optimize query performance.
TIMESTAMPS
00:00 Introduction to Temporary Tables
00:30 Creating Your First Temporary Table
02:23 Inserting Data into Temporary Tables
04:12 Adding Individual Values to Tables
05:42 Practice Exercise: Sandy Koufax Table
07:29 Creating Filtered Temporary Tables
09:05 Dropping Temporary Tables
09:56 Using CTEs with Temporary Tables
12:29 Finding Temporary Tables in Your Database
OTHER SOCIALS:
Ryan’s LinkedIn: / ryan-p-nolan
Matt’s LinkedIn: / matt-payne-ceo
Twitter/X: https://x.com/RyanMattDS
Who is Ryan
Ryan is a Data Scientist at a fintech company, where he focuses on fraud prevention in underwriting and risk. Before that, he worked as a Data Analyst at a tax software company. He holds a degree in Electrical Engineering from UCF.
Who is Matt
Matt is the founder of Width.ai, an AI and Machine Learning agency. Before starting his own company, he was a Machine Learning Engineer at Capital One.
*This is an affiliate program. We receive a small portion of the final sale at no extra cost to you.