Top Banner
Relational Database Basics In this lesson of the SQL tutorial, you will learn... 1. About the history of SQL and relational databases. 2. How relational databases are structured. 3. About some of the most popular relational databases. 4. About the major SQL statements. SQL stands for Structured Query Language and is pronounced either ess-que- el or sequel . It is the language used by relational database management systems (RDBMS) to access and manipulate data and to create, structure and destroy databases and database objects. Brief History of SQL In 1970, Dr. E.F. Codd published "A Relational Model of Data for Large Shared Data Banks," an article that outlined a model for storing and manipulating data using tables. Shortly after Codd's article was published, IBM began working on creating a relational database. Between 1979 and 1982, Oracle (then Relational Software, Inc.), Relational Technology, Inc. (later acquired by Computer Associates), and IBM all put out commercial relational databases, and by 1986 they all were using SQL as the data query language. In 1986, the American National Standards Institute (ANSI) standardized SQL. This standard was updated in 1989, in 1992 (called SQL2), and again in 1999 (called SQL3). Standard SQL is sometimes called ANSI SQL or SQL92. All major relational databases support this standard but each has its own proprietary extensions. Unless otherwise noted, the SQL taught in this course is the standard ANSI SQL. Relational Databases A relational database at its simplest is a set of tables used for storing data. Each table has a unique name and may relate to one or more other tables in the database through common values. Tables A table in a database is a collection of rows and columns. Tables are also known as entities or relations. Rows A row contains data pertaining to a single item or record in a table. Rows are also known as records or tuples.
44

Relational Database Basics

Apr 09, 2015

Download

Documents

guru95
Welcome message from author
This document is posted to help you gain knowledge. Please leave a comment to let me know what you think about it! Share it to your friends and learn new things together.
Transcript
Page 1: Relational Database Basics

Relational Database Basics

In this lesson of the SQL tutorial, you will learn...1. About the history of SQL and relational databases.2. How relational databases are structured.3. About some of the most popular relational databases.4. About the major SQL statements.

SQL stands for Structured Query Language and is pronounced either ess-que-el or sequel. It is the language used by relational database management systems (RDBMS) to access and manipulate data and to create, structure and destroy databases and database objects.

Brief History of SQL

In 1970, Dr. E.F. Codd published "A Relational Model of Data for Large Shared Data Banks," an article that outlined a model for storing and manipulating data using tables. Shortly after Codd's article was published, IBM began working on creating a relational database. Between 1979 and 1982, Oracle (then Relational Software, Inc.), Relational Technology, Inc. (later acquired by Computer Associates), and IBM all put out commercial relational databases, and by 1986 they all were using SQL as the data query language.

In 1986, the American National Standards Institute (ANSI) standardized SQL. This standard was updated in 1989, in 1992 (called SQL2), and again in 1999 (called SQL3). Standard SQL is sometimes called ANSI SQL or SQL92. All major relational databases support this standard but each has its own proprietary extensions. Unless otherwise noted, the SQL taught in this course is the standard ANSI SQL.

Relational Databases

A relational database at its simplest is a set of tables used for storing data. Each table has a unique name and may relate to one or more other tables in the database through common values.

Tables

A table in a database is a collection of rows and columns. Tables are also known as entities or relations.

Rows

A row contains data pertaining to a single item or record in a table. Rows are also known as records or tuples.

Columns

A column contains data representing a specific characteristic of the records in the table. Columns are also known as fields or attributes.

Relationships

A relationship is a link between two tables (i.e, relations). Relationships make it possible to find data in one table that pertains to a specific record in another table.

Page 2: Relational Database Basics

Datatypes

Each of a table's columns has a defined datatype that specifies the type of data that can exist in that column. For example, the FirstName column might be defined as varchar(20), indicating that it can contain a string of up to 20 characters. Unfortunately, datatypes vary widely between databases.

Primary Keys

Most tables have a column or group of columns that can be used to identify records. For example, an Employeestable might have a column called EmployeeID that is unique for every row. This makes it easy to keep track of a record over time and to associate a record with records in other tables.

Foreign Keys

Foreign key columns are columns that link to primary key columns in other tables, thereby creating a relationship. For example, the Customers table might have a foreign key column called SalesRep that links to EmployeeID, the primary key in the Employees table.

Relational Database Management System

A Relational Database Management System (RDBMS), commonly (but incorrectly) called a database, is software for creating, manipulating, and administering a database. For simplicity, we will often refer to RDBMSs as databases.

Popular Databases

Commercial Databases

Oracle

Oracle is the most popular relational database. It runs on both Unix and Windows. It used to be many times more expensive than SQL Server and DB2, but it has come down a lot in price.

SQL Server

SQL Server is Microsoft's database and, not surprisingly, only runs on Windows. It has only a slightly higher market share than Oracle on Windows machines. Many people find it easier to use than Oracle.

DB2

IBM's DB2 was one of the earliest players in the database market. It is still very commonly used on mainframes and runs on both Windows and Unix.

Popular Open Source Databases

MySQL

Page 3: Relational Database Basics

Because of its small size, its speediness, and its very good documentation, MySQL has quickly become the most popular open source database. MySQL is available on both Windows and Unix, but it lacks some key features such as support for stored procedures.

PostgreSQL

Until recently, PostgreSQL was the most popular open source database until that spot was taken over by MySQL. PostgreSQL now calls itself "the world's most advanced Open Source database software." It is certainly a featureful and robust database management system and a good choice for people who want some of the advanced features that MySQL doesn't yet have. PostgreSQL does not yet natively support Windows, but it is supposed to in the upcoming 7.5 release.

Valid Object References

server.database.owner.object server.database..object server..owner.object server...object database.owner.object database..object owner.object object

SQL Statements

Database Manipulation Language (DML)

DML statements are used to work with data in an existing database. The most common DML statements are:

SELECT INSERT UPDATE DELETE

Database Definition Language (DDL)

DDL statements are used to structure objects in a database. The most common DDL statements are:

CREATE ALTER DROP

Database Control Language (DCL)

DCL statements are used for database administration. The most common DCL statements are:

GRANT DENY (SQL Server Only) REVOKE

Page 4: Relational Database Basics

Relational Database Basics Conclusion

We have covered a little bit of the history of SQL, how databases work, and the common SQL statements. Now we will get into learning how to work with SQL.

To continue to learn SQL go to the top of this page and click on the next lesson in this SQL Tutorial's Table of Contents.

Simple SELECTs:

The SELECT statement is used to retrieve data from tables. SELECT statements can be used to perform simple tasks such as retrieving records from a single table or complicated tasks such as retrieving data from multiple tables with record grouping and sorting. In this lesson, we will look at several of the more basic ways to retrieve data from a single table.

Introduction to the Northwind Database

The Northwind database is a sample database used by Microsoft to demonstrate the features of some of its products, including SQL Server and Microsoft Access. The database contains the sales data for Northwind Traders, a fictitious specialty foods export-import company.

Although the code taught in this class is not specific to Microsoft products, we use the Northwind database for many of our examples because many people are already familiar with it and because there are many resources for related learning that make use of the same database.

The diagram below shows the table structure of the Northwind database.

The Northwind database has additional tables, but we will only be using the ones shown above. In this lesson, we will explore some of these tables.

Some Basics

Comments

The standard SQL comment is two hyphens (--). However, some databases use other forms of comments as shown in the table below.

SQL Comments

-- # /* */

Example -- Comment # Comment /* Comment */

ANSI YES NO NO

SQL Server YES NO YES

Oracle YES NO YES

MySQL YES YES YES

The code sample below shows some sample comments.

Page 5: Relational Database Basics

Code Sample: SimpleSelects/Demos/Comments.sql

-- Single-line comment/* Multi-line comment used in: -SQL Server -Oracle -MySQL*/

Whitespace and Semi-colons

Whitespace is ignored in SQL statements. Multiple statements are separated with semi-colons. The two statements in the sample below are equally valid.

Code Sample: SimpleSelects/Demos/WhiteSpace.sql

SELECT * FROM Employees;

SELECT *FROM Employees;

Case Sensitivity

SQL is not case sensitive. It is common practice to write reserved words in all capital letters. User-defined names, such as table names and column names may or may not be case sensitive depending on the operating system used.

SELECTing All Columns in All Rows

The following syntax is used to retrieve all columns in all rows of a table.

Syntax

SELECT table.*

FROM table;

-- OR

SELECT *

FROM table;

Code Sample: SimpleSelects/Demos/SelectAll.sql

--Retrieve all columns in the Region tableSELECT *FROM Region;

Code Explanation

Page 6: Relational Database Basics

The above SELECT statement will return the following results:As you can see, the Region table has only two columns, RegionID and RegionDescription, and four rows.

Exercise: Exploring the Tables

Duration: 10 to 20 minutes.

In this exercise, you will explore all the data in the Northwind database by selecting all the rows of each of the tables.

1. Select all columns of all rows from the tables below.2. The number of records that should be returned is indicated in parentheses next to

the table name.1. Categories (8)2. Customers (91)3. Employees (9)4. Orders (830)5. Products (77)6. Shippers (3)7. Suppliers (29)

Where is the solution?

SELECTing Specific Columns

The following syntax is used to retrieve specific columns in all rows of a table.

Syntax

SELECT table_name.column_name, table_name.column_name

FROM table;

-- OR

SELECT column, column

FROM table;

Code Sample: SimpleSelects/Demos/SelectCols.sql

/*Select the FirstName and LastName columns from the Employees table.*/SELECT FirstName, LastNameFROM Employees;

Code Explanation

The above SELECT statement will return the following results:

Exercise: SELECTing Specific Columns

Duration: 5 to 15 minutes.

Page 7: Relational Database Basics

In this exercise, you will practice selecting specific columns from tables in the Northwind database.

1. Select CategoryName and Description from the Categories table.2. Select ContactName, CompanyName, ContactTitle and Phone from

the Customers table.3. Select EmployeeID, Title, FirstName, LastName, and Region from

the Employees table.4. Select RegionID and RegionDescription from the Region table.5. Select CompanyName, Fax, Phone and HomePage from the Suppliers table.

Where is the solution?

Sorting Records

The ORDER BY clause of the SELECT statement is used to sort records.

Sorting By a Single Column

To sort by a single column, simply name that column in the ORDER BY clause.Syntax

SELECT column, column

FROM table

ORDER BY column;

Note that columns in the ORDER BY clause do not have to appear in the SELECT clause.

Code Sample: SimpleSelects/Demos/OrderBy1.sql

/* Select the FirstName and LastName columns from the Employees table. Sort by LastName.*/

SELECT FirstName, LastNameFROM EmployeesORDER BY LastName;

Code Explanation

The above SELECT statement will return the following results:

Sorting By Multiple Columns

To sort by multiple columns, comma-delimit the column names in the ORDER BY clause.

Syntax

SELECT column, column

FROM table

ORDER BY column, column;

Page 8: Relational Database Basics

Code Sample: SimpleSelects/Demos/OrderBy2.sql

/*Select the Title, FirstName and LastName columns from the Employees table.Sort first by Title and then by LastName.*/

SELECT Title, FirstName, LastNameFROM EmployeesORDER BY Title, LastName;

Code Explanation

The above SELECT statement will return the following results:

Sorting By Column Position

It is also possible to sort tables by the position of a column in the SELECT list. To do so, specify the column numbers in the ORDER BY clause.

Syntax

SELECT column, column

FROM table

ORDER BY column_position, column_position;

Code Sample: SimpleSelects/Demos/OrderBy3.sql

/*Select the Title, FirstName and LastName columns from the Employees table.Sort first by Title (position 1) and then by LastName (position 3).*/

SELECT Title, FirstName, LastNameFROM EmployeesORDER BY 1,3;

Code ExplanationThe above SELECT statement will return the same results as the previous query:

Ascending and Descending Sorts

By default, when an ORDER BY clause is used, records are sorted in ascending order. This can be explicitly specified with the ASC keyword. To sort records in descending order, use the DESC keyword.

Syntax

SELECT column, column

FROM table

ORDER BY column_position DESC, column_position ASC;

Page 9: Relational Database Basics

Code Sample: SimpleSelects/Demos/OrderBy4.sql

/* Select the Title, FirstName and LastName columns from the Employees table. Sort first by Title in ascending order and then by LastName in descending order.*/

SELECT Title, FirstName, LastNameFROM EmployeesORDER BY Title ASC, LastName DESC;

Code Explanation

The above SELECT statement will return the following results:

Exercise: Sorting Results

Duration: 5 to 15 minutes.In this exercise, you will practice sorting results in SELECT statements.

1. Select CategoryName and Description from the Categories table sorted byCategoryName.

2. Select ContactName, CompanyName, ContactTitle, and Phone from theCustomers table sorted by Phone.

3. Create a report showing employees' first and last names and hire dates sorted from newest to oldest employee.

4. Create a report showing Northwind's orders sorted by Freight from most expensive to cheapest. Show OrderID, OrderDate, ShippedDate, CustomerID, andFreight.

5. Select CompanyName, Fax, Phone, HomePage and Country from the Supplierstable sorted by Country in descending order and then by CompanyName in ascending order.

Where is the solution?

The WHERE Clause and Operator Symbols

The WHERE clause is used to retrieve specific rows from tables. The WHERE clause can contain one or more conditions that specify which rows should be returned.

Syntax

SELECT column, column

FROM table

WHERE conditions;

The following table shows the symbolic operators used in WHERE conditions.

SQL Symbol Operators

Operator Description= Equals<> Not Equal> Greater Than

Page 10: Relational Database Basics

SQL Symbol Operators

Operator Description< Less Than>= Greater Than or Equal To

<= Less Than or Equal ToNote that non-numeric values (e.g, dates and strings) in the WHERE clause must be enclosed in single quotes. Examples are shown below.

Checking for Equality

Code Sample: SimpleSelects/Demos/Where-Equal.sql

/*Create a report showing the title and the first and last nameof all sales representatives.*/

SELECT Title, FirstName, LastNameFROM EmployeesWHERE Title = 'Sales Representative';

Code Explanation

The above SELECT statement will return the following results:

Checking for Inequality

Code Sample: SimpleSelects/Demos/Where-NotEqual.sql

/*Create a report showing the first and last name of all employeesexcluding sales representatives.*/

SELECT FirstName, LastNameFROM EmployeesWHERE Title <> 'Sales Representative';

Code Explanation

The above SELECT statement will return the following results:

Exercise: Using the WHERE clause to check for equality or inequality

Duration: 5 to 15 minutes.In this exercise, you will practice using the WHERE clause to check for equality and inequality.

1. Create a report showing all the company names and contact names of Northwind's customers in Buenos Aires.

2. Create a report showing the product name, unit price and quantity per unit of all products that are out of stock.

Page 11: Relational Database Basics

3. Create a report showing the order date, shipped date, customer id, and freight of all orders placed on May 19, 1997.

o Oracle users will have to use following date format: 'dd-mmm-yyyy' (e.g, '19-May-1997').

o MySQL users will have to use following date format: 'yyyy-mm-dd' (e.g, '1997-05-19').

4. Create a report showing the first name, last name, and country of all employees not in the United States.

Where is the solution?

Checking for Greater or Less Than

The less than (<) and greater than (>) signs are used to compare numbers, dates, and strings.

Code Sample: SimpleSelects/Demos/Where-GreaterThanOrEqual.sql

/*Create a report showing the first and last name of all employees whoselast names start with a letter in the last half of the alphabet.*/

SELECT FirstName, LastNameFROM EmployeesWHERE LastName >= 'N';

Code Explanation

The above SELECT statement will return the following results:

Exercise: Using the WHERE clause to check for greater or less than

Duration: 5 to 15 minutes.In this exercise, you will practice using the WHERE clause to check for values greater than or less than a specified value.

1. Create a report that shows the employee id, order id, customer id, required date, and shipped date of all orders that were shipped later than they were required.

2. Create a report that shows the city, company name, and contact name of all customers who are in cities that begin with "A" or "B."

3. Create a report that shows all orders that have a freight cost of more than $500.00.4. Create a report that shows the product name, units in stock, units on order, and

reorder level of all products that are up for reorder.Where is the solution?

Checking for NULL

When a field in a row has no value, it is said to be NULL. This is not the same as having an empty string. Rather, it means that the field contains no value at all. When checking to see if a field is NULL, you cannot use the equals sign (=); rather, use the IS NULL expression.

Code Sample: SimpleSelects/Demos/Where-Null.sql

Page 12: Relational Database Basics

/*Create a report showing the first and last names ofall employees whose region is unspecified.*/

SELECT FirstName, LastNameFROM EmployeesWHERE Region IS NULL;

Code Explanation

The above SELECT statement will return the following results:

Code Sample: SimpleSelects/Demos/Where-NotNull.sql

/*Create a report showing the first and last names of allemployees who have a region specified.*/

SELECT FirstName, LastNameFROM EmployeesWHERE Region IS NOT NULL;

Code Explanation

The above SELECT statement will return the following results:

Exercise: Checking for NULL

Duration: 5 to 15 minutes.In this exercise, you will practice selecting records with fields that have NULL values.

1. Create a report that shows the company name, contact name and fax number of all customers that have a fax number.

2. Create a report that shows the first and last name of all employees who do not report to anybody.

Where is the solution?

WHERE and ORDER BY

When using WHERE and ORDER BY together, the WHERE clause must come before the ORDER BY clause.

Code Sample: SimpleSelects/Demos/Where-OrderBy.sql

/*Create a report showing the first and last name of all employees whose last names start with a letter in the last half of the alphabet.Sort by LastName in descending order.

Page 13: Relational Database Basics

*/

SELECT FirstName, LastNameFROM EmployeesWHERE LastName >= 'N'ORDER BY LastName DESC;

Code Explanation

The above SELECT statement will return the following results:

Exercise: Using WHERE and ORDER BY Together

Duration: 5 to 15 minutes.In this exercise, you will practice writing SELECT statements that use both WHERE and ORDER BY.

1. Create a report that shows the company name, contact name and fax number of all customers that have a fax number. Sort by company name.

2. Create a report that shows the city, company name, and contact name of all customers who are in cities that begin with "A" or "B." Sort by contact name in descending order.

Where is the solution?

The WHERE Clause and Operator Words

The following table shows the word operators used in WHERE conditions.

SQL Word Operators

Operator DescriptionBETWEEN Returns values in an inclusive rangeIN Returns values in a specified subsetLIKE Returns values that match a simple patternNOT Negates an operation

The BETWEEN Operator

The BETWEEN operator is used to check if field values are within a specified inclusive range.

Code Sample: SimpleSelects/Demos/Where-Between.sql

/*Create a report showing the first and last name of all employeeswhose last names start with a letter between "J" and "M". */

SELECT FirstName, LastNameFROM EmployeesWHERE LastName BETWEEN 'J' AND 'M';

-- The above SELECT statement is the same as the one below.

Page 14: Relational Database Basics

SELECT FirstName, LastNameFROM EmployeesWHERE LastName >= 'J' AND LastName <= 'M';

Code Explanation

The above SELECT statements will both return the following results:

Note that a person with the last name "M" would be included in this report.

The IN Operator

The IN operator is used to check if field values are included in a specified comma-delimited list.

Code Sample: SimpleSelects/Demos/Where-In.sql

/*Create a report showing the title of courtesy and the first andlast name of all employees whose title of courtesy is "Mrs." or "Ms.". */

SELECT TitleOfCourtesy, FirstName, LastNameFROM EmployeesWHERE TitleOfCourtesy IN ('Ms.','Mrs.');

-- The above SELECT statement is the same as the one below

SELECT TitleOfCourtesy, FirstName, LastNameFROM EmployeesWHERE TitleOfCourtesy = 'Ms.' OR TitleOfCourtesy = 'Mrs.';

Code Explanation

The above SELECT statements will both return the following results:

The LIKE Operator

The LIKE operator is used to check if field values match a specified pattern.

The Percent Sign (%)

The percent sign (%) is used to match any zero or more characters.

Code Sample: SimpleSelects/Demos/Where-Like1.sql

/*Create a report showing the title of courtesy and the firstand last name of all employees whose title of courtesy begins with "M".

Page 15: Relational Database Basics

*/

SELECT TitleOfCourtesy, FirstName, LastNameFROM EmployeesWHERE TitleOfCourtesy LIKE 'M%';

Code Explanation

The above SELECT statement will return the following results:

The Underscore (_)

The underscore (_) is used to match any single character.

Code Sample: SimpleSelects/Demos/Where-Like2.sql

/*Create a report showing the title of courtesy and the first andlast name of all employees whose title of courtesy begins with "M" andis followed by any character and a period (.).*/

SELECT TitleOfCourtesy, FirstName, LastNameFROM EmployeesWHERE TitleOfCourtesy LIKE 'M_.';

Code Explanation

The above SELECT statement will return the following results:

Wildcards and Performance

Using wildcards can slow down performance, especially if they are used at the beginning of a pattern. You should use them sparingly.

The NOT Operator

The NOT operator is used to negate an operation.

Code Sample: SimpleSelects/Demos/Where-Not.sql

/*Create a report showing the title of courtesy and the first and last nameof all employees whose title of courtesy is not "Ms." or "Mrs.".*/

SELECT TitleOfCourtesy, FirstName, LastNameFROM EmployeesWHERE NOT TitleOfCourtesy IN ('Ms.','Mrs.');

Code Explanation

The above SELECT statement will return the following results:

Exercise: More SELECTs with WHERE

Page 16: Relational Database Basics

Duration: 5 to 15 minutes.In this exercise, you will practice writing SELECT statements that use WHERE with word operators.

1. Create a report that shows the first and last names and birth date of all employees born in the 1950s.

2. Create a report that shows the product name and supplier id for all products supplied by Exotic Liquids, Grandma Kelly's Homestead, and Tokyo Traders. Hint: you will need to first do a separateSELECT on the Suppliers table to find the supplier ids of these three companies.

3. Create a report that shows the shipping postal code, order id, and order date for all orders with a ship postal code beginning with "02389".

4. Create a report that shows the contact name and title and the company name for all customers whose contact title does not contain the word "Sales".

Where is the solution?

Checking Multiple Conditions

AND

AND can be used in a WHERE clause to find records that match more than one condition.

Code Sample: SimpleSelects/Demos/Where-And.sql

/*Create a report showing the first and last name of allsales representatives whose title of courtesy is "Mr.".*/

SELECT FirstName, LastNameFROM EmployeesWHERE Title = 'Sales Representative' AND TitleOfCourtesy = 'Mr.';

Code Explanation

The above SELECT statement will return the following results:

OR

OR can be used in a WHERE clause to find records that match at least one of several conditions.

Code Sample: SimpleSelects/Demos/Where-Or.sql

/* Create a report showing the first and last name and the city of all employees who are from Seattle or Redmond.*/

SELECT FirstName, LastName, CityFROM Employees

Page 17: Relational Database Basics

WHERE City = 'Seattle' OR City = 'Redmond';

Code Explanation

The above SELECT statement will return the following results:

Order of Evaluation

By default, SQL processes AND operators before it processes OR operators. To illustrate how this works, take a look at the following example.

Code Sample: SimpleSelects/Demos/Where-AndOrPrecedence.sql

/* Create a report showing the first and last name of all sales representatives who are from Seattle or Redmond.*/

SELECT FirstName, LastName, City, TitleFROM EmployeesWHERE City = 'Seattle' OR City = 'Redmond' AND Title = 'Sales Representative';

Code Explanation

The above SELECT statement will return the following results:

Notice that Laura Callahan is returned by the query even though she is not a sales representative. This is because this query is looking for employees from Seattle OR sales representatives from Redmond.

This can be fixed by putting the OR portion of the clause in parentheses.

Code Sample: SimpleSelects/Demos/Where-AndOrPrecedence2.sql

/* Create a report showing the first and last name of all sales representatives who are from Seattle or Redmond.*/

SELECT FirstName, LastName, City, TitleFROM EmployeesWHERE (City = 'Seattle' OR City = 'Redmond') AND Title = 'Sales Representative';

Code Explanation

Page 18: Relational Database Basics

The parentheses specify that the OR portion of the clause should be evaluated first, so the above SELECT statement will return the same results minus Laura Callahan.

If only to make the code more readable, it's a good idea to use parentheses whenever the order of precedence might appear ambiguous.

Exercise: Writing SELECTs with Multiple Conditions

Duration: 5 to 15 minutes.In this exercise, you will practice writing SELECT statements that filter records based on multiple conditions.

1. Create a report that shows the first and last names and cities of employees from cities other than Seattle in the state of Washington.

2. Create a report that shows the company name, contact title, city and country of all customers in Mexico or in any city in Spain except Madrid.

Where is the solution?

Simple SELECTs Conclusion

In this lesson of the SQL tutorial, you have learned a lot about creating reports with SELECT. However, this is just the tip of the iceberg. SELECT statements can get a lot more powerful and, of course, a lot more complicated.

To continue to learn SQL go to the top of this page and click on the next lesson in this SQL Tutorial's Table of Contents.

Advanced SELECTs

In this lesson of the SQL tutorial, you will learn...1. To use SELECT statements to retrieve calculated values.2. To work with aggregate functions and grouping.3. To work with SQL's data manipulation functions.

Calculated Fields

Calculated fields are fields that do not exist in a table, but are created in the SELECT statement. For example, you might want to create FullName from FirstName and LastName.

Concatenation

Concatenation is a fancy word for stringing together different words or characters. SQL Server, Oracle and MySQL each has its own way of handling concatenation. All three of the code

samples below will return the following results:

In SQL Server, the plus sign (+) is used as the concatenation operator.

Page 19: Relational Database Basics

Code Sample: AdvancedSelects/Demos/Concatenate-SqlServer.sql

-- Select the full name of all employees. SQL SERVER.

SELECT FirstName + ' ' + LastNameFROM Employees;

In Oracle, the double pipe (||) is used as the concatenation operator.

Code Sample: AdvancedSelects/Demos/Concatenate-Oracle.sql

-- Select the full name of all employees. Oracle.

SELECT FirstName || ' ' || LastNameFROM Employees;

MySQL does this in yet another way. There is no concatenation operator. Instead, MySQL uses the CONCAT() function (see footnote).

Code Sample: AdvancedSelects/Demos/Concatenate-MySQL.sql

-- Select the full name of all employees. MySQL.SELECT CONCAT(FirstName, ' ', LastName)FROM Employees;

Note that concatenation only works with strings. To concatenate other data types, you must first convert them to strings. ( see footnote)

Mathematical Calculations

Mathematical calculations in SQL are similar to those in other languages.

Mathematical Operators

Operator Description+ Addition- Subtraction* Multiplication/ Division% Modulus

Code Sample: AdvancedSelects/Demos/MathCalc.sql

/*If the cost of freight is greater than or equal to $500.00,it will now be taxed by 10%. Create a report that shows theorder id, freight cost, freight cost with this tax for allorders of $500 or more.*/

SELECT OrderID, Freight, Freight * 1.1

Page 20: Relational Database Basics

FROM OrdersWHERE Freight >= 500;

Code Explanation

The above SELECT statement will return the following results:

Aliases

You will notice in the examples above that the calculated columns have the header "(No column name)". The keyword AS is used to provide a named header for the column.

Code Sample: AdvancedSelects/Demos/Alias.sql

SELECT OrderID, Freight, Freight * 1.1 AS FreightTotalFROM OrdersWHERE Freight >= 500;

Code ExplanationAs you can see, the third column now has the title "FreightTotal".

Exercise: Calculating Fields

Duration: 10 to 20 minutes.In this exercise, you will practice writing SELECT statements with calculated fields.

1. Create a report that shows the unit price, quantity, discount, and the calculated total price using these three fields.

o Note for SQL Server users only: You will be using theOrder Details table. Because this table name has a space in it, you will need to put it in double quotes in theFROM clause (e.g, FROM "Order Details").

2. Write a SELECT statement that outputs the following.Where is the solution?

Aggregate Functions and Grouping

Aggregate Functions

Aggregate functions are used to calculate results using field values from multiple records. There are five common aggregate functions.

Common Aggregate Functions

Aggregate Function

Description

COUNT() Returns the number of rows containing non-NULL values in the specified field.

SUM() Returns the sum of the non-NULL values in the specified field.

Page 21: Relational Database Basics

Common Aggregate Functions

Aggregate Function

Description

AVG() Returns the average of the non-NULL values in the specified field.MAX() Returns the maximum of the non-NULL values in the specified field.MIN() Returns the minimum of the non-NULL values in the specified field.

Code Sample: AdvancedSelects/Demos/Aggregate-Count.sql

-- Find the Number of Employees

SELECT COUNT(*) AS NumEmployeesFROM Employees;

Code Explanation

Returns 9.

Code Sample: AdvancedSelects/Demos/Aggregate-Sum.sql

-- Find the Total Number of Units Ordered of Product ID 3

/******************************SQL Server******************************/SELECT SUM(Quantity) AS TotalUnitsFROM "Order Details"WHERE ProductID=3;

/******************************Oracle and MySQL******************************/SELECT SUM(Quantity) AS TotalUnitsFROM Order_DetailsWHERE ProductID=3;

Code Explanation

Returns 328.

Code Sample: AdvancedSelects/Demos/Aggregate-Avg.sql

-- Find the Average Unit Price of Products

SELECT AVG(UnitPrice) AS AveragePriceFROM Products;

Code Explanation

Returns 28.8663.

Page 22: Relational Database Basics

Code Sample: AdvancedSelects/Demos/Aggregate-MinMax.sql

-- Find the Earliest and Latest Dates of Hire

SELECT MIN(HireDate) AS FirstHireDate, MAX(HireDate) AS LastHireDateFROM Employees;

Code ExplanationThe above SELECT statement will return April 1, 1992 and November 15, 1994 as the FirstHireDate andLastHireDate, respectively. The date format will vary from

database to database.

Grouping Data

GROUP BY

With the GROUP BY clause, aggregate functions can be applied to groups of records based on column values. For example, the following code will return the number of employees in each city.

Code Sample: AdvancedSelects/Demos/Aggregate-GroupBy.sql

--Retrieve the number of employees in each city

SELECT City, COUNT(EmployeeID) AS NumEmployeesFROM EmployeesGROUP BY City;

The above SELECT statement will return the following results:

HAVING

The HAVING clause is used to filter grouped data. For example, the following code specifies that we only want information on cities that have more than one employee.

Code Sample: AdvancedSelects/Demos/Aggregate-Having.sql

/* Retrieve the number of employees in each city in which there are at least 2 employees.*/

SELECT City, COUNT(EmployeeID) AS NumEmployeesFROM EmployeesGROUP BY CityHAVING COUNT(EmployeeID) > 1;

The above SELECT statement will return the following results:

Page 23: Relational Database Basics

Order of Clauses

1. SELECT2. FROM3. WHERE4. GROUP BY5. HAVING6. ORDER BY

Code Sample: AdvancedSelects/Demos/Aggregate-OrderOfClauses.sql

/* Find the number of sales representatives in each city that contains at least 2 sales representatives. Order by the number of employees.*/

SELECT City, COUNT(EmployeeID) AS NumEmployeesFROM EmployeesWHERE Title = 'Sales Representative'GROUP BY CityHAVING COUNT(EmployeeID) > 1ORDER BY NumEmployees;

The above SELECT statement will return the following results:

Grouping Rules

Every non-aggregate column that appears in the SELECT clause must also appear in the GROUP BYclause.

You may not use aliases in the HAVING clause.(see footnote) You may use aliases in the ORDER BY clause. You may only use calculated fields in the HAVING clause. You may use calculated field aliases or actual fields in the ORDER BY clause.

Exercise: Working with Aggregate Functions

Duration: 10 to 20 minutes.

In this exercise, you will practice working with aggregate functions.

1. Create a report that returns the following from the Order_Details table.The report should only return rows for which TotalUnits is less than 200.

2. Create a report that returns the following from the Products table. The report should only return rows for which the average unit price of a product is greater than 70.

3. Create a report that returns the following from the Orders table.NumOrders represents the number of orders placed by a certain customer. Only return rows whereNumOrders is greater than 15.

Page 24: Relational Database Basics

Query number 2 above has something strange about it. It is, in fact, a ridiculous query. Why? Try to get the exact same results without using an aggregate function.

Where is the solution?

Selecting Distinct Records

The DISTINCT keyword is used to select distinct combinations of column values from a table. For example, the following example shows how you would find all the distinct cities in which Northwind has employees.

Code Sample: AdvancedSelects/Demos/Distinct.sql

/*Find all the distinct cities in which Northwind has employees.*/

SELECT DISTINCT CityFROM EmployeesORDER BY City

DISTINCT is often used with aggregate functions. The following example shows how DISTINCT can be used to find out in how many different cities Northwind has employees.

Code Sample: AdvancedSelects/Demos/Distinct-Count.sql

/*Find out in how many different cities Northwind has employees.*/

SELECT COUNT(DISTINCT City) AS NumCitiesFROM Employees

Built-in Data Manipulation Functions

In this section, we will discuss some of the more common built-in data manipulation functions. Unfortunately, the functions differ greatly between databases, so you should be sure to check your database documentation when using these functions.

The tables below show some of the more common math, string, and date functions.

Common Math Functions

Common Math Functions

Description SQL Server Oracle MySQL

Absolute value

ABS ABS ABS

Smallest integer >= value

CEILING CEIL CEILING

Page 25: Relational Database Basics

Common Math Functions

Description SQL Server Oracle MySQL

Round down to nearest integer

FLOOR FLOOR FLOOR

Power POWER POWER POWER

Round ROUND ROUND ROUND

Square root SQRT SQRT SQRT

Formatting numbers to two decimal places

CAST(num AS decimal(8,2))

TO_CHAR(num,'9.00') FORMAT(num,2) or CAST(num AS decimal(8,2))

Code Sample: AdvancedSelects/Demos/Functions-Math1.sql

/*Select freight as is andfreight rounded to the first decimal (e.g, 1.150 becomes 1.200)from the Orders tables*/

SELECT Freight, ROUND(Freight,1) AS ApproxFreightFROM Orders;

Code ExplanationThe above SELECT statement will return the following results (not all rows shown):

Code Sample: AdvancedSelects/Demos/Functions-Math2.sql

/*Select the unit price as is andunit price as a decimal with 2 places to the right of the decimal pointfrom the Products tables*//******************************SQL Server and MySQL******************************/SELECT UnitPrice, CAST(UnitPrice AS Decimal(8,2))FROM Products;

/******************************Oracle******************************/SELECT UnitPrice, TO_CHAR(UnitPrice,'999.99')FROM Products;

Code Explanation

Page 26: Relational Database Basics

The above SELECT statement will return the following results (not all rows shown):

Note that you would round to a whole number by passing 0 as the second parameter: ROUND(field,0); and to the tens place by passing -1: ROUND(field,-1).

Common String Functions

Common String Functions

Description SQL Server Oracle MySQL

Convert characters to lowercase LOWER LOWER LOWER

Convert characters to uppercase UPPER UPPER UPPER

Remove trailing blank spaces RTRIM RTRIM RTRIM

Remove leading blank spaces LTRIM LTRIM LTRIM

Substring SUBSTRING SUBSTR SUBSTRING

Code Sample: AdvancedSelects/Demos/Functions-String1.sql

/*Select first and last name from employees in all uppercase letters*/SELECT UPPER(FirstName), UPPER(LastName)FROM Employees;

Code Explanation

The above SELECT statement will return the following results:

Code Sample: AdvancedSelects/Demos/Functions-String2.sql

-- Select the first 10 characters of each customer's address

/******************************SQL Server and MySQL******************************/SELECT SUBSTRING(Address,1,10)FROM Customers;

/******************************Oracle******************************/SELECT SUBSTR(Address,1,10)FROM Customers;

Code ExplanationThe above SELECT statement will return the following results (not all rows shown):

Page 27: Relational Database Basics

Common Date Functions

Common Date Functions

Description SQL Server Oracle MySQL

Date addition DATEADD (use +) DATE_ADD

Date subtraction DATEDIFF (use -) DATEDIFF

Convert date to string DATENAME TO_CHAR DATE_FORMAT

Convert date to number DATEPART TO_NUMBER(TO_CHAR) EXTRACT

Get current date and time GETDATE SYS_DATE NOW

Code Sample: AdvancedSelects/Demos/Functions-Date1.sql

-- Find the hiring age of each employee

/******************************SQL Server******************************/SELECT LastName, BirthDate, HireDate, DATEDIFF(year,BirthDate,HireDate) AS HireAgeFROM EmployeesORDER BY HireAge;

/******************************Oracle******************************/SELECT LastName, BirthDate, HireDate, FLOOR((HireDate - BirthDate)/365.25) AS HireAgeFROM EmployeesORDER BY HireAge;

/******************************MySQL******************************/-- Find the hiring age of each employee-- in versions of MySQL prior to 4.1.1SELECT LastName, BirthDate, HireDate, YEAR(HireDate)-YEAR(BirthDate) AS HireAgeFROM Employees;

-- In MySQL 4.1.1 and later, DATEDIFF() returns the number of days between-- two dates. You can then divide and floor to get age.SELECT LastName, BirthDate, HireDate, FLOOR(DATEDIFF(HireDate,BirthDate)/365) AS HireAgeFROM EmployeesORDER BY HireAge;

Code Explanation

The above SELECT statement will return the following results in SQL Server:

Page 28: Relational Database Basics

And like this in Oracle:

Note for SQL Server users: SQL Server is subtracting the year the employee was born from the year (s)he was hired. This does not give us an accurate age. We'll fix this in an upcoming exercise.

Code Sample: AdvancedSelects/Demos/Functions-Date2.sql

-- Find the Birth month for every employee

/******************************SQL Server******************************/SELECT FirstName, LastName, DATENAME(month,BirthDate) AS BirthMonthFROM EmployeesORDER BY DATEPART(month,BirthDate);

/******************************Oracle******************************/SELECT FirstName, LastName, TO_CHAR(BirthDate,'MONTH') AS BirthMonthFROM EmployeesORDER BY TO_NUMBER(TO_CHAR(BirthDate,'MM'));

/******************************MySQL******************************/SELECT FirstName, LastName, DATE_FORMAT(BirthDate, '%M') AS BirthMonthFROM EmployeesORDER BY EXTRACT(MONTH FROM BirthDate);

Code Explanation

The above SELECT statement will return the following results:

Exercise: Data Manipulation Functions

Duration: 10 to 20 minutes.

In this exercise, you will practice using data manipulation functions.

1. Create a report that shows the units in stock, unit price, the total price value of all units in stock, the total price value of all units in stock rounded down, and the total price value of all units in stock rounded up. Sort by the total price value descending.

2. SQL SERVER AND MYSQL USERS ONLY: In an earlier demo , you saw a report that returned the age of each employee when hired. That report was not entirely accurate as it didn't account for the month and day the employee was born. Fix that report, showing both the original (inaccurate) hire age and the actual hire age.

The result will look like this.3. Create a report that shows the first and last names and birth month (as a string) for

each employee born in the current month.

Page 29: Relational Database Basics

4. Create a report that shows the contact title in all lowercase letters of each customer contact.

Where is the solution?

Advanced SELECTs Conclusion

In this lesson of the SQL tutorial, you have continued to use SELECT to create reports from data stored in a single table. In the next lesson, you will learn to create reports from data in multiple tables.

Footnotes

1. We'll look at functions more in Built-in Data Manipulation Functions.2. Conversion is covered briefly in Built-in Data Manipulation Functions.3. MySQL allows usage of aliases in the HAVING clause, but you may want to avoid

this to keep your code as cross-database compatible as possible.To continue to learn SQL go to the top of this page and click on the next lesson in this SQL Tutorial's Table of Contents.

Subqueries, Joins and Unions

In this lesson of the SQL tutorial, you will learn...1. To write queries with subqueries.2. To select columns from multiple tables with joins.3. To select records from multiple tables with unions.

Subqueries

Subqueries are queries embedded in queries. They are used to retrieve data from one table based on data in another table. They generally are used when tables have some kind of relationship. For example, in the Northwind database, the Orders table has a CustomerID field, which references a customer in the Customers table. Retrieving theCustomerID for a specific order is pretty straightforward.

Code Sample: SubqueriesJoinsUnions/Demos/Subquery-SelectCustomerID.sql

/*Find the CustomerID of the company that placed order 10290.*/

SELECT CustomerIDFROM OrdersWHERE OrderID = 10290;

Code Explanation

This will return COMMI, which is very likely meaningless to the people reading the report. The next query uses a subquery to return a meaningful result.

Page 30: Relational Database Basics

Code Sample: SubqueriesJoinsUnions/Demos/Subquery-SelectCompanyName.sql

-- Find the name of the company that placed order 10290.

SELECT CompanyNameFROM CustomersWHERE CustomerID = (SELECT CustomerID FROM Orders WHERE OrderID = 10290);

Code ExplanationThe above code returns Comércio Mineiro, which is a lot more useful than COMMI.The subquery can contain any valid SELECT statement, but it must return a single column with the expected number of results. For example, if the subquery returns only one result, then the main query can check for equality, inequality, greater than, less than, etc. On the other hand, if the subquery returns more than one record, the main query must check to see if a field value is (or is NOT) IN the set of values returned.

Code Sample: SubqueriesJoinsUnions/Demos/Subquery-IN.sql

-- Find the Companies that placed orders in 1997

/******************************Both of the queries below will work in SQL Server

Oracle******************************/SELECT CompanyNameFROM CustomersWHERE CustomerID IN (SELECT CustomerID FROM Orders WHERE OrderDate BETWEEN '1-Jan-1997' AND '31-Dec-1997');

/******************************MySQL******************************/SELECT CompanyNameFROM CustomersWHERE CustomerID IN (SELECT CustomerID FROM Orders WHERE OrderDate BETWEEN '1997-01-01' AND '1997-12-31');

Code Explanation

The above SELECT statement will return the following results:

Exercise: Subqueries

Duration: 20 to 30 minutes.

In this exercise, you will practice writing subqueries.

1. Create a report that shows the product name and supplier id for all products supplied by Exotic Liquids, Grandma Kelly's Homestead, and Tokyo Traders.

Page 31: Relational Database Basics

o You will need to escape the apostrophe in "Grandma Kelly's Homestead." To do so, place another apostrophe in front of it. For example,o SELECT *

o FROM Suppliers

WHERE CompanyName='Grandma Kelly''s Homestead';

2. Create a report that shows all products by name that are in the Seafood category.3. Create a report that shows all companies by name that sell products in CategoryID 8.4. Create a report that shows all companies by name that sell products in the Seafood

category.Where is the solution?

Joins

How can we find out…

Which products are provided by which suppliers? Which customers placed which orders? Which customers are buying which products?

Such reports require data from multiple tables. Enter joins.

Syntax

SELECT table1.column, table2.column

FROM table1 JOIN table2

ON (table1.column=table2.column)

WHERE conditions

Creating a report that returns the employee id and order id from the Orders table is not difficult.

Code Sample: SubqueriesJoinsUnions/Demos/Joins-NoJoin.sql

-- Find the EmployeeID and OrderID for all orders

SELECT EmployeeID, OrderIDFROM Orders;

But this isn’t very useful as we cannot tell who the employee is that got this order. The next sample shows how we can use a join to make the report more useful.

Code Sample: SubqueriesJoinsUnions/Demos/Joins-EmployeeOrders.sql

-- Create a report showing employee orders.

SELECT Employees.EmployeeID, Employees.FirstName, Employees.LastName, Orders.OrderID, Orders.OrderDateFROM Employees JOIN Orders ON (Employees.EmployeeID = Orders.EmployeeID)ORDER BY Orders.OrderDate;

Code Explanation

Page 32: Relational Database Basics

The above SELECT statement will return the following results:

Table names are used as prefixes of the column names to identify the table in which to find the column. Although this is only required when the column name exists in both tables, it is always a good idea to include the prefixes as it makes the code more efficient and easier to read.

Table Aliases

Using full table names as prefixes can make SQL queries unnecessarily wordy. Table aliases can make the code a little more concise. The example below, which is identical in functionality to the query above, illustrates the use of table aliases.

Code Sample: SubqueriesJoinsUnions/Demos/Joins-Aliases.sql

-- Create a report showing employee orders using Aliases.

SELECT e.EmployeeID, e.FirstName, e.LastName, o.OrderID, o.OrderDateFROM Employees e JOIN Orders o ON (e.EmployeeID = o.EmployeeID)ORDER BY o.OrderDate;

Multi-table Joins

Multi-table joins can get very complex and may also take a long time to process, but the syntax is relatively straightforward.

Syntax

SELECT table1.column, table2.column, table3.column

FROM table1

JOIN table2 ON (table1.column=table2.column)

JOIN table3 ON (table2.column=table3.column)

WHERE conditions

Note that, to join with a table, that table must be in the FROM clause or must already be joined with the table in theFROM clause. Consider the following.SELECT table1.column, table2.column, table3.column

FROM table1

JOIN table3 ON (table2.column=table3.column)

JOIN table2 ON (table1.column=table2.column)

WHERE conditions

The above code would break because it attempts to join table3 with table2 before table2 has been joined withtable1.

Code Sample: SubqueriesJoinsUnions/Demos/Joins-MultiTable.sql

Page 33: Relational Database Basics

/*Create a report showing the Order ID, the name of the company that placed the order,and the first and last name of the associated employee.Only show orders placed after January 1, 1998 that shipped after they were required.Sort by Company Name.*/

/******************************Both of the queries below will work in SQL Server

Oracle******************************/SELECT o.OrderID, c.CompanyName, e.FirstName, e.LastNameFROM Orders o JOIN Employees e ON (e.EmployeeID = o.EmployeeID) JOIN Customers c ON (c.CustomerID = o.CustomerID)WHERE o.ShippedDate > o.RequiredDate AND o.OrderDate > '1-Jan-1998'ORDER BY c.CompanyName;

/******************************MySQL******************************/SELECT o.OrderID, c.CompanyName, e.FirstName, e.LastNameFROM Orders o JOIN Employees e ON (e.EmployeeID = o.EmployeeID) JOIN Customers c ON (c.CustomerID = o.CustomerID)WHERE o.ShippedDate > o.RequiredDate AND o.OrderDate > '1998-01-01'ORDER BY c.CompanyName;

Code Explanation

The above SELECT statement will return the following results:

Exercise: Using Joins

Duration: 25 to 40 minutes.

In this exercise, you will practice using joins.

1. Create a report that shows the order ids and the associated employee names for orders that shipped after the required date. It should return the following. There should be 37

rows returned.2. Create a report that shows the total quantity of products (from

the Order_Details table) ordered. Only show records for products for which the quantity ordered is fewer than 200. The report should return the following 5 rows.

3. Create a report that shows the total number of orders by Customer since December 31, 1996. The report should only return rows for which the NumOrders is greater than 15.

The report should return the following 5 rows.

Page 34: Relational Database Basics

4. Create a report that shows the company name, order id, and total price of all products of which Northwind has sold more than $10,000 worth. There is no need for a GROUP

BY clause in this report.Where is the solution?

Outer Joins

So far, all the joins we have worked with are inner joins, meaning that rows are only returned that have matches in both tables. For example, when doing an inner join between the Employees table and the Orders table, only employees that have matching orders and orders that have matching employees will be returned.

As a point of comparison, let's first look at another inner join.

Code Sample: SubqueriesJoinsUnions/Demos/OuterJoins-Inner.sql

/* Create a report that shows the number of employees and customers from each city that has employees in it.*/

SELECT COUNT(DISTINCT e.EmployeeID) AS numEmployees, COUNT(DISTINCT c.CustomerID) AS numCompanies, e.City, c.CityFROM Employees e JOIN Customers c ON (e.City = c.City)GROUP BY e.City, c.CityORDER BY numEmployees DESC;

Code Explanation

The above SELECT statement will return the following results:

Left Joins

A LEFT JOIN (also called a LEFT OUTER JOIN) returns all the records from the first table even if there are no matches in the second table.

Syntax

SELECT table1.column, table2.column

FROM table1

LEFT [OUTER] JOIN table2 ON (table1.column=table2.column)

WHERE conditions

All rows in table1 will be returned even if they do not have matches in table2.

Code Sample: SubqueriesJoinsUnions/Demos/OuterJoins-Left.sql

/* Create a report that shows the number of

Page 35: Relational Database Basics

employees and customers from each city that has employees in it.*/

SELECT COUNT(DISTINCT e.EmployeeID) AS numEmployees, COUNT(DISTINCT c.CustomerID) AS numCompanies, e.City, c.CityFROM Employees e LEFT JOIN Customers c ON (e.City = c.City)GROUP BY e.City, c.CityORDER BY numEmployees DESC;

Code ExplanationAll records in the Employees table will be counted whether or not there are matching cities in

the Customerstable. The results are shown below:

Right Joins

A RIGHT JOIN (also called a RIGHT OUTER JOIN) returns all the records from the second table even if there are no matches in the first table.

Syntax

SELECT table1.column, table2.column

FROM table1

RIGHT [OUTER] JOIN table2 ON (table1.column=table2.column)

WHERE conditions

All rows in table2 will be returned even if they do not have matches in table1.

Code Sample: SubqueriesJoinsUnions/Demos/OuterJoins-Right.sql

/* Create a report that shows the number of employees and customers from each city that has customers in it.*/

SELECT COUNT(DISTINCT e.EmployeeID) AS numEmployees, COUNT(DISTINCT c.CustomerID) AS numCompanies, e.City, c.CityFROM Employees e RIGHT JOIN Customers c ON (e.City = c.City)GROUP BY e.City, c.CityORDER BY numEmployees DESC;

Code Explanation

All records in the Customers table will be counted whether or not there are matching cities in

the Employees table. The results are shown below (not all records shown):

Full Outer Joins

Page 36: Relational Database Basics

A FULL JOIN (also called a FULL OUTER JOIN) returns all the records from each table even if there are no matches in the joined table.Full outer joins are not supported in MySQL 5.x and earlier.

Syntax

SELECT table1.column, table2.column

FROM table1

FULL [OUTER] JOIN table2 ON (table1.column=table2.column)

WHERE conditions

All rows in table1 and table2 will be returned.

Code Sample: SubqueriesJoinsUnions/Demos/OuterJoins-Full.sql

/* Create a report that shows the number of employees and customers from each city. Note that MySQL 5.x does NOT support full outer joins.*/

SELECT COUNT(DISTINCT e.EmployeeID) AS numEmployees, COUNT(DISTINCT c.CustomerID) AS numCompanies, e.City, c.CityFROM Employees e FULL JOIN Customers c ON (e.City = c.City)GROUP BY e.City, c.CityORDER BY numEmployees DESC;

Code Explanation

All records in each table will be counted whether or not there are matching cities in the other

table. The results are shown below (not all records shown):

Unions

Unions are used to retrieve records from multiple tables or to get multiple record sets from a single table.

Code Sample: SubqueriesJoinsUnions/Demos/Unions.sql

/*Get the phone numbers of all shippers, customers, and suppliers*/

SELECT CompanyName, PhoneFROM Shippers UNIONSELECT CompanyName, PhoneFROM Customers UNION

Page 37: Relational Database Basics

SELECT CompanyName, PhoneFROM SuppliersORDER BY CompanyName;

Code Explanation

This query will return the company name and phone number of all shippers, customers and suppliers.

UNION ALL

By default, all duplicates are removed in UNIONs. To include duplicates, use UNION ALL in place of UNION.

UNION Rules

Each query must return the same number of columns. The columns must be in the same order. Column datatypes must be compatible. In Oracle, you can only ORDER BY columns that have the same name in every SELECT

clause in the UNION.

Exercise: Working with Unions

Duration: 10 to 20 minutes.In this exercise, you will practice using UNION.

1. Create a report showing the contact name and phone numbers for all employees, customers, and suppliers.

Where is the solution?

Subqueries, Joins and Unions Conclusion

In this lesson of the SQL tutorial, you have learned to create reports using data from multiple tables.

To continue to learn SQL go to the top of this page and click on the next lesson in this SQL Tutorial's Table of Contents.