|
The SELECT statement is used to retrieve data from a table. The general form is as under.
Syntax:
Select fieldname1, fieldname2, fieldname3,…
from tablename;
When retrieving multiple columns, specify each field name. A comma must separate field names. The columns will display in the order you select them.
To select the content of columns named " FirstName" and " LastName" and “Email”, from the database table called " empinfo", use a SELECT statement like this:
empinfo
FirstName
|
LastName
|
Email
|
DOB
|
Steve
|
Martin
|
smart.steve@hotmail.com
|
08-04-1988
|
David
|
Fernandas
|
davidfernandas@studiesinn.com
|
04-16-1977
|
Ajay
|
Rathor
|
ajayrathor@studiesinn.com
|
08-24-1986
|
SELECT FirstName, LastName FROM empinfo;
FirstName
|
LastName
|
Email
|
Steve
|
Martin
|
smart.steve@hotmail.com
|
David
|
Fernandas
|
davidfernandas@studiesinn.com
|
Ajay
|
Rathor
|
ajayrathor@studiesinn.com
|
Selecting All Columns:
To get all columns of a table without typing all column names, use:
SELECT * FROM empinfo;
FirstName
|
LastName
|
Email
|
DOB
|
Steve
|
Martin
|
smart.steve@hotmail.com
|
08-04-1988
|
David
|
Fernandas
|
davidfernandas@studiesinn.com
|
04-16-1977
|
Ajay
|
Rathor
|
ajayrathor@studiesinn.com
|
08-24-1986
|
The Result Set
When a SQL query returns one or more rows of data, you can use the ResultSet object to get the description of the result set and to navigate within the rows of the results to retrieve the data.
SELECT DISTINCT
The DISTINCT command is used with SELECT keyword to retrieve only unique data entries depending on the column list you have specified.
empinfo
FirstName
|
LastName
|
Email
|
DOB
|
Steve
|
Martin
|
smart.steve@hotmail.com
|
08-04-1988
|
David
|
Fernandas
|
davidfernandas@studiesinn.com
|
04-16-1977
|
Ajay
|
Rathor
|
ajayrathor@studiesinn.com
|
08-24-1986
|
Steve
|
Martin
|
smart.steve@hotmail.com
|
08-04-1988
|
David
|
Fernandas
|
davidfernandas@studiesinn.com
|
04-16-1977
|
SELECT DISTINCT FirstName from empinfo;
The Result will be.
FirstName
|
LastName
|
Email
|
DOB
|
Steve
|
Martin
|
smart.steve@studiesinn.com
|
08-04-1988
|
David
|
Fernandas
|
davidfernandas@studiesinn.com
|
04-16-1977
|
Ajay
|
Rathor
|
ajayrathor@studiesinn.com
|
08-24-1986
|
|