Before you start, you need
Read a table by rows and columns
Picture a list of customers laid out like a spreadsheet. Each row brings together one person's data. If you read a row from left to right, you will find their name, their city and other attributes.
Each column stores one attribute for everyone. Its heading tells you which one: name, city or email. Reading the city column from top to bottom lets you compare where each customer lives.
A cell is where a row and a column meet. The name cell in customer 1's row contains Amy Wilson. The id column stores an identifier: a number that lets you tell people apart even if they share a name.
Sometimes an attribute is missing. NULL marks a missing value, for example an email address that has not been recorded. It is not the same as the number 0 or an empty text. For now, it is enough to recognise it; you will learn how to look for these missing values in a lesson of their own.
SELECT id, name
FROM customers;See it in action
How can we tell each customer apart and name them?
SELECT id, name
FROM customers;| id | name |
|---|---|
| 1 | Amy Wilson |
| 2 | Liam Martin |
| 3 | Megan Reed |
| 4 | David Clark |
| 5 | Sara Green |
| 6 | Peter Hill |
| 7 | Laura Ward |
| 8 | Hugh Miller |
- Each result row corresponds to one customer. id and name are the two chosen columns.
- Amy Wilson appears next to her id, 1. The cell for her name contains Amy Wilson; the cell for her id contains 1.
- Even if two people had the same name, their ids would tell them apart. A column always describes the same attribute on every row.
Practice with help
Getting things wrong is part of learning: you can run your query as many times as you like and nothing is lost. If you get stuck, open a hint (each one reveals a bit more) and save the solution for last.
Guided practice
Identify each customer
Write a query that shows the id and name of every customer.
Expected result: 12 rows with the columns id, name, in that order. Row order does not matter.
View tables and data
Open this panel to see the full data.
Each row must identify one person and show their name.
The table is customers and the columns are called id and name.
Write the two columns after SELECT, separated by a comma, and name the table with FROM.
Solution explained
SELECT id, name FROM customers;SELECT picks the two columns and FROM names the table. The result clearly shows its 12 rows and 2 columns.
Your turn
Solve this variation. Spot the part that has changed before you look for help.
Practice
Read another part of the table
Show name and city for every customer, in that column order.
Expected result: 12 rows with the columns name, city, in that order. Row order does not matter.
View tables and data
Open this panel to see the full data.
Identify the two attributes you want to read.
SELECT keeps the left-to-right order in which you write the columns.
Separate name and city with a comma.
Solution explained
SELECT name, city FROM customers;Each row is still one person. The first column holds their name and the second their city.
Key takeaways
- A row represents one item.
- A column describes one attribute.
- A cell holds the value of one attribute for one item.
- NULL marks a missing value.
Next: How to read a SQL query