Location via proxy:   [ UP ]  
[Report a bug]   [Manage cookies]                
SlideShare a Scribd company logo
SQL Query Optimization | SQL Query Optimization Techniques | SQL Basics | SQL Query | Simplilearn
Indexes For SQL Query Optimization
Indexing in SQL acts as a shortcut that helps the database find and retrieve data
much faster.
Types Of Indexes
Clustered Indexes - Clustered indexes physically sort and store
data based on actual column values.
Non Clustered Index Clustered Index
Types Of Indexes
Non Clustered Index Clustered Index
Non-Clustered Indexes –They create a separate lookup structure that points to
the actual data.
Types Of Indexes
Full-Text Indexes –Full-text indexes help by storing word positions, making
searches much faster instead of scanning the entire text.
How Indexing Speeds Up Queries
SELECT * FROM customers WHERE name = 'John Doe';
Without an index, the query scans every row:
But by creating an index on the ‘name’ column,
the query runs much faster.
CREATE INDEX idx_customer_name ON customers(name);
Avoid SELECT * And Use Necessary Columns
When you use **SELECT *** in your queries, you’re asking the database to retrieve
every single column, even if you only need a few.
SELECT * FROM customers.customer_profiles;
SELECT customer_name, customer_email, customer_phone FROM
customers.customer_profiles;
Optimizing JOIN Operations
If you use the wrong type of join, you might end up with duplicate records, slow query
performance, or unnecessary data bloating your results. Understanding INNER JOIN, OUTER
JOIN, LEFT JOIN, and RIGHT JOIN is crucial for writing efficient queries.
Outer Joins – Use with Caution!
OUTER JOIN is like keeping everything from both, even the unrelated data.
It can create duplicates and bloat your dataset unnecessarily.
Optimizing JOIN Operations
If you use the wrong type of join, you might end up with duplicate records, slow query
performance, or unnecessary data bloating your results. Understanding INNER JOIN, OUTER
JOIN, LEFT JOIN, and RIGHT JOIN is crucial for writing efficient queries.
INNER JOIN only returns records where there’s a match between both tables.
It keeps your dataset clean and ensures you only get the necessary data.
Inner Joins – Your Best Friend!
Optimizing JOIN Operations
If you use the wrong type of join, you might end up with duplicate records, slow query
performance, or unnecessary data bloating your results. Understanding INNER JOIN, OUTER
JOIN, LEFT JOIN, and RIGHT JOIN is crucial for writing efficient queries.
SELECT
customer_orders.customer_id,
order_details.order_id,
order_details.order_date
FROM customers.customer_orders customer_orders
INNER JOIN orders.order_details order_details
ON customer_orders.customer_id = order_details.customer_id
AND customer_orders.customer_order_id = order_details.order_id;
Optimizing JOIN Operations
LEFT JOIN Vs RIGHT JOIN
Use EXISTS Instead Of IN
When checking if a value exists in a smaller dataset, EXISTS is usually much faster than IN ,
because EXISTS stops searching as soon as it finds a match, whereas IN scans the entire
dataset before deciding—making it slower, especially with large tables.
SELECT * FROM orders WHERE customer_id IN (SELECT id FROM customers
WHERE active = TRUE);
SELECT * FROM orders o
WHERE EXISTS (SELECT 1 FROM customers c WHERE c.id = o.customer_id AND
c.active = TRUE);
Minimize Subqueries & Use CTEs Instead
1️
1️
⃣ Subqueries Can Get Messy – Using too many subqueries in SQL makes your code
hard to read, debug, and optimize, just like untangling a messy ball of wires.
2️
⃣ CTEs Make Queries Cleaner – Common Table Expressions (CTEs) break queries into
readable sections, making them easier to understand, troubleshoot, and maintain—
just like writing clear step-by-step instructions.
Minimize Subqueries & Use CTEs Instead
SELECT MAX(customer_signup) AS most_recent_signup
FROM (SELECT customer_name, customer_phone, customer_signup
FROM customer_details
WHERE YEAR(customer_signup) = 2023);
WITH
2023_signups AS (
SELECT customer_name, customer_phone, customer_signup
FROM customer_details
WHERE YEAR(customer_signup) = 2023
),
Most_recent_signup AS (
SELECT MAX(customer_signup) AS most_recent_signup
FROM 2023_signups
)
Avoid Queries Inside A Loop
When you run SQL queries inside a loop—it makes
execution unnecessarily slow and inefficient.
Loops like FOR, WHILE, and DO-WHILE run queries one at a time, causing
repeated parsing, compiling, and execution, which hurts performance and
scalability.
FOR EACH customer IN customer_list LOOP
UPDATE customers SET status = 'Active' WHERE id = customer.id;
END LOOP;
UPDATE customers
SET status = 'Active'
WHERE id IN (SELECT id FROM customer_list);
Avoid Queries Inside A Loop
Avoid Redundant Data Retrieval
The larger the dataset, the slower the query, and the more resources you
waste—which can increase costs, especially in cloud-based databases.
Select Only the Needed Columns – We’ve already covered why **SELECT *** is
inefficient, but it’s also important to limit the number of rows returned, not just
columns.
Use LIMIT to Control Output – Queries that return thousands or millions of rows
can drag down performance, so always limit the results when needed.
Avoid Redundant Data Retrieval
SELECT customer_name FROM customer_details
ORDER BY customer_signup DESC
LIMIT 100;
Instead of pulling all customers, retrieve only the latest 100 signups:
SELECT customer_name FROM customer_details
ORDER BY customer_signup DESC
LIMIT 100 OFFSET 20;
Want to skip the first 20 rows and get the next 100? Use OFFSET:
How to Create A Stored Procedure?
Let’s say we want to find the most recent customer signup. Instead of writing the query
every time, we can save it as a stored procedure:
CREATE PROCEDURE find_most_recent_customer
AS BEGIN
SELECT MAX(customer_signup) AS most_recent_signup
FROM 2023_signups;
END;
EXEC find_most_recent_customer;
How to Create A Stored Procedure?
Adding Parameters for Flexibility
CREATE PROCEDURE find_most_recent_customer @store_id
INT
AS BEGIN
SELECT MAX(customer_signup) AS most_recent_signup
FROM 2023_signups
WHERE store_id = @store_id;
END;
EXEC find_most_recent_customer @store_id = 1188;
Normalize Your Database Tables
1. First Normal Form (1NF) – No Nested Data!
Normalize Your Database Tables
2. Second Normal Form (2NF) – Split Multi-Value Fields
Normalize Your Database Tables
3. Third Normal Form (3NF) – Remove Column Dependencies
Monitoring Query Performance
When you run SQL queries without monitoring their performance—you have no idea
which queries are slowing down your database or costing you extra resources.
Identifies Slow Queries
Saves Costs in Cloud Databases
Prevents System Slowdowns
Monitoring Query Performance
Tools for Monitoring Query Performance
Query Profiling
Query Execution Plans
Database Logs & Server Monitoring
Monitoring Query Performance
Let’s say you have a slow query, and you want to analyze its
performance. You can use MySQL’s EXPLAIN statement:
EXPLAIN SELECT customer_name FROM customers
WHERE city = 'New York';
Use UNION ALL Instead Of UNION
If you use UNION, you’re double-checking every name to remove duplicates
before finalizing the list. But if you use UNION ALL, you simply combine both lists
as they are—faster, simpler, and without extra processing.
UNION – Combines results from two tables and removes duplicates (which takes
extra processing time).
UNION ALL – Combines results without checking for duplicates, making it faster
and more efficient.
Use UNION ALL Instead Of UNION
SELECT customer_name FROM customers_2023
UNION
SELECT customer_name FROM customers_2024;
SELECT customer_name FROM customers_2023
UNION ALL
SELECT customer_name FROM customers_2024;
Leverage Cloud Database-Specific Features
Cloud providers like Snowflake, BigQuery, and Redshift come with built-in
optimizations and custom SQL functions designed to improve performance,
simplify queries, and handle data more efficiently.
Built-in Functions
Optimized Performance
Cost Efficiency
Leverage Cloud Database-Specific Features
Example: Snowflake’s JSON Functions
SELECT
json_column:customer.name::STRING AS customer_name,
json_column:customer.email::STRING AS customer_email
FROM orders;

More Related Content

Similar to SQL Query Optimization | SQL Query Optimization Techniques | SQL Basics | SQL Query | Simplilearn (20)

Database performance tuning and query optimization
Database performance tuning and query optimizationDatabase performance tuning and query optimization
Database performance tuning and query optimization
Usman Tariq
 
02 database oprimization - improving sql performance - ent-db
02  database oprimization - improving sql performance - ent-db02  database oprimization - improving sql performance - ent-db
02 database oprimization - improving sql performance - ent-db
uncleRhyme
 
Tunning sql query
Tunning sql queryTunning sql query
Tunning sql query
vuhaininh88
 
PPT SQL CLASS.pptx
PPT SQL CLASS.pptxPPT SQL CLASS.pptx
PPT SQL CLASS.pptx
AngeOuattara
 
How to Think Like the SQL Server Engine
How to Think Like the SQL Server EngineHow to Think Like the SQL Server Engine
How to Think Like the SQL Server Engine
Brent Ozar
 
SQL Server 2008 Development for Programmers
SQL Server 2008 Development for ProgrammersSQL Server 2008 Development for Programmers
SQL Server 2008 Development for Programmers
Adam Hutson
 
Crucial Tips to Improve MySQL Database Performance.pptx
Crucial Tips to Improve MySQL Database Performance.pptxCrucial Tips to Improve MySQL Database Performance.pptx
Crucial Tips to Improve MySQL Database Performance.pptx
Tosska Technology
 
How to design a database that include planning
How to design a database that include planningHow to design a database that include planning
How to design a database that include planning
Kamal Golan
 
53 SQL Questions-Answers=53 SQL Questions-Answers
53 SQL Questions-Answers=53 SQL Questions-Answers53 SQL Questions-Answers=53 SQL Questions-Answers
53 SQL Questions-Answers=53 SQL Questions-Answers
marukochan23
 
Query Optimization in SQL Server
Query Optimization in SQL ServerQuery Optimization in SQL Server
Query Optimization in SQL Server
Rajesh Gunasundaram
 
Databases By ZAK
Databases By ZAKDatabases By ZAK
Databases By ZAK
Tabsheer Hasan
 
Performance tuning
Performance tuningPerformance tuning
Performance tuning
ami111
 
15 Ways to Kill Your Mysql Application Performance
15 Ways to Kill Your Mysql Application Performance15 Ways to Kill Your Mysql Application Performance
15 Ways to Kill Your Mysql Application Performance
guest9912e5
 
How Clean is your Database? Data Scrubbing for all Skill Sets
How Clean is your Database? Data Scrubbing for all Skill SetsHow Clean is your Database? Data Scrubbing for all Skill Sets
How Clean is your Database? Data Scrubbing for all Skill Sets
Chad Petrovay
 
Mysql For Developers
Mysql For DevelopersMysql For Developers
Mysql For Developers
Carol McDonald
 
Join_Queries_Presentation_By_Beate_.pptx
Join_Queries_Presentation_By_Beate_.pptxJoin_Queries_Presentation_By_Beate_.pptx
Join_Queries_Presentation_By_Beate_.pptx
beatereichrath9
 
MYSQL.ppt
MYSQL.pptMYSQL.ppt
MYSQL.ppt
webhostingguy
 
SQL Server 2000 Research Series - Performance Tuning
SQL Server 2000 Research Series - Performance TuningSQL Server 2000 Research Series - Performance Tuning
SQL Server 2000 Research Series - Performance Tuning
Jerry Yang
 
Optimizing Data Accessin Sq Lserver2005
Optimizing Data Accessin Sq Lserver2005Optimizing Data Accessin Sq Lserver2005
Optimizing Data Accessin Sq Lserver2005
rainynovember12
 
Database development coding standards
Database development coding standardsDatabase development coding standards
Database development coding standards
Alessandro Baratella
 
Database performance tuning and query optimization
Database performance tuning and query optimizationDatabase performance tuning and query optimization
Database performance tuning and query optimization
Usman Tariq
 
02 database oprimization - improving sql performance - ent-db
02  database oprimization - improving sql performance - ent-db02  database oprimization - improving sql performance - ent-db
02 database oprimization - improving sql performance - ent-db
uncleRhyme
 
Tunning sql query
Tunning sql queryTunning sql query
Tunning sql query
vuhaininh88
 
PPT SQL CLASS.pptx
PPT SQL CLASS.pptxPPT SQL CLASS.pptx
PPT SQL CLASS.pptx
AngeOuattara
 
How to Think Like the SQL Server Engine
How to Think Like the SQL Server EngineHow to Think Like the SQL Server Engine
How to Think Like the SQL Server Engine
Brent Ozar
 
SQL Server 2008 Development for Programmers
SQL Server 2008 Development for ProgrammersSQL Server 2008 Development for Programmers
SQL Server 2008 Development for Programmers
Adam Hutson
 
Crucial Tips to Improve MySQL Database Performance.pptx
Crucial Tips to Improve MySQL Database Performance.pptxCrucial Tips to Improve MySQL Database Performance.pptx
Crucial Tips to Improve MySQL Database Performance.pptx
Tosska Technology
 
How to design a database that include planning
How to design a database that include planningHow to design a database that include planning
How to design a database that include planning
Kamal Golan
 
53 SQL Questions-Answers=53 SQL Questions-Answers
53 SQL Questions-Answers=53 SQL Questions-Answers53 SQL Questions-Answers=53 SQL Questions-Answers
53 SQL Questions-Answers=53 SQL Questions-Answers
marukochan23
 
Query Optimization in SQL Server
Query Optimization in SQL ServerQuery Optimization in SQL Server
Query Optimization in SQL Server
Rajesh Gunasundaram
 
Performance tuning
Performance tuningPerformance tuning
Performance tuning
ami111
 
15 Ways to Kill Your Mysql Application Performance
15 Ways to Kill Your Mysql Application Performance15 Ways to Kill Your Mysql Application Performance
15 Ways to Kill Your Mysql Application Performance
guest9912e5
 
How Clean is your Database? Data Scrubbing for all Skill Sets
How Clean is your Database? Data Scrubbing for all Skill SetsHow Clean is your Database? Data Scrubbing for all Skill Sets
How Clean is your Database? Data Scrubbing for all Skill Sets
Chad Petrovay
 
Join_Queries_Presentation_By_Beate_.pptx
Join_Queries_Presentation_By_Beate_.pptxJoin_Queries_Presentation_By_Beate_.pptx
Join_Queries_Presentation_By_Beate_.pptx
beatereichrath9
 
SQL Server 2000 Research Series - Performance Tuning
SQL Server 2000 Research Series - Performance TuningSQL Server 2000 Research Series - Performance Tuning
SQL Server 2000 Research Series - Performance Tuning
Jerry Yang
 
Optimizing Data Accessin Sq Lserver2005
Optimizing Data Accessin Sq Lserver2005Optimizing Data Accessin Sq Lserver2005
Optimizing Data Accessin Sq Lserver2005
rainynovember12
 
Database development coding standards
Database development coding standardsDatabase development coding standards
Database development coding standards
Alessandro Baratella
 

More from Simplilearn (20)

Top 50 Scrum Master Interview Questions | Scrum Master Interview Questions & ...
Top 50 Scrum Master Interview Questions | Scrum Master Interview Questions & ...Top 50 Scrum Master Interview Questions | Scrum Master Interview Questions & ...
Top 50 Scrum Master Interview Questions | Scrum Master Interview Questions & ...
Simplilearn
 
Bagging Vs Boosting In Machine Learning | Ensemble Learning In Machine Learni...
Bagging Vs Boosting In Machine Learning | Ensemble Learning In Machine Learni...Bagging Vs Boosting In Machine Learning | Ensemble Learning In Machine Learni...
Bagging Vs Boosting In Machine Learning | Ensemble Learning In Machine Learni...
Simplilearn
 
Future Of Social Media | Social Media Trends and Strategies 2025 | Instagram ...
Future Of Social Media | Social Media Trends and Strategies 2025 | Instagram ...Future Of Social Media | Social Media Trends and Strategies 2025 | Instagram ...
Future Of Social Media | Social Media Trends and Strategies 2025 | Instagram ...
Simplilearn
 
SQL INterview Questions .pTop 45 SQL Interview Questions And Answers In 2025 ...
SQL INterview Questions .pTop 45 SQL Interview Questions And Answers In 2025 ...SQL INterview Questions .pTop 45 SQL Interview Questions And Answers In 2025 ...
SQL INterview Questions .pTop 45 SQL Interview Questions And Answers In 2025 ...
Simplilearn
 
How To Start Influencer Marketing Business | Influencer Marketing For Beginne...
How To Start Influencer Marketing Business | Influencer Marketing For Beginne...How To Start Influencer Marketing Business | Influencer Marketing For Beginne...
How To Start Influencer Marketing Business | Influencer Marketing For Beginne...
Simplilearn
 
Cyber Security Roadmap 2025 | How To Become Cyber Security Engineer In 2025 |...
Cyber Security Roadmap 2025 | How To Become Cyber Security Engineer In 2025 |...Cyber Security Roadmap 2025 | How To Become Cyber Security Engineer In 2025 |...
Cyber Security Roadmap 2025 | How To Become Cyber Security Engineer In 2025 |...
Simplilearn
 
How To Become An AI And ML Engineer In 2025 | AI Engineer Roadmap | AI ML Car...
How To Become An AI And ML Engineer In 2025 | AI Engineer Roadmap | AI ML Car...How To Become An AI And ML Engineer In 2025 | AI Engineer Roadmap | AI ML Car...
How To Become An AI And ML Engineer In 2025 | AI Engineer Roadmap | AI ML Car...
Simplilearn
 
What Is GitHub Copilot? | How To Use GitHub Copilot? | How does GitHub Copilo...
What Is GitHub Copilot? | How To Use GitHub Copilot? | How does GitHub Copilo...What Is GitHub Copilot? | How To Use GitHub Copilot? | How does GitHub Copilo...
What Is GitHub Copilot? | How To Use GitHub Copilot? | How does GitHub Copilo...
Simplilearn
 
Top 10 Data Analyst Certification For 2025 | Best Data Analyst Certification ...
Top 10 Data Analyst Certification For 2025 | Best Data Analyst Certification ...Top 10 Data Analyst Certification For 2025 | Best Data Analyst Certification ...
Top 10 Data Analyst Certification For 2025 | Best Data Analyst Certification ...
Simplilearn
 
Complete Data Science Roadmap For 2025 | Data Scientist Roadmap For Beginners...
Complete Data Science Roadmap For 2025 | Data Scientist Roadmap For Beginners...Complete Data Science Roadmap For 2025 | Data Scientist Roadmap For Beginners...
Complete Data Science Roadmap For 2025 | Data Scientist Roadmap For Beginners...
Simplilearn
 
Top 7 High Paying AI Certifications Courses For 2025 | Best AI Certifications...
Top 7 High Paying AI Certifications Courses For 2025 | Best AI Certifications...Top 7 High Paying AI Certifications Courses For 2025 | Best AI Certifications...
Top 7 High Paying AI Certifications Courses For 2025 | Best AI Certifications...
Simplilearn
 
Data Cleaning In Data Mining | Step by Step Data Cleaning Process | Data Clea...
Data Cleaning In Data Mining | Step by Step Data Cleaning Process | Data Clea...Data Cleaning In Data Mining | Step by Step Data Cleaning Process | Data Clea...
Data Cleaning In Data Mining | Step by Step Data Cleaning Process | Data Clea...
Simplilearn
 
Top 10 Data Analyst Projects For 2025 | Data Analyst Projects | Data Analysis...
Top 10 Data Analyst Projects For 2025 | Data Analyst Projects | Data Analysis...Top 10 Data Analyst Projects For 2025 | Data Analyst Projects | Data Analysis...
Top 10 Data Analyst Projects For 2025 | Data Analyst Projects | Data Analysis...
Simplilearn
 
AI Engineer Roadmap 2025 | AI Engineer Roadmap For Beginners | AI Engineer Ca...
AI Engineer Roadmap 2025 | AI Engineer Roadmap For Beginners | AI Engineer Ca...AI Engineer Roadmap 2025 | AI Engineer Roadmap For Beginners | AI Engineer Ca...
AI Engineer Roadmap 2025 | AI Engineer Roadmap For Beginners | AI Engineer Ca...
Simplilearn
 
Machine Learning Roadmap 2025 | Machine Learning Engineer Roadmap For Beginne...
Machine Learning Roadmap 2025 | Machine Learning Engineer Roadmap For Beginne...Machine Learning Roadmap 2025 | Machine Learning Engineer Roadmap For Beginne...
Machine Learning Roadmap 2025 | Machine Learning Engineer Roadmap For Beginne...
Simplilearn
 
Kotter's 8-Step Change Model Explained | Kotter's Change Management Model | S...
Kotter's 8-Step Change Model Explained | Kotter's Change Management Model | S...Kotter's 8-Step Change Model Explained | Kotter's Change Management Model | S...
Kotter's 8-Step Change Model Explained | Kotter's Change Management Model | S...
Simplilearn
 
Gen AI Engineer Roadmap For 2025 | How To Become Gen AI Engineer In 2025 | Si...
Gen AI Engineer Roadmap For 2025 | How To Become Gen AI Engineer In 2025 | Si...Gen AI Engineer Roadmap For 2025 | How To Become Gen AI Engineer In 2025 | Si...
Gen AI Engineer Roadmap For 2025 | How To Become Gen AI Engineer In 2025 | Si...
Simplilearn
 
Top 10 Data Analyst Certification For 2025 | Best Data Analyst Certification ...
Top 10 Data Analyst Certification For 2025 | Best Data Analyst Certification ...Top 10 Data Analyst Certification For 2025 | Best Data Analyst Certification ...
Top 10 Data Analyst Certification For 2025 | Best Data Analyst Certification ...
Simplilearn
 
Complete Data Science Roadmap For 2025 | Data Scientist Roadmap For Beginners...
Complete Data Science Roadmap For 2025 | Data Scientist Roadmap For Beginners...Complete Data Science Roadmap For 2025 | Data Scientist Roadmap For Beginners...
Complete Data Science Roadmap For 2025 | Data Scientist Roadmap For Beginners...
Simplilearn
 
Top 7 High Paying AI Certifications Courses For 2025 | Best AI Certifications...
Top 7 High Paying AI Certifications Courses For 2025 | Best AI Certifications...Top 7 High Paying AI Certifications Courses For 2025 | Best AI Certifications...
Top 7 High Paying AI Certifications Courses For 2025 | Best AI Certifications...
Simplilearn
 
Top 50 Scrum Master Interview Questions | Scrum Master Interview Questions & ...
Top 50 Scrum Master Interview Questions | Scrum Master Interview Questions & ...Top 50 Scrum Master Interview Questions | Scrum Master Interview Questions & ...
Top 50 Scrum Master Interview Questions | Scrum Master Interview Questions & ...
Simplilearn
 
Bagging Vs Boosting In Machine Learning | Ensemble Learning In Machine Learni...
Bagging Vs Boosting In Machine Learning | Ensemble Learning In Machine Learni...Bagging Vs Boosting In Machine Learning | Ensemble Learning In Machine Learni...
Bagging Vs Boosting In Machine Learning | Ensemble Learning In Machine Learni...
Simplilearn
 
Future Of Social Media | Social Media Trends and Strategies 2025 | Instagram ...
Future Of Social Media | Social Media Trends and Strategies 2025 | Instagram ...Future Of Social Media | Social Media Trends and Strategies 2025 | Instagram ...
Future Of Social Media | Social Media Trends and Strategies 2025 | Instagram ...
Simplilearn
 
SQL INterview Questions .pTop 45 SQL Interview Questions And Answers In 2025 ...
SQL INterview Questions .pTop 45 SQL Interview Questions And Answers In 2025 ...SQL INterview Questions .pTop 45 SQL Interview Questions And Answers In 2025 ...
SQL INterview Questions .pTop 45 SQL Interview Questions And Answers In 2025 ...
Simplilearn
 
How To Start Influencer Marketing Business | Influencer Marketing For Beginne...
How To Start Influencer Marketing Business | Influencer Marketing For Beginne...How To Start Influencer Marketing Business | Influencer Marketing For Beginne...
How To Start Influencer Marketing Business | Influencer Marketing For Beginne...
Simplilearn
 
Cyber Security Roadmap 2025 | How To Become Cyber Security Engineer In 2025 |...
Cyber Security Roadmap 2025 | How To Become Cyber Security Engineer In 2025 |...Cyber Security Roadmap 2025 | How To Become Cyber Security Engineer In 2025 |...
Cyber Security Roadmap 2025 | How To Become Cyber Security Engineer In 2025 |...
Simplilearn
 
How To Become An AI And ML Engineer In 2025 | AI Engineer Roadmap | AI ML Car...
How To Become An AI And ML Engineer In 2025 | AI Engineer Roadmap | AI ML Car...How To Become An AI And ML Engineer In 2025 | AI Engineer Roadmap | AI ML Car...
How To Become An AI And ML Engineer In 2025 | AI Engineer Roadmap | AI ML Car...
Simplilearn
 
What Is GitHub Copilot? | How To Use GitHub Copilot? | How does GitHub Copilo...
What Is GitHub Copilot? | How To Use GitHub Copilot? | How does GitHub Copilo...What Is GitHub Copilot? | How To Use GitHub Copilot? | How does GitHub Copilo...
What Is GitHub Copilot? | How To Use GitHub Copilot? | How does GitHub Copilo...
Simplilearn
 
Top 10 Data Analyst Certification For 2025 | Best Data Analyst Certification ...
Top 10 Data Analyst Certification For 2025 | Best Data Analyst Certification ...Top 10 Data Analyst Certification For 2025 | Best Data Analyst Certification ...
Top 10 Data Analyst Certification For 2025 | Best Data Analyst Certification ...
Simplilearn
 
Complete Data Science Roadmap For 2025 | Data Scientist Roadmap For Beginners...
Complete Data Science Roadmap For 2025 | Data Scientist Roadmap For Beginners...Complete Data Science Roadmap For 2025 | Data Scientist Roadmap For Beginners...
Complete Data Science Roadmap For 2025 | Data Scientist Roadmap For Beginners...
Simplilearn
 
Top 7 High Paying AI Certifications Courses For 2025 | Best AI Certifications...
Top 7 High Paying AI Certifications Courses For 2025 | Best AI Certifications...Top 7 High Paying AI Certifications Courses For 2025 | Best AI Certifications...
Top 7 High Paying AI Certifications Courses For 2025 | Best AI Certifications...
Simplilearn
 
Data Cleaning In Data Mining | Step by Step Data Cleaning Process | Data Clea...
Data Cleaning In Data Mining | Step by Step Data Cleaning Process | Data Clea...Data Cleaning In Data Mining | Step by Step Data Cleaning Process | Data Clea...
Data Cleaning In Data Mining | Step by Step Data Cleaning Process | Data Clea...
Simplilearn
 
Top 10 Data Analyst Projects For 2025 | Data Analyst Projects | Data Analysis...
Top 10 Data Analyst Projects For 2025 | Data Analyst Projects | Data Analysis...Top 10 Data Analyst Projects For 2025 | Data Analyst Projects | Data Analysis...
Top 10 Data Analyst Projects For 2025 | Data Analyst Projects | Data Analysis...
Simplilearn
 
AI Engineer Roadmap 2025 | AI Engineer Roadmap For Beginners | AI Engineer Ca...
AI Engineer Roadmap 2025 | AI Engineer Roadmap For Beginners | AI Engineer Ca...AI Engineer Roadmap 2025 | AI Engineer Roadmap For Beginners | AI Engineer Ca...
AI Engineer Roadmap 2025 | AI Engineer Roadmap For Beginners | AI Engineer Ca...
Simplilearn
 
Machine Learning Roadmap 2025 | Machine Learning Engineer Roadmap For Beginne...
Machine Learning Roadmap 2025 | Machine Learning Engineer Roadmap For Beginne...Machine Learning Roadmap 2025 | Machine Learning Engineer Roadmap For Beginne...
Machine Learning Roadmap 2025 | Machine Learning Engineer Roadmap For Beginne...
Simplilearn
 
Kotter's 8-Step Change Model Explained | Kotter's Change Management Model | S...
Kotter's 8-Step Change Model Explained | Kotter's Change Management Model | S...Kotter's 8-Step Change Model Explained | Kotter's Change Management Model | S...
Kotter's 8-Step Change Model Explained | Kotter's Change Management Model | S...
Simplilearn
 
Gen AI Engineer Roadmap For 2025 | How To Become Gen AI Engineer In 2025 | Si...
Gen AI Engineer Roadmap For 2025 | How To Become Gen AI Engineer In 2025 | Si...Gen AI Engineer Roadmap For 2025 | How To Become Gen AI Engineer In 2025 | Si...
Gen AI Engineer Roadmap For 2025 | How To Become Gen AI Engineer In 2025 | Si...
Simplilearn
 
Top 10 Data Analyst Certification For 2025 | Best Data Analyst Certification ...
Top 10 Data Analyst Certification For 2025 | Best Data Analyst Certification ...Top 10 Data Analyst Certification For 2025 | Best Data Analyst Certification ...
Top 10 Data Analyst Certification For 2025 | Best Data Analyst Certification ...
Simplilearn
 
Complete Data Science Roadmap For 2025 | Data Scientist Roadmap For Beginners...
Complete Data Science Roadmap For 2025 | Data Scientist Roadmap For Beginners...Complete Data Science Roadmap For 2025 | Data Scientist Roadmap For Beginners...
Complete Data Science Roadmap For 2025 | Data Scientist Roadmap For Beginners...
Simplilearn
 
Top 7 High Paying AI Certifications Courses For 2025 | Best AI Certifications...
Top 7 High Paying AI Certifications Courses For 2025 | Best AI Certifications...Top 7 High Paying AI Certifications Courses For 2025 | Best AI Certifications...
Top 7 High Paying AI Certifications Courses For 2025 | Best AI Certifications...
Simplilearn
 

Recently uploaded (20)

The Systematic Literature Search - Prof Alison Kinengyere
The Systematic Literature Search - Prof Alison KinengyereThe Systematic Literature Search - Prof Alison Kinengyere
The Systematic Literature Search - Prof Alison Kinengyere
Systematic Reviews Network (SRN)
 
LDMMIA A Free Reiki Yoga workshop session
LDMMIA A Free Reiki Yoga workshop sessionLDMMIA A Free Reiki Yoga workshop session
LDMMIA A Free Reiki Yoga workshop session
LDM Mia eStudios
 
Crude Drugs in D. Pharm Syllabus: A Comprehensive Chart in Pharmacognosy
Crude Drugs in D. Pharm Syllabus: A Comprehensive Chart in PharmacognosyCrude Drugs in D. Pharm Syllabus: A Comprehensive Chart in Pharmacognosy
Crude Drugs in D. Pharm Syllabus: A Comprehensive Chart in Pharmacognosy
PAWAN KUMAR SAHU
 
YSPH VMOC Special Report - Measles Outbreak Southwest US 4-17-2025.pptx
YSPH VMOC Special Report - Measles Outbreak  Southwest US 4-17-2025.pptxYSPH VMOC Special Report - Measles Outbreak  Southwest US 4-17-2025.pptx
YSPH VMOC Special Report - Measles Outbreak Southwest US 4-17-2025.pptx
Yale School of Public Health - The Virtual Medical Operations Center (VMOC)
 
FLAMES QUIZ HINDU QUIZZING CHAMPIONSHIP.pdf
FLAMES QUIZ HINDU QUIZZING CHAMPIONSHIP.pdfFLAMES QUIZ HINDU QUIZZING CHAMPIONSHIP.pdf
FLAMES QUIZ HINDU QUIZZING CHAMPIONSHIP.pdf
MANTHAN THE QUIZZING SOCIETY OF HINDU COLLEGE
 
Pratihar Rulers Upto Mihirbhoja NEP.pptx
Pratihar Rulers Upto Mihirbhoja NEP.pptxPratihar Rulers Upto Mihirbhoja NEP.pptx
Pratihar Rulers Upto Mihirbhoja NEP.pptx
Arya Mahila P. G. College, Banaras Hindu University, Varanasi, India.
 
UNIT-2 606 - B : CROSS CULTURAL RELATIONSHIP - II.pptx
UNIT-2 606 - B : CROSS CULTURAL RELATIONSHIP - II.pptxUNIT-2 606 - B : CROSS CULTURAL RELATIONSHIP - II.pptx
UNIT-2 606 - B : CROSS CULTURAL RELATIONSHIP - II.pptx
MAYURI LONDHE
 
B.Ed. First Year Semester IA. Meaning, Concept, Nature & Scope
B.Ed. First Year Semester IA. Meaning, Concept, Nature & ScopeB.Ed. First Year Semester IA. Meaning, Concept, Nature & Scope
B.Ed. First Year Semester IA. Meaning, Concept, Nature & Scope
ProfDrShaikhImran
 
The Types of Charts in Odoo 18 Spreadsheet
The Types of Charts in Odoo 18 SpreadsheetThe Types of Charts in Odoo 18 Spreadsheet
The Types of Charts in Odoo 18 Spreadsheet
Celine George
 
p4.pdf فن خدمة الاغذية والمشروبات الجزء الرابع
p4.pdf فن خدمة الاغذية والمشروبات الجزء الرابعp4.pdf فن خدمة الاغذية والمشروبات الجزء الرابع
p4.pdf فن خدمة الاغذية والمشروبات الجزء الرابع
HanyAtef10
 
MuleSoft for Agentforce: Unlocking API Catalog & Topic Center | Get Started w...
MuleSoft for Agentforce: Unlocking API Catalog & Topic Center | Get Started w...MuleSoft for Agentforce: Unlocking API Catalog & Topic Center | Get Started w...
MuleSoft for Agentforce: Unlocking API Catalog & Topic Center | Get Started w...
MysoreMuleSoftMeetup
 
Some Common Errors that Generative AI Produces
Some Common Errors that Generative AI ProducesSome Common Errors that Generative AI Produces
Some Common Errors that Generative AI Produces
Damian T. Gordon
 
Some Ethical Considerations of AI and GenAI
Some Ethical Considerations of AI and GenAISome Ethical Considerations of AI and GenAI
Some Ethical Considerations of AI and GenAI
Damian T. Gordon
 
How To Open The Form View Of Many2many Clicking Tag In Odoo 18
How To Open The Form View Of Many2many Clicking Tag In Odoo 18How To Open The Form View Of Many2many Clicking Tag In Odoo 18
How To Open The Form View Of Many2many Clicking Tag In Odoo 18
Celine George
 
606 - B : CROSS CULTURAL RELATIONSHIP - II IMP QUESTIONS.docx
606 - B : CROSS CULTURAL RELATIONSHIP - II IMP QUESTIONS.docx606 - B : CROSS CULTURAL RELATIONSHIP - II IMP QUESTIONS.docx
606 - B : CROSS CULTURAL RELATIONSHIP - II IMP QUESTIONS.docx
MAYURI LONDHE
 
606 - B : CROSS CULTURAL RELATIONSHIP - II.pptx
606 - B : CROSS CULTURAL RELATIONSHIP - II.pptx606 - B : CROSS CULTURAL RELATIONSHIP - II.pptx
606 - B : CROSS CULTURAL RELATIONSHIP - II.pptx
MAYURI LONDHE
 
FINALS INDIA QUIZ MANTHAN HQC 2025 .pdf
FINALS INDIA QUIZ MANTHAN HQC 2025  .pdfFINALS INDIA QUIZ MANTHAN HQC 2025  .pdf
FINALS INDIA QUIZ MANTHAN HQC 2025 .pdf
MANTHAN THE QUIZZING SOCIETY OF HINDU COLLEGE
 
p1.pdf فن الخدمة الجزء الاول د هاني عاطف و د عايده فايز
p1.pdf فن الخدمة الجزء الاول د هاني عاطف و د عايده فايزp1.pdf فن الخدمة الجزء الاول د هاني عاطف و د عايده فايز
p1.pdf فن الخدمة الجزء الاول د هاني عاطف و د عايده فايز
HanyAtef10
 
50 ĐỀ THI THỬ TỐT NGHIỆP THPT 2025 - TỪ CÁC TRƯỜNG CHUYÊN, SỞ GIÁO DỤC CẢ NƯỚ...
50 ĐỀ THI THỬ TỐT NGHIỆP THPT 2025 - TỪ CÁC TRƯỜNG CHUYÊN, SỞ GIÁO DỤC CẢ NƯỚ...50 ĐỀ THI THỬ TỐT NGHIỆP THPT 2025 - TỪ CÁC TRƯỜNG CHUYÊN, SỞ GIÁO DỤC CẢ NƯỚ...
50 ĐỀ THI THỬ TỐT NGHIỆP THPT 2025 - TỪ CÁC TRƯỜNG CHUYÊN, SỞ GIÁO DỤC CẢ NƯỚ...
Nguyen Thanh Tu Collection
 
ĐỀ CƯƠNG HK2 LỚP 7. cuoi ky 2 thcs hoa phu
ĐỀ CƯƠNG HK2 LỚP 7. cuoi ky 2 thcs hoa phuĐỀ CƯƠNG HK2 LỚP 7. cuoi ky 2 thcs hoa phu
ĐỀ CƯƠNG HK2 LỚP 7. cuoi ky 2 thcs hoa phu
NhiLinh58
 
LDMMIA A Free Reiki Yoga workshop session
LDMMIA A Free Reiki Yoga workshop sessionLDMMIA A Free Reiki Yoga workshop session
LDMMIA A Free Reiki Yoga workshop session
LDM Mia eStudios
 
Crude Drugs in D. Pharm Syllabus: A Comprehensive Chart in Pharmacognosy
Crude Drugs in D. Pharm Syllabus: A Comprehensive Chart in PharmacognosyCrude Drugs in D. Pharm Syllabus: A Comprehensive Chart in Pharmacognosy
Crude Drugs in D. Pharm Syllabus: A Comprehensive Chart in Pharmacognosy
PAWAN KUMAR SAHU
 
UNIT-2 606 - B : CROSS CULTURAL RELATIONSHIP - II.pptx
UNIT-2 606 - B : CROSS CULTURAL RELATIONSHIP - II.pptxUNIT-2 606 - B : CROSS CULTURAL RELATIONSHIP - II.pptx
UNIT-2 606 - B : CROSS CULTURAL RELATIONSHIP - II.pptx
MAYURI LONDHE
 
B.Ed. First Year Semester IA. Meaning, Concept, Nature & Scope
B.Ed. First Year Semester IA. Meaning, Concept, Nature & ScopeB.Ed. First Year Semester IA. Meaning, Concept, Nature & Scope
B.Ed. First Year Semester IA. Meaning, Concept, Nature & Scope
ProfDrShaikhImran
 
The Types of Charts in Odoo 18 Spreadsheet
The Types of Charts in Odoo 18 SpreadsheetThe Types of Charts in Odoo 18 Spreadsheet
The Types of Charts in Odoo 18 Spreadsheet
Celine George
 
p4.pdf فن خدمة الاغذية والمشروبات الجزء الرابع
p4.pdf فن خدمة الاغذية والمشروبات الجزء الرابعp4.pdf فن خدمة الاغذية والمشروبات الجزء الرابع
p4.pdf فن خدمة الاغذية والمشروبات الجزء الرابع
HanyAtef10
 
MuleSoft for Agentforce: Unlocking API Catalog & Topic Center | Get Started w...
MuleSoft for Agentforce: Unlocking API Catalog & Topic Center | Get Started w...MuleSoft for Agentforce: Unlocking API Catalog & Topic Center | Get Started w...
MuleSoft for Agentforce: Unlocking API Catalog & Topic Center | Get Started w...
MysoreMuleSoftMeetup
 
Some Common Errors that Generative AI Produces
Some Common Errors that Generative AI ProducesSome Common Errors that Generative AI Produces
Some Common Errors that Generative AI Produces
Damian T. Gordon
 
Some Ethical Considerations of AI and GenAI
Some Ethical Considerations of AI and GenAISome Ethical Considerations of AI and GenAI
Some Ethical Considerations of AI and GenAI
Damian T. Gordon
 
How To Open The Form View Of Many2many Clicking Tag In Odoo 18
How To Open The Form View Of Many2many Clicking Tag In Odoo 18How To Open The Form View Of Many2many Clicking Tag In Odoo 18
How To Open The Form View Of Many2many Clicking Tag In Odoo 18
Celine George
 
606 - B : CROSS CULTURAL RELATIONSHIP - II IMP QUESTIONS.docx
606 - B : CROSS CULTURAL RELATIONSHIP - II IMP QUESTIONS.docx606 - B : CROSS CULTURAL RELATIONSHIP - II IMP QUESTIONS.docx
606 - B : CROSS CULTURAL RELATIONSHIP - II IMP QUESTIONS.docx
MAYURI LONDHE
 
606 - B : CROSS CULTURAL RELATIONSHIP - II.pptx
606 - B : CROSS CULTURAL RELATIONSHIP - II.pptx606 - B : CROSS CULTURAL RELATIONSHIP - II.pptx
606 - B : CROSS CULTURAL RELATIONSHIP - II.pptx
MAYURI LONDHE
 
p1.pdf فن الخدمة الجزء الاول د هاني عاطف و د عايده فايز
p1.pdf فن الخدمة الجزء الاول د هاني عاطف و د عايده فايزp1.pdf فن الخدمة الجزء الاول د هاني عاطف و د عايده فايز
p1.pdf فن الخدمة الجزء الاول د هاني عاطف و د عايده فايز
HanyAtef10
 
50 ĐỀ THI THỬ TỐT NGHIỆP THPT 2025 - TỪ CÁC TRƯỜNG CHUYÊN, SỞ GIÁO DỤC CẢ NƯỚ...
50 ĐỀ THI THỬ TỐT NGHIỆP THPT 2025 - TỪ CÁC TRƯỜNG CHUYÊN, SỞ GIÁO DỤC CẢ NƯỚ...50 ĐỀ THI THỬ TỐT NGHIỆP THPT 2025 - TỪ CÁC TRƯỜNG CHUYÊN, SỞ GIÁO DỤC CẢ NƯỚ...
50 ĐỀ THI THỬ TỐT NGHIỆP THPT 2025 - TỪ CÁC TRƯỜNG CHUYÊN, SỞ GIÁO DỤC CẢ NƯỚ...
Nguyen Thanh Tu Collection
 
ĐỀ CƯƠNG HK2 LỚP 7. cuoi ky 2 thcs hoa phu
ĐỀ CƯƠNG HK2 LỚP 7. cuoi ky 2 thcs hoa phuĐỀ CƯƠNG HK2 LỚP 7. cuoi ky 2 thcs hoa phu
ĐỀ CƯƠNG HK2 LỚP 7. cuoi ky 2 thcs hoa phu
NhiLinh58
 

SQL Query Optimization | SQL Query Optimization Techniques | SQL Basics | SQL Query | Simplilearn

  • 2. Indexes For SQL Query Optimization Indexing in SQL acts as a shortcut that helps the database find and retrieve data much faster.
  • 3. Types Of Indexes Clustered Indexes - Clustered indexes physically sort and store data based on actual column values. Non Clustered Index Clustered Index
  • 4. Types Of Indexes Non Clustered Index Clustered Index Non-Clustered Indexes –They create a separate lookup structure that points to the actual data.
  • 5. Types Of Indexes Full-Text Indexes –Full-text indexes help by storing word positions, making searches much faster instead of scanning the entire text.
  • 6. How Indexing Speeds Up Queries SELECT * FROM customers WHERE name = 'John Doe'; Without an index, the query scans every row: But by creating an index on the ‘name’ column, the query runs much faster. CREATE INDEX idx_customer_name ON customers(name);
  • 7. Avoid SELECT * And Use Necessary Columns When you use **SELECT *** in your queries, you’re asking the database to retrieve every single column, even if you only need a few. SELECT * FROM customers.customer_profiles; SELECT customer_name, customer_email, customer_phone FROM customers.customer_profiles;
  • 8. Optimizing JOIN Operations If you use the wrong type of join, you might end up with duplicate records, slow query performance, or unnecessary data bloating your results. Understanding INNER JOIN, OUTER JOIN, LEFT JOIN, and RIGHT JOIN is crucial for writing efficient queries. Outer Joins – Use with Caution! OUTER JOIN is like keeping everything from both, even the unrelated data. It can create duplicates and bloat your dataset unnecessarily.
  • 9. Optimizing JOIN Operations If you use the wrong type of join, you might end up with duplicate records, slow query performance, or unnecessary data bloating your results. Understanding INNER JOIN, OUTER JOIN, LEFT JOIN, and RIGHT JOIN is crucial for writing efficient queries. INNER JOIN only returns records where there’s a match between both tables. It keeps your dataset clean and ensures you only get the necessary data. Inner Joins – Your Best Friend!
  • 10. Optimizing JOIN Operations If you use the wrong type of join, you might end up with duplicate records, slow query performance, or unnecessary data bloating your results. Understanding INNER JOIN, OUTER JOIN, LEFT JOIN, and RIGHT JOIN is crucial for writing efficient queries. SELECT customer_orders.customer_id, order_details.order_id, order_details.order_date FROM customers.customer_orders customer_orders INNER JOIN orders.order_details order_details ON customer_orders.customer_id = order_details.customer_id AND customer_orders.customer_order_id = order_details.order_id;
  • 11. Optimizing JOIN Operations LEFT JOIN Vs RIGHT JOIN
  • 12. Use EXISTS Instead Of IN When checking if a value exists in a smaller dataset, EXISTS is usually much faster than IN , because EXISTS stops searching as soon as it finds a match, whereas IN scans the entire dataset before deciding—making it slower, especially with large tables. SELECT * FROM orders WHERE customer_id IN (SELECT id FROM customers WHERE active = TRUE); SELECT * FROM orders o WHERE EXISTS (SELECT 1 FROM customers c WHERE c.id = o.customer_id AND c.active = TRUE);
  • 13. Minimize Subqueries & Use CTEs Instead 1️ 1️ ⃣ Subqueries Can Get Messy – Using too many subqueries in SQL makes your code hard to read, debug, and optimize, just like untangling a messy ball of wires. 2️ ⃣ CTEs Make Queries Cleaner – Common Table Expressions (CTEs) break queries into readable sections, making them easier to understand, troubleshoot, and maintain— just like writing clear step-by-step instructions.
  • 14. Minimize Subqueries & Use CTEs Instead SELECT MAX(customer_signup) AS most_recent_signup FROM (SELECT customer_name, customer_phone, customer_signup FROM customer_details WHERE YEAR(customer_signup) = 2023); WITH 2023_signups AS ( SELECT customer_name, customer_phone, customer_signup FROM customer_details WHERE YEAR(customer_signup) = 2023 ), Most_recent_signup AS ( SELECT MAX(customer_signup) AS most_recent_signup FROM 2023_signups )
  • 15. Avoid Queries Inside A Loop When you run SQL queries inside a loop—it makes execution unnecessarily slow and inefficient. Loops like FOR, WHILE, and DO-WHILE run queries one at a time, causing repeated parsing, compiling, and execution, which hurts performance and scalability.
  • 16. FOR EACH customer IN customer_list LOOP UPDATE customers SET status = 'Active' WHERE id = customer.id; END LOOP; UPDATE customers SET status = 'Active' WHERE id IN (SELECT id FROM customer_list); Avoid Queries Inside A Loop
  • 17. Avoid Redundant Data Retrieval The larger the dataset, the slower the query, and the more resources you waste—which can increase costs, especially in cloud-based databases. Select Only the Needed Columns – We’ve already covered why **SELECT *** is inefficient, but it’s also important to limit the number of rows returned, not just columns. Use LIMIT to Control Output – Queries that return thousands or millions of rows can drag down performance, so always limit the results when needed.
  • 18. Avoid Redundant Data Retrieval SELECT customer_name FROM customer_details ORDER BY customer_signup DESC LIMIT 100; Instead of pulling all customers, retrieve only the latest 100 signups: SELECT customer_name FROM customer_details ORDER BY customer_signup DESC LIMIT 100 OFFSET 20; Want to skip the first 20 rows and get the next 100? Use OFFSET:
  • 19. How to Create A Stored Procedure? Let’s say we want to find the most recent customer signup. Instead of writing the query every time, we can save it as a stored procedure: CREATE PROCEDURE find_most_recent_customer AS BEGIN SELECT MAX(customer_signup) AS most_recent_signup FROM 2023_signups; END; EXEC find_most_recent_customer;
  • 20. How to Create A Stored Procedure? Adding Parameters for Flexibility CREATE PROCEDURE find_most_recent_customer @store_id INT AS BEGIN SELECT MAX(customer_signup) AS most_recent_signup FROM 2023_signups WHERE store_id = @store_id; END; EXEC find_most_recent_customer @store_id = 1188;
  • 21. Normalize Your Database Tables 1. First Normal Form (1NF) – No Nested Data!
  • 22. Normalize Your Database Tables 2. Second Normal Form (2NF) – Split Multi-Value Fields
  • 23. Normalize Your Database Tables 3. Third Normal Form (3NF) – Remove Column Dependencies
  • 24. Monitoring Query Performance When you run SQL queries without monitoring their performance—you have no idea which queries are slowing down your database or costing you extra resources. Identifies Slow Queries Saves Costs in Cloud Databases Prevents System Slowdowns
  • 25. Monitoring Query Performance Tools for Monitoring Query Performance Query Profiling Query Execution Plans Database Logs & Server Monitoring
  • 26. Monitoring Query Performance Let’s say you have a slow query, and you want to analyze its performance. You can use MySQL’s EXPLAIN statement: EXPLAIN SELECT customer_name FROM customers WHERE city = 'New York';
  • 27. Use UNION ALL Instead Of UNION If you use UNION, you’re double-checking every name to remove duplicates before finalizing the list. But if you use UNION ALL, you simply combine both lists as they are—faster, simpler, and without extra processing. UNION – Combines results from two tables and removes duplicates (which takes extra processing time). UNION ALL – Combines results without checking for duplicates, making it faster and more efficient.
  • 28. Use UNION ALL Instead Of UNION SELECT customer_name FROM customers_2023 UNION SELECT customer_name FROM customers_2024; SELECT customer_name FROM customers_2023 UNION ALL SELECT customer_name FROM customers_2024;
  • 29. Leverage Cloud Database-Specific Features Cloud providers like Snowflake, BigQuery, and Redshift come with built-in optimizations and custom SQL functions designed to improve performance, simplify queries, and handle data more efficiently. Built-in Functions Optimized Performance Cost Efficiency
  • 30. Leverage Cloud Database-Specific Features Example: Snowflake’s JSON Functions SELECT json_column:customer.name::STRING AS customer_name, json_column:customer.email::STRING AS customer_email FROM orders;