SELECT (Databases)

A SELECT is the fundamental statement of structured query language (SQL).

The SELECT statement, which follows a consistent and specific format, begins with the SELECT keyword followed by the columns to be included in the format. If an asterisk (*) is placed after SELECT, this sequence is followed by the FROM clause that begins with the keyword FROM, followed by the data sources containing the columns specified after the SELECT clause. These data sources may be a single table, combination of tables, subquery or view.

Optional clauses may be added but are not mandatory, i.e., the WHERE clause that gives conditions for returning data, or the ORDER BY clause that sorts output with one or more of the specified columns.

One of the first database administration lessons is the SELECT statement, which forms the beginning of any SQL script used to query data. SELECT is the first keyword in the SELECT statement, which, like all SQL statements, is not case-sensitive.

To illustrate the SELECT statement in an example, assume that a bank database contains a CUSTOMER_MASTER table that stores basic customer details and contains several columns named as follows:
  • customer_id
  • social_security_no
  • surname
  • firstname
  • email_address
  • physical_address
  • date_of_birth
  • gender

The following SELECT statement is used to query all table data:

SELECT * FROM customer_master.

The following SELECT statement is used to sort results by customer surnames:

SELECT * FROM customer_master ORDER BY surname

To list customer surnames, first names and dates of birth, the asterisk (*) is replaced with the corresponding column names, as follows:

SELECT surname, firstname, date_of_birth FROM customer_master

To run a query of all female customers sorted by date of birth, the following statement is issued:

SELECT * FROM customer_master WHERE gender=’F’ ORDER BY date_of_birth

Note: The WHERE clause is now used to restrict output.

This explanation is a simple primer that demonstrates the power of the SELECT statement and may be used to build complex and elaborate queries beyond this scope. However, all SELECT statements, regardless of scope, are required to consistently follow the basic rules outlined above.

Post a Comment

0 Comments