Vous êtes sur la page 1sur 80

SQL LAB MANUAL

CONTENT
1. ABOUT SQL.
2. KEYS.
3. SQL CREATE.
4. SQL INSERT.
5. SQL SELECT.
6. SQL UPDATE.
7. SQL ALTER.
8. SQL DELETE AND DROP.
9. SQL ALIAS
10. JOINS AND UNION.
11. SQL FUNCTIONS AND STRING OPERATIONS.
12. SQL AND OR, SQL SELECT INTO, SQL
IN,SQL BETWEEN,SQL LIKE,SQL ORDER
BY,SQL COUNT,SQL GROUP BY,SQL HAVING,
STATEMENTS
13. SQL VIEW AND INDEXES.
14. ADVANCED SQL- SQL UNION,SQL UNION
ALL,SQL INTERSECT,SQL MINUS,SQL
Subquery,SQL EXISTS,SQL CASE,SQL
RANK,MEDIAN AND OTHER ACCOUNTING
COMMANDS.
CHAPTER – 1 ABOUT SQL
What is SQL?
SQL stands for Structured Query Language and is the standard query processing language for
all of the major relational database management systems (RDBMS).

Dr. E.F. Codd is widely considered the father of the RDMBS. He was the person who
conceptualized the theory of relational databases in June of 1970 and created the model for data
manipulation. Codd worked for IBM at the time, and they created the Structured English QUEry
Language (SEQUEL) to apply his model. SEQUEL was already a trademarked name, so IBM
switched the acronym to SQL, Structured Query Language, and it stands to this day.

IBM then began working on a prototype called System/R in 1974. It went through several phases
and was eventually first released to the public as SQL/DS in 1981. It wasn't the first commercially
available implementation of RDMBS and SQL, however; Relational Software beat IBM to the
punch and released their product, Oracle, in 1979. More about that later.

SQL first became an ANSI standard in 1986. Several other national and international
organizations first standardized it later in the 80s. ANSI has since revised it standards with
SQL89(SQ There are 3 big players in the enterprise RDMBS market. Relation Software changed
their name to Oracle, and their Oracle system is still bills itself as the top of the line
RDBMS. IBM is still a big player with their current version, DB/2, which runs on a wide
range of platforms, too. Microsoft SQL Server is the other big enterprise player and has made
huge strides in the RDBMS market, holding the record price/performance (tpmC benchmark) for
any Windows database.

There are several entry-level to mid-market database systems out there, too. Some of them
include MySQL, the most popular open-source RDBMS which continues to make huge
strides in terms of performance, capability, and scalability. Microsoft Access isn't a
true RDMBS, but it includes a lot of the same features, including SQL. Postgre,
Paradox and Firebird are some of the others available.

All of these products use SQL as the brains of their operations. Most vendors' products are
SQL92-compliant. In addition, each vendor has their own flavor of SQL. For example, Oracle
uses PL/SQL, SQL Server uses T-SQL, and Access used JET SQL. What this means is the
vendor-specific additions add some enhancements to what their version of SQL can do to help
differentiate their product.
SQL is a standard language that works with database programs like MS
Access, DB2, Informix, MS SQL Server, Oracle, MySQL, Sybase, and other
database systems.

DIFFERENT TYPES: SQL92(SQL2), and SQL99(SQL3).

TYPES:

MySQL, PostgreSQL, Oracle, mSQL, Microsoft SQL

Future of SQL

The next standard for SQL is expected to improve Internet access to data, most notably with
XML. Some vendors have already incorporated XML-specific operators to their flavor
of SQL. They are also considering how to integrate object-oriented programming
models into SQL. Again, the vendors are already ahead of the curve as the next
version of SQL Server will have to capability to write queries using any .NET
language.

SQL together with RDBMS continue to be the core of all business applications. It's always
dangerous to say that the current way of doing things cannot be improved upon, but the
combination has been very successful and popular for more than 20 years and shows no signs of
going away.

Is SQL the Same for all RDBMS?


While most RDBMS support a certain ANSI standard, most also support their own flavor of SQL
which add certain enhancements to the language.

What is the Current ANSI Standard for SQL?


SQL99, or SQL3, is the current ANSI standard for SQL.

What is the Best RDBMS?


This is definitely a loaded question as everyone has their own answers. There are many
benchmarks and usage statistics out there, but the best RDBMS depends on many things. You
have to consider the budget you have (or don't have...), the workload expected, the knowledge
of your workers among other things. This is why we strive to be RDBMS-independent.
CHAPTER – 2 IMPORTANT KEYS
PRIMARY KEY
A primary key is used to uniquely identify each row in a table. It can either be part of
the actual record itself , or it can be an artificial field (one that has nothing to do with
the actual record). A primary key can consist of one or more fields on a table. When
multiple fields are used as a primary key, they are called a composite key.
Primary keys can be specified either when the table is created (using CREATE TABLE) or
by changing the existing table structure (using ALTER TABLE).

Below are examples for specifying a primary key when creating a table:

Oracle:
CREATE TABLE Customer
(SID integer PRIMARY KEY,
Last_Name varchar(30),
First_Name varchar(30));

Oracle:
ALTER TABLE Customer ADD PRIMARY KEY (SID);

Note: Before using the ALTER TABLE command to add a primary key, you'll need to
make sure that the field is defined as 'NOT NULL' -- in other words, NULL cannot be an
accepted value for that field.

FOREIGN KEY

A foreign key is a field (or fields) that points to the primary key of another table. The
purpose of the foreign key is to ensure referential integrity of the data. In other words,
only values that are supposed to appear in the database are permitted.

For example, say we have two tables, a CUSTOMER table that includes all customer
data, and an ORDERS table that includes all customer orders. The constraint here is that
all orders must be associated with a customer that is already in the CUSTOMER table. In
this case, we will place a foreign key on the ORDERS table and have it relate to the
primary key of the CUSTOMER table. This way, we can ensure that all orders in the
ORDERS table are related to a customer in the CUSTOMER table. In other words, the
ORDERS table cannot contain information on a customer that is not in the CUSTOMER
table.

The structure of these two tables will be as follows:

Table CUSTOMER

column name characteristic


SID Primary Key
Last_Name
First_Name

Table ORDERS

column name characteristic


Order_ID Primary Key
Order_Date
Customer_SID Foreign Key
Amount

In the above example, the Customer_SID column in the ORDERS table is a foreign key
pointing to the SID column in the CUSTOMER table.

Below we show examples of how to specify the foreign key when creating the ORDERS
table:

Oracle:
CREATE TABLE ORDERS
(Order_ID integer primary key,
Order_Date date,
Customer_SID integer references CUSTOMER(SID),
Amount double);

Below are examples for specifying a foreign key by altering a table. This assumes that
the ORDERS table has been created, and the foreign key has not yet been put in:

Oracle:
ALTER TABLE ORDERS
ADD (CONSTRAINT fk_orders1) FOREIGN KEY (customer_sid) REFERENCES
CUSTOMER(SID);
CHAPTER 3 – SQL CREATE TABLE
EXAMPLE-1
CREATE TABLE Person
(
LastName varchar(30),
FirstName varchar,
Address varchar,
Age int(3)
)

The data type specifies what type of data the column can hold. The table below contains
the most common data types in SQL:

Data Type Description


integer(size) Hold integers only. The maximum number of digits are
int(size) specified in parenthesis.
smallint(size)
tinyint(size)
decimal(size,d) Hold numbers with fractions. The maximum number of digits
numeric(size,d) are specified in "size". The maximum number of digits to the
right of the decimal is specified in "d".
char(size) Holds a fixed length string (can contain letters, numbers, and
special characters). The fixed size is specified in parenthesis.
varchar(size) Holds a variable length string (can contain letters, numbers,
and special characters). The maximum size is specified in
parenthesis.
date(yyyymmdd) Holds a date

EXAMPLE-2
CREATE TABLE customer
(First_Name char(50),
Last_Name char(50),
Address char(50),
City char(50),
Country char(25),
Birth_Date date)

Sometimes, we want to provide a default value for each column. A default value is used
when you do not specify a column's value when inserting data into the table. To specify
a default value, add "Default [value]" after the data type declaration. In the above
example, if we want to default column "Address" to "Unknown" and City to "Mumbai",
we would type in

CREATE TABLE customer


(First_Name char(50),
Last_Name char(50),
Address char(50) default 'Unknown',
City char(50) default 'Mumbai',
Country char(25),
Birth_Date date)

NOT NULL

By default, a column can hold NULL. If you not want to allow NULL value in a column,
you will want to place a constraint on this column specifying that NULL is now not an
allowable value.

For example, in the following statement,

CREATE TABLE Customer


(SID integer NOT NULL,
Last_Name varchar (30) NOT NULL,
First_Name varchar(30));

Columns "SID" and "Last_Name" cannot include NULL, while "First_Name" can include
NULL.

UNIQUE

The UNIQUE constraint ensures that all values in a column are distinct.

For example, in the following statement,

CREATE TABLE Customer


(SID integer Unique,
Last_Name varchar (30),
First_Name varchar(30));

Column "SID" cannot include duplicate values, while such constraint does not hold for
columns "Last_Name" and "First_Name".
Please note that a column that is specified as a primary key must also be unique. At the
same time, a column that's unique may or may not be a primary key.

CHECK

The CHECK constraint ensures that all values in a column satisfy certain conditions.

For example, in the following statement,

CREATE TABLE Customer


(SID integer CHECK (SID > 0),
Last_Name varchar (30),
First_Name varchar(30));

Column "SID" must only include integers greater than 0.

Create a Database

To create a database:

CREATE DATABASE database_name


CHAPTER 4 – SQL INSERT TABLE
In SQL, there are essentially basically two ways to INSERT data into a table: One is to
insert it one row at a time, the other is to insert multiple rows at a time. Let's first look
at how we may INSERT data one row at a time:

Assuming that we have a table that has the following structure,

Table Store_Information

Column Name Data Type


store_name char(50)
Sales float
Date datetime

and now we wish to insert one additional row into the table representing the sales data
for Los Angeles on January 10, 1999. On that day, this store had $900 in sales. We will
hence use the following SQL script:

INSERT INTO Store_Information (store_name, Sales, Date)


VALUES ('Los Angeles', 900, 'Jan-10-1999')

The second type of INSERT INTO allows us to insert multiple rows into a table. Unlike
the previous example, where we insert a single row by specifying its values for all
columns, we now use a SELECT statement to specify the data that we want to insert
into the table. If you are thinking whether this means that you are using information
from another table

INSERT INTO "table1" ("column1", "column2", ...)


SELECT "column3", "column4", ...
FROM "table2"

Note that this is the simplest form. The entire statement can easily contain WHERE,
GROUP BY, and HAVING clauses, as well as table joins and aliases.

So for example, if we wish to have a table, Store_Information, that collects the sales
information for year 1998, and you already know that the source data resides in the
Sales_Information table, we'll type in:
INSERT INTO Store_Information (store_name, Sales, Date)
SELECT store_name, Sales, Date
FROM Sales_Information
WHERE Year(Date) = 1998

Here I have used the SQL Server syntax to extract the year information out of a date. Other
relational databases will have different syntax. For example, in Oracle, you will use
to_char(date,'yyyy')=1998.

Insert a New Row

This "Persons" table:

LastName FirstName Address City


Pettersen Kari Storgt 20 Stavanger

And this SQL statement:

INSERT INTO Persons


VALUES ('Hetland', 'Camilla', 'Hagabakka 24', 'Sandnes')

Will give this result:

LastName FirstName Address City


Pettersen Kari Storgt 20 Stavanger
Hetland Camilla Hagabakka 24 Sandnes

Insert Data in Specified Columns

This "Persons" table:

LastName FirstName Address City


Pettersen Kari Storgt 20 Stavanger
Hetland Camilla Hagabakka 24 Sandnes

And This SQL statement:

INSERT INTO Persons (LastName, Address)


VALUES ('Rasmussen', 'Storgt 67')

Will give this result:

LastName FirstName Address City


Pettersen Kari Storgt 20 Stavanger
Hetland Camilla Hagabakka 24 Sandnes
Rasmussen Storgt 67

CHAPTER 5 – SQL SELECT TABLE


The SQL SELECT Statement

The SELECT statement is used to select data from a table. The tabular result is stored in
a result table (called the result-set).

Syntax
SELECT column_name(s)
FROM table_name

Note: SQL statements are not case sensitive. SELECT is the same as select.

SQL SELECT Example

To select the content of columns named "LastName" and "FirstName", from the
database table called "Persons", use a SELECT statement like this:

SELECT LastName,FirstName FROM Persons

The database table "Persons":

LastName FirstName Address City


Hansen Ola Timoteivn 10 Sandnes
Svendson Tove Borgvn 23 Sandnes
Pettersen Kari Storgt 20 Stavanger

The result

LastName FirstName
Hansen Ola
Svendson Tove
Pettersen Kari

Select All Columns


To select all columns from the "Persons" table, use a * symbol instead of column names,
like this:

SELECT * FROM Persons

Result

LastName FirstName Address City


Hansen Ola Timoteivn 10 Sandnes
Svendson Tove Borgvn 23 Sandnes
Pettersen Kari Storgt 20 Stavanger

The Result Set

The result from a SQL query is stored in a result-set. Most database software systems
allow navigation of the result set with programming functions, like: Move-To-First-
Record, Get-Record-Content, Move-To-Next-Record, etc.

Semicolon after SQL Statements?

Semicolon is the standard way to separate each SQL statement in database systems that
allow more than one SQL statement to be executed in the same call to the server.

The SELECT DISTINCT Statement

The DISTINCT keyword is used to return only distinct (different) values.

The SELECT statement returns information from table columns. But what if we only
want to select distinct elements?

With SQL, all we need to do is to add a DISTINCT keyword to the SELECT statement:

Syntax
SELECT DISTINCT column_name(s)
FROM table_name

Using the DISTINCT keyword

To select ALL values from the column named "Company" we use a SELECT statement
like this:

SELECT Company FROM Orders


"Orders" table

Company OrderNumber
Sega 3412
W3Schools 2312
Trio 4678
W3Schools 6798

Result

Company
Sega
W3Schools
Trio
W3Schools

Note that "W3Schools" is listed twice in the result-set.

To select only DIFFERENT values from the column named "Company" we use a SELECT
DISTINCT statement like this:

SELECT DISTINCT Company FROM Orders

Result:

Company
Sega
W3Schools
Trio

Now "W3Schools" is listed only once in the result-set.

The WHERE clause is used to specify a selection criterion.

The WHERE Clause

To conditionally select data from a table, a WHERE clause can be added to the SELECT
statement.

Syntax
SELECT column FROM table
WHERE column operator value

With the WHERE clause, the following operators can be used:


Operator Description
= Equal
<> Not equal
> Greater than
< Less than
>= Greater than or equal
<= Less than or equal
BETWEEN Between an inclusive range
LIKE Search for a pattern

Note: In some versions of SQL the <> operator may be written as !=

Using the WHERE Clause

To select only the persons living in the city "Sandnes", we add a WHERE clause to the
SELECT statement:

SELECT * FROM Persons


WHERE City='Sandnes'

"Persons" table

LastName FirstName Address City Year


Hansen Ola Timoteivn 10 Sandnes 1951
Svendson Tove Borgvn 23 Sandnes 1978
Svendson Stale Kaivn 18 Sandnes 1980
Pettersen Kari Storgt 20 Stavanger 1960

Result

LastName FirstName Address City Year


Hansen Ola Timoteivn 10 Sandnes 1951
Svendson Tove Borgvn 23 Sandnes 1978
Svendson Stale Kaivn 18 Sandnes 1980

Using Quotes

Note that we have used single quotes around the conditional values in the examples.

SQL uses single quotes around text values (most database systems will also accept
double quotes). Numeric values should not be enclosed in quotes.
For text values:

This is correct:
SELECT * FROM Persons WHERE FirstName='Tove'
This is wrong:
SELECT * FROM Persons WHERE FirstName=Tove

For numeric values:

This is correct:
SELECT * FROM Persons WHERE Year>1965
This is wrong:
SELECT * FROM Persons WHERE Year>'1965'

The LIKE Condition

The LIKE condition is used to specify a search for a pattern in a column.

Syntax
SELECT column FROM table
WHERE column LIKE pattern

A "%" sign can be used to define wildcards (missing letters in the pattern) both before
and after the pattern.

Using LIKE

The following SQL statement will return persons with first names that start with an 'O':

SELECT * FROM Persons


WHERE FirstName LIKE 'O%'

The following SQL statement will return persons with first names that end with an 'a':

SELECT * FROM Persons


WHERE FirstName LIKE '%a'

The following SQL statement will return persons with first names that contain the
pattern 'la':

SELECT * FROM Persons


WHERE FirstName LIKE '%la%'
CHAPTER 6 – SQL UPDATE TABLE
The Update Statement

The UPDATE statement is used to modify the data in a table.

Syntax
UPDATE table_name
SET column_name = new_value
WHERE column_name = some_value

Person:

LastName FirstName Address City


Nilsen Fred Kirkegt 56 Stavanger
Rasmussen Storgt 67

Update one Column in a Row

We want to add a first name to the person with a last name of "Rasmussen":

UPDATE Person SET FirstName = 'Nina'


WHERE LastName = 'Rasmussen'

Result:

LastName FirstName Address City


Nilsen Fred Kirkegt 56 Stavanger
Rasmussen Nina Storgt 67

Update several Columns in a Row


We want to change the address and add the name of the city:

UPDATE Person
SET Address = 'Stien 12', City = 'Stavanger'
WHERE LastName = 'Rasmussen'

Result:

LastName FirstName Address City


Nilsen Fred Kirkegt 56 Stavanger
Rasmussen Nina Stien 12 Stavanger

EXAMPLE2

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

and we notice that the sales for Los Angeles on 01/08/1999 is actually $500 instead of
$300, and that particular entry needs to be updated. To do so, we use the following
SQL:

UPDATE Store_Information
SET Sales = 500
WHERE store_name = "Los Angeles"
AND Date = "Jan-08-1999"

The resulting table would look like

Table Store_Information

store_name Sales Date


Los Angeles $1500 Jan-05-1999
San Diego $250 Jan-07-1999
Los Angeles $500 Jan-08-1999
Boston $700 Jan-08-1999

In this case, there is only one row that satisfies the condition in the WHERE clause. If
there are multiple rows that satisfy the condition, all of them will be modified.
It is also possible to UPDATE multiple columns at the same time. The syntax in this
case would look like the following:

UPDATE "table_name"
SET column_1 = [value1], column_2 = [value2]
WHERE {condition}

CHAPTER 7 – SQL ALTER TABLE


ALTER TABLE

The ALTER TABLE statement is used to add or drop columns in an existing table.

ALTER TABLE table_name


ADD column_name datatype
ALTER TABLE table_name
DROP COLUMN column_name

Note: Some database systems don't allow the dropping of a column in a database table
(DROP COLUMN column_name).

Person:

LastName FirstName Address


Pettersen Kari Storgt 20

Example

To add a column named "City" in the "Person" table:

ALTER TABLE Person ADD City varchar(30)

Result:

LastName FirstName Address City


Pettersen Kari Storgt 20

Example

To drop the "Address" column in the "Person" table:


ALTER TABLE Person DROP COLUMN Address

Result:

LastName FirstName City


Pettersen Kari

EXMAPLE2
Once a table is created in the database, there are many occasions where one may wish
to change the structure of the table. Typical cases include the following:

- Add a column
- Drop a column
- Change a column name
- Change the data type for a column

Please note that the above is not an exhaustive list. There are other instances where
ALTER TABLE is used to change the table structure, such as changing the primary key
specification or adding a unique constraint to a column.

The SQL syntax for ALTER TABLE is

ALTER TABLE "table_name"


[alter specification]

[alter specification] is dependent on the type of alteration we wish to perform. For the
uses cited above, the [alter specification] statements are:

• Add a column: ADD "column 1" "data type for column 1"
• Drop a column: DROP "column 1"
• Change a column name: CHANGE "old column name" "new column name" "data
type for new column name"
• Change the data type for a column: MODIFY "column 1" "new data type"

Let's run through examples for each one of the above, using the "customer" table
created in the CREATE TABLE section:

Table customer

Column Name Data Type


First_Name char(50)
Last_Name char(50)
Address char(50)
City char(50)
Country char(25)
Birth_Date date

First, we want to add a column called "Gender" to this table. To do this, we key in:

ALTER table customer add Gender char(1)

Resulting table structure:

Table customer

Column Name Data Type


First_Name char(50)
Last_Name char(50)
Address char(50)
City char(50)
Country char(25)
Birth_Date date
Gender char(1)

Next, we want to rename "Address" to "Addr". To do this, we key in,

ALTER table customer change Address Addr char(50)

Resulting table structure:

Table customer

Column Name Data Type


First_Name char(50)
Last_Name char(50)
Addr char(50)
City char(50)
Country char(25)
Birth_Date date
Gender char(1)

Then, we want to change the data type for "Addr" to char(30). To do this, we key in,

ALTER table customer modify Addr char(30)


Resulting table structure:

Table customer

Column Name Data Type


First_Name char(50)
Last_Name char(50)
Addr char(30)
City char(50)
Country char(25)
Birth_Date date
Gender char(1)

Finally, we want to drop the column "Gender". To do this, we key in,

ALTER table customer drop Gender

Resulting table structure:

Table customer

Column Name Data Type


First_Name char(50)
Last_Name char(50)
Addr char(30)
City char(50)
Country char(25)
Birth_Date date
CHAPTER 8 – SQL DELETE AND DROP TABLE
Sometimes we may wish to get rid of records from a table. To do so, we can use the
DELETE FROM command. The syntax for this is

DELETE FROM "table_name"


WHERE {condition}

It is easiest to use an example. Say we currently have a table as below:

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

and we decide not to keep any information on Los Angeles in this table. To accomplish
this, we type the following SQL:

DELETE FROM Store_Information


WHERE store_name = "Los Angeles"

Now the content of table would look like,

Table Store_Information

store_name Sales Date


San Diego $250 Jan-07-1999
Boston $700 Jan-08-1999

EXAMPLE2

The DELETE Statement

The DELETE statement is used to delete rows in a table.

Syntax
DELETE FROM table_name
WHERE column_name = some_value
Person:

LastName FirstName Address City


Nilsen Fred Kirkegt 56 Stavanger
Rasmussen Nina Stien 12 Stavanger

Delete a Row

"Nina Rasmussen" is going to be deleted:

DELETE FROM Person WHERE LastName = 'Rasmussen'

Result

LastName FirstName Address City


Nilsen Fred Kirkegt 56 Stavanger

Delete All Rows

It is possible to delete all rows in a table without deleting the table. This means that the
table structure, attributes, and indexes will be intact:

DELETE FROM table_name


or
DELETE * FROM table_name

DROP STATEMENT
Delete a Table or Database

To delete a table (the table structure, attributes, and indexes will also be deleted):

DROP TABLE table_name

To delete a database:

DROP DATABASE database_name


Truncate a Table

What if we only want to get rid of the data inside a table, and not the table itself? Use
the TRUNCATE TABLE command (deletes only the data inside the table):

TRUNCATE TABLE table_name


CHAPTER 9 – SQL ALIAS
With SQL, aliases can be used for column names and table names.

Column Name Alias

The syntax is:

SELECT column AS column_alias FROM table

Table Name Alias

The syntax is:

SELECT column FROM table AS table_alias

Example: Using a Column Alias

This table (Persons):

LastName FirstName Address City


Hansen Ola Timoteivn 10 Sandnes
Svendson Tove Borgvn 23 Sandnes
Pettersen Kari Storgt 20 Stavanger

And this SQL:

SELECT LastName AS Family, FirstName AS Name


FROM Persons

Returns this result:

Family Name
Hansen Ola
Svendson Tove
Pettersen Kari
Example: Using a Table Alias

This table (Persons):

LastName FirstName Address City


Hansen Ola Timoteivn 10 Sandnes
Svendson Tove Borgvn 23 Sandnes
Pettersen Kari Storgt 20 Stavanger

And this SQL:

SELECT LastName, FirstName


FROM Persons AS Employees

Returns this result:

Table Employees:

LastName FirstName
Hansen Ola
Svendson Tove
Pettersen Kari
CHAPTER – 10 JOINS AND UNIONS
Joins and Keys

Sometimes we have to select data from two or more tables to make our result complete.
We have to perform a join.

Tables in a database can be related to each other with keys. A primary key is a column
with a unique value for each row. Each primary key value must be unique within the
table. The purpose is to bind data together, across tables, without repeating all of the
data in every table.

In the "Employees" table below, the "Employee_ID" column is the primary key, meaning
that no two rows can have the same Employee_ID. The Employee_ID distinguishes two
persons even if they have the same name.

When you look at the example tables below, notice that:

• The "Employee_ID" column is the primary key of the "Employees" table


• The "Prod_ID" column is the primary key of the "Orders" table
• The "Employee_ID" column in the "Orders" table is used to refer to the persons
in the "Employees" table without using their names

Employees:

Employee_ID Name
01 Hansen, Ola
02 Svendson, Tove
03 Svendson, Stephen
04 Pettersen, Kari

Orders:

Prod_ID Product Employee_ID


234 Printer 01
657 Table 03
865 Chair 03

Referring to Two Tables

We can select data from two tables by referring to two tables, like this:
Example

Who has ordered a product, and what did they order?

SELECT Employees.Name, Orders.Product


FROM Employees, Orders
WHERE Employees.Employee_ID=Orders.Employee_ID

Result

Name Product
Hansen, Ola Printer
Svendson, Stephen Table
Svendson, Stephen Chair

Example

Who ordered a printer?

SELECT Employees.Name
FROM Employees, Orders
WHERE Employees.Employee_ID=Orders.Employee_ID
AND Orders.Product='Printer'

Result

Name
Hansen, Ola

Using Joins

OR we can select data from two tables with the JOIN keyword, like this:

Example INNER JOIN

Syntax

SELECT field1, field2, field3


FROM first_table
INNER JOIN second_table
ON first_table.keyfield = second_table.foreign_keyfield

Who has ordered a product, and what did they order?


SELECT Employees.Name, Orders.Product
FROM Employees
INNER JOIN Orders
ON Employees.Employee_ID=Orders.Employee_ID

The INNER JOIN returns all rows from both tables where there is a match. If there are
rows in Employees that do not have matches in Orders, those rows will not be listed.

Result

Name Product
Hansen, Ola Printer
Svendson, Stephen Table
Svendson, Stephen Chair

Example LEFT JOIN

Syntax

SELECT field1, field2, field3


FROM first_table
LEFT JOIN second_table
ON first_table.keyfield = second_table.foreign_keyfield

List all employees, and their orders - if any.

SELECT Employees.Name, Orders.Product


FROM Employees
LEFT JOIN Orders
ON Employees.Employee_ID=Orders.Employee_ID

The LEFT JOIN returns all the rows from the first table (Employees), even if there are no
matches in the second table (Orders). If there are rows in Employees that do not have
matches in Orders, those rows also will be listed.

Result

Name Product
Hansen, Ola Printer
Svendson, Tove
Svendson, Stephen Table
Svendson, Stephen Chair
Pettersen, Kari
Example RIGHT JOIN

Syntax

SELECT field1, field2, field3


FROM first_table
RIGHT JOIN second_table
ON first_table.keyfield = second_table.foreign_keyfield

List all orders, and who has ordered - if any.

SELECT Employees.Name, Orders.Product


FROM Employees
RIGHT JOIN Orders
ON Employees.Employee_ID=Orders.Employee_ID

The RIGHT JOIN returns all the rows from the second table (Orders), even if there are
no matches in the first table (Employees). If there had been any rows in Orders that did
not have matches in Employees, those rows also would have been listed.

Result

Name Product
Hansen, Ola Printer
Svendson, Stephen Table
Svendson, Stephen Chair

Example

Who ordered a printer?

SELECT Employees.Name
FROM Employees
INNER JOIN Orders
ON Employees.Employee_ID=Orders.Employee_ID
WHERE Orders.Product = 'Printer'

Result

Name
Hansen, Ola

EXAMPLE2
Let's assume that we have the following two tables,
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

Table Geography

region_name store_name
East Boston
East New York
West Los Angeles
West San Diego

and we want to find out sales by region. We see that table Geography includes
information on regions and stores, and table Store_Information contains sales
information for each store. To get the sales information by region, we have to combine
the information from the two tables. Examining the two tables, we find that they are
linked via the common field, "store_name". We will first present the SQL statement and
explain the use of each segment later:

SELECT A1.region_name REGION, SUM(A2.Sales) SALES


FROM Geography A1, Store_Information A2
WHERE A1.store_name = A2.store_name
GROUP BY A1.region_name

Result:

REGION SALES
East $700
West $2050

The first two lines tell SQL to select two fields, the first one is the field "region_name"
from table Geography (aliased as REGION), and the second one is the sum of the field
"Sales" from table Store_Information (aliased as SALES). Notice how the table aliases
are used here: Geography is aliased as A1, and Store_Information is aliased as A2.
Without the aliasing, the first line would become

SELECT Geography.region_name REGION, SUM(Store_Information.Sales)


SALES
which is much more cumbersome. In essence, table aliases make the entire SQL
statement easier to understand, especially when multiple tables are included.

Next, we turn our attention to line 3, the WHERE statement. This is where the
condition of the join is specified. In this case, we want to make sure that the content in
"store_name" in table Geography matches that in table Store_Information, and the
way to do it is to set them equal. This WHERE statement is essential in making sure
you get the correct output. Without the correct WHERE statement, a Cartesian Join will
result. Cartesian joins will result in the query returning every possible combination of the
two (or whatever the number of tables in the FROM statement) tables. In this case, a
Cartesian join would result in a total of 4 x 4 = 16 rows being returned.

Previously, we had looked at left join, or inner join, where we select rows common to
the participating tables to a join. What about the cases where we are interested in
selecting elements in a table regardless of whether they are present in the second table?
We will now need to use the SQL OUTER JOIN command.

The syntax for performing an outer join in SQL is database-dependent. For example, in
Oracle, we will place an "(+)" in the WHERE clause on the other side of the table for
which we want to include all the rows.

Let's assume that we have the following two tables,

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

Table Geography

region_name store_name
East Boston
East New York
West Los Angeles
West San Diego

and we want to find out the sales amount for all of the stores. If we do a regular join,
we will not be able to get what we want because we will have missed "New York," since
it does not appear in the Store_Information table. Therefore, we need to perform an
outer join on the two tables above:
SELECT A1.store_name, SUM(A2.Sales) SALES
FROM Geography A1, Store_Information A2
WHERE A1.store_name = A2.store_name (+)
GROUP BY A1.store_name

Note that in this case, we are using the Oracle syntax for outer join.

Result:

store_name SALES
Boston $700
New York
Los Angeles $1800
San Diego $250

Note: NULL is returned when there is no match on the second table. In this case, "New
York" does not appear in the table Store_Information, thus its corresponding "SALES"
column is NULL.

SQL UNION
UNION

The UNION command is used to select related information from two tables, much like
the JOIN command. However, when using the UNION command all selected columns
need to be of the same data type.

Note: With UNION, only distinct values are selected.

SQL Statement 1
UNION
SQL Statement 2

Employees_Norway:

E_ID E_Name
01 Hansen, Ola
02 Svendson, Tove
03 Svendson, Stephen
04 Pettersen, Kari
Employees_USA:

E_ID E_Name
01 Turner, Sally
02 Kent, Clark
03 Svendson, Stephen
04 Scott, Stephen

Using the UNION Command

Example

List all different employee names in Norway and USA:

SELECT E_Name FROM Employees_Norway


UNION
SELECT E_Name FROM Employees_USA

Result

E_Name
Hansen, Ola
Svendson, Tove
Svendson, Stephen
Pettersen, Kari
Turner, Sally
Kent, Clark
Scott, Stephen

Note: This command cannot be used to list all employees in Norway and USA. In the
example above we have two employees with equal names, and only one of them is
listed. The UNION command only selects distinct values.

UNION ALL

The UNION ALL command is equal to the UNION command, except that UNION ALL
selects all values.

SQL Statement 1
UNION ALL
SQL Statement 2
Using the UNION ALL Command

Example

List all employees in Norway and USA:

SELECT E_Name FROM Employees_Norway


UNION ALL
SELECT E_Name FROM Employees_USA

Result

E_Name
Hansen, Ola
Svendson, Tove
Svendson, Stephen
Pettersen, Kari
Turner, Sally
Kent, Clark
Svendson, Stephen
Scott, Stephen
CHAPTER 11 SQL FUNCTIONS AND STRING
OPERATIONS.
SQL has a lot of built-in functions for counting and calculations.

Function Syntax

The syntax for built-in SQL functions is:

SELECT function(column) FROM table

Types of Functions

There are several basic types and categories of functions in SQL. The basic types of
functions are:

• Aggregate Functions
• Scalar functions

Aggregate functions

Aggregate functions operate against a collection of values, but return a single value.

Note: If used among many other expressions in the item list of a SELECT statement,
the SELECT must have a GROUP BY clause!!

"Persons" table (used in most examples)


Name Age
Hansen, Ola 34
Svendson, Tove 45
Pettersen, Kari 19

Aggregate functions in MS Access


Function Description
AVG(column) Returns the average value of a column
COUNT(column) Returns the number of rows (without a NULL value) of a
column
COUNT(*) Returns the number of selected rows
FIRST(column) Returns the value of the first record in a specified field
LAST(column) Returns the value of the last record in a specified field
MAX(column) Returns the highest value of a column
MIN(column) Returns the lowest value of a column
STDEV(column)
STDEVP(column)
SUM(column) Returns the total sum of a column
VAR(column)
VARP(column)

Aggregate functions in SQL Server


Function Description
AVG(column) Returns the average value of a column
BINARY_CHECKSUM
CHECKSUM
CHECKSUM_AGG
COUNT(column) Returns the number of rows (without a NULL value) of a
column
COUNT(*) Returns the number of selected rows
COUNT(DISTINCT column) Returns the number of distinct results
FIRST(column) Returns the value of the first record in a specified field
(not supported in SQLServer2K)
LAST(column) Returns the value of the last record in a specified field
(not supported in SQLServer2K)
MAX(column) Returns the highest value of a column
MIN(column) Returns the lowest value of a column
STDEV(column)
STDEVP(column)
SUM(column) Returns the total sum of a column
VAR(column)
VARP(column)

Scalar functions

Scalar functions operate against a single value, and return a single value based on the
input value.

Useful Scalar Functions in MS Access


Function Description
UCASE(c) Converts a field to upper case
LCASE(c) Converts a field to lower case
MID(c,start[,end]) Extract characters from a text field
LEN(c) Returns the length of a text field
INSTR(c,char) Returns the numeric position of a named character within
a text field
LEFT(c,number_of_char) Return the left part of a text field requested
RIGHT(c,number_of_char) Return the right part of a text field requested
ROUND(c,decimals) Rounds a numeric field to the number of decimals
specified
MOD(x,y) Returns the remainder of a division operation
NOW() Returns the current system date
FORMAT(c,format) Changes the way a field is displayed
DATEDIFF(d,date1,date2) Used to perform date calculations

EXAMPLE2

Since we have started dealing with numbers, the next natural question to ask is if it is
possible to do math on those numbers, such as summing them up or taking their
average. The answer is yes! SQL has several arithematic functions, and they are:

- AVG
- COUNT
- MAX
- MIN
- SUM

The syntax for using functions is,

SELECT "function type"("column_name")


FROM "table_name"

For example, if we want to get the sum 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 would type in

SELECT SUM(Sales) FROM Store_Information

Result:

SUM(Sales)
$2750
$2750 represents the sum of all Sales entries: $1500 + $250 + $300 + $700.

In addition to using functions, it is also possible to use SQL to perform simple tasks such
as addition (+) and subtraction (-). For character-type data, there are also several string
functions available, such as concatenation, trim, and substring functions. Different
RDBMS vendors have different string functions implementations, and it is best to consult
the references for your RDBMS to see how these functions are used.

Another arithmetic function is COUNT. This allows us to COUNT up the number of row
in a certain table. The syntax is,

SELECT COUNT("column_name")
FROM "table_name"

For example, if we want to find the number of store entries in our 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 COUNT(store_name)
FROM Store_Information

Result:

Count(store_name)
4

COUNT and DISTINCT can be used together in a statement to fetch the number of
distinct entries in a table. For example, if we want to find out the number of distinct
stores, we'd type,

SELECT COUNT(DISTINCT store_name)


FROM Store_Information
Result:

Count(DISTINCT store_name)
3

STRINGS
The Substring function in SQL is used to grab a portion of the stored data. This function
is called differently for the different databases:

• MySQL: SUBSTR(), SUBSTRING()


• Oracle: SUBSTR()
• SQL Server: SUBSTRING()

The most frequent uses are as follows (we will use SUBSTR() here):

SUBSTR(str,pos): Select all characters from <str> starting with position <pos>. Note
that this syntax is not supported in SQL Server.

SUBSTR(str,pos,len): Starting with the <pos>th character in string <str> and select
the next <len> characters.

Assume we have the following table:

Table Geography

region_name store_name
East Boston
East New York
West Los Angeles
West San Diego

Example 1:

SELECT SUBSTR(store_name, 3)
FROM Geography
WHERE store_name = 'Los Angeles';

Result:

's Angeles'

Example 2:
SELECT SUBSTR(store_name,2,4)
FROM Geography
WHERE store_name = 'San Diego';

Result:

'an D' The TRIM function in SQL is used to remove specified prefix or suffix from a
string. The most common pattern being removed is white spaces. This function is called
differently in different databases:

• MySQL: TRIM(), RTRIM(), LTRIM()


• Oracle: RTRIM(), LTRIM()
• SQL Server: RTRIM(), LTRIM()

The syntax for these trim functions are:

TRIM([[LOCATION] [remstr] FROM ] str): [LOCATION] can be either LEADING,


TRAILING, or BOTH. This function gets rid of the [remstr] pattern from either the
beginning of the string or the end of the string, or both. If no [remstr] is specified, white
spaces are removed.

LTRIM(str): Removes all white spaces from the beginning of the string.

RTRIM(str): Removes all white spaces at the end of the string.

Example 1:

SELECT TRIM(' Sample ');

Result:

'Sample'

Example 2:

SELECT LTRIM(' Sample ');

Result:

'Sample '

Example 3:

SELECT RTRIM(' Sample ');

Result:
' Sample'

Sometimes it is necessary to combine together (concatenate) the results from several


different fields. Each database provides a way to do this:

• MySQL: CONCAT()
• Oracle: CONCAT(), ||
• SQL Server: +

The syntax for CONCAT() is as follows:

CONCAT(str1, str2, str3, ...): Concatenate str1, str2, str3, and any other strings
together. Please note the Oracle CONCAT() function only allows two arguments -- only
two strings can be put together at a time using this function. However, it is possible to
concatenate more than two strings at a time in Oracle using '||'.

Let's look at some examples. Assume we have the following table:

Table Geography

region_name store_name
East Boston
East New York
West Los Angeles
West San Diego

Example 1:

MySQL/Oracle:
SELECT CONCAT(region_name,store_name) FROM Geography
WHERE store_name = 'Boston';

Result:

'EastBoston'

Example 2:

Oracle:
SELECT region_name || ' ' || store_name FROM Geography
WHERE store_name = 'Boston';

Result:

'East Boston'
Example 3:

SQL Server:
SELECT region_name + ' ' + store_name FROM Geography
WHERE store_name = 'Boston';

Result:

'East Boston'
CHAPTER-12 SQL AND OR,SQL IN,SQL
BETWEEN,SQL LIKE,SQL ORDER BY,SQL
COUNT,SQL GROUP BY,SQL HAVING
STATEMENTS.
1>The SELECT INTO Statement

The SELECT INTO statement is most often used to create backup copies of tables or for
archiving records.

Syntax
SELECT column_name(s) INTO newtable [IN externaldatabase]
FROM source

Make a Backup Copy

The following example makes a backup copy of the "Persons" table:

SELECT * INTO Persons_backup


FROM Persons

The IN clause can be used to copy tables into another database:

SELECT Persons.* INTO Persons IN 'Backup.mdb'


FROM Persons

If you only want to copy a few fields, you can do so by listing them after the SELECT
statement:

SELECT LastName,FirstName INTO Persons_backup


FROM Persons

You can also add a WHERE clause. The following example creates a "Persons_backup"
table with two columns (FirstName and LastName) by extracting the persons who lives
in "Sandnes" from the "Persons" table:
SELECT LastName,Firstname INTO Persons_backup
FROM Persons
WHERE City='Sandnes'

Selecting data from more than one table is also possible. The following example creates
a new table "Empl_Ord_backup" that contains data from the two tables Employees and
Orders:

SELECT Employees.Name,Orders.Product
INTO Empl_Ord_backup
FROM Employees
INNER JOIN Orders
ON Employees.Employee_ID=Orders.Employee_ID

2>SQL IN
IN

The IN operator may be used if you know the exact value you want to return for at least
one of the columns.

SELECT column_name FROM table_name


WHERE column_name IN (value1,value2,..)

Original Table (used in the examples)

LastName FirstName Address City


Hansen Ola Timoteivn 10 Sandnes
Nordmann Anna Neset 18 Sandnes
Pettersen Kari Storgt 20 Stavanger
Svendson Tove Borgvn 23 Sandnes

Example 1

To display the persons with LastName equal to "Hansen" or "Pettersen", use the
following SQL:

SELECT * FROM Persons


WHERE LastName IN ('Hansen','Pettersen')

Result:

LastName FirstName Address City


Hansen Ola Timoteivn 10 Sandnes
Pettersen Kari Storgt 20 Stavanger

EXAMPLE2

In SQL, there are two uses of the IN keyword, and this section introduces the one that
is related to the WHERE clause. When used in this context, we know exactly the value
of the returned values we want to see for at least one of the columns. The syntax for
using the IN keyword is as follows:

SELECT "column_name"
FROM "table_name"
WHERE "column_name" IN ('value1', 'value2', ...)

The number of values in the parenthesis can be one or more, with each values
separated by comma. Values can be numerical or characters. If there is only one value
inside the parenthesis, this commend is equivalent to

WHERE "column_name" = 'value1'

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

we key in,

SELECT *
FROM Store_Information
WHERE store_name IN ('Los Angeles', 'San Diego')

Result:

store_name Sales Date


Los Angeles $1500 Jan-05-1999
San Diego $250 Jan-07-1999
3> SQL BETWEEN
BETWEEN ... AND

The BETWEEN ... AND operator selects a range of data between two values. These
values can be numbers, text, or dates.

SELECT column_name FROM table_name


WHERE column_name
BETWEEN value1 AND value2

Original Table (used in the examples)

LastName FirstName Address City


Hansen Ola Timoteivn 10 Sandnes
Nordmann Anna Neset 18 Sandnes
Pettersen Kari Storgt 20 Stavanger
Svendson Tove Borgvn 23 Sandnes

Example 1

To display the persons alphabetically between (and including) "Hansen" and exclusive
"Pettersen", use the following SQL:

SELECT * FROM Persons WHERE LastName


BETWEEN 'Hansen' AND 'Pettersen'

Result:

LastName FirstName Address City


Hansen Ola Timoteivn 10 Sandnes
Nordmann Anna Neset 18 Sandnes

IMPORTANT! The BETWEEN...AND operator is treated differently in different


databases. With some databases a person with the LastName of "Hansen" or
"Pettersen" will not be listed (BETWEEN..AND only selects fields that are between and
excluding the test values). With some databases a person with the last name of
"Hansen" or "Pettersen" will be listed (BETWEEN..AND selects fields that are between
and including the test values). With other databases a person with the last name of
"Hansen" will be listed, but "Pettersen" will not be listed (BETWEEN..AND selects fields
between the test values, including the first test value and excluding the last test value).
Therefore: Check how your database treats the BETWEEN....AND operator!

Example 2

To display the persons outside the range used in the previous example, use the NOT
operator:

SELECT * FROM Persons WHERE LastName


NOT BETWEEN 'Hansen' AND 'Pettersen'

Result:

LastName FirstName Address City


Pettersen Kari Storgt 20 Stavanger
Svendson Tove Borgvn 23 Sandnes

Whereas the IN keyword help people to limit the selection criteria to one or more
discrete values, the BETWEEN keyword allows for selecting a range. The syntax for the
BETWEEN clause is as follows:

SELECT "column_name"
FROM "table_name"
WHERE "column_name" BETWEEN 'value1' AND 'value2'

This will select all rows whose column has a value between 'value1' and 'value2'.

For example, we may wish to select view all sales information between January 6, 1999,
and January 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

we key in,
SELECT *
FROM Store_Information
WHERE Date BETWEEN 'Jan-06-1999' AND 'Jan-10-1999'

Note that date may be stored in different formats in different databases. This tutorial
simply choose one of the formats.

Result:

store_name Sales Date


San Diego $250 Jan-07-1999
San Francisco $300 Jan-08-1999
Boston $700 Jan-08-1999

4> LIKE is another keyword that is used in the WHERE clause. Basically, LIKE allows
you to do a search based on a pattern rather than specifying exactly what is desired (as
in IN) or spell out a range (as in BETWEEN). The syntax for is as follows:

SELECT "column_name"
FROM "table_name"
WHERE "column_name" LIKE {PATTERN}

{PATTERN} often consists of wildcards. Here are some examples:

• 'A_Z': All string that starts with 'A', another character, and end with 'Z'. For
example, 'ABZ' and 'A2Z' would both satisfy the condition, while 'AKKZ' would not
(because there are two characters between A and Z instead of one).

• 'ABC%': All strings that start with 'ABC'. For example, 'ABCD' and 'ABCABC' would
both satisfy the condition.

• '%XYZ': All strings that end with 'XYZ'. For example, 'WXYZ' and 'ZZXYZ' would
both satisfy the condition.

• '%AN%': All string that contain the pattern 'AN' anywhere. For example, 'LOS
ANGELES' and 'SAN FRANCISCO' would both satisfy the condition.

Let's say we have the following table:

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

We want to find all stores whose name contains 'AN'. To do so, we key in,

SELECT *
FROM Store_Information
WHERE store_name LIKE '%AN%'

Result:

store_name Sales Date


LOS ANGELES $1500 Jan-05-1999
SAN DIEGO $250 Jan-07-1999
SAN FRANCISCO $300 Jan-08-1999

5>SQL ORDER BY

So far, we have seen how to get data out of a table using SELECT and WHERE
commands. Often, however, we need to list the output in a particular order. This could
be in ascending order, in descending order, or could be based on either numerical value
or text value. In such cases, we can use the ORDER BY keyword to achieve our goal.

The syntax for an ORDER BY statement is as follows:

SELECT "column_name"
FROM "table_name"
[WHERE "condition"]
ORDER BY "column_name" [ASC, DESC]

The [] means that the WHERE statement is optional. However, if a WHERE clause
exists, it comes before the ORDER BY clause. ASC means that the results will be
shown in ascending order, and DESC means that the results will be shown in
descending order. If neither is specified, the default is ASC.

It is possible to order by more than one column. In this case, the ORDER BY clause
above becomes

ORDER BY "column_name1" [ASC, DESC], "column_name2" [ASC, DESC]


Assuming that we choose ascending order for both columns, the output will be ordered
in ascending order according to column 1. If there is a tie for the value of column 1, we
the sort in ascending order by column 2.

For example, we may wish to list the contents of Table Store_Information by dollar
amount, 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

we key in,

SELECT store_name, Sales, Date


FROM Store_Information
ORDER BY Sales DESC

Result:

store_name 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

In addition to column name, we may also use column position (based on the SQL query)
to indicate which column we want to apply the ORDER BY clause. The first column is 1,
second column is 2, and so on. In the above example, we will achieve the same results
by the following command:

SELECT store_name, Sales, Date


FROM Store_Information
ORDER BY 2 DESC

The ORDER BY keyword is used to sort the result.


Sort the Rows

The ORDER BY clause is used to sort the rows.

Orders:

Company OrderNumber
Sega 3412
ABC Shop 5678
W3Schools 2312
W3Schools 6798

Example

To display the company names in alphabetical order:

SELECT Company, OrderNumber FROM Orders


ORDER BY Company

Result:

Company OrderNumber
ABC Shop 5678
Sega 3412
W3Schools 6798
W3Schools 2312

Example

To display the company names in alphabetical order AND the OrderNumber in numerical
order:

SELECT Company, OrderNumber FROM Orders


ORDER BY Company, OrderNumber

Result:

Company OrderNumber
ABC Shop 5678
Sega 3412
W3Schools 2312
W3Schools 6798

Example

To display the company names in reverse alphabetical order:


SELECT Company, OrderNumber FROM Orders
ORDER BY Company DESC

Result:

Company OrderNumber
W3Schools 6798
W3Schools 2312
Sega 3412
ABC Shop 5678

Example

To display the company names in reverse alphabetical order AND the OrderNumber in
numerical order:

SELECT Company, OrderNumber FROM Orders


ORDER BY Company DESC, OrderNumber ASC

Result:

Company OrderNumber
W3Schools 2312
W3Schools 6798
Sega 3412
ABC Shop 5678

Notice that there are two equal company names (W3Schools) in the result above. The
only time you will see the second column in ASC order would be when there are
duplicated values in the first sort column, or a handful of nulls.

6>SQL HAVING

Another thing people may want to do is to limit the output based on the corresponding
sum (or any other aggregate functions). For example, we might want to see only the
stores with sales over $1,500. Instead of using the WHERE clause in the SQL
statement, though, we need to use the HAVING clause, which is reserved for
aggregate functions. The HAVING clause is typically placed near the end of the SQL
statement, and a SQL statement with the HAVING clause may or may not include the
GROUP BY clause. The syntax for HAVING is,

SELECT "column_name1", SUM("column_name2")


FROM "table_name"
GROUP BY "column_name1"
HAVING (arithmetic function condition)
Note: the GROUP BY clause is optional.

In our 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 would type,

SELECT store_name, SUM(sales)


FROM Store_Information
GROUP BY store_name
HAVING SUM(sales) > 1500

Result:

store_name SUM(Sales)
Los Angeles $1800

7>SQL WHERE

The WHERE clause is used to specify a selection criterion.

The WHERE Clause

To conditionally select data from a table, a WHERE clause can be added to the SELECT
statement.

Syntax
SELECT column FROM table
WHERE column operator value

With the WHERE clause, the following operators can be used:

Operator Description
= Equal
<> Not equal
> Greater than
< Less than
>= Greater than or equal
<= Less than or equal
BETWEEN Between an inclusive range
LIKE Search for a pattern

Note: In some versions of SQL the <> operator may be written as !=

Using the WHERE Clause

To select only the persons living in the city "Sandnes", we add a WHERE clause to the
SELECT statement:

SELECT * FROM Persons


WHERE City='Sandnes'

"Persons" table

LastName FirstName Address City Year


Hansen Ola Timoteivn 10 Sandnes 1951
Svendson Tove Borgvn 23 Sandnes 1978
Svendson Stale Kaivn 18 Sandnes 1980
Pettersen Kari Storgt 20 Stavanger 1960

Result

LastName FirstName Address City Year


Hansen Ola Timoteivn 10 Sandnes 1951
Svendson Tove Borgvn 23 Sandnes 1978
Svendson Stale Kaivn 18 Sandnes 1980

Using Quotes

Note that we have used single quotes around the conditional values in the examples.

SQL uses single quotes around text values (most database systems will also accept
double quotes). Numeric values should not be enclosed in quotes.

For text values:

This is correct:
SELECT * FROM Persons WHERE FirstName='Tove'
This is wrong:
SELECT * FROM Persons WHERE FirstName=Tove

For numeric values:

This is correct:
SELECT * FROM Persons WHERE Year>1965
This is wrong:
SELECT * FROM Persons WHERE Year>'1965'

The LIKE Condition

The LIKE condition is used to specify a search for a pattern in a column.

Syntax
SELECT column FROM table
WHERE column LIKE pattern

A "%" sign can be used to define wildcards (missing letters in the pattern) both before
and after the pattern.

Using LIKE

The following SQL statement will return persons with first names that start with an 'O':

SELECT * FROM Persons


WHERE FirstName LIKE 'O%'

The following SQL statement will return persons with first names that end with an 'a':

SELECT * FROM Persons


WHERE FirstName LIKE '%a'

The following SQL statement will return persons with first names that contain the
pattern 'la':

SELECT * FROM Persons


WHERE FirstName LIKE '%la%'

Next, we might want to conditionally select the data from a table. For example, we may
want to only retrieve stores with sales above $1,000. To do this, we use the WHERE
keyword. The syntax is as follows:
SELECT "column_name"
FROM "table_name"
WHERE "condition"

For example, to select all stores with sales above $1,000 in 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 key in,

SELECT store_name
FROM Store_Information
WHERE Sales > 1000

Result:

store_name
Los Angeles

8>SQL AND & OR


AND & OR

AND and OR join two or more conditions in a WHERE clause.

The AND operator displays a row if ALL conditions listed are true. The OR operator
displays a row if ANY of the conditions listed are true.

Original Table (used in the examples)

LastName FirstName Address City


Hansen Ola Timoteivn 10 Sandnes
Svendson Tove Borgvn 23 Sandnes
Svendson Stephen Kaivn 18 Sandnes

Example
Use AND to display each person with the first name equal to "Tove", and the last name
equal to "Svendson":

SELECT * FROM Persons


WHERE FirstName='Tove'
AND LastName='Svendson'

Result:

LastName FirstName Address City


Svendson Tove Borgvn 23 Sandnes

Example

Use OR to display each person with the first name equal to "Tove", or the last name
equal to "Svendson":

SELECT * FROM Persons


WHERE firstname='Tove'
OR lastname='Svendson'

Result:

LastName FirstName Address City


Svendson Tove Borgvn 23 Sandnes
Svendson Stephen Kaivn 18 Sandnes

Example

You can also combine AND and OR (use parentheses to form complex expressions):

SELECT * FROM Persons WHERE


(FirstName='Tove' OR FirstName='Stephen')
AND LastName='Svendson'

Result:

LastName FirstName Address City


Svendson Tove Borgvn 23 Sandnes
Svendson Stephen Kaivn 18 Sandnes

In the previous section, we have seen that the WHERE keyword can be used to
conditionally select data from a table. This condition can be a simple condition (like the
one presented in the previous section), or it can be a compound condition. Compound
conditions are made up of multiple simple conditions connected by AND or OR. There is
no limit to the number of simple conditions that can be present in a single SQL
statement.

The syntax for a compound condition is as follows:

SELECT "column_name"
FROM "table_name"
WHERE "simple condition"
{[AND|OR] "simple condition"}+

The {}+ means that the expression inside the bracket will occur one or more times.
Note that AND and OR can be used interchangeably. In addition, we may use the
parenthesis sign () to indicate the order of the condition.

For example, we may wish to select all stores with sales greater than $1,000 or all
stores with sales less than $500 but greater than $275 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

we key in,

SELECT store_name
FROM Store_Information
WHERE Sales > 1000
OR (Sales < 500 AND Sales > 275)

Result:

store_name
Los Angeles
San Francisco

9>GROUP BY

Aggregate functions (like SUM) often need an added GROUP BY functionality.


GROUP BY...

GROUP BY... was added to SQL because aggregate functions (like SUM) return the
aggregate of all column values every time they are called, and without the GROUP BY
function it was impossible to find the sum for each individual group of column values.

The syntax for the GROUP BY function is:

SELECT column,SUM(column) FROM table GROUP BY column

GROUP BY Example

This "Sales" Table:

Company Amount
W3Schools 5500
IBM 4500
W3Schools 7100

And This SQL:

SELECT Company, SUM(Amount) FROM Sales

Returns this result:

Company SUM(Amount)
W3Schools 17100
IBM 17100
W3Schools 17100

The above code is invalid because the column returned is not part of an aggregate. A
GROUP BY clause will solve this problem:

SELECT Company,SUM(Amount) FROM Sales


GROUP BY Company

Returns this result:

Company SUM(Amount)
W3Schools 12600
IBM 4500
HAVING...

HAVING... was added to SQL because the WHERE keyword could not be used against
aggregate functions (like SUM), and without HAVING... it would be impossible to test for
result conditions.

The syntax for the HAVING function is:

SELECT column,SUM(column) FROM table


GROUP BY column
HAVING SUM(column) condition value

This "Sales" Table:

Company Amount
W3Schools 5500
IBM 4500
W3Schools 7100

This SQL:

SELECT Company,SUM(Amount) FROM Sales


GROUP BY Company
HAVING SUM(Amount)>10000

Returns this result

Company SUM(Amount)
W3Schools 12600

10>SQL CONSTRAINTS

You can place constraints to limit the type of data that can go into a table. Such
constraints can be specified when the table when the table is first created via the
CREATE TABLE statement, or after the table is already created via the ALTER TABLE
statement.

Common types of constraints include the following:

- NOT NULL
- UNIQUE
- CHECK
- Primary Key
- Foreign Key
Each is described in detail below.

NOT NULL

By default, a column can hold NULL. If you not want to allow NULL value in a column,
you will want to place a constraint on this column specifying that NULL is now not an
allowable value.

For example, in the following statement,

CREATE TABLE Customer


(SID integer NOT NULL,
Last_Name varchar (30) NOT NULL,
First_Name varchar(30));

Columns "SID" and "Last_Name" cannot include NULL, while "First_Name" can include
NULL.

UNIQUE

The UNIQUE constraint ensures that all values in a column are distinct.

For example, in the following statement,

CREATE TABLE Customer


(SID integer Unique,
Last_Name varchar (30),
First_Name varchar(30));

Column "SID" cannot include duplicate values, while such constraint does not hold for
columns "Last_Name" and "First_Name".

Please note that a column that is specified as a primary key must also be unique. At the
same time, a column that's unique may or may not be a primary key.

CHECK

The CHECK constraint ensures that all values in a column satisfy certain conditions.

For example, in the following statement,

CREATE TABLE Customer


(SID integer CHECK (SID > 0),
Last_Name varchar (30),
First_Name varchar(30));

Column "SID" must only include integers greater than 0.


13 CHAPTER - SQL VIEW AND INDEXES.
VIEWS

Views can be considered as virtual tables. Generally speaking, a table has a set of
definition, and it physically stores the data. A view also has a set of definitions, which is
build on top of table(s) or other view(s), and it does not physically store the data.

The syntax for creating a view is as follows:

CREATE VIEW "VIEW_NAME" AS "SQL Statement"

"SQL Statement" can be any of the SQL statements we have discussed in this tutorial.

Let's use a simple example to illustrate. Say we have the following table:

TABLE Customer
(First_Name char(50),
Last_Name char(50),
Address char(50),
City char(50),
Country char(25),
Birth_Date date)

and we want to create a view called V_Customer that contains only the First_Name,
Last_Name, and Country columns from this table, we would type in,

CREATE VIEW V_Customer


AS SELECT First_Name, Last_Name, Country
FROM Customer

Now we have a view called V_Customer with the following structure:

View V_Customer
(First_Name char(50),
Last_Name char(50),
Country char(25))
We can also use a view to apply joins to two tables. In this case, users only see one
view rather than two tables, and the SQL statement users need to issue becomes much
simpler. Let's say we have the following two tables:

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

Table Geography

region_name store_name
East Boston
East New York
West Los Angeles
West San Diego

and we want to build a view that has sales by region information. We would issue the
following SQL statement:

CREATE VIEW V_REGION_SALES


AS SELECT A1.region_name REGION, SUM(A2.Sales) SALES
FROM Geography A1, Store_Information A2
WHERE A1.store_name = A2.store_name
GROUP BY A1.region_name

This gives us a view, V_REGION_SALES, that has been defined to store sales by
region records. If we want to find out the content of this view, we type in,

SELECT * FROM V_REGION_SALES

Result:

REGION SALES
East $700
West $2050

A view is a virtual table based on the result-set of a SELECT statement.


What is a View?

In SQL, a VIEW is a virtual table based on the result-set of a SELECT statement.

A view contains rows and columns, just like a real table. The fields in a view are fields
from one or more real tables in the database. You can add SQL functions, WHERE, and
JOIN statements to a view and present the data as if the data were coming from a
single table.

Note: The database design and structure will NOT be affected by the functions, where,
or join statements in a view.

Syntax
CREATE VIEW view_name AS
SELECT column_name(s)
FROM table_name
WHERE condition

Note: The database does not store the view data! The database engine recreates the
data, using the view's SELECT statement, every time a user queries a view.

Using Views

A view could be used from inside a query, a stored procedure, or from inside another
view. By adding functions, joins, etc., to a view, it allows you to present exactly the data
you want to the user.

The sample database Northwind has some views installed by default. The view "Current
Product List" lists all active products (products that are not discontinued) from the
Products table. The view is created with the following SQL:

CREATE VIEW [Current Product List] AS


SELECT ProductID,ProductName
FROM Products
WHERE Discontinued=No

We can query the view above as follows:

SELECT * FROM [Current Product List]

Another view from the Northwind sample database selects every product in the Products
table that has a unit price that is higher than the average unit price:

CREATE VIEW [Products Above Average Price] AS


SELECT ProductName,UnitPrice
FROM Products
WHERE UnitPrice>(SELECT AVG(UnitPrice) FROM Products)

We can query the view above as follows:

SELECT * FROM [Products Above Average Price]

Another example view from the Northwind database calculates the total sale for each
category in 1997. Note that this view selects its data from another view called "Product
Sales for 1997":

CREATE VIEW [Category Sales For 1997] AS


SELECT DISTINCT CategoryName,Sum(ProductSales) AS CategorySales
FROM [Product Sales for 1997]
GROUP BY CategoryName

We can query the view above as follows:

SELECT * FROM [Category Sales For 1997]

We can also add a condition to the query. Now we want to see the total sale only for the
category "Beverages":

SELECT * FROM [Category Sales For 1997]


WHERE CategoryName='Beverages'

INDEXES

Indexes help us retrieve data from tables quicker. Let's use an example to illustrate this
point: Say we are interested in reading about how to grow peppers in a gardening book.
Instead of reading the book from the beginning until we find a section on peppers, it is
much quicker for us to go to the index section at the end of the book, locate which
pages contain information on peppers, and then go to these pages directly. Going to the
index first saves us time and is by far a more efficient method for locating the
information we need.

The same principle applies for retrieving data from a database table. Without an index,
the database system reads through the entire table (this process is called a 'table scan')
to locate the desired information. With the proper index in place, the database system
can then first go through the index to find out where to retrieve the data, and then go
to these locations directly to get the needed data. This is much faster.

Therefore, it is often desirable to create indexes on tables. An index can cover one or
more columns. The general syntax for creating an index is:

CREATE INDEX "INDEX_NAME" ON "TABLE_NAME" (COLUMN_NAME)


Let's assume that we have the following table,

TABLE Customer
(First_Name char(50),
Last_Name char(50),
Address char(50),
City char(50),
Country char(25),
Birth_Date date)

and we want to create an index on the column Last_Name, we would type in,

CREATE INDEX IDX_CUSTOMER_LAST_NAME


on CUSTOMER (Last_Name)

If we want to create an index on both City and Country, we would type in,

CREATE INDEX IDX_CUSTOMER_LOCATION


on CUSTOMER (City, Country)

There is no strict rule on how to name an index. The generally accepted method is to
place a prefix, such as "IDX_", before an index name to avoid confusion with other
database objects. It is also a good idea to provide information on which table and
column(s) the index is used on.

Please note that the exact syntax for CREATE INDEX may be different for different
databases. You should consult with your database reference manual for the precise
syntax.
CHAPTER 14 ADVANCED SQL SQL UNION,SQL
UNION ALL,SQL INTERSECT,SQL MINUS,SQL
Subquery,SQL EXISTS,SQL CASE

SQL UNION
UNION

The UNION command is used to select related information from two tables, much like
the JOIN command. However, when using the UNION command all selected columns
need to be of the same data type.

Note: With UNION, only distinct values are selected.

SQL Statement 1
UNION
SQL Statement 2

Employees_Norway:

E_ID E_Name
01 Hansen, Ola
02 Svendson, Tove
03 Svendson, Stephen
04 Pettersen, Kari

Employees_USA:

E_ID E_Name
01 Turner, Sally
02 Kent, Clark
03 Svendson, Stephen
04 Scott, Stephen
Using the UNION Command

Example

List all different employee names in Norway and USA:

SELECT E_Name FROM Employees_Norway


UNION
SELECT E_Name FROM Employees_USA

Result

E_Name
Hansen, Ola
Svendson, Tove
Svendson, Stephen
Pettersen, Kari
Turner, Sally
Kent, Clark
Scott, Stephen

Note: This command cannot be used to list all employees in Norway and USA. In the
example above we have two employees with equal names, and only one of them is
listed. The UNION command only selects distinct values.

UNION ALL

The UNION ALL command is equal to the UNION command, except that UNION ALL
selects all values.

SQL Statement 1
UNION ALL
SQL Statement 2

Using the UNION ALL Command

Example

List all employees in Norway and USA:

SELECT E_Name FROM Employees_Norway


UNION ALL
SELECT E_Name FROM Employees_USA

Result

E_Name
Hansen, Ola
Svendson, Tove
Svendson, Stephen
Pettersen, Kari
Turner, Sally
Kent, Clark
Svendson, Stephen
Scott, Stephen

SQL INTERSECT,SQL MINUS,SQL Subquery,SQL


EXISTS,SQL CASE
The MINUS operates on two SQL statements. It takes all the results from the first SQL
statement, and then subtract out the ones that are present in the second SQL statement to get
the final answer. If the second SQL statement includes results not present in the first SQL
statement, such results are ignored.

The syntax is as follows:

[SQL Statement 1]
MINUS
[SQL Statement 2]

Let's continue with the same example:

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

Table Internet_Sales

Date Sales
Jan-07-1999 $250
Jan-10-1999 $535
Jan-11-1999 $320
Jan-12-1999 $750

and we want to find out all the dates where there are store sales, but no internet sales. To do so,
we use the following SQL statement:

SELECT Date FROM Store_Information


MINUS
SELECT Date FROM Internet_Sales

Result:

Date
Jan-05-1999
Jan-08-1999

"Jan-05-1999", "Jan-07-1999", and "Jan-08-1999" are the distinct values returned from "SELECT
Date FROM Store_Information." "Jan-07-1999" is also returned from the second SQL
statement, "SELECT Date FROM Internet_Sales," so it is excluded from the final result set.

Please note that the MINUS command will only return distinct values.

Some databases may use EXCEPT instead of MINUS. Please check the documentation for your
specific database for the correct usage.

Similar to the UNION command, INTERSECT also operates on two SQL statements. The
difference is that, while UNION essentially acts as an OR operator (value is selected if it appears
in either the first or the second statement), the INTERSECT command acts as an AND operator
(value is selected only if it appears in both statements).

The syntax is as follows:

[SQL Statement 1]
INTERSECT
[SQL Statement 2]

Let's assume that we have the following two tables,

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

Table Internet_Sales
Date Sales
Jan-07-1999 $250
Jan-10-1999 $535
Jan-11-1999 $320
Jan-12-1999 $750

and we want to find out all the dates where there are both store sales and internet sales. To do
so, we use the following SQL statement:

SELECT Date FROM Store_Information


INTERSECT
SELECT Date FROM Internet_Sales

Result:

Date
Jan-07-1999

Please note that the INTERSECT command will only return distinct values.

It is possible to embed a SQL statement within another. When this is done on the WHERE or the
HAVING statements, we have a subquery construct.

The syntax is as follows:

SELECT "column_name1"
FROM "table_name1"
WHERE "column_name2" [Comparison Operator]
(SELECT "column_name3"
FROM "table_name2"
WHERE [Condition])

[Comparison Operator] could be equality operators such as =, >, <, >=, <=. It can also be a
text operator such as "LIKE". The portion in red is considered as the "inner query", while the
portion in green is considered as the "outer query".

Let's use the same example as we did to illustrate SQL joins:

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

Table Geography
region_name store_name
East Boston
East New York
West Los Angeles
West San Diego

and we want to use a subquery to find the sales of all stores in the West region. To do so, we
use the following SQL statement:

SELECT SUM(Sales) FROM Store_Information


WHERE Store_name IN
(SELECT store_name FROM Geography
WHERE region_name = 'West')

Result:

SUM(Sales)
2050

In this example, instead of joining the two tables directly and then adding up only the sales
amount for stores in the West region, we first use the subquery to find out which stores are in
the West region, and then we sum up the sales amount for these stores.

SQL EXISTS AND CASE

In the above example, the inner query is first executed, and the result is then fed into the outer
query. This type of subquery is called a simple subquery. If the inner query is dependent on
the outer query, we will have a correlated subquery. An example of a correlated subquery
is shown below:

SELECT SUM(a1.Sales) FROM Store_Information a1


WHERE a1.Store_name IN
(SELECT store_name FROM Geography a2
WHERE a2.store_name = a1.store_name)

Notice the WHERE clause in the inner query, where the condition involves a table from the outer
query.

In the previous section, we used IN to link the inner query and the outer query in a subquery
statement. IN is not the only way to do so -- one can use many operators such as >, <, or =.
EXISTS is a special operator that we will discuss in this section.

EXISTS simply tests whether the inner query returns any row. If it does, then the outer query
proceeds. If not, the outer query does not execute, and the entire SQL statement returns
nothing.

The syntax for EXISTS is:


SELECT "column_name1"
FROM "table_name1"
WHERE EXISTS
(SELECT *
FROM "table_name2"
WHERE [Condition])

Please note that instead of *, you can select one or more columns in the inner query. The effect
will be identical.

Let's use the same example tables:

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

Table Geography

region_name store_name
East Boston
East New York
West Los Angeles
West San Diego

and we issue the following SQL query:

SELECT SUM(Sales) FROM Store_Information


WHERE EXISTS
(SELECT * FROM Geography
WHERE region_name = 'West')

We'll get the following result:

SUM(Sales)
2750

At first, this may appear confusing, because the subquery includes the [region_name = 'West']
condition, yet the query summed up stores for all regions. Upon closer inspection, we find that
since the subquery returns more than 0 row, the EXISTS condition is true, and the condition
placed inside the inner query does not influence how the outer query is run.

CASE is used to provide if-then-else type of logic to SQL. Its syntax is:
SELECT CASE ("column_name")
WHEN "condition1" THEN "result1"
WHEN "condition2" THEN "result2"
...
[ELSE "resultN"]
END
FROM "table_name"

"condition" can be a static value or an expression. The ELSE clause is optional.

In our Table Store_Information example,

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

if we want to multiply the sales amount from 'Los Angeles' by 2 and the sales amount from 'San
Diego' by 1.5, we key in,

SELECT store_name, CASE store_name


WHEN 'Los Angeles' THEN Sales * 2
WHEN 'San Diego' THEN Sales * 1.5
ELSE Sales
END
"New Sales",
Date
FROM Store_Information

"New Sales" is the name given to the column with the CASE statement.

Result:

store_name New Sales Date


Los Angeles $3000 Jan-05-1999
San Diego $375 Jan-07-1999
San Francisco $300 Jan-08-1999
Boston $700 Jan-08-1999

SQL RANK
Displaying the rank associated with each row is a common request, and there is no
straightforward way to do so in SQL. To display rank in SQL, the idea is to do a self-join, list out
the results in order, and do a count on the number of records that's listed ahead of (and
including) the record of interest. Let's use an example to illustrate. Say we have the following
table,

Table Total_Sales

Name Sales
John 10
Jennifer 15
Stella 20
Sophia 40
Greg 50
Jeff 20

we would type,

SELECT a1.Name, a1.Sales, COUNT(a2.sales) Sales_Rank


FROM Total_Sales a1, Total_Sales a2
WHERE a1.Sales <= a2.Sales or (a1.Sales=a2.Sales and a1.Name = a2.Name)
GROUP BY a1.Name, a1.Sales
ORDER BY a1.Sales DESC, a1.Name DESC;

Result:

Name Sales Sales_Rank


Greg 50 1
Sophia 40 2
Stella 20 3
Jeff 20 3
Jennifer 15 5
John 10 6

Let's focus on the WHERE clause. The first part of the clause, (a1.Sales <= a2.Sales), makes
sure we are only counting the number of occurrences where the value in the Sales column is less
than or equal to itself. If there are no duplicate values in the Sales column, this portion of the
WHERE clause by itself would be sufficient to generate the correct ranking.

The second part of the clause, (a1.Sales=a2.Sales and a1.Name = a2.Name), ensures that when
there are duplicate values in the Sales column, each one would get the correct rank.

SQL MEDIAN

To get the median, we need to be able to accomplish the following:


• Sort the rows in order and find the rank for each row.
• Determine what is the "middle" rank. For example, if there are 9 rows, the middle rank
would be 5.
• Obtain the value for the middle-ranked row.

Let's use an example to illustrate. Say we have the following table,

Table Total_Sales

Name Sales
John 10
Jennifer 15
Stella 20
Sophia 40
Greg 50
Jeff 20

we would type,

SELECT Sales Median FROM


(SELECT a1.Name, a1.Sales, COUNT(a1.Sales) Rank
FROM Total_Sales a1, Total_Sales a2
WHERE a1.Sales < a2.Sales OR (a1.Sales=a2.Sales AND a1.Name <= a2.Name)
group by a1.Name, a1.Sales
order by a1.Sales desc) a3
WHERE Rank = (SELECT (COUNT(*)+1) DIV 2 FROM Total_Sales);

Result:

Median
20

You will find that Lines 2-6 are the same as how we find the rank of each row. Line 7 finds the
"middle" rank. DIV is the way to find the quotient in MySQL, the exact way to obtain the quotient
may be different with other databases. Finally, Line 1 obtains the value for the middle-ranked
row.

Displaying running totals is a common request, and there is no straightforward way to do so in


SQL. The idea for using SQL to display running totals similar to that for displaying rank: first do a
self-join, then, list out the results in order. Where as finding the rank requires doing a count on
the number of records that's listed ahead of (and including) the record of interest, finding the
running total requires summing the values for the records that's listed ahead of (and including)
the record of interest.

Let's use an example to illustrate. Say we have the following table,

Table Total_Sales
Name Sales
John 10
Jennifer 15
Stella 20
Sophia 40
Greg 50
Jeff 20

we would type,

SELECT a1.Name, a1.Sales, SUM(a2.Sales) Running_Total


FROM Total_Sales a1, Total_Sales a2
WHERE a1.Sales <= a2.sales or (a1.Sales=a2.Sales and a1.Name = a2.Name)
GROUP BY a1.Name, a1.Sales
ORDER BY a1.Sales DESC, a1.Name DESC;

Result:

Name Sales Running_Total


Greg 50 50
Sophia 40 90
Stella 20 110
Jeff 20 130
Jennifer 15 145
John 10 155

The combination of the WHERE clause and the ORDER BY clause ensure that the proper
running totals are tabulated when there are duplicate values.

To display percent to total in SQL, we want to leverage the ideas we used for rank/running total
plus subquery. Different from what we saw in the SQL Subquery section, here we want to use
the subquery as part of the SELECT. Let's use an example to illustrate. Say we have the
following table,

Table Total_Sales

Name Sales
John 10
Jennifer 15
Stella 20
Sophia 40
Greg 50
Jeff 20
we would type,

SELECT a1.Name, a1.Sales, a1.Sales/(SELECT SUM(Sales) FROM Total_Sales)


Pct_To_Total
FROM Total_Sales a1, Total_Sales a2
WHERE a1.Sales <= a2.sales or (a1.Sales=a2.Sales and a1.Name = a2.Name)
GROUP BY a1.Name, a1.Sales
ORDER BY a1.Sales DESC, a1.Name DESC;

Result:

Name Sales Pct_To_Total


Greg 50 0.3226
Sophia 40 0.2581
Stella 20 0.1290
Jeff 20 0.1290
Jennifer 15 0.0968
John 10 0.0645

The subquery "SELECT SUM(Sales) FROM Total_Sales" calculates the sum. We can then divide
the individual values by this sum to obtain the percent to total for each row.

To display cumulative percent to total in SQL, we use the same idea as we saw in the Percent
To Total section. The difference is that we want the cumulative percent to total, not the
percentage contribution of each individual row. Let's use the following example to illuatrate:

Table Total_Sales

Name Sales
John 10
Jennifer 15
Stella 20
Sophia 40
Greg 50
Jeff 20

we would type,

SELECT a1.Name, a1.Sales, SUM(a2.Sales)/(SELECT SUM(Sales) FROM Total_Sales)


Pct_To_Total
FROM Total_Sales a1, Total_Sales a2
WHERE a1.Sales <= a2.sales or (a1.Sales=a2.Sales and a1.Name = a2.Name)
GROUP BY a1.Name, a1.Sales
ORDER BY a1.Sales DESC, a1.Name DESC;

Result:
Name Sales Pct_To_Total
Greg 50 0.3226
Sophia 40 0.5806
Stella 20 0.7097
Jeff 20 0.8387
Jennifer 15 0.9355
John 10 1.0000

The subquery "SELECT SUM(Sales) FROM Total_Sales" calculates the sum. We can then divide
the running total, "SUM(a2.Sales)", by this sum to obtain the cumulative percent to total for each
row.

Vous aimerez peut-être aussi