Kodewick
Lesson 1 of 21Contents

Tables and SELECT

Lesson 1 of 21 · Part 1 · Querying data · about 15 minutes

A database keeps its data in tables. This course uses the data of a small online shop: customers, products, orders. Every query on this page runs right in your browser: press Run, change something, and run it again.

main.sql
1SELECT * FROM products;
Output
idnamecategorypricestock
1NotebookStationery4.50120
2Pens (10)Stationery6.0080
3BackpackBags39.9015
4Water bottleKitchen12.0040
5HeadphonesElectronics59.0010
6Desk lampElectronics25.000
7USB cableElectronics8.50200
8Coffee mugKitchen9.9060
9Phone caseElectronics15.0035
10Tote bagBags11.0025
10 rows

This is the shop's products table. Each row is one product, and each column is one piece of information about it:

ColumnWhat it holds
idthe product's number, different for every product
namethe product's name
categoryits category
priceits price
stockhow many are left

SELECT and FROM

A query says: from (FROM) this table, select (SELECT) these columns. * means "every column". To see only the name and the price, write the column names, separated by commas:

main.sql
1SELECT name, price FROM products;
Output
nameprice
Notebook4.50
Pens (10)6.00
Backpack39.90
Water bottle12.00
Headphones59.00
Desk lamp25.00
USB cable8.50
Coffee mug9.90
Phone case15.00
Tote bag11.00
10 rows

The result is a table too: only the two columns you asked for, with the same rows. The columns come in the order you wrote them:

main.sql
1SELECT price, name FROM products;
Output
pricename
4.50Notebook
6.00Pens (10)
39.90Backpack
12.00Water bottle
59.00Headphones
25.00Desk lamp
8.50USB cable
9.90Coffee mug
15.00Phone case
11.00Tote bag
10 rows

The shop's other tables

The shop also has a customers table, an orders table, an order_items table (what is in each order) and an employees table. Later lessons use them one by one. First, a look at the customers:

main.sql
1SELECT id, name, city FROM customers;
Output
idnamecity
1Ana SilvaLondon
2Ben OkaforParis
3Chloe MartinLondon
4David KimTokyo
5Emma NovakBerlin
6Farah KhanParis
7George BrownTokyo
8Hana SatoLondon
9Ivan PetrovBerlin
10Julia CostaSydney
10 rows

Tip: When you don't know which columns a table has, take a look with SELECT * first.

Several at once

A query ends with a semicolon ;. You can write several at once, and each one gives its own result:

main.sql
1SELECT name FROM products;2SELECT name FROM customers;
Output
Statement on line 1
name
Notebook
Pens (10)
Backpack
Water bottle
Headphones
Desk lamp
USB cable
Coffee mug
Phone case
Tote bag
10 rows
Statement on line 2
name
Ana Silva
Ben Okafor
Chloe Martin
David Kim
Emma Novak
Farah Khan
George Brown
Hana Sato
Ivan Petrov
Julia Costa
10 rows

Keywords in capitals, names in lowercase

Words like SELECT and FROM are keywords; capitals or not, both work. By habit, keywords go in capitals and table and column names in lowercase, so you can tell them apart at a glance. Line breaks and spaces are up to you, so a long query can take several lines:

main.sql
1select name,2    stock3from products;
Output
namestock
Notebook120
Pens (10)80
Backpack15
Water bottle40
Headphones10
Desk lamp0
USB cable200
Coffee mug60
Phone case35
Tote bag25
10 rows

This is exactly the same as SELECT name, stock FROM products;.

Guess first

How many columns does the result have?

1SELECT name, city FROM customers;

Your first error

Misspell a column name and the database tells you which name it doesn't know:

main.sql
1SELECT name, prise FROM products;
Output
ERROR 1054 (42S22): Unknown column 'prise' in 'field list'

ERROR 1054 is MySQL's error number; after it, it says there is no column called prise. Change it to price and run it again.

Practice

Write the query, then press Check. A hint costs nothing; the answer is there when you need it.

Exercise 1

Products and stock

From the products table, get two columns for every product: name and stock.

It should print
namestock
Notebook120
Pens (10)80
Backpack15
Water bottle40
Headphones10
Desk lamp0
USB cable200
Coffee mug60
Phone case35
Tote bag25
10 rows
exercise1.sql
1SELECT * FROM products;
Output
Press Run or Ctrl+Enter
Exercise 2

Employees

From the employees table, get the name and title columns, title first.

It should print
titlename
ManagerGrace Lee
Sales leadOmar Haddad
Warehouse leadLucia Romero
SalesTom Becker
SalesMei Tanaka
PackerSam Wilson
6 rows
exercise2.sql
1SELECT name FROM employees;
Output
Press Run or Ctrl+Enter
Exercise 3

Fix it: the column name

This query gives an error. Fix it so it gets the customers' name and email.

It should print
nameemail
Ana Silva[email protected]
Ben Okafor[email protected]
Chloe MartinNULL
David Kim[email protected]
Emma Novak[email protected]
Farah KhanNULL
George Brown[email protected]
Hana Sato[email protected]
Ivan PetrovNULL
Julia Costa[email protected]
10 rows
exercise3.sql
1SELECT name, mail FROM customers;
Output
Press Run or Ctrl+Enter

Some customers left no email: that cell shows NULL, which means "no value". Lesson 5 is all about it.

What you learned

  • Data lives in tables: each row is one record, each column one piece of information.
  • SELECT column, column FROM table; gets the columns you want; * is every column.
  • Every query ends with a semicolon, and you can run several at once.
  • Keywords by habit in capitals, names in lowercase; line breaks and spaces as you like.
  • A misspelled column name gives ERROR 1054: first see which name it doesn't know.