Easier to read with cream paper, a tint, more contrast or larger text? Choose your colours and text, once, for every page.

Free lesson

SQL Basics

The whole lesson is here to read. Create a free account to run it in a live lab.

Container lab17 min read20 commands

What you will do

A database keeps information in tables. A table is like a spreadsheet: each column is one kind of information, and each row is one thing, for example one customer.

SQL is the language you use to talk to a database. In this lesson you add one new table to a small online shop, put rows in it, ask questions about it, change a row and remove a row.

Before you start

Note

Press Start the lab on the bar at the top of this lesson and wait until it says it is running. The first start takes a little longer, because the shop database is made then. Each Run button below types its command into your lab's terminal, one after another in the same session. To watch them, or to type your own, press Open terminal on the bar.

1. Tell the terminal how to sign in

MySQL makes its own security certificate, so the first command tells the database client to accept it. The connection is still private. The second command makes a short name, sql, for the long command that signs in to the shop database and runs one SQL statement. After this you only type sql and the statement in quotes.

Command
printf '[client]\nssl-verify-server-cert=0\n' > ~/.my.cnf

Shown here, not run. Create a free account to press Run and type it into your own lab’s terminal.

Command
alias sql='mariadb -h db -u student -pstudent shop -t -e'

Shown here, not run. Create a free account to press Run and type it into your own lab’s terminal.

2. See the tables that are there

The shop already has tables. SHOW TABLES lists them. Each one holds a different kind of information: customers are the people who buy, products are what they buy.

Command
sql "SHOW TABLES"

Shown here, not run. Create a free account to press Run and type it into your own lab’s terminal.

3. Look inside one table

DESCRIBE shows the columns of a table: its name, its type, and more. Look at the Key column. PRI means primary key. A primary key is a column whose value is different in every row, so it names one row and no other. Here it is id.

Command
sql "DESCRIBE customers"

Shown here, not run. Create a free account to press Run and type it into your own lab’s terminal.

Command
sql "DESCRIBE products"

Shown here, not run. Create a free account to press Run and type it into your own lab’s terminal.

4. Make a new table

Now you make your own table, wishlist: which customer would like which product. CREATE TABLE makes it, and the part in brackets lists its columns.

  • id INT AUTO_INCREMENT PRIMARY KEY: a whole number, counted up by MySQL for each new row. It is the primary key.
  • customer_id and product_id: which customer, and which product. They hold the id of a row in another table.
  • priority: how much they want it, from 1 (a lot) to 5. It is 3 if you do not say.
  • UNIQUE KEY: the same customer and product may not be in the table twice.
  • FOREIGN KEY: the ids must exist in customers and products, so you cannot wish for a product that is not in the shop.

The statement is long, but you only run it once.

Command
sql "CREATE TABLE wishlist (id INT AUTO_INCREMENT PRIMARY KEY, customer_id INT NOT NULL, product_id INT NOT NULL, priority INT NOT NULL DEFAULT 3, UNIQUE KEY customer_product (customer_id, product_id), FOREIGN KEY (customer_id) REFERENCES customers(id), FOREIGN KEY (product_id) REFERENCES products(id))"

Shown here, not run. Create a free account to press Run and type it into your own lab’s terminal.

Command
sql "DESCRIBE wishlist"

Shown here, not run. Create a free account to press Run and type it into your own lab’s terminal.

5. Put rows in the table

INSERT INTO adds rows. First the table and its columns, then VALUES and one bracket for each row. You do not give an id: MySQL counts for you. This adds three rows: customer 1 wants product 1 and product 5, and customer 2 wants product 5. SELECT * then shows every column of every row.

Command
sql "INSERT INTO wishlist (customer_id, product_id, priority) VALUES (1, 1, 3), (1, 5, 2), (2, 5, 1)"

Shown here, not run. Create a free account to press Run and type it into your own lab’s terminal.

Command
sql "SELECT * FROM wishlist"

Shown here, not run. Create a free account to press Run and type it into your own lab’s terminal.

6. The unique key says no

Customer 1 already wants product 1. Try to add the same pair again. MySQL refuses with an error, because of the UNIQUE KEY from step 4. This error is not your mistake: it shows the table protecting its own data. Nothing was added.

Command
sql "INSERT INTO wishlist (customer_id, product_id, priority) VALUES (1, 1, 1)"

Shown here, not run. Create a free account to press Run and type it into your own lab’s terminal.

Command
sql "SELECT * FROM wishlist"

Shown here, not run. Create a free account to press Run and type it into your own lab’s terminal.

The foreign key says no too

Note

Try sql "INSERT INTO wishlist (customer_id, product_id) VALUES (1, 999)" in the terminal. There is no product 999, so the FOREIGN KEY refuses it.

7. Ask a simple question

SELECT asks a question. FROM says which table. WHERE keeps only the rows that match, and ORDER BY puts them in order. This one shows what customer 1 wants, the most wanted first.

Command
sql "SELECT * FROM wishlist WHERE customer_id = 1 ORDER BY priority"

Shown here, not run. Create a free account to press Run and type it into your own lab’s terminal.

Command
sql "SELECT name, price FROM products WHERE price > 50 ORDER BY price DESC"

Shown here, not run. Create a free account to press Run and type it into your own lab’s terminal.

8. Join two tables

The wishlist only has numbers. The product names are in products. A join puts two tables side by side. JOIN products ON products.id = wishlist.product_id means: for each wishlist row, find the product whose id is in product_id. Now you can read it.

Command
sql "SELECT wishlist.id, products.name, products.price, wishlist.priority FROM wishlist JOIN products ON products.id = wishlist.product_id"

Shown here, not run. Create a free account to press Run and type it into your own lab’s terminal.

9. Join three tables

You can join again to add the customer's name. Same idea, one more JOIN.

Command
sql "SELECT customers.first_name, products.name, wishlist.priority FROM wishlist JOIN customers ON customers.id = wishlist.customer_id JOIN products ON products.id = wishlist.product_id ORDER BY customers.first_name"

Shown here, not run. Create a free account to press Run and type it into your own lab’s terminal.

10. Change a row

UPDATE changes rows. SET says the new value. WHERE id = 2 says which row. Customer 1 now wants product 5 the most, so its priority becomes 1. Then check with a SELECT that the row really changed.

Command
sql "UPDATE wishlist SET priority = 1 WHERE id = 2"

Shown here, not run. Create a free account to press Run and type it into your own lab’s terminal.

Command
sql "SELECT * FROM wishlist WHERE id = 2"

Shown here, not run. Create a free account to press Run and type it into your own lab’s terminal.

Never forget WHERE

Warning

It is easy to think UPDATE and DELETE change only the row you mean. Without WHERE, they change every row in the table. Always write the WHERE first, and check with a SELECT before and after.

11. Remove a row

DELETE FROM removes rows. Again WHERE says which. Customer 2 changed their mind, so remove row 3. Then look at the table: row 3 is gone, and the other rows are still there.

Command
sql "DELETE FROM wishlist WHERE id = 3"

Shown here, not run. Create a free account to press Run and type it into your own lab’s terminal.

Command
sql "SELECT * FROM wishlist"

Shown here, not run. Create a free account to press Run and type it into your own lab’s terminal.

12. Count what is left

COUNT(*) counts rows. Ask for row 3 and the answer is 0, which proves the row is gone.

Command
sql "SELECT COUNT(*) AS rows_found FROM wishlist WHERE id = 3"

Shown here, not run. Create a free account to press Run and type it into your own lab’s terminal.

13. See it in phpMyAdmin

On the lab bar, press phpMyAdmin to open it in a new tab. Choose shop on the left. Your new table wishlist is in the list with the others. Press wishlist, then Browse to see its rows, and Structure to see the primary key and the unique key you made.

Do it again on your own

Tip

To start the lesson again, remove your table with sql "DROP TABLE wishlist" and go back to step 4. Then try your own questions: which products have a price under 10? Which customers live in Paris?

If the terminal was restarted, run the alias in step 1 again first. The database keeps your table.

More in this lab

About this lab and its source: MySQL and phpMyAdmin lab with a sample shop database to practise SQL

Teaching a class? Set these lessons for your students with marking and a lab for every student.