A database stores data in tables. A table looks like a spreadsheet: it has named columns, and each row is one record.
The practice database belongs to a small shop and has three tables: customers, products and orders.
To read data you write a query. The simplest one asks for everything in a table:
SELECT * FROM products;
SELECTsays which columns you want. The star*means "all of them".FROMsays which table to read.- The semicolon ends the query.
Choosing columns
Usually you do not need every column. List the ones you want, separated by commas:
SELECT name, price FROM products;
The columns come back in the order you list them.
Style
SQL keywords are not case sensitive: select works as well as SELECT. By tradition, keywords are written in capitals so they stand out from table and column names. Longer queries are usually split over several lines:
SELECT name, price
FROM products;
Press Run (or Ctrl+Enter) to try the starting query. The result appears as a table.
Tables in the practice database
| Table | Columns |
|---|---|
customers | id, name, city, joined |
products | id, name, category, price, stock |
orders | id, customer_id, product_id, quantity, ordered |
Every run starts from a fresh copy of the database, so nothing you do can break it.
Your Task
- Run the starting query to see the whole
customerstable. - Change it so it returns only the
nameandcitycolumns, in that order.
- The query returns exactly two columns:
name, thencity - All eight customers are in the result