Usage of SELECT statement in SQL

The SELECT statement in SQL is used to retrieve data from a relational database.

Syntax for to select one column:

SELECT “column_name” FROM “table_name”;

Syntax for to select more than one column:

SELECT “column_name1″[,”column_name2”] FROM “table_name”;

Syntax for to select ALL :

SELECT * FROM “table_name”;

In the above all 3 syntax “table_name” is the name of the table where data is stored and “column_name” is the name of the column containing the data to be retrieved.

To select more than one column, add a comma to the name of the previous column and then add the column name.

NOTE : There is no comma after the last column selected.

SPECIAL CASE:

Note that the FROM keyword appears in all of the scenarios above, since the FROM keyword is used to indicate which table(s) one is retrieving the data from. There is one special case where FROM does not exist, and that is when you are doing a mathematical operation. In this case, the syntax is :

SELECT [Math_Operation];

Let’s use the following table to illustrate all three examples

Table Customers

Example1: To select one column

To select a single column, we need to specify the column name between SELECT and FROM

SELECT first_name FROM Customers;

Result:

Example2: To select multiple columns

We can use the SELECT statement to retrieve more than one column. To select first_name and last_name columns from Customers, we use the following SQL query:

SELECT first_name,last_name FROM Customers;

Result:

Example3: To select all columns

There are two ways to select all columns from a table. The first is to list the column name of each column. The second, and the easier, way is to use the symbol *.

SELECT * FROM Customers

Result:

Example4: For Math Operation

SELECT 9+5;

Result:

14

Leave a Comment

Your email address will not be published. Required fields are marked *