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;| title | price |
|---|---|
| Dragon Forge | 40 |
| Pixel Racer | 15 |
| Moon Farm | 20 |
| … | … |
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:
| title | genre |
|---|---|
| Dragon Forge | RPG |
| Pixel Racer | Racing |
| … | … |
Below it, add a query that shows the name and the city of every customer:
| name | city |
|---|---|
| Emma Clark | London |
| Liam Turner | Manchester |
| … | … |
Start programming for free
2/8