Documenti di Didattica
Documenti di Professioni
Documenti di Cultura
What Is SQL?
SQL (pronounced as the letters S-Q-L or as sequel) is an abbreviation for Structured Query
Language. SQL is a language designed specifically for communicating with databases.
Unlike other languages (spoken languages like English, or programming languages like Java
or Visual Basic), SQL is made up of very few words. This is deliberate. SQL is designed to
do one thing and do it wellprovide you with a simple and efficient way to read and write
data from a database.
What are the advantages of SQL?
SQL is not a proprietary language used by specific database vendors. Almost every
major DBMS supports SQL, so learning this one language will enable you to interact
with just about every database you'll run into.
SQL is easy to learn. The statements are all made up of descriptive English words,
and there aren't that many of them.
Despite its apparent simplicity, SQL is actually a very powerful language, and by
cleverly using its language elements you can perform very complex and sophisticated
database operations
Use of White Space All extra white space within a SQL statement is ignored when that
statement is processed. SQL statements can be specified on one long line or broken up over
many lines. Most SQL developers find that breaking up statements over multiple lines makes
them easier to read and debug.
SELECT *
FROM Products;
When a wildcard (*) is specified, all the columns in the table are returned. The column order
will typically, but not always, be the physical order in which the columns appear in the table
definition. However, SQL data is seldom displayed as is. (Usually, it is returned to an
application that formats or presents the data as needed.) As such, this should not pose a
problem.
As a rule, you are better off not using the * wildcard unless you really do need every column in the
table. There is one big advantage to using wildcards. As you do not explicitly specify column names
(because the asterisk retrieves every column), it is possible to retrieve columns whose names are
unknown.
Sorting Data
As you learned in the last lesson, the following SQL statement returns a single column from a
database table. But look at the output. The data appears to be displayed in no particular order at all.
SELECT prod_name
FROM Products;
prod_name
-------------------Fish bean bag toy
Bird bean bag toy
Rabbit bean bag toy
8 inch teddy bear
12 inch teddy bear
18 inch teddy bear
Raggedy Ann
King doll
Queen doll
Actually, the retrieved data is not displayed in a mere random order. If unsorted, data will
typically be displayed in the order in which it appears in the underlying tables. This could be
the order in which the data was added to the tables initially. However, if data was
subsequently updated or deleted, the order will be affected by how the DBMS reuses
reclaimed storage space. The end result is that you cannot (and should not) rely on the sort
order if you do not explicitly control it. Relational database design theory states that the
sequence of retrieved data cannot be assumed to have significance if ordering was not
explicitly specified.
Clause SQL statements are made up of clauses, some required and some optional. A clause
usually consists of a keyword and supplied data. An example of this is the SELECT statement's
FROM clause, which you saw in the last lesson.
To explicitly sort data retrieved using a SELECT statement, the ORDER BY clause is used. ORDER BY
takes the name of one or more columns by which to sort the output. Look at the following example:
SELECT prod_name
FROM Products
ORDER BY prod_name;
This statement is identical to the earlier statement, except it also specifies an ORDER BY
clause instructing the Database Management System software to sort the data alphabetically
by the prod_name column. The results are as follows:
prod_name
-------------------12 inch teddy bear
18 inch teddy bear
8 inch teddy bear
Bird bean bag toy
Fish bean bag toy
King doll
Queen doll
Rabbit bean bag toy
Raggedy Ann
Position of ORDER BY Clause When specifying an ORDER BY clause, be sure that it is the
last clause in your SELECT statement. Using clauses out of order will generate an error
message.
Sorting by Nonselected Columns More often than not, the columns used in an ORDER BY clause
will be ones that were selected for display. However, this is actually not required, and it is perfectly
legal to sort data by a column that is not retrieved
prod_id
prod_price
prod_name
-------
----------
--------------------
BNBG02
3.4900
BNBG01
3.4900
BNBG03
3.4900
RGAN01
4.9900
Raggedy Ann
BR01
5.9900
BR02
8.9900
RYL01
9.4900
King doll
RYL02
9.4900
Queen doll
BR03
11.9900
It is important to understand that when you are sorting by multiple columns, the sort sequence
is exactly as specified. In other words, using the output in the example above, the products
are sorted by the prod_name column only when multiple rows have the same prod_price value. If
all the values in the prod_price column had been unique, no data would have been sorted by
prod_name.
prod_id
prod_price
prod_name
-------
----------
--------------------
BNBG02
3.4900
BNBG01
3.4900
BNBG03
3.4900
RGAN01
4.9900
Raggedy Ann
BR01
5.9900
BR02
8.9900
RYL01
9.4900
King doll
RYL02
9.4900
Queen doll
As you can see, the output is identical to that of the query above. The difference here is in the
ORDER BY clause. Instead of specifying column names, the relative positions of selected
columns in the SELECT list are specified. ORDER BY 2 means sort by the second column in the
SELECT list, the prod_price column. ORDER BY 2, 3 means sort by prod_price and then by
prod_name.
The primary advantage of this technique is that it saves retyping the column names. But there are
some downsides too. First, not explicitly listing column names increases the likelihood of you
mistakenly specifying the wrong column. Second, it is all too easy to mistakenly reorder data when
making changes to the SELECT list (forgetting to make the corresponding changes to the ORDER BY
clause). And finally, obviously you cannot use this technique when sorting by columns that are not in
the SELECT list.
prod_price
prod_name
-------
----------
--------------------
BR03
11.9900
RYL01
9.4900
King doll
RYL02
9.4900
Queen doll
BR02
8.9900
BR01
5.9900
RGAN01
4.9900
Raggedy Ann
BNBG01
3.4900
BNBG02
3.4900
BNBG03
3.4900
But what if you were to sort by multiple columns? The following example sorts the products
in descending order (most expensive first), plus product name:
SELECT prod_id, prod_price, prod_name
FROM Products
ORDER BY prod_price DESC, prod_name;
prod_id
prod_price
prod_name
-------
----------
--------------------
BR03
11.9900
RYL01
9.4900
King doll
RYL02
9.4900
Queen doll
BR02
8.9900
BR01
5.9900
RGAN01
4.9900
Raggedy Ann
BNBG02
3.4900
BNBG01
3.4900
BNBG03
3.4900
The DESC keyword only applies to the column name that directly precedes it. In the example
above, DESC was specified for the prod_price column, but not for the prod_name column.
Therefore, the prod_price column is sorted in descending order, but the prod_name column
(within each price) is still sorted in standard ascending order.
Sorting Descending on Multiple Columns If you want to sort descending on multiple
columns, be sure each column has its own DESC keyword.
It is worth noting that DESC is short for DESCENDING, and both keywords may be used. The
opposite of DESC is ASC (or ASCENDING), which may be specified to sort in ascending order.
In practice, however, ASC is not usually used because ascending order is the default sequence
(and is assumed if neither ASC nor DESC are specified).