SQL SELECT Statement

SQL SELECT Statement : SQL SELECT statement is used to select data from a database.

  • The SELECT statement is used to select data from a database.
  • The data returned is stored in a result table, called the result-set.

 

SELECT Syntax

SELECT column1, column2, …
FROM table_name;

Here, column1, column2, … are the field names of the table you want to select data from. If you want to select all the fields available in the table, use the following syntax:

SELECT * FROM table_name;

Demo Database

Below is a selection from the “Customers” table in the Northwind sample database:

CustomerID CustomerName ContactName Address City PostalCode Country
1 Alfreds Futterkiste Maria Anders Obere Str. 57 Berlin 12209 Germany
2 Ana Trujillo Emparedados y helados Ana Trujillo Avda. de la Constitución 2222 México D.F. 05021 Mexico
3 Antonio Moreno Taquería Antonio Moreno Mataderos 2312 México D.F. 05023 Mexico
4 Around the Horn Thomas Hardy 120 Hanover Sq. London WA1 1DP UK
5 Berglunds snabbköp Christina Berglund Berguvsvägen 8 Luleå S-958 22 Sweden

SELECT Column Example

The following SQL statement selects the “CustomerName” and “City” columns from the “Customers” table:

Example

SELECT CustomerName, City FROM Customers;

Output

Number of Records: 26

CustomerName City
Alfreds Futterkiste Berlin
Ana Trujillo Emparedados y helados México D.F.
Antonio Moreno Taquería México D.F.
Around the Horn London
Berglunds snabbköp Luleå
Blauer See Delikatessen Mannheim
Blondel père et fils Strasbourg
Bólido Comidas preparadas Madrid
Bon app’ Marseille
Bottom-Dollar Marketse Tsawassen
B’s Beverages London
Cactus Comidas para llevar Buenos Aires
Centro comercial Moctezuma México D.F.
Chop-suey Chinese Bern
Comércio Mineiro São Paulo
Consolidated Holdings London
Drachenblut Delikatessend Aachen
Du monde entier Nantes
Eastern Connection London
Ernst Handel Graz
Familia Arquibaldo São Paulo
FISSA Fabrica Inter. Salchichas S.A. Madrid
Folies gourmandes Lille
Folk och fä HB Bräcke
Frankenversand München
France restauration

SELECT * Example

The following SQL statement selects all the columns from the “Customers” table:

Example

SELECT * FROM Customers;

Output

Number of Records: 10

CustomerID CustomerName ContactName Address City PostalCode Country
1 Alfreds Futterkiste Maria Anders Obere Str. 57 Berlin 12209 Germany
2 Ana Trujillo Emparedados y helados Ana Trujillo Avda. de la Constitución 2222 México D.F. 05021 Mexico
3 Antonio Moreno Taquería Antonio Moreno Mataderos 2312 México D.F. 05023 Mexico
4 Around the Horn Thomas Hardy 120 Hanover Sq. London WA1 1DP UK
5 Berglunds snabbköp Christina Berglund Berguvsvägen 8 Luleå S-958 22 Sweden
6 Blauer See Delikatessen Hanna Moos Forsterstr. 57 Mannheim 68306 Germany
7 Blondel père et fils Frédérique Citeaux 24, place Kléber Strasbourg 67000 France
8 Bólido Comidas preparadas Martín Sommer C/ Araquil, 67 Madrid 28023 Spain
9 Bon app’ Laurence Lebihans 12, rue des Bouchers Marseille 13008 France
10 Bottom-Dollar Marketse Elizabeth Lincoln 23 Tsawassen Blvd. Tsawassen T2F 8M4 Canada