SQL Select

The SELECT statement is how you read data from a database, choosing exactly which columns and rows you want back.

The SELECT statement

SELECT is the statement you will use more than any other. It asks the database to return data without changing anything, which makes it safe to run and experiment with. A SELECT statement names the columns you want and, with FROM, the table those columns live in. The database then hands back a result set: a temporary table of rows that match your request.

To read specific columns, list their names after SELECT, separated by commas. The query below returns the name and country of every customer.

Example

SELECT CustomerName, Country
FROM Customers;

Selecting all columns

When you want every column in a table, you can use the asterisk symbol (*) as a shortcut instead of typing out each column name. This is handy while exploring a table you do not know well yet.

Example

SELECT * FROM Customers;
Note: Using * is convenient for quick exploration, but in real applications it is usually better to name the columns you actually need. That way your query returns less data and keeps working even if new columns are added later.

Reading the result set

The result of a SELECT is arranged in rows and columns, one row for each matching record. For example, running a query against the Customers table might return data like the following.

CustomerNameCityCountry
Maria AndersenBerlinGermany
Thomas HardyLondonUK
Elizabeth LincolnVancouverCanada

The order of columns in the result follows the order you list them after SELECT, so you control how the output looks. To narrow the rows that come back rather than the columns, you add a condition with the WHERE clause. To remove repeated values from a column, see SQL Select Distinct.

Exercise: SQL Select

What is the purpose of the SELECT statement?