You are on page 1of 27

SQL QURIES

SQL - Data Types


SQL recognizes 4 general types of data. As the database designer you will be selecting which type of data
that can be placed in each table column. Before we look at each type of table column we will elaborate on specific
data types and how they are handled in SQL.

• Character Strings - ('Words or numbers')


• Numbers - (3, 3.423, -17)
• Booleans - (True / False)
• Nulls - (empty fields)

SQL - NULL Values


A null value may be the most foreign to new programmers. Stating that a value has a null value indicates that
nothing exists in that table field. When the table is created you may either allow a table to have a null value or may
disallow null values for each table column.

SQL Code:
CREATE TABLE weekly_payroll
(employee_id VARCHAR(10) PRIMARY KEY,
total_hours INT NULL,
hourly_rate MONEY NOT NULL,);

SQL - Numeric Data


Dates, time stamps, integers, and money are all numeric data types. The advantage of working with numbers
is that SQL has built in functions such as the AVG() or SUM() functions that will return the average or the sum of a
numeric column.

Numbers:
rate_of_pay
27
26.66
28.40

SQL - Boolean Data


Boolean values are either yes/no (true/false) types of data. Others use a 1/0 (1 for yes 0 for no) approach.
Either something is or something is not.

Boolean Values:
admin
1
1
0

SQL - Character Strings

Character strings are sentences, symbols, or a combination of both. Math functions can not be performed with
character strings.

Character Strings:
employee_id
TS_0036
TS_0078
CL_1099

Tips

• Dates and times should always be set to "NOT NULL" since time always exists.
• Character Strings are sometimes referred to as varchar(s).

SQL - Commands

SQL commands can be categorized into three distinct groups. Each type of command plays an essential role
while working with your database. One analogy might be to think of each SQL Command as a possible tool in your
tool shed. Certain duties require specific tools and the more tools you have in your shed, the greater the chances
that you will have the exact tool you need for the appropriate job.

SQL - What is a Clause

Clauses were mentioned briefly in the SQL Queries lesson. To briefly recap, they are the commands issued
during a query. SELECT, INSERT, ADD, DROP, CREATE, etc are all clauses that begin each SQL Query and
execute some sort of action upon your database.

SQL - What is a Function

There are a number of functions built into SQL that can add column values for you, average column values, or
even change lowercase to uppercase strings. These functions are used directly inside of your queries and are
excellent tools for you to use.

SQL Code:
Count() function

SELECT COUNT(*) FROM table_one;

The query above will return a numeric value representing the number of rows that have been inserted into
your database. We will be covering this function as well as many others as this tutorial continues.

SQL - What is an Operator


Operators are a means by which SQL can manipulate numbers and strings or test for equality. They come in
four flavors including: Arithmatic, Range, Equality, and Logical. As your skills with SQL grow, you may want the
language to start performing some basic arithmatic for you or perhaps you wish to select a range of rows with a
numeric column value larger than 5. This becomes possible with operators.

SQL Code:
SELECT * FROM table_one WHERE column_one > 5;

Operators are used in expressions or conditional statements. they show equality, inequality, or a combination
of both. Mathematical operators are found in every computer language and may be familiar to you. SQL operators
follow the same rules you may have learned in a math class or know from previous programming experience.

Operators come in three flavors, mathematical, logical, or range operations. Mathematical operators add,
subtract, multiply, divide, and compare equality of numbers and strings. There are two logical operators, AND / OR.
Range operators include the infamous < and > symbols used to compare equality. Take note of the following tables
for future reference.

SQL Arithmetic Operators:


Operator Example Result Definition
+ 7+7 = 14 Addition
- 7-7 =0 Subtraction
* 7*7 = 49 Multiplication
/ 7/7 =1 Division
% 7%7 =0 Modulus

Modulus may be the only unfamiliar term on the chart. this term describes the result when one number is
divided by another number resulting in a remainder. For example 4 % 3 would result with a value of 1 since 1 is left
over after 4 is divided by 3.

SQL Range Operators:


Operator Example Defined Result
< 7<4 7 less than 4? False
> 7>4 greater than 4? True
<= 7 <= 11 Is 7 less than or equal to 11? True
>= 7 >= 11 Is 7 greater than or equal to 11? False

SQL Equality Operators:


Operator Example Defined Result
= 5=5 Is 5 equal to 5? True
<> 7 <> 2 Is 7 not equal to 2? True

SQL Logical Operators:


Operator Defined Example
AND Associates two values using AND if (($x AND $y) == 5)...
OR Associates two values using OR if (($x OR $y) == 5)...

SQL - Expressions
In the programming world an expression is a special statement that returns a value. SQL is no exception to
this standard rule.

SQL - Expression Types

Expressions in SQL generally fall into one of four categories including: Boolean, Numeric, Character, and/or
Date Expressions.

SQL Code:
SELECT column_one FROM table_name;

The simplest form of an expression appears as column_one of our table. Select is our clause telling our
database what we want to do, and column_one acts as the defined expression returning each row of that particular
column.

Expressions after the where clause might appear more familiar to programmers.

SQL Code:
SELECT * FROM table_name WHERE column_one = 'some value';

The latter example returns rows of the specified column containing 'some value'. Using expressions like the
one above gives you precise control over what results will be returned. More information on the where clause is
available at SQL Where.

SQL - Boolean Expressions

A boolean expression in any programming language returns a true/false result. Returning to the previous
example containing the where clause.

SQL Code:
SELECT * FROM table_name WHERE column_one = 'some value';

The logic behind this query is that each row is being tested for 'some value' to appear in our column_one.
Each time a match is found (testing true), that row is selected and returned for our viewing pleasure.

SQL - Numeric Expression


A numeric expression simply returns a numeric value. There are some built in functions that we will be
examining in greater detail later on. Using one of the following functions is perhaps the easiest way to demonstrate
the return of a number:

• AVG() -- Returns the average of a stated column.


• COUNT() -- Returns a count of the number of rows of a given column.
• SUM() -- Returns the sum of a given column.

SQL Code:
SELECT COUNT(*) FROM table_name;

Our expression above returns a numeric value representing the number of rows that have been inserted into
your table thus far. Please be aware that the AVG(), COUNT(), and SUM() only return results for integer table
columns. Using one of these functions with a varchar column will result in an error message.

SQL - Character Expressions


Character expressions are used to test for values of a string. Often these expressions will appear in a where
clause as follows.

SQL Code:
SELECT * FROM table_name WHERE
column_one LIKE '%string';

Here we have used the percent(%) symbol to signify the start of our string. SQL tests our expression against
column_one and returns all the rows and columns where column_one contains our string.

This might come across easier if we use a live example. Say we have created a table with employee
information. In this table we have set up a column a last_name column. The query above will come in handy if we
were wanting to pull all the employees with a last_name that begins with a "T". Now if we plug in our hypothetical
situation into our code, we should have something like the following.

SQL Code:
SELECT * FROM employees WHERE
last_name LIKE '%T';

Keep in mind that SQL is case sensitive, using a lowercase t would not yield results for a last_name that has
been capitalized.

SQL - Date Expressions


Date expressions come in three flavors. These expressions are very straight forward, simply type in any of
those listed below and SQL will return exactly what you have requested.

• Current_Date -- Returns the current date.


• Current_Time -- Returns the current time.
• Current_Timestamp -- Returns the current timestamp.

These expressions can also be placed into your tables as column values for any given row with an insert
statement. We will be looking more indepth at the insert clause, however here is a glimpse of what is to come.

SQL Code:
INSERT INTO table_name(column_one,column_two,)
Values(Current_Date,Current_Timestamp);

This statement inserts a new row into our imaginary table with the current date value for column one and the
current timestamp value for columne_two.

SQL - Create
A database is nothing more than an empty shell, like a vacant warehouse. It offers no real functionality what
so ever, other than holding a name. Tables are the next tier of our tree offering a wide scope of functionality. If you
follow our warehouse example, a SQL table would be the physical shelving inside our vacant warehouse.
Depending on the situation, your goods may require reorganization, reshelving, or removal. SQL tables can be
manipulated in this same way or in any fashion the situation calls for.

SQL - Create Database


We first need to build our warehouse, or create our database. The create clause is straight forward. Here we
have a one line script using the create clause to create a database we named business. The exact number of
databases a SQL program can handles is entirely up to the manufacturer, visit your sites manufacturer if you would
like to have specifics.

SQL Code:
CREATE DATABASE business;

We will be using this database for the remainder of the tutorial.

SQL - Create Table

Our emtpy shell of a database will do nothing standing alone, next we create our tables to store and organize
our data. We use the same create clause and follow the same syntax above. Tables, however, are fairly complex.
In our script we must also include parameters for each table column as well as name each one. Naming your table
columns can be as simple or complicated as desired. SQL programs are case sensitive, keep this in mind as you
will be calling on our table columns by name a great deal as you enhance your SQL knowledge. Also, most
programs do not support spaces in column names, you must use the underscore (_).

Column types specify what type of data can be placed inside a table column ranging from numbers,
paragraphs, or brief strings. For example, setting a column type to an int value means your database will ONLY
accept an interger value for this column type.

Column Types:
Column Type Description Syntax
int Accepts integer values only tinyint, int
varchar Accepts any symbol, character, or number varchar(char limit value)
text/blob Accepts paragraph style data including line returns and page breaks text, blob

Int, varchar, and text are the 3 most common types of columns. Text and int columns have 3 flavors tiny,
medium, and large. Every SQL program has its unique sizes, but for this tutorial we will be using medium sized
column fields for each exercise. Later on as your database exapands it becomes extremely important to not
overdue the size of your column fields. Using the correct size field will dramatically increase performance including
query speeds.

Now create the table.

SQL Code:
CREATE TABLE employees
(
id INT(3) NOT NULL AUTO_INCREMENT,
Lastname VARCHAR(50),
Firstname VARCHAR(25),
Title VARCHAR(10) DEFAULT 'crew' NULL
);

Above is our table, the first column simply numbers each row that will be added to the table up to a maximum
of 3 digits wide (999) automatically(auto_increment). Our second line is a varchar meaning it will hold numbers,
digits, or symbols, which is perfect for short names of people. The last column has a specified default value of
"crew" so that if we add a new crew member we can simply place a default value.

We do not always have to specify a default value or state weather each column may have a NULL value, By
default, most table columns will allow null values to be recorded but certain table columns will not allow a null value
to be inserted. We recommend reading up on each column type available to you from your database manufacturer.

SQL - Primary Key


A primary key is a property given to a table column that distinguishes that record apart from each. For each
record in the table the primary key acts like a driver's license number, only one number exists for each person. The
same principle applys here. Any table can only be given one auto increment field and as such it is forced to be the
primary key of the table. Therefore in our following examples we use the 'id' field as our primary key.

The alter clause is used to add or drop primary keys and indexes. To change a primary key you must drop the
first one, then add your desired primary as shown below. More about the alter clause later.

SQL Code:
ALTER TABLE 'employees' DROP PRIMARY KEY,
ADD PRIMARY KEY ('id');

SQL - Indexes
SQL automatically creates some indexes based on column types and attributes. An index can also be given to
a table column to optimize speeds. When a query is executed searching for a specific column value, SQL will start
at the top of the table and search each and every record until it finds matches. This becomes a performance issue
when a table holds a vast amount of records. By adding an index to columns, SQL will no longer search the entire
table, it will pinpoint your index columns and search those first. The downside to indexing is that it enlarges the
disk space consumed by a table on your webserver. Use indexes when you notice a drop in your query speeds.

SQL Code:
ALTER TABLE 'employees' ADD INDEX ('id');
or
ALTER TABLE `employees` DROP INDEX `Lastname`;

The insert clause has one function; to insert data into a table. Insert populates each table column with a value.
Rows are inserted one right after another into the coresponding column.

SQL Code:
INSERT INTO employees (Lastname,Firstname,Title)
VALUES(Johnson,David,crew);

Above we have a single line example of the insert syntax. Since our 'id' column is set up to auto increment, we
can omit this field from our script. Our SQL program automatically begins counting starting with one, when the auto
increment attribute is added to an interger field.

Display:
id Lastname Firstname Title
1 Johnson David crew

SQL - Insert defaults and nulls

We mentioned setting up default or null values for table columns. Simply placing the word default or null, in
place of a value is the solution.

SQL Code:
INSERT INTO employees (Lastname,Firstname,Title)
VALUES('Hively','Jessica',DEFAULT);
or
INSERT INTO employees (Lastname,Firstname,Title)
VALUES('Hively','Jessica',NULL);

SQL - Inserting multilpe values


Here's an example of how to insert more than one record at a time. Many web developers will use the single
example above along with HTML forms to continually insert and update their SQL tables.

SQL Code:
INSERT INTO employees VALUES
(DEFAULT,'Hicks','Freddy','crew'),
(DEFAULT,'Harris','Joel','crew'),
(DEFAULT,'Davis','Julie','manager');
We use a default value for the id field so that it will continue to auto increment for each new record. Using this
method you must have some value for each table column.

SQL - Insert into multiple tables

This concept isn't widely supported by open source database programs, however they do offer alternative
methods to achieve the same goal. The idea is to insert similar record values into 2 or more tables with one
statement. Using the example from above, we want to place Julie's information into our manager table as well as
the general employee table.

SQL Code:
INSERT ALL INTO employees (Lastname,Firstname,Title)
VALUES('Davis','Julie','manager')
INTO manager (training,salary)
VALUES ('yadayada','22500');

SQL - Select

In SQL, queries begin with the select clause. A typical query statement needs only two parts, we select what
from where. The what will represent columns of your table you wish to select and the where represents the table
name of your data table.

To avoid typing a list of every single column in your table, you can use the astric (*) to select all table columns
from a table. However, query speeds are very important and always selecting every table column can dramatically
decrease your SQL performance. As good practice, it is best to only select the table columns that you wish to use
or display.

SQL Code:
SELECT * FROM employees;

SQL Table:
ID Lastname Firstname Title
1 Johnson David crew
2 Hively Jessica crew
9 Hicks Freddy crew
10 Harris Joel crew
11 Davis Julie manager

Our result should print every table column and every bit of data stored in each row thus far.

SQL Code:
SELECT Lastname,Firstname FROM employees;

SQL Table:
Lastname Firstname
Johnson David
Hively Jessica
Hicks Freddy
Harris Joel
Davis Julie

SQL - Select Multiple Tables

Multiple columns from different tables can also be selected. The syntax is relatively the same, the difference
being when you choose that table column you wish to select, you must now name the table of the desired table
column.

The period is necessary to differentiate between the table name and the column name, other than that all the
same query rules apply.

SQL Code:
SELECT employees.Fisrtname, employees.Lastname, crew.experience FROM
employees, crew;

SQL - Select Functions

We havn't mentioned anything about SQL Functions yet and we will eventually cover them in greater detail, for
the moment we would like to introduce this concept of using a selection query with a function. For instance we can
query our database program for the current_timestamp or retrieve the number of rows in our table using the
COUNT() function.

SQL Code:
SELECT CURRENT_TIMESTAMP;

This will return a server timestamp representing the date and time of when the query was executed, more on
this toward the end of the tutorial.

SQL Code:
SELECT COUNT(Lastname) FROM employees;

We will dive deeper into detail as the tutorial progresses, but be aware of this concept of selecting date by
means of calling a function as it will prove more useful later on.

SQL - Order By

The order by statement allows for table column assortment. It allows for ascending or descending lists of your
table column values permitting SQL to reorder your table rows for the purpose of viewing.
SQL Code:
SELECT * FROM employees ORDER BY Lastname;

SQL Table:
ID Lastname Firstname Title
11 Davis Julie manager
10 Harris Joel crew
9 Hicks Freddy crew
2 Hively Jessica crew
1 Johnson David crew

The above example resorts our rows of data alphabetically by lastname. Here is another example ordering by
two different columns First we alphabatize our job titles and again we order by lastname.

SQL Code:
SELECT * FROM employees ORDER BY Title,Lastname;

SQL Table:
ID Lastname Firstname Title
10 Harris Joel crew
9 Hicks Freddy crew
2 Hively Jessica crew
1 Johnson David crew
11 Davis Julie manager

Hint: It is possible to order by a column you do not wish to display such as an ID field. Also notice at the
bottom of each query display will be the number of rows affected by your query. This number will also be displayed
anytime an insert, select, update, or delete statement is properly executed by your SQL program.

Distinct is used to retrieve rows of your database that have unique values for a given column. For example say
we have a table of employees and in this table of employees we have several job titles from janitors to CEOs. We
would like to know just how many distinct job titles we have.

SQL Code:
SELECT DISTINCT job_titles FROM employees;

Our statement will return the number of rows with a distinct value for our column (job_titles).

SQL - Distinct Multiple Columns


Using the distinct function with more than one column yields some substantial results. SQL will return the rows
with distinct or unique combinations of those columns. Assume this same employee table has another column
including the salary of each employee. With the use of the distinct function we can pull a list of unique job titles -
salaries.
SQL Code:
SELECT DISTINCT job_titles, salary FROM employees;

SQL has returned all the rows with unique combinations of job titles and salaries. Any duplicate combinations
of job titles and salaries will be ignored. For example if we had two CEOs in the table making the same salary each
year, only one row would be returned but if we had two CEOs with different salaries, both rows would be returned.

SQL - Where
The where clause sets a conditional statement for your queries. Your server will query the database searching
for rows that meet your specific where condition. A typical query will have the following syntax.

A conditional statement has 2 parts, the left side is your table column and the right side is the condition to be
met. Example:WHERE table_column(s) = condition.

SQL Code:
SELECT * FROM employees WHERE Title = 'crew' ORDER BY Lastname;

SQL Table:
ID Lastname Firstname Title
10 Harris Joel crew
9 Hicks Freddy crew
2 Hively Jessica crew
1 Johnson David crew

All the known typical operators can be used in the conditional statements, for a complete list backtrack to SQL
Operators

SQL - Where multiple conditionals

SQL also supports multiple conditional statements within one script by using the AND or OR operators.
Continuing the example from above we can select only those employees that worked 36 or more hours for the
week, perhaps meaning they are full time employees.

SQL Code:
SELECT employees.Lastname,
employees.Firstname,
employees.Title,
payroll.Hours
FROM employees,payroll
WHERE employees.Lastname = payroll.Lastname
AND payroll.Hours => '36'
ORDER BY employees.Lastname;

As you can see, the where clause is a very powerful tool used to quickly pull rows from one table or many.

SQL - In
In is a special kind of operator for use in your where clauses. Recall that in a where expression only one value
is allowed to be sent through the query. With the in operator one can send multiple values in the where clause.

SQL Code:
SELECT * FROM table_name
WHERE some_column IN('value1','value2','value3');

The rows returned will have one of the values listed (value1, value2, or value3) in brackets as the value for the
specified column (column_one).

SQL - In Subqueries

To better understand the power of this function, let's use an example involving a subquery. Say we have a
database with the following tables.

employees logbook
user_id last first user_id timestamp
0045 Davis Julie 0045 000000002345
0067 Smith John 0045 000000045666
0098 Hodgensen Bruce 0098 000000076444

Now above we have an employees table giving us a user_id along with a first and last name of employees we
have working in the office. The second table might be a log type table that keeps track of who accessed the
database and at what time. This a security check. A standard operating procedure if many people will be accessing
the database.

What we want to know is the last name and first name of those that accessed the database. To do this we may
use a subquery like in the example below.

SQL Code:
SELECT first,last FROM employees WHERE user_id IN
(Select user_id FROM logbook);

The subquery highlighted in red selects all the values of user_id in the logbook and returns those to the
previous in statement. The result is a complete listing of the names of the employees that have accessed the
database and have been recorded inside the logbook table. The good news is that this feat was accomplished
without having to know the user_ids of every employee in the office. This definatly adds some depth to your SQL
knowledge thus far.

SQL - Between

Between is a special operator appearing in a where statement. It allows for the selection of a range of values
between one value and another.

SQL Code:
SELECT * FROM table_name WHERE column_one
BETWEEN value1 AND value2;
Be aware that the values you specify will also return results. For example if we were looking for values
between 5 and 10, all rows would be retrieved where our column value is 5,6,7,8,9, or 10.

SQL - In Between (Subqueries)

This next example requires that you are familiar with the previous lesson, SQL In. You may recall that the In
operator allows the where clause to return more than one single value for a column. Below are two tables. The
employee table only stores personal information about employees while the logbook records when and who
accessed our database.

employees logbook
user_id last first user_id timestamp
0045 Davis Julie 0045 000000002345
0048 Thomas David 0045 000000045666
0067 Smith John 0048 000000055767
0098 Hodgensen Bruce 0098 000000076444

Say we were thinking ahead when issuing user_ids and coordinated each user_id number with a department.
For example our data security department employees will be numbered 004X, where X represents any number 0-9
(So data security employees are numbered 0040-0049). Since we konw this information it will prove useful if we
want to track down database users by department.

SQL Code:
SELECT * FROM employees WHERE user_id IN
(SELECT user_id FROM logbook WHERE user_id BETWEEN 0040 AND 0049);

SQL will now retrieve exactly what we need. A listing of all the data security employees (user_id 004X) that
have accessed the database thus far.

The alter clause changes a table by adding, removing, or modifying an existing table column.

The following example adds a new column to our table.

SQL Code:
ALTER TABLE employees
ADD dob varchar(10);

SQL Table:
id Lastname Firstname Title dob
1 Johnson David crew

Now we will remove the same column from the table.

SQL Code:
ALTER TABLE employees
DROP dob;
id Lastname Firstname Title
1 Johnson David crew

SQL - Alter Columns

Alter is used again when changing attributes of the table cloumns. For instance if we wanted to change a table
column name or change a column from a varchar to an interger type.

SQL Code:
ALTER TABLE 'employees' CHANGE 'ID' 'id' TINYINT ( 3 )
NOT NULL AUTO_INCREMENT;

Above we changed 'ID' to 'id' because SQL is case sensative and this will remove a potential bug issue. After
we have selected a new name, we list the new attributes to be given to the altered column.

The update clause updates column values of a table. Update requires a conditional statement to select the
row to be updated.

SQL Code:
UPDATE employees
SET Lastname = 'Fletcher', Firstname = 'Ron'
WHERE id = '11';

SQL Table:
ID Lastname Firstname Title
11 Fletcher Ron manager

Here we changed record 11, our manager, to a new manager.

SQL - Updating Multiple Rows


With the use of expressions and operators, update allows for the manipulation of several rows at once.

SQL Code:
UPDATE employees
SET Title = UPPER(Title);

SQL Table:
ID Lastname Firstname Title
1 Johnson David CREW
2 Hively Jessica CREW
9 Hicks Freddy CREW
10 Harris Joel CREW
11 Davis Julie MANAGER
Using the UPPER expression we changed our Title field to all capital letters. Update also supports the use of
subqueries.

SQL - Delete

Entire rows can be deleted from a table using the delte clause.

SQL Code:
DELETE FROM employees
WHERE id='4';

We could use the above code to delete an employee using their unique employee number that has been
assigned by the auto_increment field.

SQL - Deleting Multiple Rows

Delete is not limited to one row at a time. We may use all of our known SQL operators as well as subqueries
to delete any or all rows that apply to our conditional.

SQL Code:
DELETE FROM employees
WHERE id <= '999';

The above example would delete every row from our table since we had previously limited our "id" field when
we set up the table. Predicates, expressions, subqueries, and operators can be used to delete any rows you would
like to remove from your table.

SQL - Truncate

We can clear an entire table using a truncate statement. Truncate quickly clears all rows of a table without
deleting the table itself. If you are following along, we don't recommend that you run this script because you will
essentially undo all of your inserts and have only empty table field to show for it, but it is a handy statement to
know and use when the time is right.

SQL Code:
TRUNCATE TABLE employees;

SQL - Predicates
Predicates follow the where clause. Predicates allow the searching through database records to recover
specific strings and ranges or characters. Rows will be returned if they match the predicate condition.

SQL - Like; Not Like


Like is a means of quering data in your database by keyword or keyletter means. After a where clause, use
LIKE to match a character or a string. SQL retrieves all rows containing all or part of the string placed within the
parameters. Generally, a percent sign (%) is used to define the start and ending or your string. Escape characters
can be used for special situations.

SQL Code:
SELECT * FROM employees WHERE Lastname LIKE '%H%';

SQL Table:
ID Lastname Firstname Title
10 Harris Joel crew
9 Hicks Freddy crew
2 Hively Jessica crew

Our example will retrieve any and all rows with a capital letter H in the lastname field. Case sensitivity is
important in this situation.

On a contrary note, use the Not Like predicate to find all rows that do not match the string.

SQL Code:
SELECT * FROM employees WHERE Lastname NOT LIKE '%H%';

SQL Table:
ID Lastname Firstname Title
1 Johnson David crew
11 Davis Julie manager

SQL - Predicates Escaping Characters

Say you want to find a percent character(%) in your database, or all rows associated with one. SQL Server
and Oracle, require an additional statement over MySQL. With Oracle and SQL Server, you must specify the
character used to escape. Here's code example.

SQL Code:

Oracle/SQL Server

SELECT * FROM employees WHERE Lastname LIKE '%\%%' ESCAPE '\';

MySQL

SELECT * FROM employees WHERE Lastname LIKE '%\%%'

MySQL has a built in default escape character the backslash (\). MySQL also supports the escape predicate
allowing you to change your escape character exactly as it is done in the other programs.
SQL - Between

Between is a predicate to call a range of numeric values such as 1-20 or the like. Its syntax follows the same
as above.

SQL Code:
SELECT * FROM employees WHERE id BETWEEN 1 AND 4;

SQL Table:
ID Lastname Firstname Title
1 Johnson David crew
2 Hively Jessica crew

Between is essentially replacing your range operators. Always start your ranges with the lowest number first.

SQL - Limit
The limit predicate allows you to limit the number of rows selected.

SQL Code:
SELECT * FROM employees LIMIT 2;

SQL Table:
ID Lastname Firstname Title
1 Johnson David crew
2 Hively Jessica crew

SQL - Inner Join

The join clause combines columns of one table to that of another to create a single table. Join matches up a
column with one table to a column in another table. A join query does not alter either table, but temporarily
combines data from each table to be viewed as a single table. There are three types of join statements, inner, left,
and right.

We will be using our employees table from previous examples, and a new table to track sales of each
employee called invoices.

Our invoices table is set up with 3 fields, EmployeeID, Sale, and Price. If we were a business owner we now
have a means to track what was sold, and by whom and we can bring this information together using an inner join
clause.

Inner Join: An inner join returns all rows that result in a match such as the example above.

SQL Code:
SELECT employees.Lastname, employees.Firstname, invoices.Sale, invoices.Price
FROM employees
INNER JOIN invoices
ON employees.id = invoices.EmployeeID

SQL Table:
Lastname Firstname Sale Price
Johnson David HOT DOG 1.99
Hively Jessica LG SFT DRK 1.49
Davis Julie CD SLD 3.99
Davis Julie CD SLD 3.99

We haven't changed or updated any information in either of our tables but we were able to fashion together a
new table using a conditional that matches one table column to another.

SQL - Left Join

A Left join returns all rows of the left of the conditional even if there is no right column to match.

SQL Code:
SELECT employees.Lastname, employees.Firstname, invoices.Sale, invoices.Price
FROM employees
LEFT JOIN invoices
ON employees.id = invoices.EmployeeID

SQL Table:
Lastname Firstname Sale Price
Johnson David HOT DOG 1.99
Hively Jessica LG SFT DRK 1.49
Hicks Freddy
Harris Joel
Davis Julie CD SLD 3.99
Davis Julie CD SLD 3.99

This would be a great way to track sales per person per day if the invoice table had a date field as well.

SQL - Right Join

A right join will display rows on the right side of the conditional that may or may not have a match.

SQL Code:
SELECT employees.Lastname, employees.Firstname, invoices.Sale, invoices.Price
FROM employees
RIGHT JOIN invoices
ON employees.id = invoices.EmployeeID

SQL Table:
Lastname Firstname Sale Price
Davis Julie CD SLD 3.99
Davis Julie CD SLD 3.99
Johnson David HOT DOG 1.99
HOT DOG 1.99
Hively Jessica LG SFT DRK 1.49
LG SFT DRK 1.49

This would happen generally if perhaps nobody recieved credit for the sale or the sale was credited to the
store by default.

SQL - Union

The union clause places two serarate queries together forming one table. A union works best when using two
tables with similar columns because each cloumn must have the same data type.

Say we had another table called employees2 with the names and information of employees from our second
store. With 2 queries, we can combine the tables into a list of all employees.

SQL Code:
SELECT * FROM employees
UNION
SELECT * FROM employees2;

SQL Table:
ID Lastname Firstname Title
1 Johnson David crew
2 Hively Jessica crew
9 Hicks Freddy crew
10 Harris Joel crew
11 Davis Julie manager
101 Yazzow Jim crew
102 Anderson Craig crew
103 Carlson Kevin crew
104 Maines Brad crew

The result is a complete listing every employee from store 1 and 2.

The next example shows a more practical means of using a union clause. Here we will select all of our
employees from both tables, and join them with our invoices table to generate a complete list of sales from both
stores on a given day.

SQL Code:
SELECT employees.Lastname, employees.Firstname, invoices.Sale, invoices.Price
FROM employees
INNER JOIN invoices
ON employees.id = invoices.EmployeeID
UNION
SELECT employees2.Lastname, employees2.Firstname, invoices.Sale, invoices.Price
FROM employees2
INNER JOIN invoices
ON employees2.id = invoices.EmployeeID;

SQL Table:
Lastname Firstname Sale Price
Johnson David HOT DOG 1.99
Hively Jessica LG SFT DRK 1.49
Davis Julie CK SLD 3.99
Yazzow Jim HOT DOG 1.99
Carlson Kevin LG SFT DRK 1.49

Here we combined a join query with the union clause to create one table.

SQL - Union All

Union all selects all rows from each table and combines them into a single table. The difference between
Union and Union all is that Union all will not eliminate duplicate rows, instead it just pulls all rows from all tables
fitting your query specifics and combines them into a table.

SQL Code:
SELECT * FROM employees
UNION ALL
SELECT * FROM employees2;

SQL Table:
ID Lastname Firstname Title
1 Johnson David crew
2 Hively Jessica crew
9 Hicks Freddy crew
10 Harris Joel crew
11 Davis Julie manager
101 Yazzow Jim crew
102 Anderson Craig crew
103 Carlson Kevin crew
11 Davis Julie manager
104 Maines Brad crew

SQL Code:
SELECT employees.Lastname, employees.Firstname, invoices.Sale, invoices.Price
FROM employees
INNER JOIN invoices
ON employees.id = invoices.EmployeeID
UNION ALL
SELECT employees2.Lastname, employees2.Firstname, invoices.Sale, invoices.Price
FROM employees2
INNER JOIN invoices
ON employees2.id = invoices.EmployeeID;

SQL Table:
Lastname Firstname Sale Price
Johnson David HOT DOG 1.99
Hively Jessica LG SFT DRK 1.49
Davis Julie CK SLD 3.99
11 Davis Julie manager
Yazzow Jim HOT DOG 1.99
Carlson Kevin LG SFT DRK 1.49
11 Davis Julie manager
11 Davis Julie manager

SQL - Subqueries
MySQL offers a very limited support for subqueries, however Oracle and DB2 fully support them. Subqueries
are Select queries placed within an existing SQL statement. They may exist in any of the following types of SQL
statements.

• Select
• Insert
• Update
• Delete
• Set
• Do

Subqueries are great for answering very specific questions regarding your data inside your database. For
instance, as the employer you may notice employee number 101 had a great day yesterday with sales. Just given
this information we can use a subquery to pull the employee lastname and first name from our database.

SQL Code:
SELECT * FROM employees
WHERE id =
(SELECT EmployeeID FROM invoices WHERE EmployeeID='1');

SQL Table:
id Lastname Firstname Title
11 Davis Julie MANAGER

Here we have pulled our employee information from the employees table by only knowing the employee
number from the invoices table.

SQL - Subquery Inserts


Subqueries can be used to pull old data from your database and insert it into new tables. For instance if we
opened up a third store and we wanted to place the same manager over 3 stores we could do this by pulling the
manager's information using a subquery and then inserting the records. Also note that this form of insert will insert
all cases where the subquery is true, therefore several rows may or may not be inserted depending upon how your
table is set up.

SQL Code:
INSERT INTO employees3
(id,Lastname,Firstname,Title)
(SELECT id,Lastname,Firstname,Title
FROM employees WHERE Title='manager');

With complete mastery of a subqueries you can now see the power of the SQL language. The language is
capable of nearly all things imaginable.

SQL - Dates

Unfortunately, every SQL platform has its own version of date functions, the few listed work in DB2, Oracle,
and MySQL. Microsoft SQL Server users should skip to our SQL Datepart lesson.

SQL - Timestamp

A timestamp servers as the catch all for dates and times. Retrieving a timestamp is very simple and the result
can be converted or manipulated in nearly every way imaginable.

SQL Code:
SELECT CURRENT_TIMESTAMP;

Return a Timestamp:
2004-06-22 10:33:11.840

Keep in mind that each platform of SQL (DB2, Oracle, SQL Server, etc...) may return dates and times which
are formatted differently.

SQL - Date Functions

As we just mentioned, it is possible to breakdown timestamps into their individual pieces using any of the
following date functions.

SQL Code:
SELECT MONTH(CURRENT_TIMESTAMP);

Return a Month:
6

SQL Code:
SELECT DAY(CURRENT_TIMESTAMP);

Return a Day:
22

There are many more functions available, including functions to extract milliseconds, names of the months,
names of each week day, etc. Each SQL platform varies in the actual naming of date functions. Here's a few c\The
following is a list of other date functions available to most platforms of SQL with the exception of MS's SQL Server.

SQL Function Code:


SELECT DATE(CURRENT_TIMESTAMP); - returns a date (2004-06-22)

SELECT TIME(CURRENT_TIMESTAMP); - returns the time (10:33:11.840)

SELECT DAYOFWEEK(CURRENT_TIMESTAMP); - returns a numeric value (1-7)

SELECT DAYOFMONTH(CURRENT_TIMESTAMP); - returns a day of month (1-31)

SELECT DAYOFYEAR(CURRENT_TIMESTAMP); - returns the day of the year (1-365)

SELECT MONTHNAME(CURRENT_TIMESTAMP);
- returns the month name (January - December

SELECT DAYNAME(CURRENT_TIMESTAMP);
- returns the name of the day (Sunday - Saturday)

SELECT WEEK(CURRENT_TIMESTAMP); - returns number of the week (1-53)

Timestamps are often the easiest to work with, but we certainly are not limited to using only the
current_timestamp as our parameter. We can send any date, time, or timestamp to the function which then returns
our result.

SQL Code:
SELECT MONTHNAME('2004-11-27');

Return a Month Name:


MONTHNAME('2004-11-27')
November

Date functions can also be performed on table columns similarly to numeric and mathematical functions such
as SUM() or AVG().

SQL Code:
SELECT DAYOFYEAR(column_name) FROM table_name WHERE name = 'Joe';

SQL will return a numeric result from 1 - 365 representing the day of the year that Joe's record was
created/inserted.
We can expand this concept one step further with the use of a subquery. Say we have a table with a column
named timestamp. In this table column are timestamps of when each record was entered and we would like to
know what was the numeric day of the year that a record was entered.

SQL Code:
SELECT DAYOFYEAR(
(SELECT DATE(timestamp) FROM employees WHERE name = 'James Bond')
);

Above you can see how it is possible to combine several date functions as well as a subquery to return very
specific information about Mr. James Bond.

SQL - Inserting Date Data


Date data exists as numbers, strings, and timestamps. Built into most platforms are several date column types
such as DATE or TIMESTAMP. By setting the default value to the current date or timestamp, the table column will
automatically be filled with a current date/timestamp as each record is inserted.

Here's the code to add a timestamp column to an existing table.

SQL Code:
ALTER TABLE `orders` ADD `order_date`
TIMESTAMP DEFAULT CURRENT_TIMESTAMP NOT NULL;

Now each time an order is placed in our make believe business, a timestamp of that order is also recorded.

A date or timestamp table column will only allow date data types to be inserted as values so be sure to convert
any strings or numbers to dates and timestamps before trying to insert them.

Tips

• Each platform has some sort of special date, time, or timestamp table column which will automatically post
the current_dates/times/timestamps.

SQL - Case

In SQL case works with either the select or update clauses. It provides when-then-else functionality (WHEN
this_happens THEN do_this) also known as conditional statements.

employee_name admin
Ted 0
Terry 0
Trish 1
As you can see, we have a list of a few employees and then an admin column with boolean (true/false) values
for their administration rights (admin). A zero means they are not an admin a 1 mean they are.

SQL Code:
SELECT employee_name
CASE admin
WHEN 1 THEN 'yes'
ELSE 'no'
END 'admin'
FROM employees;

SQL Case:
employee_name admin
Ted no
Terry no
Trish yes

In short all we really did is replace 1's and 0's with the words 'yes' and 'no'.

SQL - Case (update)

Case functions additionally allow for the updating of records within your table. For example, we could update
the prices of items in our online store, but more importantly we could update very specific records because of the
conditional logic allowed by case.

item quantity price


goldfish 12 1.00
guppy 24 0.50
blow fish 1 5.00

Let's say we wanted to have a sale to clean out some of our overstock. We'll go ahead and take 25% off of all
our items that we currently have 20 or more of (>20). Then we'll take 10% off the items that we have 10-20 of
(between 10 and 20) and everything else we will discount only 5%.

SQL Code:
UPDATE inventory SET price = price *
CASE
WHEN quantity > 20 THEN 0.75
WHEN quantity BETWEEN 10 AND 20 THEN 0.90
ELSE 0.95
END;

SQL Case:
item quantity price
goldfish 12 .9
guppy 24 0.375
blow fish 1 4.75
What is Normalization?
Normalization is the process of efficiently organizing data in a database.
There are two goals of the normalization process: eliminating redundant data (for example, storing the same data in
more than one table) and ensuring data dependencies make sense (only storing related data in a table). Both of these
are worthy goals as they reduce the amount of space a database consumes and ensure that data is logically stored.

The Normal Forms


The database community has developed a series of guidelines for ensuring that databases are normalized. These are
referred to as normal forms and are numbered from one (the lowest form of normalization, referred to as first normal form
or 1NF) through five (fifth normal form or 5NF). In practical applications, you'll often see 1NF, 2NF, and 3NF along with
the occasional 4NF. Fifth normal form is very rarely seen and won't be discussed in this article.

Before we begin our discussion of the normal forms, it's important to point out that they are guidelines and guidelines
only. Occasionally, it becomes necessary to stray from them to meet practical business requirements. However, when
variations take place, it's extremely important to evaluate any possible ramifications they could have on your system and
account for possible inconsistencies. That said, let's explore the normal forms.

First Normal Form (1NF)


First normal form (1NF) sets the very basic rules for an organized database:

• Eliminate duplicative columns from the same table.


• Create separate tables for each group of related data and identify each row with a unique column or set of columns (the
primary key).

Second Normal Form (2NF)


Second normal form (2NF) further addresses the concept of removing duplicative data:

• Meet all the requirements of the first normal form.


• Remove subsets of data that apply to multiple rows of a table and place them in separate tables.
• Create relationships between these new tables and their predecessors through the use of foreign keys.

Third Normal Form (3NF)


Third normal form (3NF) goes one large step further:

• Meet all the requirements of the second normal form.


• Remove columns that are not dependent upon the primary key.

Fourth Normal Form (4NF)


Finally, fourth normal form (4NF) has one additional requirement:

• Meet all the requirements of the third normal form.


• A relation is in 4NF if it has no multi-valued dependencies.

Remember, these normalization guidelines are cumulative. For a database to be in 2NF, it must first fulfill all the
criteria of a 1NF database.