How to select several hardcoded SQL rows?
Master System Design with Codemia
Enhance your system design skills with over 120 practice problems, detailed solutions, and hands-on exercises.
Introduction
Sometimes you need to return a small fixed set of rows without reading a full table scan. This happens in quick reports, feature flags, demo datasets, or temporary migration scripts. SQL provides multiple clean ways to express hardcoded row selection, and the best choice depends on readability and whether you need to join that data with real tables.
Using IN for Existing Table Rows
If the rows already exist in a table and you know the keys, IN is usually the shortest and clearest option.
This is easy to read and works well for short lists. For larger lists, maintainability drops and query plans can become less predictable depending on the database engine.
For string keys:
Keep list size reasonable and consider a temporary table if the list grows.
Building Hardcoded Rows With VALUES
When the rows are synthetic and not stored yet, VALUES is a strong pattern. It creates an inline table that can be selected directly or joined.
This pattern keeps columns aligned and avoids repeated SELECT statements. It is especially useful in migration scripts where you seed a small lookup set.
You can also join it to real data:
UNION ALL for Cross Database Compatibility
Some engines have syntax differences around VALUES placement. UNION ALL is widely supported and explicit.
This is verbose for larger sets but works almost everywhere. Use it when you need portable SQL across multiple vendors.
Choosing the Right Pattern
Use IN when filtering existing table rows by key. Use VALUES when generating inline records for joins or expected result sets. Use UNION ALL when dialect constraints make VALUES awkward.
In production code, prioritize clarity over micro optimization for small hardcoded sets. Keep the intent obvious so later maintainers know whether values are test data, business constants, or migration artifacts.
Parameterized Alternatives in Application Code
If hardcoded values come from user choices, do not build SQL text by string concatenation. Use parameterized queries and pass values through the database driver. For engines that support table valued parameters or temporary tables, this pattern remains safe and scales better than expanding long literal lists.
In migration tooling, keep inline rows near the migration step that uses them and add comments describing business meaning. That way reviewers can distinguish temporary patch data from long term reference data.
Common Pitfalls
- Hardcoding long lists in application strings: this harms readability and can hit query length limits.
- Mixing data types in inline rows: implicit casts can degrade performance or fail unexpectedly.
- Forgetting aliases for
VALUEScolumns: many engines require explicit column names. - Using
UNIONinstead ofUNION ALL: duplicate elimination adds unnecessary work. - Keeping temporary hardcoded rows in permanent code paths: stale values become hidden bugs.
Summary
- Use
INfor short key filters on existing tables. - Use inline
VALUESwhen you need a small derived table. - Use
UNION ALLfor broad SQL dialect compatibility. - Name inline columns clearly to avoid type and readability issues.
- Promote large or long lived hardcoded sets into real tables.

