Search Helium

Home > Computers & Technology > Software > Software & Web Development

Retrieving data with SQL queries: An introduction

by Holly Styles

Created on: January 22, 2008

Before we dive into the basics of SQL it is pertinent to give a little background on it's origins. SQL was originally coined SEQUEL by IBM in about 1975. It stands for Structured English QUEry Language. At the time, IBM was researching the commercial viability of a relational database system, based on the work of Dr E. Codd and his seminal paper on the relational model published in 1970. SQL is still often pronounced 'see-quel' even today, although the more common term now is 'es-que-el.'

There are many relational database systems now available. The most well known arguably being: Microsoft SQL Server, Oracle, MySql and PostgreSQL. Each system has it's own proprietry implementation of the SQL language. But they all support the SQL99 standard at a minimum.The latest standard at the time of writing is SQL 2006. Each vendor also supplies a command line or GUI client into which SQL queries can be typed and executed against the data stored in the database. Check your documentation for how to use it.

The most basic of SQL Queries is the single select statement. Before we see how to create one and see what it does, it is helpful to describe a database table against which we can work.

tbName
dbNameId dbFirstName dbLastName dbMiddleName
1 John Smith Andrew
2 John Doe NULL
3 Susan Wells Alice
4 Denise Taylor Trudy

The table is a convenient way of representing a set of tuples (rows) in a relation (table). The first column is the key that uniquely identifies each tuple, known as the Primary Key. The other columns are the attributes of each tuple. It is possible for an attribute to be unknown, John Doe's middle name for example is unknown. Unknown attributes are represented as 'NULL' in relational databases.

You can create this table in your database using the following SQL.

CREATE TABLE tbName
(
dbNameId int identity(1,1) Primary Key,
dbFirstName varchar(32) NOT NULL,
dbLastName varchar(32) NOT NULL,
dbNiddleName varchar(32) NULL
)

You can then populate the table with data using INSERT statements.

INSERT INTO tbName (dbFirstName, dbLastName, dbMiddleName)
VALUES('John', 'Smith', 'Andrew')

INSERT INTO tbName (dbFirstName, dbLastName, dbMiddleName)
VALUES('John', 'Doe', NULL)

INSERT INTO tbName (dbFirstName, dbLastName, dbMiddleName)
VALUES('Susan', 'Wells', 'Alice')

INSERT INTO tbName (dbFirstName, dbLastName, dbMiddleName)
VALUES('Denise', 'Taylor', 'Trudy')

To retrieve all the rows from the table, or more correctly, retrieve all the attributes of each tuple in the tbName relation. We use the

Helium Debate

Cast your vote!

Is Microsoft Internet Explorer 8's version targeting good or bad?

Click for your side.

136151

Featured Partner

MENTOR - National Mentoring Partnership

MENTOR has partnered with Helium, giving you the chance to write for a cause. Browse MENTOR's featured titles, pick an issue and write! You can also donate your article earnings. Share what you know, learn new perspectives...more


CONNECT WITH US

Read
our blog
Helum for writers

Write and get published
Share with other writers
Polish your freelancing skills

Join our active writing community
Helium Content Source for Publishers

Quality articles from proven freelancers
Exclusive rights, fast turnaround
Brand engagement, business blogging -- our writers do it all

Get custom content today!

INFORMATION


Helium, Inc.
200 Brickstone Square Andover, MA 01810 USA
#