Comparing Text

To compare a column with a text, write the text in single quotes, as you did with || in the previous lesson:

SELECT title, price
FROM games
WHERE genre = 'Puzzle';
titleprice
Cube Logic5
Word Quest10

Typical mistake: text without quotes. SQLite then takes Puzzle for the name of a column, and WHERE genre = Puzzle ends with the error no such column: Puzzle.

= compares text exactly, character by character, and a capital letter is a different character than a lowercase one. WHERE genre = 'puzzle' finds nothing, and neither does 'Puzzle ' with a space at the end. The result of such a query is just the line with the column names, with no rows. When a query that should find something finds nothing, compare its text with the Tables tab.

The title Knight's Oath has an apostrophe in it, and that's the same character that ends a text. Here, the text ends after Knight, and SQLite doesn't know what to do with the rest:

SELECT *
FROM games
WHERE title = 'Knight's Oath';
Error on line 1: near "s": syntax error

Inside text, write an apostrophe twice: 'Knight''s Oath'. Two apostrophes in a row mean one apostrophe in the text, and the text goes on.

And double quotes? In SQL, they're for the names of tables and columns, not for text. SQLite sometimes takes "Puzzle" for a text too, but only when no column has that name, and other databases don't accept it at all. Text always goes in single quotes.

Text can be compared with <, >, <= and >= too. Then it goes by the alphabet: a text is “less” when it comes earlier in the alphabet. The games whose title comes before the letter D:

SELECT title
FROM games
WHERE title < 'D'
ORDER BY title;
title
Bakery Story
Castle Siege
Cube Logic

Castle Siege and Cube Logic are there, even though they're longer than 'D': text is compared from the first character, and C comes before D. When one text is the beginning of another, the shorter one comes first, so 'Castle' is less than 'Castle Siege'.

Strictly speaking, SQLite compares text by the codes of its characters, and in them, all capital letters come before all lowercase ones: 'Zebra' < 'apple' is true. ORDER BY sorts text the same way, so a title starting with a lowercase letter would come after all the others.

Instructions

Show the title and the price of the strategy games (the genre Strategy), the cheapest first:

titleprice
Tiny Kingdoms25
Castle Siege30
Galaxy Trader35

Below it, show all the columns of the game Knight's Oath. Find it by its title:

idtitlegenrepricereleasedage_rating
15Knight's OathRPG602025-11-1416

The shop is rearranging its shelves, and you're putting up the games from S to Z. Show the titles of the games that start with S or any later letter, sorted by title:

title
Shadow Ninja
Star Pilots
Tiny Kingdoms
Turbo Karts
Word Quest
Zombie Town

Start programming for free

By signing up, you agree to the Terms of Service and Privacy Policy.

Or sign up with:

3/12