SQL Basics · Lesson 1 of 9

Your First Query

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;
  • SELECT says which columns you want. The star * means "all of them".
  • FROM says 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
TableColumns
customersid, name, city, joined
productsid, name, category, price, stock
ordersid, 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

  1. Run the starting query to see the whole customers table.
  2. Change it so it returns only the name and city columns, in that order.
  • The query returns exactly two columns: name, then city
  • All eight customers are in the result