Sie sind auf Seite 1von 15

B.Sc.

(BIT) Special Begiee


ITN FILE 0RuERNIZATI0N ANB BATABASE NANAuENENT
SYSTEN
Yeai II Semestei II
SQL - SeIf Study Note
By Pramudith Kandambi
1. SELECT
Table Store_Information
store_name Sales Date
Los Angeles $1500 Jan-05-1999
San Diego $250 Jan-07-1999
Los Angeles $300 Jan-08-1999
Boston $700 Jan-08-1999
SELECT store_name # Store_nformation
#esult:
store_name
Los Angeles
San Diego
Los Angeles
Boston
:ltiple col:mn names can be selected, as well as m:ltiple table names.
. DISTINCT
SELECT DISTINCT store_name # Store_Information
Result:
store_name
Los Angeles
San Diego
Boston

store_name Sales Date


Los Angeles $1500 Jan-05-1999
San Diego $250 Jan-07-1999
Los Angeles $300 Jan-08-1999
Boston $700 Jan-08-1999
SELECT store_name
# Store_Information
WHE#E SaIes > 1000

Result:
store_name
Los Angeles

AND|#
SELECT "coIumn_name"
# "tabIe_name"
WHE#E "simpIe condition"
{[AND|#( "simpIe condition"}+
Table Store_Information
store_name Sales Date
Los Angeles $1500 Jan-05-1999
San Diego $250 Jan-07-1999
San Francisco $300 Jan-08-1999
Boston $700 Jan-08-1999
SELECT store_name
# Store_Information
WHE#E SaIes > 1000
# (SaIes < 500 AND SaIes > 5)

Result:
store_name
Los Angeles
San Francisco

IN
'al:es can be n:merical or characters. f there is only one val:e inside the parenthesis,
this commend is eq:ivalent to
WHE#E "coIumn_name" = 'vaIue1'
For example, we may wish to select all records for the Los Angeles and the San Diego
stores in Table Store_Information,
Table Store_Information
store_name Sales Date
Los Angeles $1500 Jan-05-1999
San Diego $250 Jan-07-1999
San Francisco $300 Jan-08-1999
Boston $700 Jan-08-1999
SELECT *
# Store_Information
WHE#E store_name IN ('Los AngeIes', 'San Diego')

Result:
storename Sales Date
Los Angeles $1500 Jan-05-1999
San Diego $250 Jan-07-1999

. BETWEEN
For example, we may wish to select view all sales information between Jan:ary 6,
1999, and Jan:ary 10, 1999, in Table Store_Information,
Table Store_Information
store_name Sales Date
Los Angeles $1500 Jan-05-1999
San Diego $250 Jan-07-1999
San Francisco $300 Jan-08-1999
Boston $700 Jan-08-1999
SELECT *
# Store_Information
WHE#E Date BETWEEN 'Jan-0-1999' AND 'Jan-10-1999'
Note that date may be stored in different formats in different databases. This t:torial
simply choose one of the formats.
Result:
storename Sales Date
San Diego $250 Jan-07-1999
San Francisco $300 Jan-08-1999
Boston $700 Jan-08-1999

LIKE
SQL Wildcards
SQL wildcards can substitute Ior one or more characters when searching
Ior data in a database.
SQL wildcards must be used with the SQL LIKE operator.
ith SQL, the Iollowing wildcards can be used:
Wildcard Description
A substitute Ior zero or more characters
A substitute Ior exactly one character
|charlist| Any single character in charlist
|`charlist|
or
|!charlist|
Any single character not in charlist


SQL Wildcard Examples
e have the Iollowing "Persons" table:
P_Id LastName FirstName Address City
1 Hansen Ola Timoteivn 10 Sandnes
2 Svendson Tove Borgvn 23 Sandnes
3 Pettersen Kari Storgt 20 Stavanger


Using the Wildcard
Now we want to select the persons living in a city that starts with "sa"
Irom the "Persons" table.
e use the Iollowing SELECT statement:
SELECT * FROM Persons
HERE City LIKE 'sa'
The result-set will look like this:
P_Id LastName FirstName Address City
1 Hansen Ola Timoteivn 10 Sandnes
2 Svendson Tove Borgvn 23 Sandnes

Using the _ Wildcard
Now we want to select the persons with a Iirst name that starts with any
character, Iollowed by "la" Irom the "Persons" table.
e use the Iollowing SELECT statement:
SELECT * FROM Persons
HERE FirstName LIKE 'la'
The result-set will look like this:
P_Id LastName FirstName Address City
1 Hansen Ola Timoteivn 10 Sandnes
Using the charlist] Wildcard
Now we want to select the persons with a last name that starts with "b"
or "s" or "p" Irom the "Persons" table.
e use the Iollowing SELECT statement:
SELECT * FROM Persons
HERE LastName LIKE '|bsp|'


The result-set will look like this:
P_Id LastName FirstName Address City
2 Svendson Tove Borgvn 23 Sandnes
3 Pettersen Kari Storgt 20 Stavanger
Next, we want to select the persons with a last name that do not start
with "b" or "s" or "p" Irom the "Persons" table.
e use the Iollowing SELECT statement:
SELECT * FROM Persons
HERE LastName LIKE '|!bsp|'
The result-set will look like this:
P_Id LastName FirstName Address City
1 Hansen Ola Timoteivn 10 Sandnes

#DE# BY
SELECT "coIumn_name"
# "tabIe_name"
[WHE#E "condition"(
#DE# BY "coIumn_name" [ASC, DESC(
The [] means that the WHE#E statement is optional. However, if a WHE#E cla:se
exists, it comes before the #DE# BY cla:se. ASC means that the res:lts will be
shown in ascending order, and DESC means that the res:lts will be shown in
descending order. f neither is specified, the defa:lt is ASC.
t is possible to order by more than one col:mn. n this case, the #DE# BY cla:se
above becomes
#DE# BY "coIumn_name1" [ASC, DESC(, "coIumn_name" [ASC, DESC(
Ass:ming that we choose ascending order for both col:mns, the o:tp:t will be ordered
in ascending order according to col:mn 1. f there is a tie for the val:e of col:mn 1, we
then sort in ascending order by col:mn 2.
For example, we may wish to list the contents of Table Store_Information by dollar
amo:nt, in descending order:
Table Store_Information
store_name Sales Date
Los Angeles $1500 Jan-05-1999
San Diego $250 Jan-07-1999
San Francisco $300 Jan-08-1999
Boston $700 Jan-08-1999
SELECT store_name, SaIes, Date
# Store_Information
#DE# BY SaIes DESC

Result:
storename Sales Date
Los Angeles $1500 Jan-05-1999
Boston $700 Jan-08-1999
San Francisco $300 Jan-08-1999
San Diego $250 Jan-07-1999
n addition to col:mn name, we may also :se col:mn position (based on the SQL
q:ery) to indicate which col:mn we want to apply the #DE# BY cla:se. The first
col:mn is 1, second col:mn is 2, and so on. n the above example, we will achieve the
same res:lts by the following command:
SELECT store_name, SaIes, Date
# Store_Information
#DE# BY DESC
unction
The syntax for :sing f:nctions is,
SELECT "function type" ("coIumn_name")
# "tabIe_name"
Examples of how these f:nctions are :sed are presented individ:ally in the next few
pages.
n addition to :sing f:nctions, it is also possible to :se SQL to perform simple tasks
s:ch as addition (+) and s:btraction (-). For character-type data, there are also several
string f:nctions available, s:ch as concatenation, trim, and substring f:nctions.
Different RDBS vendors have different string f:nctions implementations, and it is best
to cons:lt the references for yo:r RDBS to see how these f:nctions are :sed.
SQL :ses the A'() f:nction to calc:late the average of a col:mn. The syntax for :sing
this f:nction is,
SELECT AVG("coIumn_name")
# "tabIe_name"
For example, if we want to get the average of all sales from the following table,


Table Store_Information
store_name Sales Date
Los Angeles $1500 Jan-05-1999
San Diego $250 Jan-07-1999
Los Angeles $300 Jan-08-1999
Boston $700 Jan-08-1999
we wo:ld type in
SELECT AVG(SaIes) # Store_Information

Result:
AVG(Sales)
$687.5
687.5 represents the average of all Sales entries: (1500 + 250 + 00 + 700) / 4.
C&NT
Another arithmetic f:nction is C&NT. This allows :s to C&NT :p the n:mber of row
in a certain table. The syntax is,
SELECT C&NT("coIumn_name")
# "tabIe_name"
For example, if we want to find the n:mber of store entries in o:r table,
Table Store_Information
store_name Sales Date
Los Angeles $1500 Jan-05-1999
San Diego $250 Jan-07-1999
Los Angeles $300 Jan-08-1999
Boston $700 Jan-08-1999
we'd key in
SELECT C&NT(store_name)
# Store_Information
Result:
ount(store_name)
4
C&NT and DISTINCT can be :sed together in a statement to fetch the n:mber of
distinct entries in a table. For example, if we want to find o:t the n:mber of distinct
stores, we'd type,
SELECT C&NT(DISTINCT store_name)
# Store_Information
Result:
ount(DISTINT store_name)
3

AX
SQL :ses the AX f:nction to find the maxim:m val:e in a col:mn. The syntax for
:sing the AX f:nction is,
SELECT AX("coIumn_name")
# "tabIe_name"
For example, if we want to get the highest sales from the following table,
Table Store_Information
store_name Sales Date
Los Angeles $1500 Jan-05-1999
San Diego $250 Jan-07-1999
Los Angeles $300 Jan-08-1999
Boston $700 Jan-08-1999
we wo:ld type in
SELECT AX(SaIes) # Store_Information

Result:
AX(Sales)
$1500
1500 represents the maxim:m val:e of all Sales entries: 1500, 250, 00, and
700.

IN
SQL :ses the N f:nction to find the maxim:m val:e in a col:mn. The syntax for :sing
the N f:nction is,
SELECT IN("coIumn_name")
# "tabIe_name"
For example, if we want to get the lowest sales from the following table,
Table Store_Information
store_name Sales Date
Los Angeles $1500 Jan-05-1999
San Diego $250 Jan-07-1999
Los Angeles $300 Jan-08-1999
Boston $700 Jan-08-1999
we wo:ld type in
SELECT IN(SaIes) # Store_Information

Result:
IN(Sales)
$250
250 represents the minim:m val:e of all Sales entries: 1500, 250, 00, and 700.

S&
The SU f:nction is :sed to calc:late the total for a col:mn. The syntax is,
SELECT S&("coIumn_name")
# "tabIe_name"
For example, if we want to get the s:m of all sales from the following table,
Table Store_Information
store_name Sales Date
Los Angeles $1500 Jan-05-1999
San Diego $250 Jan-07-1999
Los Angeles $300 Jan-08-1999
Boston $700 Jan-08-1999
we wo:ld type in
SELECT S&(SaIes) # Store_Information

Result:
SU(Sales)
$2750
2750 represents the s:m of all Sales entries: 1500 + 250 + 00 + 700.

G#&P BY
Now we ret:rn to the aggregate f:nctions. Remember we :sed the S& keyword to
calc:late the total sales for all stores? What if we want to calc:late the total sales for
each store? Well, we need to do two things: First, we need to make s:re we seIect the
store name as well as total sales. Second, we need to make s:re that all the sales
fig:res are grouped by stores. The corresponding SQL syntax is,
SELECT "coIumn_name1", S&("coIumn_name")
# "tabIe_name"
G#&P BY "coIumn_name1"
Let's ill:strate :sing the following table,
Table Store_Information
store_name Sales Date
Los Angeles $1500 Jan-05-1999
San Diego $250 Jan-07-1999
Los Angeles $300 Jan-08-1999
Boston $700 Jan-08-1999
We want to find total sales for each store. To do so, we wo:ld key in,
SELECT store_name, S&(SaIes)
# Store_Information
G#&P BY store_name
Result:
store_name SUM(Sales)
Los Angeles $1800
San Diego $250
Boston~ $700
The G#&P BY keyword is :sed when we are selecting m:ltiple col:mns from a table
(or tables) and at least one arithmetic operator appears in the SELECT statement.
When that happens, we need to G#&P BY all the other selected col:mns, e all
col:mns except the one(s) operated on by the arithmetic operator.

HAVING
Another thing people may want to do is to limit the o:tp:t based on the corresponding
s:m (or any other aggregate f:nctions). For example, we might want to see only the
stores with sales over 1,500. nstead of :sing the WHE#E cla:se in the SQL
statement, tho:gh, we need to :se the HAVING cla:se, which is reserved for aggregate
f:nctions. The HAVING cla:se is typically placed near the end of the SQL statement,
and a SQL statement with the HAVING cla:se may or may not incl:de the G#&P BY
cla:se. The syntax for HAVING is,
SELECT "coIumn_name1", S&("coIumn_name")
# "tabIe_name"
G#&P BY "coIumn_name1"
HAVING (arithmetic function condition)
Note: the G#&P BY cla:se is optional.
n o:r example, table Store_Information,
Table Store_Information
store_name Sales Date
Los Angeles $1500 Jan-05-1999
San Diego $250 Jan-07-1999
Los Angeles $300 Jan-08-1999
Boston $700 Jan-08-1999
we wo:ld type,
SELECT store_name, S&(saIes)
# Store_Information
G#&P BY store_name
HAVING S&(saIes) > 1500
Result:
store_name SUM(Sales)
Los Angeles $1800

Das könnte Ihnen auch gefallen