Free courses in AI, Data Science & Machine Learning Written lessons · Quizzes · Final exams
Future Data Academy
Log inGet started ↗
← SQL essentials: your first useful queries
FREE LESSON PREVIEW

Tables, rows and your first SELECT

Think in rows and columns

A relational table describes one kind of thing. Each row is an observation; each column is an attribute. A primary key identifies a row. A foreign key connects a row to a record in another table.

Use this small example throughout the course. The values are invented for practice:

order_id customer_id city amount
1 10 Leeds 50
2 11 York 30
3 10 Leeds 20
SELECT order_id, city, amount
FROM orders;

SELECT names the columns to return. FROM names the source table. A query describes the result you want; the database chooses how to obtain it. SELECT * returns all columns, which is convenient for exploration but can make application code fragile when the table changes.

Sorting is explicit

SELECT order_id, amount
FROM orders
ORDER BY amount DESC, order_id ASC;

This returns amounts 50, 30, 20. Without ORDER BY, row order is not guaranteed. The second sort column breaks ties consistently.

Practice

Write a query returning only customer_id and city. Predict the number of returned rows: three, because SELECT does not automatically remove duplicates. SELECT DISTINCT customer_id, city FROM orders returns two distinct combinations.

Explore the full course →