How to Choose SQL Sort Columns and Directions Safely
In a sortable list, users choose a field and a direction: for example, name in ascending order or price in descending order. For developers, the trust boundary matters: interface parameters express the user’s preference, while the server defines the permitted SQL fragments. This article explains how to define a finite set of options, explicitly reject unsupported values, and verify both the result order and query construction. All catalog records and labels below are fictional and intended for testing your own application.
Why a query parameter cannot replace a column name
SQL parameters are intended for data values, not table names, column names, or keywords, including ASC and DESC. Passing a column name as a bound value therefore does not solve the problem of choosing a sort order. The same applies to the direction: it must become a predefined SQL literal.
The handler’s job is to translate external labels into permitted parts of a specific query. The user’s original string must not become an SQL identifier or sort direction. This article covers that translation specifically; binding text values for search and other query validation mechanisms are outside its scope.
Define a small set of permitted options
For the fictional catalog, allow two external field labels: name and price. Define the following mapping on the server: name → product_name, price → unit_price. Use a separate direction mapping: up → ASC, down → DESC. This gives four permitted combinations, each corresponding to a fixed sort option.
These mappings belong to the application and apply to the catalog query. They do not mean that users can select any existing database column. Check that each input value exactly matches an entry in its corresponding set, even when the interface offers only a dropdown. Browser validation does not replace server validation.
Validate both values before building SQL
The processing sequence is straightforward: the server receives the field and direction labels, checks both against the permitted sets, then selects the internal column and direction literal. The handler builds the query only after both parameters pass validation. For example, price/down produces the fragment ORDER BY unit_price DESC; name/up produces ORDER BY product_name ASC.
If either label is unknown, reject the request with a clear error instead of continuing with partially validated data. For this example, use the same policy for a missing or empty parameter: reject the request without executing the catalog query. This is an explicit choice for the example and should be reflected in the handler’s behavior and tests. An unknown value must not silently turn into a different sort option.
Verify all four orders using your own records
For acceptance checks in your own test catalog, create three synthetic records: A — “Alpha”, price 300; B — “Beta”, price 100; C — “Gamma”, price 200. The names and prices are distinct, so you can specify the expected order for each permitted combination in advance.
Start with name/up as a positive control, then check the remaining combinations. The acceptance criterion is the exact sequence of record identifiers in the results. A successful HTTP response alone does not confirm that the selected sort order was actually applied.
- name/up: A, B, C.
- name/down: C, B, A.
- price/up: B, C, A.
- price/down: A, C, B.
Check rejection directly in the handler
Using the same catalog you own, pass an unknown field label, such as unknown, to the server handler with the permitted direction up. Then pass the permitted field name with the unknown direction unknown. Separately test a missing and an empty value for each parameter according to the example’s chosen policy.
For each case, expect a clear error and no execution of the catalog query. In the test adapter, verify that the handler never reached SQL execution. The HTTP status alone is insufficient: it does not show whether processing continued after the error was detected. This check verifies that the catalog is not read; modifying records is outside the scenario.
Compare the actual SQL with the expected query
Correct result ordering does not by itself confirm safe query construction. In your own application’s test adapter, capture the SQL before execution and compare it with a predefined expected query for each of the four combinations. In particular, check the selected column and direction.
Then review the code: both fragments must come exclusively from the fixed server mappings for this query. The original interface parameter strings must not be inserted into SQL or used as bound values in place of the identifier and direction. Acceptance requires both observations: the expected record order and the expected query construction.
What to fix first and what these checks cover
First, remove direct insertion of input strings when selecting the column and direction. Then establish server mappings, exact validation of both parameters, and explicit rejection of unknown options. Next, check the permitted combinations, rejection cases, and actual SQL. The result is a concrete sorting contract that you can verify.
These checks apply to the specified query and fictional data. They do not prove the security of the entire application or cover search, object permissions, or other query handling mechanisms. If you need help reviewing sorting in your product, you can start with a free audit of your website or the web part of your product.