Before you start, you need
Decide which column appears first
You can present the same information in several ways. If you are checking destinations, you may prefer to see the city first and then the name. If you are looking for people, the opposite order may help.
The SELECT list decides the column order. SELECT city, name puts the city on the left. SELECT name, city puts the name on the left. In both cases, each value stays next to the right person.
This changes the presentation, not the stored data or which customers appear. Separate each column with a comma.
Moving a column does not sort the rows either. ORDER BY sorts rows, for example alphabetically; you will learn it later. Here you only decide which column is read first.
SELECT city, name
FROM customers;See it in action
We want to review people starting with their city.
SELECT city, name
FROM customers;| city | name |
|---|---|
| London | Amy Wilson |
| Manchester | Liam Martin |
| London | Megan Reed |
| Bristol | David Clark |
| Leeds | Sara Green |
| London | Peter Hill |
| Bristol | Laura Ward |
| Manchester | Hugh Miller |
- city appears on the left because it is the first name in the SELECT. name comes next.
- Each city stays next to the right person. Swapping the list does not mix up different people's attributes.
- The list of cities has not been sorted: moving a column and sorting rows are different decisions.
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
Put the city first
Return city, name and id for all customers, with the columns in exactly that order. The rows may appear in any order.
Expected result: 12 rows with the columns city, name, id, in that order. Row order does not matter.
View tables and data
Open this panel to see the full data.
The city must appear before the name and the id; the requirement is about the order of the columns.
Then write name and id.
Keep the commas between columns.
Solution explained
SELECT city, name, id FROM customers;The SELECT list makes city, name and id the first, second and third columns. The row order is free because no ORDER BY is requested.
Your turn
Solve this variation. Spot the part that has changed before you look for help.
Practice
Design a stock check
Show stock, name and price for every product, in that column order. The row order is free.
Expected result: 9 rows with the columns stock, name, price, in that order. Row order does not matter.
View tables and data
Open this panel to see the full data.
The requirement is about left-to-right order: put first the attribute that will be checked.
You do not need to sort the rows.
The list starts with stock, name.
Solution explained
SELECT stock, name, price FROM products;stock appears on the left because it is the first expression in the SELECT. Each row's contents still belong to the same product.
Key takeaways
- The SELECT list controls the order of the columns.
- Changing that order keeps the rows as they are.
- ORDER BY is used to sort rows.