Module 1 · Selecting data

Choose the order of the columns

By the end you will be able to place the columns of the result in the order you need.

About 6 min

0 of 52 lessons

Before you start, you need

Select data with SELECT and FROM

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.

Minimal syntax
SELECT city, name
FROM customers;

See it in action

We want to review people starting with their city.

SQL
SELECT city, name
FROM customers;
Example result: 8 of 12 rows
cityname
LondonAmy Wilson
ManchesterLiam Martin
LondonMegan Reed
BristolDavid Clark
LeedsSara Green
LondonPeter Hill
BristolLaura Ward
ManchesterHugh Miller
  1. city appears on the left because it is the first name in the SELECT. name comes next.
  2. Each city stays next to the right person. Swapping the list does not mix up different people's attributes.
  3. 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

Not started

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.

Your turn

Solve this variation. Spot the part that has changed before you look for help.

Practice

Design a stock check

Not started

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.

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.

Next: Use aliases and create calculated columns