Choosing Columns

A table can have many columns, and you rarely need all of them. Instead of the star, list the columns you want after SELECT, separated by commas:

SELECT title, price FROM games;
titleprice
Dragon Forge40
Pixel Racer15
Moon Farm20
……

The result has the columns in the order in which you listed them, so SELECT price, title FROM games; shows the price first. You can even list a column twice, although that's rarely useful.

A column's name must be written as the table has it. The title of a game is in the column title, not name or game. You'll find the names in the Tables tab, in the first row of each table. SQLite doesn't mind whether you write them in lowercase or in capitals (TITLE works too), but it's usual to write them as the table has them.

Why not always use the star? A real shop's table can have dozens of columns and millions of rows. A query that asks only for the columns it needs is easier to read, and the database has less to send. The star is handy when you just want to look at a table; in queries that a program runs again and again, programmers list the columns.

Typical mistake: a missing comma. SELECT title price FROM games; is not an error, but its result has just one column, called price, with the titles of the games in it. Without the comma, SQLite reads price as a new name for the column title. You'll learn how to rename columns on purpose in the next lesson.

Instructions

Change the query, so that it shows only the title and the genre of each game, in this order:

titlegenre
Dragon ForgeRPG
Pixel RacerRacing
……

Below it, add a query that shows the name and the city of every customer:

namecity
Emma ClarkLondon
Liam TurnerManchester
……

Start programming for free

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

Or sign up with:

2/7