Tips 1–7: Fetching & Filtering
The first 7 of the 30 performance tips: select only what you need, avoid needless DISTINCT/ORDER BY, explore with TOP, and write index-friendly WHERE clauses.
This is the start of the 30 SQL Performance Tips checklist. Every tip comes with a ❌ bad and ✅ good IPL example. But first, the golden rule that applies to all 30: always test using the execution plan — never guess. The same query can run 1000× faster after a small change, or not at all faster — only measurement tells you.
Always test: the 4-step loop
| Step | What you do |
|---|---|
| 1. Write the query | your original SQL |
| 2. Read the plan | scan? seek? nested loop? sort? |
| 3. Apply a tip | index, rewrite, filter early |
| 4. Compare plans | is it really faster? measure plan, reads and time before and after |
The 30 tips at a glance
| Category | Tips |
|---|---|
| Fetching data | #1 – #3 |
| Filtering data | #4 – #7 |
| Joining data | #8 – #16 |
| Aggregating data | #17 – #18 |
| Subqueries | #19 – #20 |
| DDL (table design) | #21 – #25 |
| Indexing | #26 – #30 |
Tip 1 — Select only what you need
Avoid
SELECT *— fetch only the columns you really use.
sql
-- ❌ Bad
SELECT * FROM players;
-- ✅ Good
SELECT name, runs FROM players;Why: fewer columns = less data read, less memory, less network traffic. It also lets a narrow index cover the query (all needed columns are in the index, so no lookup to the table is needed). And your code won't break when someone adds a new column.
Tip 2 — Avoid DISTINCT and ORDER BY unless necessary
Use
DISTINCTandORDER BYonly when absolutely necessary — both force a sort (or hash), which slows the query down.
sql
-- ❌ Bad: de-duplicating and sorting "just in case"
SELECT DISTINCT * FROM players ORDER BY name;
-- ✅ Good: sort / de-duplicate only if the result really needs it
SELECT name, team FROM players;The playground plan for SELECT DISTINCT team FROM players ORDER BY team shows an extra line USE TEMP B-TREE FOR DISTINCT — that's the cost you pay. DISTINCT on a primary-key column (or on SELECT * of a table with a PK) is pure waste: rows are already unique.
Tip 3 — Explore with TOP
Limit the rows while exploring a table, to avoid fetching data you don't need.
sql
-- ❌ Bad: pulls millions of rows just to "have a look"
SELECT * FROM innings;
-- ✅ Good
SELECT TOP 10 * FROM innings;Note:
TOPwithoutORDER BYreturns any 10 rows — fine for exploring, wrong for "top 10 scorers". MySQL/PostgreSQL/SQLite useLIMIT 10at the end instead.
Tip 4 — Index your WHERE columns
Create non-clustered indexes on columns that are frequently used in WHERE.
sql
-- ❌ Bad: WHERE team = 'MI' scans the whole table every time
SELECT name FROM players WHERE team = 'MI';
-- ✅ Good
CREATE INDEX IX_team ON players (team);Tip 5 — No functions on columns in WHERE
Functions like
UPPER(),YEAR(),LEFT(),ISNULL()wrapped around a column inWHEREblock index usage.
sql
-- ❌ Bad: the function must run on every row -> scan
SELECT name FROM player_profiles WHERE YEAR(debut) = 2008;
-- ✅ Good: compare the raw column with a range -> seek
SELECT name FROM player_profiles
WHERE debut >= '2008-01-01' AND debut < '2009-01-01';Verified in the playground with an index on debut:
| Query | Plan |
|---|---|
WHERE YEAR(debut) = 2008 | SCAN player_profiles |
WHERE debut >= '2008-01-01' AND debut < '2009-01-01' | SEARCH player_profiles USING INDEX IX_debut (debut>? AND debut<?) |
Both return the same 3 players (Dhoni, Kohli, Rohit). A condition that can use an index is called SARGable (Search ARGument-able): keep the column alone on one side of the operator.
Tip 6 — No leading wildcard
A
LIKEpattern that starts with % cannot use an index (the B-Tree is sorted from the first character).
sql
-- ❌ Bad: must check every name
SELECT name FROM players WHERE name LIKE '%Kohli';
-- ✅ Good: known prefix -> SQL Server can seek the name index
SELECT name FROM players WHERE name LIKE 'Virat%';Think of a phone book: finding everyone whose name starts with "Vir" is easy; finding everyone whose name ends with "ohli" means reading the whole book. (For heavy "contains" searches, SQL Server offers full-text indexes.)
Tip 7 — IN instead of many ORs
Use
INinstead of a long chain ofORconditions on the same column — better readability and performance.
sql
-- ❌ Bad
SELECT name, team FROM players
WHERE team = 'MI' OR team = 'CSK' OR team = 'RCB';
-- ✅ Good
SELECT name, team FROM players
WHERE team IN ('MI', 'CSK', 'RCB');| name | team |
|---|---|
| Virat Kohli | RCB |
| Rohit Sharma | MI |
| MS Dhoni | CSK |
| Jasprit Bumrah | MI |
Same result. The optimizer often treats both forms the same way, so IN is never worse — and it is much easier to read and maintain.
Common mistakes
- ❌
WHERE ISNULL(team, '') = 'MI'✅WHERE team = 'MI'(NULL never equals 'MI' anyway). - ❌
WHERE runs + 100 > 600✅WHERE runs > 500— move the arithmetic to the constant side. - ❌
SELECT TOP 3 name FROM playersfor "top 3 scorers" ✅ addORDER BY runs DESC.
Interview tip
Q: What does "SARGable" mean?
A: A predicate that can use an index seek — the indexed column appears alone, without a function or calculation around it, e.g. debut >= '2008-01-01' instead of YEAR(debut) = 2008, or LIKE 'Vir%' instead of LIKE '%ohli'.
Key points to remember
- Always test with the execution plan: write → read plan → apply a tip → compare plans, reads and time.
- Tip 1: avoid SELECT * — select only the columns you use.
- Tip 2: use DISTINCT and ORDER BY only when needed — both add a sort/hash step.
- Tip 3: explore big tables with TOP n (LIMIT n in MySQL/PostgreSQL).
- Tip 4: create non-clustered indexes on columns often used in WHERE.
- Tip 5: never wrap a filtered column in a function (YEAR(), UPPER()) — use a SARGable range instead.
- Tip 6: avoid leading wildcards (LIKE '%x'); a known prefix (LIKE 'x%') can use an index.
- Tip 7: use IN instead of many ORs on the same column.
Try it yourself
Tip 5: function on column vs SARGable range
Runs on a fresh IPL practice database in your browser · edit the query and press Ctrl + Enter
Tips 1 & 3: only needed columns, only a few rows
Runs on a fresh IPL practice database in your browser · edit the query and press Ctrl + Enter
Tip 7: IN instead of OR chains
Runs on a fresh IPL practice database in your browser · edit the query and press Ctrl + Enter
Practice: make the filter SARGable
Runs on a fresh IPL practice database in your browser · edit the query and press Ctrl + Enter
Revision Flashcards
Tap a card to flip it and see the answer.
