Lesson 4 +10 XP

SELECT Statement

The SELECT statement is used to retrieve data from a database. The data is returned as a table of results.

Select specific columns


sql

### SELECT CustomerName, Country FROM Customers;

This returns only the CustomerName and Country columns.

Select all columns


sql

### SELECT * FROM Customers;

The * wildcard returns every column.

Without FROM

Some databases let you run SELECT without a table to compute a value:


sql

### SELECT 5 + 5; - returns 10 (MySQL)

### SELECT NOW(); - current date and time

Real example output


sql

### SELECT CustomerName, Country FROM Customers;
CustomerNameCountry
Alfreds FutterkisteGermany
Ana TrujilloMexico
Antonio MorenoMexico

Column order matters

The columns come back in the order you list them, so SELECT Country, CustomerName returns Country first.

TL;DR

  • SELECT column1, column2 returns chosen columns.
  • SELECT * returns every column.
  • Results come back as a table in the order you list them.
  • You can compute values with SELECT too.