Tutorial:Programming
How To Write SQL
We explain how to use Structured Query Language to interrogate databases and to construct
both simple and complex queries. This is the first part of a two part article.
By Pan Pantziarka
S
tructured
Query
Language,
SQL differs from other program
bedded into an application program or
commonly abbreviated to SQL
ming languages in other ways as well.
database front end, but in other cases
and pronounced as sequel, is not
SQL is not compiled or interpreted in
the SQL can be entered and processed
a conventional programming lan
the normal sense of those words there
dynamically. For example it is possible
guage in the normal sense of the
is no SQL compiler as there is with
to write Visual Basic code to act as a
phrase. With SQL you cannot write
C++, for example. Instead SQL code is
user interface to a database and which
applications, utilities, batch programs,
used as a set of instructions which are
contains
predefined SQL queries
GUI interfaces or any of the other
then passed to a database management
which the user can run to interrogate a
things for which you'd use languages
system which acts on the instructions
database, add new records, delete re
such as Visual Basic, C++, Java etc.
and returns a result. This is an impor
cords, make amendments and so on. In
SQL is a language used exclusively to
tant point to grasp: SQL is about data
such a case the SQL code for the data
create, manipulate and interrogate da
and results; each SQL statement re
base access is qualitatively different to
tabases, and as such is likely to be used
turns a result, whether that result be a
the Visual Basic code that surrounds it.
by a greater range of people than any
query, an update to a record or the
of the other computer languages pre
creation of a database table.
Standards
viously mentioned.
In some cases SQL code can be em
In some respects SQL is like other
programming languages. For one
FirstName
First Name
a text string
thing there is a SQL standard, just as
LastName
Last Name
a text string
there are for other programming lan
UserID
Network User ID
a text string
Dept
Department
a text string
guages. SQL was created by IBM in
EmpNo
Employee Number from Payroll system
an integer
1981 but was first standardised in 1986
PCType
Unique identifier for type of PC
a text string
by the American National Standards
Institute (ANSI), which was later
adopted by the International Stand
Figure 1 Format of the UserTable.
ards Organisation (ISO). Both ANSI
and ISO worked on the second SQL
standard in 1989, and this in turn has
ProductName
Name of Product
a text string
Version
Product Version
a text string
been superseded by the 1992 ANSI
ProductID
Unique identifier for product
a text string
standard, usually called SQL 92. Of
Licenses
Number of users licensed
an integer
course, where there are standards
DateOfPurchase
Purchase date from invoicing system
a date
there are also differences, and there are
Cost
Purchase cost of product
a currency code
many implementations of SQL, some
more standard than others. Each of the
Figure 2 Structure of the software table.
database vendors, such as Microsoft,
Sybase, Oracle and others, provides a
SQL implementation with varying de
PCType
Name
a text string
grees of standardisation and may often
Processor
CPU Processor
a text string
feature their own specific extensions to
OpSys
Operating system name
a text string
the language. However, despite the
RAM
MB of RAM
an integer
Disk
GB of hard disk
an floating point
different extensions there is a core part
number
of SQL which is common across all of
ScreenSize
Screen size in inches
a text string
the dialects of the language.
DateOfPurchase
Purchase date from invoicing system
a date code
The purpose of this article is to give
Cost
Cost of purchase
a currency code
an introduction to the key concepts of
SQL, and to provide sample code illus
Figure 3 The PC table.
trating the most common and useful of
Issue 108 (June 1999) page 17
File: T1125.1
PC Network Advisor
Next page >
New! The best sites for quality inkjet printer cartridges and the best sites for cheap inkjet cartridges