SlideShare a Scribd company logo
8/10/13 SQL QueryTips or QueryOptimization
beginner-sql-tutorial.com/sql-tutorial-tips.htm 1/2
Beginner SQL Tutorial Tips
Here is a few SQL Tips that may save you time and trouble with your SQL table work, SQL command statements
...
Ideally, you want to get best results when writing SQL queries, minimizing errors and getting the best query
performance when needed. Here is a small list of queries tips that may help you in your SQL queries work and
that can optimized SQL for better performance.
SQL Tutorial Tips:
1) SELECT only the columns needed, avoid using SELECT *. First, for each column that you do not need every
SQL Server performs additional work to retrieve and return them to the client, and the second volume of data
exchanged between the client and SQL Server increases unnecessary.
2) SELECT only the rows needed. The less rows retrieved, the faster the query will run.
3) Prune SELECT lists. Every column that is SELECTed consumes resources for processing. There are several
areas that can be examined to determine if column selection is really necessary.
Example: WHERE (COL8 = ‘X’)
If a SELECT contains a predicate where a column is equal to one value, that column should not have to be
retrieved for each row, the value will always be ‘X’.
4) When you create a new table always create a unique clustered index belong to it, possibly it is a numeric
type.
5) Use JOIN instead of subqueries. As a programmer, subqueries are something that you can be tempted to use
and abuse. Subqueries, as show below, can be very useful:
SELECT a.id,
(SELECT MAX(created)
FROM posts
WHERE author_id = a.id)
AS latest_post FROM authors a
8/10/13 SQL QueryTips or QueryOptimization
beginner-sql-tutorial.com/sql-tutorial-tips.htm 2/2
Although subqueries are useful, they often can be replaced by a join, which is definitely faster to execute.
SELECT a.id, MAX(p.created) AS latest_post
FROM authors a
INNER JOIN posts p
ON (a.id = p.author_id)
GROUP BY a.id
6)

More Related Content

Similar to Sql query tips or query optimization (20)

Advanced MySQL Query Optimizations
Advanced MySQL Query Optimizations
Dave Stokes
 
Database development coding standards
Database development coding standards
Alessandro Baratella
 
Oracle notes
Oracle notes
Prashant Dadmode
 
02 database oprimization - improving sql performance - ent-db
02 database oprimization - improving sql performance - ent-db
uncleRhyme
 
Guide To Mastering The MySQL Query Execution Plan
Guide To Mastering The MySQL Query Execution Plan
Optimiz DBA
 
Application sql issues_and_tuning
Application sql issues_and_tuning
Anil Pandey
 
Myth busters - performance tuning 102 2008
Myth busters - performance tuning 102 2008
paulguerin
 
SQL -Beginner To Intermediate Level.pdf
SQL -Beginner To Intermediate Level.pdf
DraguClaudiu
 
SQL Database Performance Tuning for Developers
SQL Database Performance Tuning for Developers
BRIJESH KUMAR
 
Access tips access and sql part 1 setting the sql scene
Access tips access and sql part 1 setting the sql scene
quest2900
 
MySQL Query And Index Tuning
MySQL Query And Index Tuning
Manikanda kumar
 
Ebook8
Ebook8
kaashiv1
 
Sql interview question part 8
Sql interview question part 8
kaashiv1
 
Ebook7
Ebook7
kaashiv1
 
Sql interview question part 7
Sql interview question part 7
kaashiv1
 
SQL – The Natural Language for Analysis - Oracle - Whitepaper - 2431343
SQL – The Natural Language for Analysis - Oracle - Whitepaper - 2431343
Edgar Alejandro Villegas
 
SQL Tunning
SQL Tunning
Dhananjay Goel
 
MSSQL_Book.pdf
MSSQL_Book.pdf
DubsmashTamizhan
 
Mysql Optimization
Mysql Optimization
KLabCyscorpions-TechBlog
 
Sq lite
Sq lite
Revuru Bharadwaja
 
Advanced MySQL Query Optimizations
Advanced MySQL Query Optimizations
Dave Stokes
 
Database development coding standards
Database development coding standards
Alessandro Baratella
 
02 database oprimization - improving sql performance - ent-db
02 database oprimization - improving sql performance - ent-db
uncleRhyme
 
Guide To Mastering The MySQL Query Execution Plan
Guide To Mastering The MySQL Query Execution Plan
Optimiz DBA
 
Application sql issues_and_tuning
Application sql issues_and_tuning
Anil Pandey
 
Myth busters - performance tuning 102 2008
Myth busters - performance tuning 102 2008
paulguerin
 
SQL -Beginner To Intermediate Level.pdf
SQL -Beginner To Intermediate Level.pdf
DraguClaudiu
 
SQL Database Performance Tuning for Developers
SQL Database Performance Tuning for Developers
BRIJESH KUMAR
 
Access tips access and sql part 1 setting the sql scene
Access tips access and sql part 1 setting the sql scene
quest2900
 
MySQL Query And Index Tuning
MySQL Query And Index Tuning
Manikanda kumar
 
Sql interview question part 8
Sql interview question part 8
kaashiv1
 
Sql interview question part 7
Sql interview question part 7
kaashiv1
 
SQL – The Natural Language for Analysis - Oracle - Whitepaper - 2431343
SQL – The Natural Language for Analysis - Oracle - Whitepaper - 2431343
Edgar Alejandro Villegas
 

More from Vivek Singh (20)

C programming session 14
C programming session 14
Vivek Singh
 
C programming session 13
C programming session 13
Vivek Singh
 
C programming session 11
C programming session 11
Vivek Singh
 
C programming session 10
C programming session 10
Vivek Singh
 
C programming session 08
C programming session 08
Vivek Singh
 
C programming session 07
C programming session 07
Vivek Singh
 
C programming session 05
C programming session 05
Vivek Singh
 
C programming session 04
C programming session 04
Vivek Singh
 
C programming session 02
C programming session 02
Vivek Singh
 
C programming session 01
C programming session 01
Vivek Singh
 
C programming session 16
C programming session 16
Vivek Singh
 
Niit aptitude question paper
Niit aptitude question paper
Vivek Singh
 
Excel shortcut and tips
Excel shortcut and tips
Vivek Singh
 
Sql where clause
Sql where clause
Vivek Singh
 
Sql update statement
Sql update statement
Vivek Singh
 
Sql tutorial, tutorials sql
Sql tutorial, tutorials sql
Vivek Singh
 
Sql subquery
Sql subquery
Vivek Singh
 
Sql select statement
Sql select statement
Vivek Singh
 
Sql rename
Sql rename
Vivek Singh
 
Sql order by clause
Sql order by clause
Vivek Singh
 
C programming session 14
C programming session 14
Vivek Singh
 
C programming session 13
C programming session 13
Vivek Singh
 
C programming session 11
C programming session 11
Vivek Singh
 
C programming session 10
C programming session 10
Vivek Singh
 
C programming session 08
C programming session 08
Vivek Singh
 
C programming session 07
C programming session 07
Vivek Singh
 
C programming session 05
C programming session 05
Vivek Singh
 
C programming session 04
C programming session 04
Vivek Singh
 
C programming session 02
C programming session 02
Vivek Singh
 
C programming session 01
C programming session 01
Vivek Singh
 
C programming session 16
C programming session 16
Vivek Singh
 
Niit aptitude question paper
Niit aptitude question paper
Vivek Singh
 
Excel shortcut and tips
Excel shortcut and tips
Vivek Singh
 
Sql where clause
Sql where clause
Vivek Singh
 
Sql update statement
Sql update statement
Vivek Singh
 
Sql tutorial, tutorials sql
Sql tutorial, tutorials sql
Vivek Singh
 
Sql select statement
Sql select statement
Vivek Singh
 
Sql order by clause
Sql order by clause
Vivek Singh
 
Ad

Recently uploaded (20)

How to Manage & Create a New Department in Odoo 18 Employee
How to Manage & Create a New Department in Odoo 18 Employee
Celine George
 
Webcrawler_Mule_AIChain_MuleSoft_Meetup_Hyderabad
Webcrawler_Mule_AIChain_MuleSoft_Meetup_Hyderabad
Veera Pallapu
 
MATERI PPT TOPIK 4 LANDASAN FILOSOFIS PENDIDIKAN
MATERI PPT TOPIK 4 LANDASAN FILOSOFIS PENDIDIKAN
aditya23173
 
Allomorps and word formation.pptx - Google Slides.pdf
Allomorps and word formation.pptx - Google Slides.pdf
Abha Pandey
 
The Man In The Back – Exceptional Delaware.pdf
The Man In The Back – Exceptional Delaware.pdf
dennisongomezk
 
Vikas Bansal Himachal Pradesh: A Visionary Transforming Himachal’s Educationa...
Vikas Bansal Himachal Pradesh: A Visionary Transforming Himachal’s Educationa...
Himalayan Group of Professional Institutions (HGPI)
 
Final Sketch Designs for poster production.pptx
Final Sketch Designs for poster production.pptx
bobby205207
 
BUSINESS QUIZ PRELIMS | QUIZ CLUB OF PSGCAS | 9 SEPTEMBER 2024
BUSINESS QUIZ PRELIMS | QUIZ CLUB OF PSGCAS | 9 SEPTEMBER 2024
Quiz Club of PSG College of Arts & Science
 
Overview of Off Boarding in Odoo 18 Employees
Overview of Off Boarding in Odoo 18 Employees
Celine George
 
MATERI PPT TOPIK 1 LANDASAN FILOSOFIS PENDIDIKAN
MATERI PPT TOPIK 1 LANDASAN FILOSOFIS PENDIDIKAN
aditya23173
 
SPENT QUIZ NQL JR FEST 5.0 BY SOURAV.pptx
SPENT QUIZ NQL JR FEST 5.0 BY SOURAV.pptx
Sourav Kr Podder
 
Revista digital preescolar en transformación
Revista digital preescolar en transformación
guerragallardo26
 
How to Create an Event in Odoo 18 - Odoo 18 Slides
How to Create an Event in Odoo 18 - Odoo 18 Slides
Celine George
 
Overview of Employee in Odoo 18 - Odoo Slides
Overview of Employee in Odoo 18 - Odoo Slides
Celine George
 
Exploring Ocean Floor Features for Middle School
Exploring Ocean Floor Features for Middle School
Marie
 
Unit 3 Poster Sketches with annotations.pptx
Unit 3 Poster Sketches with annotations.pptx
bobby205207
 
FEBA Sofia Univercity final diplian v3 GSDG 5.2025.pdf
FEBA Sofia Univercity final diplian v3 GSDG 5.2025.pdf
ChristinaFortunova
 
Battle of Bookworms 2025 - U25 Literature Quiz by Pragya
Battle of Bookworms 2025 - U25 Literature Quiz by Pragya
Pragya - UEM Kolkata Quiz Club
 
IDF 30min presentation - December 2, 2024.pptx
IDF 30min presentation - December 2, 2024.pptx
ArneeAgligar
 
How to Manage Upselling of Subscriptions in Odoo 18
How to Manage Upselling of Subscriptions in Odoo 18
Celine George
 
How to Manage & Create a New Department in Odoo 18 Employee
How to Manage & Create a New Department in Odoo 18 Employee
Celine George
 
Webcrawler_Mule_AIChain_MuleSoft_Meetup_Hyderabad
Webcrawler_Mule_AIChain_MuleSoft_Meetup_Hyderabad
Veera Pallapu
 
MATERI PPT TOPIK 4 LANDASAN FILOSOFIS PENDIDIKAN
MATERI PPT TOPIK 4 LANDASAN FILOSOFIS PENDIDIKAN
aditya23173
 
Allomorps and word formation.pptx - Google Slides.pdf
Allomorps and word formation.pptx - Google Slides.pdf
Abha Pandey
 
The Man In The Back – Exceptional Delaware.pdf
The Man In The Back – Exceptional Delaware.pdf
dennisongomezk
 
Final Sketch Designs for poster production.pptx
Final Sketch Designs for poster production.pptx
bobby205207
 
Overview of Off Boarding in Odoo 18 Employees
Overview of Off Boarding in Odoo 18 Employees
Celine George
 
MATERI PPT TOPIK 1 LANDASAN FILOSOFIS PENDIDIKAN
MATERI PPT TOPIK 1 LANDASAN FILOSOFIS PENDIDIKAN
aditya23173
 
SPENT QUIZ NQL JR FEST 5.0 BY SOURAV.pptx
SPENT QUIZ NQL JR FEST 5.0 BY SOURAV.pptx
Sourav Kr Podder
 
Revista digital preescolar en transformación
Revista digital preescolar en transformación
guerragallardo26
 
How to Create an Event in Odoo 18 - Odoo 18 Slides
How to Create an Event in Odoo 18 - Odoo 18 Slides
Celine George
 
Overview of Employee in Odoo 18 - Odoo Slides
Overview of Employee in Odoo 18 - Odoo Slides
Celine George
 
Exploring Ocean Floor Features for Middle School
Exploring Ocean Floor Features for Middle School
Marie
 
Unit 3 Poster Sketches with annotations.pptx
Unit 3 Poster Sketches with annotations.pptx
bobby205207
 
FEBA Sofia Univercity final diplian v3 GSDG 5.2025.pdf
FEBA Sofia Univercity final diplian v3 GSDG 5.2025.pdf
ChristinaFortunova
 
Battle of Bookworms 2025 - U25 Literature Quiz by Pragya
Battle of Bookworms 2025 - U25 Literature Quiz by Pragya
Pragya - UEM Kolkata Quiz Club
 
IDF 30min presentation - December 2, 2024.pptx
IDF 30min presentation - December 2, 2024.pptx
ArneeAgligar
 
How to Manage Upselling of Subscriptions in Odoo 18
How to Manage Upselling of Subscriptions in Odoo 18
Celine George
 
Ad

Sql query tips or query optimization

  • 1. 8/10/13 SQL QueryTips or QueryOptimization beginner-sql-tutorial.com/sql-tutorial-tips.htm 1/2 Beginner SQL Tutorial Tips Here is a few SQL Tips that may save you time and trouble with your SQL table work, SQL command statements ... Ideally, you want to get best results when writing SQL queries, minimizing errors and getting the best query performance when needed. Here is a small list of queries tips that may help you in your SQL queries work and that can optimized SQL for better performance. SQL Tutorial Tips: 1) SELECT only the columns needed, avoid using SELECT *. First, for each column that you do not need every SQL Server performs additional work to retrieve and return them to the client, and the second volume of data exchanged between the client and SQL Server increases unnecessary. 2) SELECT only the rows needed. The less rows retrieved, the faster the query will run. 3) Prune SELECT lists. Every column that is SELECTed consumes resources for processing. There are several areas that can be examined to determine if column selection is really necessary. Example: WHERE (COL8 = ‘X’) If a SELECT contains a predicate where a column is equal to one value, that column should not have to be retrieved for each row, the value will always be ‘X’. 4) When you create a new table always create a unique clustered index belong to it, possibly it is a numeric type. 5) Use JOIN instead of subqueries. As a programmer, subqueries are something that you can be tempted to use and abuse. Subqueries, as show below, can be very useful: SELECT a.id, (SELECT MAX(created) FROM posts WHERE author_id = a.id) AS latest_post FROM authors a
  • 2. 8/10/13 SQL QueryTips or QueryOptimization beginner-sql-tutorial.com/sql-tutorial-tips.htm 2/2 Although subqueries are useful, they often can be replaced by a join, which is definitely faster to execute. SELECT a.id, MAX(p.created) AS latest_post FROM authors a INNER JOIN posts p ON (a.id = p.author_id) GROUP BY a.id 6)