Ask the data a question
Picture a shop that keeps a list of customers. You want to know which cities they live in without going through their records one by one. SQL lets you ask the data that question and get back a list with the answer.
SQL is a language that lets you query and modify data in a database, a collection of information stored in an organised way. Its name stands for Structured Query Language. In this course we will work with tables, which organise data into rows and columns, much like a spreadsheet.
A query is an instruction written in SQL. To read data, we will start with two words: SELECT says which information you want to see and FROM says which table it comes from. Asking for each customer's city does not change their data; it just produces a result you can read.
The data and the editor are already set up. Read the example, write your query in the practice task and press «Run and check». You do not need to install anything. Later you will learn to filter, summarise and change data, one idea at a time.
SELECT column_name
FROM table_name;See it in action
Which cities do our customers live in?
SELECT city
FROM customers;| city |
|---|
| London |
| Manchester |
| London |
| Bristol |
| Leeds |
| London |
| Bristol |
| Manchester |
- The customers table contains one row per person. FROM customers says where to look.
- SELECT city asks for one attribute of each person. The result keeps all twelve rows, even though some cities appear more than once.
- Reading a table does not change its data. You can query name instead of city to ask a different question.
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
Look up the cities
Write a query that returns only the city column for every customer.
Expected result: 12 rows with the column city, in that order. Row order does not matter.
View tables and data
Open this panel to see the full data.
Choose the attribute that holds each customer's city.
The column is called city.
The table is called customers.
Solution explained
SELECT city FROM customers;SELECT city picks the column and FROM customers names its source.
Your turn
Solve this variation. Spot the part that has changed before you look for help.
Practice
Change the question
Show only the name of every customer. Do not filter or sort the rows.
Expected result: 12 rows with the column name, in that order. Row order does not matter.
View tables and data
Open this panel to see the full data.
Think about which attribute you want to read for each person.
Name that attribute in SELECT and keep the source in FROM.
The attribute you need is called name.
Solution explained
SELECT name FROM customers;There is one row per customer and one column. Changing the chosen column changes the result, not the customers.
Key takeaways
- SQL lets you ask questions of stored data.
- A SELECT query returns a result.
- FROM names the table you are querying.