Académique Documents
Professionnel Documents
Culture Documents
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.
TYPES:
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.
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.
Table CUSTOMER
Table ORDERS
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:
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
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.
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.
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.
Create a Database
To create a database:
Table Store_Information
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:
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
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.
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.
To select the content of columns named "LastName" and "FirstName", from the
database table called "Persons", use a SELECT statement like this:
The result
LastName FirstName
Hansen Ola
Svendson Tove
Pettersen Kari
Result
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 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 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
To select ALL values from the column named "Company" we use a SELECT statement
like this:
Company OrderNumber
Sega 3412
W3Schools 2312
Trio 4678
W3Schools 6798
Result
Company
Sega
W3Schools
Trio
W3Schools
To select only DIFFERENT values from the column named "Company" we use a SELECT
DISTINCT statement like this:
Result:
Company
Sega
W3Schools
Trio
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
To select only the persons living in the city "Sandnes", we add a WHERE clause to the
SELECT statement:
"Persons" table
Result
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
This is correct:
SELECT * FROM Persons WHERE Year>1965
This is wrong:
SELECT * FROM Persons WHERE Year>'1965'
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':
The following SQL statement will return persons with first names that end with an 'a':
The following SQL statement will return persons with first names that contain the
pattern 'la':
Syntax
UPDATE table_name
SET column_name = new_value
WHERE column_name = some_value
Person:
We want to add a first name to the person with a last name of "Rasmussen":
Result:
UPDATE Person
SET Address = 'Stien 12', City = 'Stavanger'
WHERE LastName = 'Rasmussen'
Result:
EXAMPLE2
Table Store_Information
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"
Table Store_Information
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}
The ALTER TABLE statement is used to add or drop columns in an existing table.
Note: Some database systems don't allow the dropping of a column in a database table
(DROP COLUMN column_name).
Person:
Example
Result:
Example
Result:
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.
[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
First, we want to add a column called "Gender" to this table. To do this, we key in:
Table customer
Table customer
Then, we want to change the data type for "Addr" to char(30). To do this, we key in,
Table customer
Table customer
Table Store_Information
and we decide not to keep any information on Los Angeles in this table. To accomplish
this, we type the following SQL:
Table Store_Information
EXAMPLE2
Syntax
DELETE FROM table_name
WHERE column_name = some_value
Person:
Delete a Row
Result
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:
DROP STATEMENT
Delete a Table or Database
To delete a table (the table structure, attributes, and indexes will also be deleted):
To delete a database:
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):
Family Name
Hansen Ola
Svendson Tove
Pettersen Kari
Example: Using a Table Alias
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.
Employees:
Employee_ID Name
01 Hansen, Ola
02 Svendson, Tove
03 Svendson, Stephen
04 Pettersen, Kari
Orders:
We can select data from two tables by referring to two tables, like this:
Example
Result
Name Product
Hansen, Ola Printer
Svendson, Stephen Table
Svendson, Stephen Chair
Example
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:
Syntax
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
Syntax
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
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
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
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:
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
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.
Table Store_Information
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.
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
Example
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
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
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!!
Scalar functions
Scalar functions operate against a single value, and return a single value based on the
input value.
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
For example, if we want to get the sum of all sales from the following table,
Table Store_Information
we would type in
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
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,
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:
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.
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:
LTRIM(str): Removes all white spaces from the beginning of the string.
Example 1:
Result:
'Sample'
Example 2:
Result:
'Sample '
Example 3:
Result:
' Sample'
• MySQL: CONCAT()
• Oracle: CONCAT(), ||
• SQL Server: +
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 '||'.
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
If you only want to copy a few fields, you can do so by listing them after the SELECT
statement:
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.
Example 1
To display the persons with LastName equal to "Hansen" or "Pettersen", use the
following SQL:
Result:
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
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
we key in,
SELECT *
FROM Store_Information
WHERE store_name IN ('Los Angeles', 'San Diego')
Result:
The BETWEEN ... AND operator selects a range of data between two values. These
values can be numbers, text, or dates.
Example 1
To display the persons alphabetically between (and including) "Hansen" and exclusive
"Pettersen", use the following SQL:
Result:
Example 2
To display the persons outside the range used in the previous example, use the NOT
operator:
Result:
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
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:
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}
• '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.
Table Store_Information
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:
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.
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
For example, we may wish to list the contents of Table Store_Information by dollar
amount, in descending order:
Table Store_Information
we key in,
Result:
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:
Orders:
Company OrderNumber
Sega 3412
ABC Shop 5678
W3Schools 2312
W3Schools 6798
Example
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:
Result:
Company OrderNumber
ABC Shop 5678
Sega 3412
W3Schools 2312
W3Schools 6798
Example
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:
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,
Table Store_Information
we would type,
Result:
store_name SUM(Sales)
Los Angeles $1800
7>SQL WHERE
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
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
To select only the persons living in the city "Sandnes", we add a WHERE clause to the
SELECT statement:
"Persons" table
Result
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.
This is correct:
SELECT * FROM Persons WHERE FirstName='Tove'
This is wrong:
SELECT * FROM Persons WHERE FirstName=Tove
This is correct:
SELECT * FROM Persons WHERE Year>1965
This is wrong:
SELECT * FROM Persons WHERE Year>'1965'
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':
The following SQL statement will return persons with first names that end with an 'a':
The following SQL statement will return persons with first names that contain the
pattern '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
we key in,
SELECT store_name
FROM Store_Information
WHERE Sales > 1000
Result:
store_name
Los Angeles
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.
Example
Use AND to display each person with the first name equal to "Tove", and the last name
equal to "Svendson":
Result:
Example
Use OR to display each person with the first name equal to "Tove", or the last name
equal to "Svendson":
Result:
Example
You can also combine AND and OR (use parentheses to form complex expressions):
Result:
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.
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
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
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.
GROUP BY Example
Company Amount
W3Schools 5500
IBM 4500
W3Schools 7100
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:
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.
Company Amount
W3Schools 5500
IBM 4500
W3Schools 7100
This SQL:
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.
- 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.
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.
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.
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.
"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,
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
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:
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,
Result:
REGION SALES
East $700
West $2050
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:
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:
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":
We can also add a condition to the query. Now we want to see the total sale only for the
category "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:
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,
If we want to create an index on both City and Country, we would type in,
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.
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
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
Example
Result
E_Name
Hansen, Ola
Svendson, Tove
Svendson, Stephen
Pettersen, Kari
Turner, Sally
Kent, Clark
Svendson, Stephen
Scott, Stephen
[SQL Statement 1]
MINUS
[SQL Statement 2]
Table Store_Information
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:
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).
[SQL Statement 1]
INTERSECT
[SQL Statement 2]
Table Store_Information
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:
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.
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".
Table Store_Information
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:
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.
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:
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.
Please note that instead of *, you can select one or more columns in the inner query. The effect
will be identical.
Table Store_Information
Table Geography
region_name store_name
East Boston
East New York
West Los Angeles
West San Diego
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"
Table Store_Information
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,
"New Sales" is the name given to the column with the CASE statement.
Result:
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,
Result:
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
Table Total_Sales
Name Sales
John 10
Jennifer 15
Stella 20
Sophia 40
Greg 50
Jeff 20
we would type,
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.
Table Total_Sales
Name Sales
John 10
Jennifer 15
Stella 20
Sophia 40
Greg 50
Jeff 20
we would type,
Result:
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,
Result:
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,
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.