Sql Excel : A Basic SQL Query

A good place to start with SQL is with the simplest type of query, one that selects a
column from a table. Consider, once again, the query that returns zip codes along
with the SCF:
SELECT zc.zcta5, LEFT(zc.zcta5, 3) as scf
FROM ZipCensus zc
This query returns a table with two columns, the zip code and the SCF. The rows
might be returned in any order. If you want the rows in a particular order, include
an explicit ORDER BY clause:
SELECT zc.zcta5, LEFT(zc.zcta5, 3) as scf
FROM ZipCensus zc
ORDER BY zc.zcta5
Without an ORDER BY, never assume that the result of a query will be in a particular
order

This simple query already shows some of the structure of the SQL language. All
queries begin with the SELECT clause that lists the columns being returned. The
tables being accessed are in the FROM clause, which follows the SELECT statement.
And, the ORDER BY is the last clause in the query.
This example uses only one table, ZipCensus. In the query, this table has a table
alias, or abbreviation, called zc. The first part of the SELECT statement is taking the
zcta5 column from zc. Although table aliases are optional in SQL, as a rule this
book uses them extensively because aliases clarify where columns come from and
make queries easier to write and to read.

The second column returned by the query is calculated from the zip code itself,
using the LEFT() function. LEFT() is just one of dozens of functions provided by
SQL, and specific databases generally support user-defined functions as well. The
second column has a column alias. That is, the column is named SCF, which is the
header of the column in the output.
A simple modification that returns the zip codes and SCFs only in Minnesota:
SELECT zc.zcta5, LEFT(zc.zcta5, 3) as scf
FROM ZipCensus zc
WHERE stab = MN
ORDER BY 1
The query has an additional clause, the WHERE clause, which, if present, always
follows the FROM clause. The WHERE clause specifies a condition; in this case, that
only rows where stab is equal to “MN” are included in the result set. The ORDER BY
clause then sorts the rows by the first column; the “1” is a reference to the first
column being selected, in this case, zc.zcta5. The preferred method, however, is to
use the column name (or alias) in the ORDER BY clause.
The dataflow corresponding to this modified query is in Figure 1.4. In this
dataflow, the WHERE clause has turned into a filter after the data source, and the
ORDER BY clause has turned into a SORT operator just before the output. Also
notice that the dataflow contains several operators, even for a simple SQL query.
SQL is a parsimonious language; complex operations can often be specified quite
simply.

WARNING
The results of a query are unordered, unless you use an ORDER BY clause at the
outermost level. Never depend on a “default ordering,” because there is not
one.

WARNING
When a column value is NULL, any comparison in a WHERE clause—with the
important exception of IS NULL— always returns unknown, which is treated as
FALSE. So, the clause WHERE stab <> MN really means WHERE stab IS NOT
NULL AND stab <> MN.