Join our Newsletter — 33% off our NHI Course
Home› Glossary› Cyber Security› Raw SQL
Cyber Security

Raw SQL

← Back to Glossary
By NHI Mgmt Group Updated October 31, 2026 Domain: Cyber Security

Raw SQL is a direct, hand-written database query used when an ORM cannot express a needed operation. It offers flexibility, but it also places security responsibility on the developer. Safe use requires a static query string and explicit parameter handling for every dynamic value.

What Raw SQL Changes in Practice

Raw SQL is different from ORM-generated access because the developer is now responsible for the query shape, the data flow into that query, and the database behaviour that results. That makes it useful for edge cases such as complex joins, vendor-specific functions, performance tuning, or bulk operations that an ORM handles poorly, but it also removes a layer of safety that many teams rely on by default.

The practical consequence is that correctness and security become inseparable. A raw query can be perfectly valid and still be unsafe if application data is concatenated into the SQL text, if dynamic identifiers are not constrained, or if business logic assumes the ORM would have handled escaping or parameterization automatically.

Why Raw SQL Is Powerful and Fragile

Raw SQL gives direct access to the database engine, which means the query can express exactly what the application needs. That flexibility is why developers reach for it when the ORM abstraction becomes too restrictive. The same flexibility also makes it easier to create injection paths, leak assumptions about schema structure, or bypass application-level checks that were previously enforced by ORM conventions.

In security terms, raw SQL is a trust boundary shift. The code is no longer asking the ORM to mediate the query construction, so every dynamic value, identifier, or clause becomes part of the attack surface. The safer pattern is to keep the statement text static and treat all variable input as data, not SQL syntax.

Where Raw SQL Most Often Goes Wrong

The most common failures are not exotic. They usually involve string concatenation, interpolated filters, dynamic ORDER BY or table names, and ad hoc query building that mixes trusted and untrusted content. ORM bypass also creates inconsistency, where one code path is protected by parameter binding and another identical business action is implemented as raw text with weaker handling.

Unsafe raw SQL is especially risky when it is used deep in shared libraries or helper functions, because the vulnerable pattern can spread across many call sites. That is why reviews should focus on whether the query text is fixed, whether the database driver is using true parameter binding, and whether any dynamic SQL fragments are tightly constrained to known-safe values.

For developers working in ORM-heavy stacks, the ORM Security Guide is useful because it covers the unsafe raw query patterns that appear when teams step outside the ORM’s normal guardrails.

Secure Use and Review Expectations

Raw SQL is not inherently insecure, but it should be treated as a deliberate exception with a higher review bar. That usually means static query templates, explicit parameter binding, and careful handling of dynamic identifiers rather than dynamic values being inserted directly into the SQL string. Reviewers should also look for places where the query might expose more rows or columns than the ORM path did by default.

When the application uses a framework such as Django, raw SQL should be reviewed alongside the ORM and deployment guidance because safe use is part of the wider data-access model, not an isolated coding trick. The Django Security Guide is relevant here because it discusses both safe ORM and raw SQL use in the broader secure deployment context.

Where raw SQL is necessary, the right question is not whether it is “allowed”, but whether the team can still prove the query is parameterized, minimal, and reviewed like any other security-sensitive code path.

Risk and Threat Considerations

Raw SQL creates a direct injection and privilege-abuse surface because the application is now assembling executable database syntax by hand. If untrusted input reaches the SQL text, an attacker may be able to alter filters, extract data, or change the intended query logic.

Failure mechanism: Developers treat user-controlled values as string fragments instead of bound parameters, or they allow dynamic SQL elements such as identifiers and clauses without strict allowlisting.

Impact: The result can be data disclosure, unauthorized modification, query escalation, or a wider compromise of application trust boundaries if the database account has more privilege than the code actually needs.

Standards & Framework Alignment

This section maps relevant standards and security frameworks to the operational risks and controls described in this guidance.

OWASP ASVS and NIST SP 800-53 Rev 5 set the technical controls, while ISO/IEC 27001:2022 defines the regulatory obligations.

FrameworkControl / ReferenceRelevance
OWASP ASVSV8 — AuthorizationRaw SQL can bypass intended access checks if query logic is manually constructed.
V4 — API and Web ServiceRaw SQL often sits behind request handling where injected inputs reach database calls.
Recommendation — Validate authorization for every query path that can reach sensitive rows or actions. Enforce parameterized request handling before inputs reach database queries.
NIST SP 800-53 Rev 5SI-10 — Information Input ValidationRaw SQL safety depends on strict validation of dynamic values before query execution.
AC-6 — Least PrivilegeRaw SQL risks are amplified when the database account can do more than the operation needs.
Recommendation — Validate input before it is incorporated into any SQL statement. Constrain database accounts to the minimum privileges required by the query.
ISO/IEC 27001:2022A.8.28 — Secure codingRaw SQL is a coding practice that requires secure implementation and review controls.
Recommendation — Apply secure coding rules to all manually written database queries.

Practitioner Guidance

What to watch for: Any raw query that uses concatenation, interpolation, or helper code that builds SQL from mixed trusted and untrusted inputs deserves immediate review. Pay special attention to code paths that were moved out of the ORM for convenience, because those paths often lose the protections the team assumed were still present.

Practitioner takeaway: Use raw SQL only when the ORM cannot express the operation cleanly, and require the same discipline you would apply to any security-sensitive interface, static SQL text, explicit parameters, and a code review that treats query construction as part of the threat model.

Free weekly newsletter

Subscribe to the NHI & AI Identity Journal

The latest on NHI and Agentic AI security – articles, research, breaches, news and events every week.

Bonus 33% off our NHI Course when you subscribe.

NHIMG Editorial Note
Reviewed and updated by the NHIMG editorial team on October 31, 2026.
NHI Mgmt Group — the #1 independent authority on Non-Human Identity, IAM, and Agentic AI security. nhimg.org