There are 5 articles on this title. You are reading the article ranked and rated #5 by Helium's members.
The most common query on a database is to retrieve data from the database tables. In SQL, the SELECT statement performs this task. This article describes the basic structure of a SELECT statement, and how to use it to retrieve specific rows and columns from a table, and group and filter them.
A SELECT statement can be broken down into numerous elements, each beginning with a keyword. The basic elements of a SELECT statement are as follows:
SELECT
FROM
WHERE
ORDER BY
Based on the above keywords, a basic SELECT statement may look like
SELECT FROM WHERE ORDER BY
where, the angular brackets (< >) denote the variables specific to the query being performed. Now let us examine the SELECT statement more closely.
Throughout this article, the following sample from the Employee' table of the MS Access sample database NorthWind', is used for examples:
ID LastName FirstName Title City Country ReportsTo
-
1 Davolio Nancy Sales Representative Seattle USA Fuller, Andrew
2 Fuller Andrew Vice President, Sales Tacoma USA
3 Leverling Janet Sales Representative Kirkland USA Fuller, Andrew
4 Peacock Margaret Sales Representative Redmond USA Fuller, Andrew
5 Buchanan Steven Sales Manager London UK Fuller, Andrew
6 Suyama Michael Sales Representative London UK Buchanan, Steven
7 King Robert Sales Representative London UK Buchanan, Steven
8 Callahan Laura Sales Coordinator Seattle USA Fuller, Andrew
9 Dodsworth Anne Sales Representative London UK Buchanan, Steven
The SELECT ... FROM Clause
-
The most basic SELECT statement specifies what columns have to be retrieved from what table(s). For example, to retrieve all the first and last names from the Employee' table, write the query as
SELECT FirstName, LastName FROM Employee
This query returns the values
FirstName LastName
-
Nancy Davolio
Andrew Fuller
Janet Leverling
Margaret Peacock
Steven Buchanan
Michael Suyama
Robert King
Laura Callahan
Anne Dodsworth
In the result set, the columns will be in the same order as in the query. In the above example, the result displays FirstName before LastName, even though the table stores FirstName after LastName.
Few useful symbols and keywords:
To retrieve all the columns from the table, use the
Below are the top articles rated and ranked by Helium members on:
by bluephin
SQL or Structured Query Language is an ANSI standard for manipulating and accessing database systems. SQL can retriev... read more
by Joseph Love
In my 20+ years in I.T., I've taught several developers how to interact with a database. Although I could teach a 3 w... read more
by Ray Cook
SQL (Structured Query Language, pronounced SEQUEL) has been around now for about 30 years. SQL provides a n English-... read more
by Holly Styles
Before we dive into the basics of SQL it is pertinent to give a little background on it's origins. SQL was originally... read more
by Meera
The most common query on a database is to retrieve data from the database tables. In SQL, the SELECT statement perfor... read more
Add your voice
Know something about Retrieving data with SQL queries: An introduction?
We want to hear your view.
Write now!
Already a member? Log in.
Cast your vote!
Click for your side. Must be logged in.
Featured Partner
Time 4A Change (T4AC) is committed to educating citizens about social issues and mobilizing those citizens as partici...more
hide