Coding With Fun
Home Docker Django Node.js Articles Python pip guide FAQ Policy

SQL Select syntax


May 16, 2021 SQL


Table of contents


SQL SELECT syntax


Select syntax is used to select data from the database.

The returned data is stored in the result table, called the result set.

Basic syntax: SELECT and FROM

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;

Demonstrate the database


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

SELECT Column instance


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.

SELECT retrieves a column

The following SQL statement picks the "City" column from the "Customers" table:

SELECT City FROM Customers;

SELECT retrieves multiple columns

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

SELECT CustomerName, City FROM Customers;

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.

SELECT - Instance - Retrieves all columns

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

SELECT * FROM Customers;

If you want to select all the columns in the table, you can use , without having to list all the columns in the query.


Navigation in the result set


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.


Chapter test


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