Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 696747
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 14, 20262026-05-14T03:05:44+00:00 2026-05-14T03:05:44+00:00

I just found some strange behavior of database’s order by clause. In string comparison,

  • 0

I just found some strange behavior of database’s “order by” clause. In string comparison, I expected some characters such as ‘[‘ and ‘_’ are greater than latin characters/digits such as ‘I’ or ‘2’ considering their orders in the ASCII table. However, the sorting results from database’s “order by” clause is different with my expectation. Here’s my test:


SQLite version 3.6.23
Enter “.help” for instructions
Enter SQL statements terminated with a “;”
sqlite> create table products(name varchar(10));
sqlite> insert into products values(‘ipod’);
sqlite> insert into products values(‘iphone’);
sqlite> insert into products values(‘[apple]’);
sqlite> insert into products values(‘_ipad’);
sqlite> select * from products order by name asc;
[apple]
_ipad
iphone
ipod

select * from products order by name asc;
name
...
[B@
_ref
123
1ab
...

This behavior is different from Java’s string comparison (which cost me some time to find this issue). I can verify this in both SQLite 3.6.23 and Microsoft SQL Server 2005. I did some web search but cannot find any related documentation. Could someone shed me some light on it? Is it a SQL standard? Where can I find some information about this? Thanks in advance.

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-14T03:05:44+00:00Added an answer on May 14, 2026 at 3:05 am

    The concept of comparing and ordering the characters in a database is called collation.

    How the strings are stored depends on the collation which is usually set in the server, client or session properties.

    In MySQL:

    SELECT  *
    FROM    (
            SELECT  'a' AS str
            UNION ALL
            SELECT  'A' AS str
            UNION ALL
            SELECT  'b' AS str
            UNION ALL
            SELECT  'B' AS str
            ) q
    ORDER BY
            str COLLATE UTF8_BIN
    
    
    --
    'A'
    'B'
    'a'
    'b'
    

    and

    SELECT  *
    FROM    (
            SELECT  'a' AS str
            UNION ALL
            SELECT  'A' AS str
            UNION ALL
            SELECT  'b' AS str
            UNION ALL
            SELECT  'B' AS str
            ) q
    ORDER BY
            str COLLATE UTF8_GENERAL_CI
    
    
    --
    'a'
    'A'
    'b'
    'B'
    

    UTF8_BIN sorts characters according to their unicode. Caps have lower unicodes and therefore go first.

    UTF8_GENERAL_CI sorts characters according to their alphabetical position, disregarding case.

    Collation is also important for indexes, since the indexes rely heavily on sorting and comparison rules.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I've just found some C++ code (at http://msdn.microsoft.com/en-us/library/k8336763(VS.71).aspx ), which uses a technique I've
I just found this in some old code, and I'm not sure what it
Found some old code, circa VS 2003. Now I have just VS 2008 (SP1)
I was just reviewing some old code and found the following (inside foo.asp): Const
I just switched to CMake. And yet found it very useful and realized some
Good day, everyone. I found strange behavior within ASP.NET engine when it handles non-existent
I've just started to use dataGridView, and some things seem strange to me: when
I see some strange behavior on the maximum heap size I get on Sun's
Just found this out, so i am answering my own question :) Use a
Just found out that the video output of the iPad is not a system

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.