Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

This SQL 'ORDER BY' is not working properly

Tags:

sql

SELECT test_column FROM test_table ORDER BY test_column gives me this:

1
12
123
2
3

Why not:

1
2
3
12
123

How can I sort strings like numbers?

like image 669
CDT Avatar asked Jul 20 '13 13:07

CDT


People also ask

Why does my ORDER BY not work in SQL?

The ORDER BY clause is invalid in views, inline functions, derived tables, subqueries, and common table expressions, unless TOP or FOR XML is also specified. The ORDER BY clause is invalid in views, inline functions, derived tables, subqueries, and common table expressions, unless TOP or FOR XML is also specified.

Can we use ORDER BY without Group By?

Key Differences between GROUP BY and ORDER BY The Group By clause is used to group data based on the same value in a specific column. The ORDER BY clause, on the other hand, sorts the result and shows it in ascending or descending order. It is mandatory to use the aggregate function to use the Group By.

Why we cant put ORDER BY inside the view?

Because a view is by definition an unordered set of rows. The only purpose of an ORDER BY in a view is in combination with TOP in order to determine which subset of the qualifying rows is selected.

How does ORDER BY work in SQL?

The ORDER BY keyword is used to sort the result-set in ascending or descending order. The ORDER BY keyword sorts the records in ascending order by default. To sort the records in descending order, use the DESC keyword.


1 Answers

Try

SELECT test_column 
FROM test_table 
ORDER BY cast(test_column as int)

But you should look into changing the column types to the correct ones.

like image 169
JBrooks Avatar answered Sep 18 '22 12:09

JBrooks