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 7763439
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 1, 20262026-06-01T14:39:32+00:00 2026-06-01T14:39:32+00:00

I need a SQLite query that searches 1 field only using LIKE. Basic example:

  • 0

I need a SQLite query that searches 1 field only using LIKE.

Basic example:

SELECT name FROM table WHERE name LIKE "%John%" ORDER BY name LIMIT 10;

The problem is that I want the result to be ordered in this way:

  1. If the field is equal (e.g. “John”)
  2. If the field starts with “John” (e.g. “John Doe”)
  3. If the field contains “John” (e.g. “Jane John Doe”)

The following query achieves the expected result, but is slow:

SELECT name FROM table WHERE name LIKE "%John%" ORDER BY CASE WHEN name = "John" 
THEN 1 ELSE 2 END, CASE WHEN name LIKE "John%" THEN 1 ELSE 2 END, name LIMIT 10;

The query above is slower (or I tested it incorrectly) than the alternative of using 3 separate queries (one for exact match, one for starts with and one for contains).

Are there any other alternatives?

  • 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-06-01T14:39:33+00:00Added an answer on June 1, 2026 at 2:39 pm

    Try in this way :

    SELECT name 
    FROM table 
    WHERE name LIKE "%John%" 
    ORDER BY (CASE WHEN name = "John" THEN 1 WHEN name LIKE "John%" THEN 2 ELSE 3 END),name LIMIT 10 ;
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have an sqlite query that looks like this: db_rows = db_cursor.execute( SELECT *
Using SQLite I need to copy nearly all of an existing row from a
I want to execute a sqlite query: select * from table_name where id in
I've a complex query with SQLite that seeks to grab a total from 3
I need to query a SQLite database for items that either match a given
I need to query a table in an SQLite database to return all the
I have an SQLite query that, potentially, returns thousands of records. I need to
I am trying to send a query to SQLite from the command line using
I need to make a webpage (from scratch) that will interrogate a SQLite database
Need to build a Top 10 query that works with SQLite and Postgres. Client

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.