SQL SELECT

The SQL SELECT Statement

The SELECT statement is used to query the database and retrieve data that matches criteria that you specify. It is the most frequently used command in SQL.


Syntax

You can select one or more columns from a table.

To select all columns, use the * wildcard:

SELECT * FROM table_name;

To select specific columns, list their names:

SELECT column1, column2, ... FROM table_name;

Demo Database

Throughout this tutorial, we will use a sample Customers table for our examples.

Customers Table:

CustomerID CustomerName ContactName Country
1 Alfreds Futterkiste Maria Anders Germany
2 Ana Trujillo Emparedados Ana Trujillo Mexico
3 Antonio Moreno Taquería Antonio Moreno Mexico
4 Around the Horn Thomas Hardy UK
5 Berglunds snabbköp Christina Berglund Sweden

Example 1: Selecting All Columns

The following SQL statement selects all columns from the Customers table. The result set will contain every row and every column from the table.

SELECT * Example

SELECT * FROM Customers;

Example 2: Selecting Specific Columns

The following SQL statement selects only the CustomerName and Country columns from the Customers table.

SELECT Specific Columns Example

SELECT CustomerName, Country FROM Customers;

The result set would look like this:

CustomerName Country
Alfreds Futterkiste Germany
Ana Trujillo Emparedados Mexico
Antonio Moreno Taquería Mexico
Around the Horn UK
Berglunds snabbköp Sweden

Best Practice: While `SELECT *` is convenient for exploring data, it's generally not recommended for production code. You should always specify the exact columns you need. This makes the query more readable, more efficient (as the database does less work), and less prone to breaking if the table structure changes in the future.