SQL > SQL Commands > Select

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

Syntax

Number of ColumnsSQL Syntax
1  SELECT "column_name" FROM "table_name";
More Than 1  SELECT "column_name1"[, "column_name2"] FROM "table_name";
All  SELECT * FROM "table_name";

"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. If you are selecting three columns, the syntax will be,

SELECT "column_name1", "column_name2", "column_name3" FROM "table_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 simply,

SELECT [Math Operation];

Examples

We will provide examples for each of the following four use cases:

Let's use the following table to illustrate all three cases:

Table Store_Information

 Store_Name  Sales  Txn_Date 
 Los Angeles  1500  Jan-05-1999 
 San Diego  250  Jan-07-1999 
 Los Angeles  300  Jan-08-1999 
 Boston  700  Jan-08-1999 

Example 1: Select one column

To select a single column, we specify the column name between SELECT and FROM as follows:

SELECT Store_Name FROM Store_Information;

Result:

Store_Name
Los Angeles
San Diego
Los Angeles
Boston

Example 2: Select multiple columns

We can use the SELECT statement to retrieve more than one column. To select Store_Name and Sales columns from Store_Information, we use the following SQL:

SELECT Store_Name, Sales FROM Store_Information;

Result:

Store_NameSales
Los Angeles1500
San Diego250
Los Angeles300
Boston700

Example 3: 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 *. For example, to select all columns from Store_Information, we issue the following SQL:

SELECT * FROM Store_Information;

Result:

Store_Name Sales Txn_Date
Los Angeles 1500 Jan-05-1999
San Diego 250 Jan-07-1999
Los Angeles 300 Jan-08-1999
Boston 700 Jan-08-1999

Example 4: Math operation

If we want to use the SELECT statement to calculate 2+3, we issue the following SQL:

SELECT 2+3;

Result:

5

Exercises

For these exercises, assume we have a table called Users with the following columns:

Table Users

 Column Name 
 First_Name 
 Last_Name 
 Birth_Date 
 Gender 
 Date_Joined 

1. Which of the following SQL statement is incorrect? (There can be more than one answer)
a) SELECT * FROM Users;
b) SELECT First_Name, Gender, Last_Name FROM Users;
c) SELECT First_Name, Last_Name Users;
d) SELECT All FROM Users;

2. (True Or False) In SQL, the order of the columns in a SELECT statement must be the same as the order of the columns in the underlying table. For example, in the table Users, you must select First_Name before Last_Name.

3. (True Or False) The following two SQL statements are equivalent:
a) Select * From Users;
b) SELECT * FROM Users;

Next: SQL DISTINCT

This page was last updated on October 11, 2024.




Copyright © 2024   1keydata.com   All Rights Reserved     Privacy Policy     About   Contact