You are on page 1of 33

Database Concepts

Lesson objectives
To be able to understand database concepts, you will:

· Explain the term database and describe what it typically consists of.

· Explain general database concepts and terminology.


Introduction to Database Concepts
A database is a collection of related information (related in the sense that the data is about a
single topic). An example of a commonly used database is the telephone book; it contains
information about people's names, addresses, and telephone numbers.
Database design is both an art and a science. Entire careers are built on teaching, refining, using,
and writing about design theories. Although this is not the focus of this course, we will take a
moment to cover some commonly used terminology and guidelines.
Typically, a database consists of one or more tables of information organized for a specific
purpose. Among other things, it may also contain associated forms, queries, and reports.
A table is a collection of rows that have associated columns. Each table within a database must
have a unique name. Also, each table should contain one or more columns that uniquely identify
a row within a table. These columns are referred to as primary keys.
Vertical categories of information within a table are called columns or fields. A column contains a
single attribute or characteristic of a subject. For example, there are four columns in a phone
book: last name, first name, address, and phone number. Each column must have a unique name.
A row or record represents all of the columns for one item in a table. For example, in a phone
book, a row would consist of a last name, a first name, an address, and a phone number of one
person.
Although it is true that databases are made up of related information, that is not where the term
"relational database" has its origins. Originally, two-dimensional databases or flat files were
used. Data was stored in a single table consisting of a series of columns and rows. That sounds
very neat and manageable doesn't it? However, over time, it was discovered that data has a
tendency to grow and change constantly. A more complex method of managing the data was
needed.
Dr. Edgar (E.F.) Codd, a mathematician at IBM in the 1960's, was researching new and better
ways to handle large amounts of data. His background as a mathematician led him to apply
mathematical set theory to develop a solution. In 1970, he published his work in the paper A
Relational Model of Data for Large Shared Databanks.
In mathematical set theory, a relation is the equivalent of a table. Therefore, a bank of data held
in relations, or tables, is referred to as a relational database.
The Bindery Database
Throughout this course you will be working with a single database. This database was created for
a hypothetical book-publishing company called The Bindery. Illustrated below is a diagram of
The Bindery's database of information.
1. What is a database and what does it typically consist of?
A database is a collection of related information, meaning that the data is about a single topic.
Typically it consists of one or more tables of information organized for a specific purpose. It may
also contain associated forms, queries and reports.
2. What is a "relational database"?
A table is used to store data that is arranged in a grid of rows and columns. In mathematical set
theory, the definition of a table is a relation. Therefore, a bank of data held in relations became a
relational database. The word relational has nothing to do with the fact that you can relate or
connect the data in different tables or the fact that you define relationships between
Understanding SQL Concepts
Lesson objectives
To be able to understanding SQL Concepts, you will:

· List and describe the three language components of SQL.

· Explain the function of the SQL Server Query Analyzer.


SQL Concepts
If you were an employee of The Bindery and it was your job to work with The Bindery's
database, what might that involve? You might need to insert new information in the database. For
example, you might need to insert information when a new book is created.
Another task you might have is updating information. For example, an upcoming book's
publishing date might slip, requiring the database information to be modified.
Even more regularly, there are business questions that must be answered: How many books did
we sell in June?; What is our most expensive book?; Which sales person is selling the most
books? Bindery employees might be relying on you to use the database to help answer important
questions like these.
This is where the SQL language is useful. SQL is a tool used to work with databases. Just as
Latin is the "root" of many languages, SQL is the "root" of database communication. In other
words, just as Latin helps in our understanding of other languages, SQL helps in our
understanding of databases.
Originally SQL was an acronym for Structured Query Language. However, like all technology,
as the years have passed the language has expanded to serve a larger purpose than simply
querying.
SQL is made up of three major language components: Data Manipulation Language (DML),
Data Definition Language (DDL), and Data Control Language (DCL). In other words, you could
define SQL with following equation:
SQL = DML + DDL + DCL
What exactly does this alphabet soup mean? Let's take a look at each of these language
components.
Roll your cursor over the three language components listed below to learn more about them and
the statements associated with each...
This course focuses on the SELECT statement. Yes, it appears to be a very tiny portion of the
language. However, it is perhaps the most commonly used statement of the entire language.
The SELECT statement enables you to query a database. A query is a request for retrieval of
certain data from a database. Queries can be used to answer business questions, like the ones
mentioned earlier in this topic.
An Introduction to Client/Server Architecture

A client/server architecture is made up of a server and one or more clients. In this model,
processing functions are distributed between the server and client to provide greater efficiency.
The server, or back-end, is responsible for such tasks as optimizing and parsing queries,
processing data and queries, and returning the results.
A client, or front-end, is usually a PC. This computer contains software to enable users to access
and work with the data on the server. For example, with a client you can write and send queries
to the server, and analyze the results when they are returned with such tools as graphs and
reports.
In developing this course, a client/server architecture was used. Microsoft SQL Server was the
software running on the back-end to enable that computer to understand and process requests.
SQL Server Query Analyzer was the application running on the front-end to provide an
environment in which to write the requests and view the results. Transact-SQL was the language
version used to compose the requests.
Keep in mind that this is only one example of a database management system. Other software is
available for the server, such as Oracle. Similarly, other software is available for the client, such
as Access. Finally, there are even other variations of the SQL language.
In an effort to maintain some similarity among language versions, the American National
Standards Institute (ANSI) is responsible for setting standards for languages such as SQL.
Transact-SQL is extremely close to ANSI-SQL; therefore, it provides a good introduction to the
language. This course is primarily concerned with those standards that are applicable across
software products and generally refrains from discussing product-specific information.
Using a Query Editor to Access a Database
The software running on a client is sometimes referred to as the front-end application. The
phrase front-end refers to the fact that the application provides a user-friendly tool to
communicate with the server working behind-the-scenes. As mentioned earlier, there are many
front-end applications available to create SQL statements. Each of these applications may look
and work slightly different from its counterparts; however, the overall purpose of each is the
same. The examples of front-end applications you will see in this course are SQL Server Query
Analyzer, and Microsoft Access.
An Introduction to the SQL Server Query Analyzer

The SQL Server Query Analyzer is the front-end application used in developing this course. It
provides a generic, yet user-friendly, editing environment in which to compose SQL statements.
Follow the steps given below to learn how to work with the SQL Server Query Analyzer...
1. List and describe the three language components that currently make up SQL.
The three language components are: DML (Data Manipulation Language) that provides
statements to enable the user to query, update, delete, or insert database information. DDL (Data
Definition Language) that provides statements to enable a database designer to create and delete
such database objects as tables, views, and indexes. DCL (Data Control Language) that provides
statements to enable a database administrator to grant and revoke privileges to the database.
2. What is the function of the SQL Server Query Analyzer?
It is the front-end application used to provide a generic, user-friendly editing environment in
which to compose SQL statements. It is a front-end tool and it communicates with the server that
is working behind the scenes.
An Overview of the SELECT Statement
Lesson objectives
To be able to use a select statement, you will:

· List and explain the mandatory clauses in a SELECT statement.

· List the optional clauses that can be used in a SELECT statement


Introduction
Querying is one of the most common uses of SQL. In fact, the SELECT statement, which is used
to query data, is thought of as the basic element or heart of the SQL language. SQL is popular
because it is a non-procedural language, meaning that you need only specify which data to find,
not how to find it. That's relief isn't it? This allows you more freedom from knowing precisely
how the database is designed.
Mandatory Clauses
Every SELECT statement must contain at least two clauses: the SELECT clause and the FROM
clause. You use the SELECT clause to specify which column(s) you want to retrieve. You use the
FROM clause to specify the table(s) in which the column(s) is located. For example, using the
Bindery database, suppose you want to find the titles of all books published by the company:
A generic example of the syntax:
SELECT col1, col2, col3 (to choose which columns)
FROM table (to choose the table containing
those columns)
An applied example:
SELECT bktitle
FROM titles
Optional Clauses
The SELECT statement can be made more useful by adding other clauses. There are many such
clauses available, but only some of these are universal and can be found in most variations of
SQL. Others are specific to the front-end application or language version being used. For this
reason, this course is concerned primarily with those clauses that are universal. The order of
these optional clauses is important. They must be used in the same order you see them listed
below. However, it is not necessary to use all optional clauses in a single SELECT statement.
The examples below illustrate acceptable variations; however, notice in all examples that the
order shown below is preserved.
Learn about the different ways to use Optional Clauses in SELECT statements by observing the
examples given below
1. Explain the mandatory clauses in a SELECT statement.
The SELECT statement contains two mandatory clauses; the SELECT clause and the FROM
clause. The SELECT clause is used to specify which columns you want to retrieve and the
FROM clause is to specify the tables in which the columns are located.
2. List some of the more universal optional clauses that can be used in SELECT statements.
Some optional clauses are:
WHERE

Syntax Rules You'll Want to Know


Lesson objectives
To be able to understand syntax rules, you will:

· Explain the syntax rules to follow when writing SELECT statements.


· State the syntax for viewing all columns in the Output.
Writing Proper Syntax
When you write a SELECT statement, it is important to be aware of syntax rules. The queries
you will see within topics throughout this book will emphasize keywords, such as SELECT and
FROM, with capitalization. This is done only for emphasis and clarity and is not necessary when
you write an SQL statement.
Some syntax rules that will be important to follow when using the SELECT and FROM clauses
are:

· Each keyword is followed by a space.

· In the SELECT clause, if more than one column is listed, each column must be separated
by a comma (,).
After these rules are followed, you are left to your own creativity. For example, columns must be
separated by a comma; however, once that is done, you can decide whether they will also have a
space between them, or no space between them, or if they will be written on separate lines. Your
goal is to achieve clarity. Use your best judgement to write statements that are easy to read and
understand.
Each of the following SELECT clauses is acceptable:
· SELECT bktitle, devcost, pubdate
· SELECT bktitle,devcost,pubdate
· SELECT bktitle,
devcost,
pubdate
Throughout this book, queries are written with each clause beginning on its own line. The second
bullet below is an example of this style. However, this style was chosen for clarity and is not
required. Both of the following queries are correct:
· SELECT bktitle, devcost FROM titles
· SELECT bktitle, devcost
FROM titles
Rearranging the Order of the Results
The order of the columns in the SELECT clause determines the order in which the results are
displayed. Therefore, the user has control over this display. In the SELECT clause, list the
columns in the order in which you desire to see them in the results.
Viewing all Columns in the Output
In addition to retrieving one or two columns from a table, it might be necessary to retrieve all
columns from a table. This type of SELECT statement would need to include all column names
and, depending on the size of the table, this could be a large amount of typing. You will be glad
to know that the asterisk (*) can be used as shorthand. Use the asterisk when you want to retrieve
all columns of information in the table listed in the FROM clause.
Generic example:
SELECT *
FROM table
Applied example:
SELECT *
FROM titles
The order of the columns displayed in the result is identical to the order in which the columns
appear in the original table. You have no control over the ordering of the display when the
asterisk is used.
1. What are the syntax rules to follow when writing SELECT statements?
The syntax rules to follow are: Each keyword should be followed by a space. In the SELECT
clause, if more than one column is listed, each column must be separated by a comma.
2. Write the syntax for viewing all the columns of information in a table.
SELECT * FROM table
Syntax Rules You'll Want to Know
Lesson objectives
To be able to understand syntax rules, you will:

· Explain the syntax rules to follow when writing SELECT statements.

· State the syntax for viewing all columns in the Output.


Writing Proper Syntax
When you write a SELECT statement, it is important to be aware of syntax rules. The queries
you will see within topics throughout this book will emphasize keywords, such as SELECT and
FROM, with capitalization. This is done only for emphasis and clarity and is not necessary when
you write an SQL statement.
Some syntax rules that will be important to follow when using the SELECT and FROM clauses
are:
· Each keyword is followed by a space.

· In the SELECT clause, if more than one column is listed, each column must be separated
by a comma (,).
After these rules are followed, you are left to your own creativity. For example, columns must be
separated by a comma; however, once that is done, you can decide whether they will also have a
space between them, or no space between them, or if they will be written on separate lines. Your
goal is to achieve clarity. Use your best judgement to write statements that are easy to read and
understand.
Each of the following SELECT clauses is acceptable:
· SELECT bktitle, devcost, pubdate
· SELECT bktitle,devcost,pubdate
· SELECT bktitle,
devcost,
pubdate
Throughout this book, queries are written with each clause beginning on its own line. The second
bullet below is an example of this style. However, this style was chosen for clarity and is not
required. Both of the following queries are correct:
· SELECT bktitle, devcost FROM titles
· SELECT bktitle, devcost
FROM titles
Rearranging the Order of the Results
The order of the columns in the SELECT clause determines the order in which the results are
displayed. Therefore, the user has control over this display. In the SELECT clause, list the
columns in the order in which you desire to see them in the results.
Viewing all Columns in the Output
In addition to retrieving one or two columns from a table, it might be necessary to retrieve all
columns from a table. This type of SELECT statement would need to include all column names
and, depending on the size of the table, this could be a large amount of typing. You will be glad
to know that the asterisk (*) can be used as shorthand. Use the asterisk when you want to retrieve
all columns of information in the table listed in the FROM clause.
Generic example:
SELECT *
FROM table
Applied example:
SELECT *
FROM titles
The order of the columns displayed in the result is identical to the order in which the columns
appear in the original table. You have no control over the ordering of the display when the
asterisk is used.
. What are the syntax rules to follow when writing SELECT statements?
The syntax rules to follow are: Each keyword should be followed by a space. In the SELECT
clause, if more than one column is listed, each column must be separated by a comma.
2. Write the syntax for viewing all the columns of information in a table.
SELECT * FROM table
Aggregate Functions
Lesson objectives
To be able to use Aggregate functions, you will:

· Explain the purpose of Aggregate functions.

· Explain the purpose of the DISTINCT keyword.

Introduction
Up to this point, you have been writing queries that return data exactly as it is stored in the
Bindery database. The results are very straightforward, but limited in what they can readily
communicate. For example, suppose you want to know what the average sale price is for a
Bindery book? You could write a query to return all the sales prices, get out your trusty
calculator, and perform the math yourself; or, you could write a query to do that for you and
simply return the average sale price. The latter option certainly sounds more appealing. It
involves using aggregate functions.
An aggregate function produces a single, summary value for a group of values in a specified
column.

Writing Statements Containing Aggregate Functions

You can use aggregate functions in two clauses: the SELECT clause and the HAVING clause.
Aggregates require an expression, which is enclosed in parentheses. In general, aggregate
functions are written as follows:
aggregate_function(expression)
An expression is usually a column name, or any combination of column names, constants, and
functions connected by arithmetic operators. It might be helpful to think of the parentheses, (), as
"of." In other words, sum(devcost) could be thought of as "the sum OF the devcost column."
Throughout this book, aggregate function names are capitalized within the topic sections. This is
done only for clarity and is not required syntax.
The SUM and AVG functions will operate only on numeric values and therefore require numeric
expressions. The MAX, MIN, and COUNT functions will operate on all types of data.
Aggregate Functions and NULL Values
Now that you have been introduced to aggregate functions, there is something you should know
about how they work:
Aggregate functions ignore NULL values.
This piece of information will be very important when you interpret results produced by
aggregate functions. A NULL value is used to represent an unknown value. It does not equate to a
value of zero. Therefore, if the development cost, or devcost, of a book is NULL, rest assured
there is a development cost, you just do not know what that cost is.
When you use an aggregate function on a column that contains NULL values, those values are
ignored. For example, suppose your SELECT statement contained the following aggregate
function:
COUNT(devcost)
If there were 90 devcosts in your database, but 3 of them had a value of NULL, the COUNT
function would return 87. The NULL values would be ignored.
Consider the effect on AVG(devcost). Because there are no values for the 3 NULL devcosts, they
are not included in the average calculation. This certainly produces a more accurate result than
averaging the NULLs in as zero values. However, because the data is not 100% accurate (there
are NULLs), it follows that the average will not be 100% accurate. This is very important to note
when interpreting results.
Multiple Aggregates
There is no limit to the number of aggregate functions that you can use in a query. They can be
written on separate lines for clarity, or on a single line. The only requirement is that a comma
separates each function. Each of the following examples is correct:
· SELECT aggregate_fcn(col),aggregate_fcn(col)
· SELECT aggregate_fcn(col), aggregate_fcn(col)
· SELECT aggregate_fcn(col),
aggregate_fcn(col)
In the SQL Server Query Analyzer, aggregate functions are not automatically given a column
heading in the output. This is addressed in Topic B of this lesson.

Distinct Aggregates
The DISTINCT keyword can be added to aggregate functions to eliminate duplicate values
before a calculation is performed. For example, AVG(DISTINCT slprice) would include a
specific value only once in the average; whereas AVG(slprice) would include all prices in the
average, even if they were duplicated. Similarly, COUNT(DISTINCT slprice) would include a
specific value once in the count; any duplicates would not be counted. COUNT(slprice) would
count all sale prices.

TIP The DISTINCT keyword does not have to be used with aggregate functions only.
:
Generic example:
SELECT AVG(DISTINCT col)
FROM table
As illustrated in the examples above, the word DISTINCT must be written inside the parentheses
before the argument; however, the word DISTINCT does not have to be capitalized. DISTINCT
is not used with MAX and MIN functions.
1. What are Aggregate functions?
Aggregate functions, such as AVG, COUNT, MAX, MIN, SUM, produce a single summary
value for a group of values in a specified column.
2. What is the DISTINCT keyword used for?
The DISTINCT keyword is added to aggregate functions to eliminate duplicate values before a
calculation is performed.

Column Aliases
Lesson objectives
To be able to use column aliases, you will:

· Explain the purpose in using column aliases.

· Explain the use of column aliases for both aggregate and non-aggregate columns.
Introduction
As you have seen, if you're using the SQL Server Query Analyzer, aggregate columns are not
automatically given a heading in the results. This can make reading and interpreting the results a
challenge.
You can remedy this, however, by creating your own headings for these columns. These headings
are called column aliases. To create a column alias, simply add the desired column heading after
the appropriate argument in the SELECT statement. Some things to keep in mind when creating
column aliases:

· If the alias has any spaces, then the alias should be inside single quotes. Although you can
use double quotes, single quotes are recommended to be in compliance with ANSI
standards, thereby making SQL applications more portable.

· Column aliases can contain no more than 30 characters, including spaces.

TIP If you are using an Oracle database, column aliases must be inside double
: quotes(""). Like Transact_SQL, Oracle databases also have a limit of 30 characters
for column aliases.
Following are some generic examples of code:
SELECT ColumnAlias = AVG(col)

FROM table

SELECT AVG(col) `Column Alias'

FROM table

SELECT AVG(col) as ColumnAlias, MAX(col) as `Column Alias'

FROM table

Aliases and Non-aggregate Columns


Although column aliases are especially helpful for aggregate columns, which might otherwise
not have a column heading at all, they can also be used for non-aggregate columns. For example,
if you were to execute the following SQL query:
SELECT repid
FROM slspers
The result would display the column of sales representatives' identification numbers along with a
column heading of "repid." If you are presenting the query results to others not as familiar with
the database, the heading "repid" might be cryptic, not easily recognizable. To make your results
more clear, you could use a column alias of "Sales Rep Id."
In summation, column aliases are not for use with aggregate columns alone. In fact, they can be
used for any column that will be displayed in your query results. They can be especially helpful
in making your query results easier to read and understand.
1. What is the purpose of using a column alias?
A column alias or column heading is used to make the results easier to read and understand.
2. Can column aliases be used only for aggregate columns? Explain.
Column aliases are especially helpful for aggregate columns, which otherwise might not have
any column headings, but they can also be used for non-aggregate columns. They can be used for
any column that will be displayed in your query results as they make the results easier to
understand.

Performing Calculations
Lesson objectives
To be able to perform calculations, you will:

· Explain how concatenation is used in SELECT statements.

· Explain the use of additional mathematical functions and expressions in SELECT


statements
Introduction
Concatenation is a type of calculation. It results in the linking of two or more expressions,
strings, or data. The concatenation operator is the plus sign (+).
Generic example:
SELECT col1 + col2 `Column Alias'

FROM table

Applied example:

SELECT fname + lname `Sales Reps Names'

FROM slspers

There are several functions available to manipulate strings. One such function is the RTRIM
function. This function trims space to the right of a given string. It is used as follows to reduce
the blank space to the right of the specified column or expression:
RTRIM(expression)

Using Mathematical Expressions


You can create your own mathematical calculations by writing them in the form of an expression.
Listed below are common mathematical operators and their duties:
+ performs addition between two expressions
- performs subtraction between two expressions
* performs multiplication between two expressions
/ performs division between two expressions
As you have seen earlier, there are functions available to perform mathematical calculations,
namely SUM and AVG. In addition to these, there are other mathematical functions. Here we
will use three more functions: ROUND, STR, and POWER.
1. Explain what is concatenation.
Concatenation is a type of calculation. It results in the linking of two or more expressions,
strings, or data and the concatenation operator is the plus (+) sign.
2. Explain what the ROUND, STR and the POWER functions do.
The ROUND function is to round a given mathematical expression to the designated position
which is indicated by an integer. The STR function converts numeric data to character data. The
POWER function raises the specified numeric expression to the specified power.

Date Functions
Lesson objectives
To be able to use date functions, you will:

· Explain the purpose of Date functions.

· List and explain the different types of date functions.


Using a Date Functions
Sometimes dates and times are helpful in answering business questions. In a date column, the
default format for dates includes both a date and a time. Before we talk about date functions,
however, it will be helpful to understand how dates are stored.
This very general idea of how dates are stored can be applied for all types of databases.
Basically, a starting date exists, such as December 31, 1899 at 12 a.m. That date is given the
number 1.0. It follows then that January 1, 1900 at noon would be 1.5 and January 2, 1900 at 12
a.m. would be 2.0, January 3, 1900 at 11:50 a.m. would be approximately 3.48, and so forth. The
whole number represents the date and the fraction represents the time.
Given that dates are stored in this manner, do you know what today's date number is? Well, you
could certainly figure it out, but it would take some time and effort. If you want to search for all
the books that were published in 1998, the last thing you want to spend time on is figuring out
what the corresponding date number might be. This is where date functions come in handy. Date
functions enable you to work with dates without ever having to know what the stored
representation of the date is.
Date functions are used to manipulate date/time values and return something we can easily ready
and understand. You will notice that there is a datepart function. After learning of the date-
storage scheme, this term should seem natural. The datepart function allows you to extract a
specific portion of the date (month, day, year, and so forth) and have it returned in a format that
can be easily understood (March, 21, 1998, and so forth).

TIP If you are using an Oracle database, the to_char function is used to specify a date
: formt. It looks like this: to_char(pubdate,'Day dth Month yyyy') where "Day"
represents the day of the week. If you choose not to use the to_char function, dates
must be specified in the following format: 'DD-MON-YY', such as '13-JUL-99'.

TIP If you are using Oracle, the months_between function returns the difference in
: months between dates. If you use the minus sign(-) between the two dates instead,
the difference is given in days.
The following is a list of functions, their arguments, and their descriptions. If you are not using
Transact-SQL, use your SQL reference material to determine if each of these functions is
available and what syntax is required.

TIP Date functions are not a part of ANSI SQL, and the syntax or very existence of such
: functions will differ in Oracle, Informix, and so forth.
1. Explain the purpose of Date functions.
Date functions are used to manipulate date/time values and return a value you can easily read and
understand.
2. Explain what the datepart function does?
The datepart function is used to return a specified portion of the date (hour, day, month, year) as
an integer.

The WHERE Clause


Lesson objectives
To be able to use the WHERE Clause, you will:

· State the purpose of the WHERE clause.

· Use the WHERE clause appropriately


Introduction
The SELECT and FROM clauses, by themselves, output all rows of data from the specified
table. For example,
SELECT slprice FROM titles
selects all sales prices in the slprice column of the table. This is because the SELECT clause
specifies an entire column.
The WHERE clause modifies a SELECT statement by specifying rows of data to be output. For
example,
SELECT slprice FROM titles WHERE slprice = 20.50
selects only those rows whose sale price is equal to $20.50.
Generic example:
SELECT col1, col2
FROM table
WHERE condition
Applied example:
SELECT bktitle, slprice
FROM titles
WHERE slprice = 20.50
Comparison Operators
You are not limited to searching for values that equal a specific quantity. You can use comparison
operators to form a variety of numeric relationships in a WHERE clause. The following is a list
of comparison operators and their meaning:
Flexibility of the WHERE Clause
The WHERE clause states a condition that all output data must meet. Although it is sometimes
helpful to see the column in the condition also displayed in the result (to verify that the condition
has indeed been met), it is not necessary. The column in the condition of the WHERE clause
does not have be included in the SELECT clause.
. State the purpose of the WHERE clause.
The WHERE clause provides criteria that specify only certain rows to be returned in the output.
2. Is the following statement true or false? Any columns used in a WHERE condition must also
appear in the SELECT clause.
False

The ORDER BY Clause


Lesson objectives

· State the purpose of the ORDER BY clause.

· Describe the different ways of sorting output.


Examples
By default, rows are output in the same order they occur in the original table of the database.
However, you can change this order by using the ORDER BY clause. ORDER BY can sort up to
16 columns, each either ascending (ASC) or descending (DESC) in sequence. Ascending is the
default if no sequence is specified.
Generic example:
SELECT col1, col2
FROM table
ORDER BY col
Applied example:
SELECT bktitle, slprice
FROM titles
ORDER BY slprice DESC
. What is the purpose of the ORDER BY clause?
The purpose of the ORDER BY clause is to sort the results so that they will be helpful in later
use. The sorting can be in alphabetic or numeric order.
2. What are the different ways of sorting the output or displaying the results and what is the
default order?
With the ORDER BY clause, you can sort in ascending, descending order or order by a
calculated column and also do secondary sorts. However, ascending order is the default order.
Searching for Text
Lesson objectives
To be able to search for text, you will:

· Explain the purpose of the LIKE keyword.

· Explain the syntax for using the LIKE keyword in a WHERE clause

Introduction
In addition to searching for rows that meet a numeric criteria, you can also search for rows that
meet a string criteria. The LIKE keyword is used to match portions of character strings. A
character string is made up of alphabetic and numeric characters. Numeric characters are
considered text; they do not have a value.
You can use wildcards in conjunction with the LIKE keyword for a more powerful effect. Below
are listed some of the wildcards that may be available in your version of SQL. For further
information, consult your SQL reference material.
When you use the LIKE keyword, the wildcard and character string must be enclosed in single
quotes. Listed below are some examples of how the LIKE keyword might be used with
alphabetic characters, and a corresponding description (Note: Although SQL Server is not case
sensitive, most databases (such as Oracle) are case sensitive. Therefore, it is good practice to
write your search criteria as if the database you are working with were case sensitive.).

TIP If you're using an Oracle database, enclose text in single quotes ('text') when writing
: a text comparison. Also remember Oracle databases are case sensitive.
Although these examples deal with letters, keep in mind that numeric characters can also be used
with the LIKE keyword. Below is an example of using the LIKE keyword in a query to find all
customer numbers that begin with a "9." Because these are numeric characters, the customer
number is considered text rather than a value.
Here is a generic example:
SELECT col1, col2, col3_FROM table_WHERE condition LIKE condition
Here is an applied example:
SELECT custnum_FROM customer_WHERE custnum LIKE `9%'
1. Explain what the LIKE keyword is used for?
The LIKE keyword is used to match portions of character strings which could be made up of
alphabetic and numeric characters.
2. Write an example of a WHERE clause that uses the LIKE keyword to search for a numeric
string of your choice.
select bktitle, partnum from titles where partnum like '406%'

Logical Operators
Lesson objectives
To be able to use logical operators, you will:

· Compare the different logical operators used for searching data.

· Explain the operator precedence


Using Operators
The flexibility of the WHERE clause can be stretched even further. There may be times when
you need to search on two or more criteria simultaneously. If this is the case, you can use the
logical operator AND to relate search conditions in the WHERE clause to one another. AND is
used to connect two or more conditions and only returns those rows meeting all conditions. For
example, the following query returns only rows in the titles table in which the publishing date is
1998 or later AND the development cost is greater than $10,000:
Generic example:
SELECT col1, col2, col3
FROM table
WHERE condition1 AND condition2
Applied example:
SELECT pubdate, devcost
FROM titles
WHERE datepart(year, pubdate) > 1998 AND devcost > 10000
The OR Operator
The logical operator OR can also be used to relate search conditions in the WHERE clause to one
another. OR connects two or more conditions and returns any rows that meet any of these
conditions. For instance, the following query returns all rows in the titles table in which the
publishing date is 1998 or later OR the development cost is greater than $10,000:
Here is a generic example:
SELECT col1, col2, col3
FROM table
WHERE condition1 OR condition2
Here is an applied example:
SELECT pubdate, devcost
FROM titles
WHERE datepart(year, pubdate) > 1998 OR devcost > 10000
An OR condition generally returns more information than an AND condition because in an OR
condition, only one of the conditions has to be met. In other words, at least one condition must
be evaluated to be true. However, in an AND condition, all conditions must be evaluated to be
true.
Suppose you are checking in at the airport to board a plane. The airline ticket agent may ask you
the following questions:
May I see your license?
The agent is verifying your identification. If you look like the picture on the license, and your
airline ticket is for the person of the same name, this condition (your identification) is evaluated
to be True.
Are you carrying only items packed by you?
If you answer "yes" this condition is evaluated to be True.
In this case, both conditions must be met before you can board the plane.
If you are using both AND and OR together in a single search condition, there is an order of
precedence. AND conditions, regardless of where they are located in the WHERE clause, will
receive a higher priority than OR conditions. In other words, all AND conditions will be
evaluated before OR conditions
The IN Keyword
The IN keyword can be used as shorthand for OR conditions. It is an implied OR list that can be
used for a single field. So rather than writing:
WHERE city = `Rochester' OR city = `Ryebrook' OR city = `Syracuse'
you can write:
WHERE city IN (`Rochester', `Ryebrook', `Syracuse')
BETWEEN/AND
The BETWEEN keyword can be used to specify an inclusive range, meaning the lower and
upper values are searched for as well as those values in between. This is the equivalent of using
>= and <= together. You might consider using BETWEEN/AND rather than the mathematical
symbols because it is easier to read and understand. For example, to find all books whose
development cost is between and including $9000 and $11000, you would use the following
query:
Here is the generic version:
SELECT col1, col2, col3
FROM table
WHERE column_name BETWEEN value1 AND value2
Here is an applied version:
SELECT bktitle, devcost
FROM titles
WHERE devcost BETWEEN 9000 AND 11000
This query has the same meaning as:
Generic version:
SELECT col1, col2, col3
FROM table
WHERE column_name >= value1
AND column_name <= value2
Applied version:
SELECT bktitle, devcost
FROM title
WHERE devcost >= 9000
AND devcost <= 11000
The NOT Keyword
The NOT keyword is another logical operator; it is used to negate conditions. The following
query finds all customers who do not reside in Texas:
Here is a generic example:
SELECT col1, col2, col3
FROM table
WHERE NOT condition
Here is an applied example:
SELECT custname
FROM customer
WHERE NOT state = `TX'
Let's break down the WHERE clause to take a closer look at the placement of the NOT keyword.
WHERE column operator value (or WHERE state = `TX')
Based on this example, you might conclude that the NOT keyword should be placed before the
column. Although this is true in cases such as the one illustrated here, it is not true for all
situations.
When you use the LIKE, IN, or BETWEEN operators, the NOT keyword is placed before the
operator.
· WHERE column NOT LIKE value
· WHERE column NOT IN value
· WHERE column NOT BETWEEN value1 AND value2
1. Compare the AND and OR operators.
An AND condition requires that all conditions be met before a row can be returned; an OR
condition merely requires that any condition be met before a row can be returned. Generally,
AND conditions return fewer results than OR conditions.
2. Explain what the operator precedence is.
There is an operator precedence. When AND and OR are used in the same condition, they are not
just executed from left to right. The condition is looked at in its entirety and the AND clauses are
executed before the OR clauses. AND clauses have a higher precedence.

The GROUP BY Clause


Lesson objectives
To be able to use the GROUP BY Clause, you will:

· Explain the function of the GROUP BY clause.

· Explain the function of the Cube operator.


Introduction
You may have noticed that when we first used aggregate functions (SUM, AVG, COUNT, and so
forth), the queries were simple. We only searched for one or two aggregate columns. Non-
aggregate columns were not included. SELECT clauses looked like this:
SELECT SUM(devcost)
Or
SELECT SUM(devcost), AVG(devcost).
We did not use SELECT clauses like this:
SELECT SUM(devcost), bktitle.
This is because you cannot combine aggregate columns and non-aggregate columns in a single
SELECT clause, unless you use a GROUP BY clause.
The GROUP BY clause, as the name suggests, is used to organize data into groups. Since
aggregate functions are used to determine summary values of either entire columns or groups of
data, the GROUP BY clause is needed to group any non-aggregate columns that are being
queried simultaneously. Review the example below containing one non-aggregate column and
one aggregate column:
Here is a generic example:
SELECT non-aggregate col1, aggregate(col2)
FROM table
GROUP BY non-aggregate col1
Here is an applied example:
SELECT partnum, SUM(qty) FROM sales GROUP BY partnum

Multiple Non-aggregate Columns


You can have any number of aggregate and non-aggregate columns in a single SELECT clause.
However, as long as at least one column of each type is included in the SELECT statement, a
GROUP BY clause is needed. The GROUP BY clause must contain all non-aggregate columns.
Here is a generic example:
SELECT non-aggregate col1, aggregate(col2), non-aggregate col13, non-aggregate col4
FROM table
GROUP BY non-aggregate col1, non-aggregate col3, non-aggregate col4
Here is an applied example:
SELECT partnum, SUM(qty), custnum, sldate
FROM sales
GROUP BY partnum, custnum, sldate
The CUBE Operator
You have used aggregate functions to produce summary information. For example, you can find
the sum of all books sold for each part number. Suppose you wanted to take this one step further
and calculate the sum of a sum-a grand total of all books sold. This is sometimes referred to as
super-aggregate information.
A super-aggregate is a summary of all the aggregate information produced for a single non-
aggregate expression contained in the GROUP BY clause. The CUBE operator is used to
produce super-aggregate information. To use this operator in your query, end your GROUP BY
clause with the keywords WITH CUBE.
Here is a generic example:
SELECT col1, aggregate_fcn(col2)
FROM table
GROUP BY col1 WITH CUBE
Here is an applied example:
SELECT partnum, sum(qty)
FROM sales
GROUP BY partnum WITH CUBE
The Effect of the WHERE Clause on Grouping
The WHERE clause is a condition, or criteria, restricting the number of results that will be
displayed. How does this affect the GROUP BY clause? The clauses of a SELECT statement are
executed in the order in which they appear. The WHERE clause is always placed before the
GROUP BY clause, and therefore is always executed before the GROUP BY clause. As a result,
the conditions, or criteria, of the WHERE clause are met before the GROUP BY clause is
performed. The GROUP BY clause then groups only the restricted output.
1. Explain when a GROUP BY clause is needed.
Generally, when the arguments of the SELECT clause include aggregate (SUM, AVG, MIN,
MAX, COUNT) and non-aggregate columns, a GROUP BY clause should be used.
2. Explain the function of the CUBE operator.
The CUBE operator is used to produce super-aggregate information which is a summary of all
the aggregate information produced for a single non-aggregate expression contained in the
GROUP BY clause. To use it, you should end the GROUP BY clause with the keywords WITH
CUBE.
The HAVING Clause
Lesson objectives
To be able to use the HAVING Clause, you will:

· Explain the limitations of the WHERE clause.

· Explain the function of the HAVING clause.


Using the HAVING Clause
The WHERE clause is used to filter rows being returned by a query. However, there are some
limitations to the WHERE clause. The WHERE clause cannot contain aggregate functions and it
cannot place conditions on the groups specified by the GROUP BY clause. This is where the
HAVING clause can be useful.
The HAVING clause is like the WHERE clause in that it is also used to filter rows being returned
by a query. The difference is that the HAVING clause is used to specify conditions for groups
created by the GROUP BY clause; therefore, a HAVING clause is used only in conjunction with
a GROUP BY clause. Also, the HAVING clause can contain aggregate functions.
Here is a generic example:
SELECT aggregate(col1), non-aggregate col2
FROM table
GROUP BY non-aggregate col2
HAVING condition
Here is an applied example:
SELECT repid, partnum, SUM(qty)
FROM sales
GROUP BY repid, partnum
HAVING SUM(qty)>250
It is possible to have more than one condition in a HAVING clause by using AND, OR, or NOT
as they were used earlier with the WHERE clause.
Here is an applied example:
SELECT repid, partnum, SUM(qty)
FROM sales
GROUP BY repid, partnum
HAVING SUM(qty)>250 OR SUM(qty)<100
1. What are the limitations of the WHERE clause?
An aggregate function cannot be used in a WHERE clause. If it is used in this way an error
message will be generated. The HAVING clause can contain aggregate functions and so should
be used. However, the HAVING clause must always follow the GROUP BY clause.
2. Does a HAVING clause select rows before or after grouping?
A HAVING clause selects rows after grouping.
Comparing the WHERE and HAVING Clauses
Lesson objectives
To be able to use the WHERE and HAVING Clauses, you will:

· Compare the WHERE and HAVING clauses.

· Write queries using the WHERE and HAVING clauses.


Distinguishing between the WHERE and HAVING Clauses

The HAVING clause specifies a condition that is similar to the purpose of a WHERE clause;
however, these two clauses are not interchangeable. Listed below are some differences to help
distinguish between the two:

· The WHERE clause can be used without the GROUP BY clause; the HAVING clause
cannot be used without the GROUP BY clause.

· The WHERE clause selects rows before grouping; the HAVING clause selects rows after
grouping.

· The WHERE clause cannot contain aggregate functions; the HAVING clause can contain
aggregate functions.
1. Find the book title and sale price of books whose sale price is over $32.
select bktitle, slpricefrom titleswhere slprice > 32 There should be 41 records listed in the
Results page.
2. Find the customer number, sale date, and the sum of all books sold for each sale date. Only
include books sold after the month of March.
select custnum, sldate, sum(qty)from saleswhere datepart(month, sldate) > 3group by custnum,
sldate There should be 64 records listed.
3. Find the sale date, and total books sold each day. Only include days in which the total
exceeded $600.
select sldate, sum(qty)from salesgroup by sldatehaving sum(qty) > 600 There should be 7
records listed.
1. Compare the WHERE and HAVING clauses.
The WHERE clause can be used without the GROUP BY clause; the HAVING clause cannot be
used without the GROUP BY clause. The WHERE clause selects rows before grouping; the
HAVING clause selects rows after grouping. The WHERE clause cannot contain aggregate
functions; the HAVING clause can contain aggregate functions.
2. Write a SELECT statement to find the book titles and development costs of only those books
titled 'How to'.
select bktitle, devcost from titles where bktitle like 'How to %'

Joins
Lesson objectives
To be able to use joins, you will:

· Describe the term Joining tables.


Introduction
Until now, the SELECT statements you have executed contained only one table name in the
FROM clause. Of course, this results in data being extracted only from one table at a time. In
real-world applications, however, it is more likely that you will need to combine data from two
or more tables in a single query. For example, you might want to see a customer name and the
quantity of books sold to that customer. In the Bindery database, this would involve selecting the
quantity from the sales table and the customer name from the customer table. This type of query
is known as a join. A join can be performed between any two tables, provided they have a
column of data in common.
There are many kinds of joins, but the most common is the equijoin. In some applications, an
equijoin is considered the same as an inner join. This course uses the term inner join. The inner
join returns only those rows that exist in both tables, based on values in the common column.
There are other types of joins; an outer join will be discussed later in this lesson.
An inner join will return the intersection of two tables. In the graphical representation below,
there are two tables being joined, Table A and Table B. Table A contains data in areas 1 and 3 and
Table B contains data in areas 2 and 3. If an inner join were performed, area 3 would be returned.
1. Define the term Joining tables.
Joining tables means combining data from two or more tables in a single query, based on the
value of common columns.
Joining Tables
Lesson objectives
To be able to join tables, you will:

· State the syntax for constructing two and three table joins.

· Explain how multiple joins work


Joining Table Procedure
When you know that you will be querying data from two different tables simultaneously, the first
thing to do is identify which tables you will be querying. Once this is accomplished, you will
need to identify which column of data the tables have in common. With this key information, you
will then be ready to write your SELECT statement.
When joining tables, the structure of the FROM clause changes. The two pieces of key
information (which tables will need to be used, and how the tables will be linked) is specified
within the FROM clause. In addition to the keyword FROM, the keywords INNER JOIN and
ON are used to indicate that a join is necessary to complete the query successfully.
Here is a generic example:
SELECT col1, col2
FROM table1 INNER JOIN table2 ON table1.common_col = table2.common_col
Here is an applied example:
SELECT bktitle, qty
FROM titles INNER JOIN sales ON titles.partnum = sales.partnum
As you can see, this makes for a lengthy FROM clause. You can break up the text so that it is
easier to read. Here are two other suggestions:
· FROM titles
INNER JOIN sales ON titles.partnum = sales.partnum
· FROM titles INNER JOIN sales
ON titles.partnum = sales.partnum
Table Name Aliases
Table names are repeated in the FROM clause and can be anywhere from 1 to 30 characters long.
Depending on the length of that name, this could require a lot of typing. To save keystrokes, you
can use a table name alias. Aliases are assigned in the FROM clause by writing them
immediately after the table name using the keyword AS. Below is an example of a two-table join
using aliases:
Here is the generic example:
SELECT col1, col2
FROM table1 AS alias1
INNER JOIN table2 AS alias2 ON alias1.common_col = alias2.common_col
Here is an applied example:
SELECT bktitle, qty
FROM titles AS t INNER JOIN sales AS s ON t.partnum = s.partnum
Multiple-table Joins
It is possible to retrieve data from more than two tables and, in some cases, it is possible to
retrieve data from two tables that do not have a common column. To perform these tasks, a
multiple-table join is performed. For this to be possible, two of the tables used must have a
common column of data. One of those tables, in turn, must have a common column with another
table being used and so on.
Below is an example of a three-table join being performed to retrieved data from two columns
whose tables do not have a common column:
Here is the generic example:
SELECT col1, col2
FROM table1
INNER JOIN table2 ON table1.common_col = table2.common_col
INNER JOIN table3 ON table2.common_col = table3.common_col
Here is an applied example:
SELECT bktitle, custname
FROM titles
INNER JOIN sales ON titles.partnum = sales.partnum
INNER JOIN customer ON sales.custnum = customer.custnum
Below is an example of a three-table join being performed to retrieve data from three columns.
Here is an applied example:
SELECT bktitle, qty, custname
FROM titles
INNER JOIN sales ON titles.partnum = sales.partnum
INNER JOIN customer ON sales.custnum = customer.custnum
1. Give a generic example of the syntax for constructing two and three table joins.
SELECT col1, col2 FROM table1 INNER JOIN table2 ON table1.common_col =
table2.common_col INNER JOIN table3 ON table2.common_col = table3.common_col
2. Explain how you retrieve data from two or more tables that do not have a common column?
This is done by performing a multiple-table join. It is important that two of the tables used must
have a common column of data. One of the tables in turn must have a common column with
another table being used and so on.
Outer Joins
Lesson objectives
To be able to use Outer Joins, you will:

· Explain the use of Outer Joins.

· Explain the difference between the left outer join and the right outer join.
Using Outer Joins
The inner join, discussed in Topic B of this lesson, returns only those rows that satisfy the join
condition. Sometimes, in addition to that information, it is necessary to return rows from one
table that do not satisfy the condition. This is known as an outer join. In the graphical
representation below, an outer join performed on Table A will return rows from areas 1 and 3; an
outer join performed on Table B will return rows from areas 2 and 3.

Typically, an outer join returns all rows that satisfy a join condition and all rows that did not
qualify from the specified table. Some SQL systems provide additional outer-join operators, such
as the asterisk (*), to specify which table the outer join is to be performed on. For example in
Transact-SQL:
*= returns all rows from the first-named table (referred to as a left outer join)
=* returns all rows from the second-named table (referred to as a right outer join)

TIP If you are using an Oracle database, the plus sign (+) is substituted for the asterisk.
: For example, a left outer join would be represented as +=. A right outer join would
be represented as =+.
The following example will return all titles, even those that did not sell. This is considered a left
outer join.
Here is an applied example:
SELECT bktitle, sum(qty)
FROM titles AS t
LEFT OUTER JOIN sales AS s ON t.partnum* = s.partnum
GROUP BY bktitle
1. Explain what is an Outer join?
Typically an outer join returns all rows that satisfy a join condition and also all rows that did not
qualify from the specified table.
2. What is the difference between the left outer join and the right outer join and how does it
work?
In some SQL systems, the asterisk (*) may be used as an additional outer join operator to specify
which table the outer join is to be performed on. In Transact-SQL, the *= returns all rows from
the first-named table. This is referred to as a left outer join. the =* returns all rows from the
second-named table. This is referred to as a right outer join.

Creating a Blank Database


Lesson objectives
To be able to create a blank database, you will:

· State the purpose of the DSN.

· Explain the process for creating a DSN.


Building Your Blank Database
A database is stored on your SQL Server. This database is called Bindery. You are currently
working on a client machine with Access 97 as your front-end application. You must build a
bridge from your client machine, where you will write the queries, to your server containing the
data.
The first step in building this bridge, or connection, is to create a blank database in Access.
Eventually, you will link this blank database to the Bindery. This will enable you to see the
Bindery data on your client machine.
Creating a DSN
The DSN (Data Source Name) is the bridge that will allow the data on the server to be seen by
the client. To build this bridge, you will need to use the ODBC Data Source Administrator. This
will be opened by double-clicking on the ODBC (32-bit) icon located in the Control Panel.
Linking Tables
To reiterate, you have the Bindery database on the SQL Server, and you have a blank database
within Access on your client. Now that you have created the DSN, or bridge, you can link these
two databases. The result will be that you can see the Bindery data from your within Access on
your client machine.
To link the tables, you can work from within Access by choosing File >acute;Get External
Data>acute;Link Tables from the menu.
1. What is the purpose of the DSN?
The DSN forms the "bridge" between the database on the server and the client.
2. What icon do you need to open from the Control Panel before you can start building the DSN
or the bridge.
To build the bridge or the DSN you will need to use ODBC Data Sources which can be opened
from the Control Panel.

Querying a Database
Lesson objectives
To be able to query a database, you will:

· Create a query in Access 97.

· Explain the function of the Design grid in Access.


Introduction
When using Access 97, you create a query in the query Design View window. First, you will
need to select the tables you will be using. Once this is done, you can select the columns within
those tables. During this process, you will be using what Access refers to as a design grid.
There is a direct relationship between what you place in the design grid and what is displayed in
the query results. For a column of data to appear in the query results, you must drag the column
from the field list to an empty column of the design grid (or double-click the column). The
selected columns are displayed in the query result in the same order as they are placed in the
design grid.
Once you have your query set up the way you like, you can run it. The results of a select query
are placed in a temporary datasheet. You can navigate within the temporary datasheet as you
would within any table.
1. In what view do you create a query in Access 97?
In Access 97, queries are created in Design View.
2. What is the function of the Design grid in Access?
There is a direct relation between what you place in the design grid and what is displayed in the
query results. For example, you drag a column of data from the field list to an empty column of
the design grid. The selected columns are displayed in the query result in the same order as they
are placed in the design grid.

Generating a Report of the Data


Lesson objectives
To be able to generate a report of the data, you will:

· State the purpose of a Report.

· List the formatted elements possible in a report.


Report Generation
In Access, you can print datasheets but by creating reports you usually make your data more
presentable and meaningful. Reports can contain nicely formatted titles, other formatted text,
lines, borders, and other graphical elements that can make query results more interesting and
more helpful to read.
1. What is a report? State the purpose of a report.
A report is a formatted version of a query result. The purpose is to make the data more
presentable and meaningful by displaying the query results accordingly.
2. What are some of the formatting elements possible in a report?
Reports can contain nicely formatted titles, formatted text lines, borders and other graphical
elements.

You might also like