May 16, 2021 SQL
Select syntax is used to select data from the database.
The returned data is stored in the result table, called the result set.
In any SQL query statement: SELECT and FROM must be in order. SELECT indicates which columns to see and from identifies the table on which they are located.
The SQL SELECT syntax looks like this:
SELECT column1, column2, ...
FROM table_name;
Here, column1, column2,... I
s the field name of the table from which you want to select the data. I
f you want to select all the fields available in the table, use the following syntax:
SELECT * FROM table_name;
In this tutorial, we'll use the well-known Northwind sample database.
Here's an option for the "Customers" table in the Rosevin 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 |
We will provide examples for the following three use cases
1, retrieve a column
2, retrieving multiple columns
3, retrieve all columns
We'll use the "Customers" table above to illustrate the use of the three use cases.
The following SQL statement picks the "City" column from the "Customers" table:
The following SQL statement selects the "CustomerName" and "City" columns from the "Customers" table:
Note: The two column names are separated by commas in the query. Each time you select more than one column, they must be separated by commas, but they cannot be added after the last column name.
The following SQL statement selects all columns from the "Customers" table:
If you want to select all the columns in the table, you can use , without having to list all the columns in the query.
Most database software systems allow navigation in the result set using programming functions such as Move-To-First-Record, Get-Record-Content, Move-To-Next-Record, and so on.
Features similar to these programming functions are not included in this tutorial. To learn how to access data through function calls, visit our ADO tutorial or PHP tutorial.
Now, take a look at how well you know the SQL Select syntax with the following topics!
SQL Select Multiple Columns: Click here to test
For more questions, please refer to: SQL Quiz Library