Query Optimization

SQL · Extra Concepts

Theory

Query optimization means making a query return the same result while doing less work: reading fewer rows, fewer columns and less data from disk or the network. Most gains come from a few habits rather than clever tricks.

First, keep filters sargable (search-argument-able): write WHERE hire_date >= '2024-01-01' AND hire_date < '2025-01-01' instead of WHERE YEAR(hire_date) = 2024, because wrapping the column in a function hides it from the index. Second, select only the columns you need instead of SELECT *, which matters a lot in columnar warehouses. Third, filter early, before joins and aggregations, so later steps handle fewer rows. Fourth, add indexes on the columns used in filters and joins, and check the execution plan to confirm they are used. Other common fixes: avoid leading wildcards like LIKE '%kumar', and use UNION ALL instead of UNION when duplicates are impossible, to skip a costly de-duplication step.

Why we use it

Slow queries waste time, block other users and, in cloud warehouses, cost money for every byte or second used. Optimizing gives the same answer faster and cheaper.

When to use it

Optimize queries that run often, run on large tables or feed dashboards. Measure first with the execution plan, change one thing at a time and re-measure. Do not spend effort tuning a one-off query that already runs in seconds.

How it works

  1. A query is too slow
  2. Read the execution plan to find the costliest step
  3. Function on a filtered column? Yes: Rewrite as a sargable range. No: Check filter/join indexes.
  4. Select only needed columns; filter before joins
  5. Re-run the plan and compare timings
  6. Keep the change if the result is identical and faster

Syntax

-- techniques, not a single statement

Example

-- Instead of a full scan with a function on the column:
-- WHERE YEAR(hire_date) = 2024
-- Prefer a sargable range so an index can be used:
SELECT * FROM employees
WHERE hire_date >= '2024-01-01' AND hire_date < '2025-01-01';

Key points

SQL topics

Introduction to SQL

Querying Data

Sorting Rows

Limiting Rows

Filtering Rows

Joining Multiple Tables

Grouping Rows

SET Operators

Working with Tables

Modifying Data

Views & Triggers

Conditional Expressions

SQL Subqueries

Aggregate Functions

Window Functions

String Functions

Date Functions

Math Functions

Extra Concepts

T
Tech Town
Log InSign Up Free
Home SQL Python DSA Power BI Snowflake Databricks Spark Gen AI Practice Problems Contest Blog

Master SQL from Zero to Pro

All SQL topics in one place. Click any topic in the sidebar — content opens right here without leaving the page.

{{ sectionCount }}
Sections
{{ topicCount }}
Topics
Free
Forever
Hands-on practice Practice SQL Online Solve 150 SQL interview problems in a live editor with instant checking. Start practising →
Section {{ sec.num }}

{{ sec.label }}

SQL  /  {{ current.sectionLabel }}  /  {{ current.name }}

{{ current.name }}

Theory

{{ current.def }}

Why we use it

{{ rich.why }}

When to use it

{{ rich.when }}

Diagram

{{ rich.dg.caption }}

{{ tb.title }}
{{ c.v }}
{{ rich.dg.op }}
➜
{{ rich.dg.output.title }}
{{ c.v }}
{{ nd.label }}
{{ nd.sub }}
{{ nd.label }}
{{ nd.sub }}
{{ rich.dg.left.title }}
{{ it }}
{{ rich.dg.right.title }}
{{ it }}
Flowchart — how it works
{{ nd.text }}
{{ nd.text }}
◇ {{ nd.text }}
Yes → {{ nd.yes }}
No → {{ nd.no }}
Syntax
{{ current.syntax }}
Example
{{ current.example }}
▶ Try It Live — runs against a real sample database
{{ liveError }}
{{ col }}
{{ cell }}
Key Points — click to check off as you learn
  • {{ pt.mark }} {{ pt.text }}

Share this topic