You are on page 1of 21

Bruin Actuarial Society | University of California, Los Angeles | 2017

Microsoft Access / SQL


Tutorial
Part I
Presented by Bruin Actuarial Society in Winter 2017 as
part of the Technical Series Workshops at UCLA

ManYee Yu Brittani Kellogg


President Vice President
Dan Sui
Greta Xiong Cullen Im Annie Thornton
Director of Professional
Corporate Liaison Corporate Liaison Director of Finance
Development
Bruin Actuarial Society | University of California, Los Angeles | 2017

First Things First

SQL (pronounced sequel) is a language that we use to interact with databases


SQL stands for Structured Query Language
What’s a database?
A database is a set of data tables (spreadsheets with rows and columns). Imagine an
Excel workbook on steroids.

Data Storage Systems

Data Storage System: A structure used to add, delete, update and retrieve data
SQL statements are entered into a computer to communicate with a database
The statement is delivered to and executed by the server
Oracle Database and Teradata are examples of SQL platforms used in industry
We’ll be using Microsoft Access

Our Business

Let’s say the whole actuary thing doesn’t work out for any of us. Our backup? Being
an accountant? Nah. Opening our own café.
Any business has lots of data to keep track of, and a database is the way to do it
Open CaféDatabase.accdb
Your job: Every business has a database, and every database has a manager.

Overview of Access
List of All Access Objects on the left (Tables, Queries, Forms and Reports)
Double click an object to open
Once an object is open, you can right click on its tab to change View
Tables have two views: Datasheet and Design
Queries have three views: Datasheet, Design and SQL

ManYee Yu Brittani Kellogg


President Vice President
Dan Sui
Greta Xiong Cullen Im Annie Thornton
Director of Professional
Corporate Liaison Corporate Liaison Director of Finance
Development
Bruin Actuarial Society | University of California, Los Angeles | 2017

The MENU table contains information on the items and drinks we’ll be selling.

ManYee Yu Brittani Kellogg


President Vice President
Dan Sui
Greta Xiong Cullen Im Annie Thornton
Director of Professional
Corporate Liaison Corporate Liaison Director of Finance
Development
Bruin Actuarial Society | University of California, Los Angeles | 2017

The EMPLOYEE table contains info on our employees.

ManYee Yu Brittani Kellogg


President Vice President
Dan Sui
Greta Xiong Cullen Im Annie Thornton
Director of Professional
Corporate Liaison Corporate Liaison Director of Finance
Development
Bruin Actuarial Society | University of California, Los Angeles | 2017

The LOCATIONS table contains info on each of the locations we have opened
across the country.

The SELECT Statement

SELECT is used to recall data from the data tables.


SELECT ItemName, Price, Calories FROM Menu;
ItemName, Price and Calories are columns in the Menu table

ManYee Yu Brittani Kellogg


President Vice President
Dan Sui
Greta Xiong Cullen Im Annie Thornton
Director of Professional
Corporate Liaison Corporate Liaison Director of Finance
Development
Bruin Actuarial Society | University of California, Los Angeles | 2017

SELECT * FROM Menu;


The asterisk represents all columns.

SELECT DISTINCT Type FROM Menu;


This shows us all the different kinds of items found on our menu.

ManYee Yu Brittani Kellogg


President Vice President
Dan Sui
Greta Xiong Cullen Im Annie Thornton
Director of Professional
Corporate Liaison Corporate Liaison Director of Finance
Development
Bruin Actuarial Society | University of California, Los Angeles | 2017

The WHERE Clause

The WHERE clause is attached to SELECT statements to limit the rows from which
the computer will recall data.
For health-conscious hipsters who want to watch their caloric intake,
SELECT ItemName, Calories FROM Menu WHERE Calories < 200;

Let’s say this hipster is broke, too.


SELECT ItemName, Calories, Price FROM Menu WHERE Calories < 200 AND
Price < 2.50;
OR is also valid to use.

ManYee Yu Brittani Kellogg


President Vice President
Dan Sui
Greta Xiong Cullen Im Annie Thornton
Director of Professional
Corporate Liaison Corporate Liaison Director of Finance
Development
Bruin Actuarial Society | University of California, Los Angeles | 2017

For customers who want an espresso and a baked good,


SELECT ItemName, Price FROM Menu WHERE Type IN (‘Espresso’ , ‘Baked’);
NOT IN is also valid to use.

The KEY to it all

Every data table has a column whose entries serve as the PRIMARY KEY of the table
Every row will have a unique PRIMARY KEY.
Our student ID numbers can be thought of as primary keys for UCLA students.
SELECT * FROM Menu;

ItemNo is the primary key for the Menu table


ManYee Yu Brittani Kellogg
President Vice President
Dan Sui
Greta Xiong Cullen Im Annie Thornton
Director of Professional
Corporate Liaison Corporate Liaison Director of Finance
Development
Bruin Actuarial Society | University of California, Los Angeles | 2017

A FOREIGN concept

Tables may also have Foreign Keys; these make references to rows in other tables
SELECT * FROM Locations;

Manager is a foreign key in Locations referring to ItemNo in Employees

SELECT * FROM Employees;

Foreign Keys cannot point to nothing! A Manager in Locations cannot be a number


that is not in EmpNo in Employees. This is called referential integrity.
ManYee Yu Brittani Kellogg
President Vice President
Dan Sui
Greta Xiong Cullen Im Annie Thornton
Director of Professional
Corporate Liaison Corporate Liaison Director of Finance
Development
Bruin Actuarial Society | University of California, Los Angeles | 2017

NULL and void

Null values are cells that are blank in the data table
They aren’t the same as zeros or blank spaces!
Primary Keys cannot be null

SELECT ItemName, Type, Price, Calories FROM Menu WHERE Type = ‘Espresso’;
The Dark Chocolate Latte is a new item; we haven’t set its price yet.
It’s a null value (different from $0.00, or free!)

But we can arrange for null values to be treated as zeros


SELECT ItemName, Type, NZ(Price, 0) AS Price2, Calories FROM Menu
WHERE Type = ‘Espresso’;

ManYee Yu Brittani Kellogg


President Vice President
Dan Sui
Greta Xiong Cullen Im Annie Thornton
Director of Professional
Corporate Liaison Corporate Liaison Director of Finance
Development
Bruin Actuarial Society | University of California, Los Angeles | 2017

Smooth Operators

Coffee beans just became more expensive due to war in <insert random country>.
SELECT ItemName, Price AS OldPrice, 1.15*Price AS NewPrice FROM Menu
WHERE Type IN (‘Coffee’, ‘Iced’);

The arithmetic operators: + - * / %


The comparison operators = > >= < <= can be used in the WHERE clause
Fields can be concatenated with the + sign.
SELECT FirstName + ‘ ‘ + LastName AS Name FROM Employees;

For all employees whose last names start with the letter J
SELECT FirstName, LastName FROM Employees where LastName LIKE ‘J*’;

* represents any combination of letters


? represents a single letter

ManYee Yu Brittani Kellogg


President Vice President
Dan Sui
Greta Xiong Cullen Im Annie Thornton
Director of Professional
Corporate Liaison Corporate Liaison Director of Finance
Development
Bruin Actuarial Society | University of California, Los Angeles | 2017

Functions

We can concatenate strings using the ampersand:


SELECT FirstName & ‘ ‘ & LastName AS Name FROM Employees;

We can extract substrings:


SELECT MID(FirstName,1,3) AS Abbrev, LastName, Title FROM Employees;

We can find the number of characters in a string:


SELECT FirstName, LEN(FirstName) as LengthofFirstName FROM Employees;

Et cetera:
INSTR(‘MISSISSIPPI’,’S’) = 3
ROUND (45.926 , 2) = 45.93 1600 MOD 300 = 100

ManYee Yu Brittani Kellogg


President Vice President
Dan Sui
Greta Xiong Cullen Im Annie Thornton
Director of Professional
Corporate Liaison Corporate Liaison Director of Finance
Development
Bruin Actuarial Society | University of California, Los Angeles | 2017

On a Case-by-case Basis

SQL has Case-Conversion Functions


UCASE(‘bob’) = “BOB’
LCASE(‘BOB’) = ‘bob’
StrConv(‘boB’,3) = ‘Bob’ (StrConv works like UCASE is the second parameter is a
1 and LCASE if it is a 2)

A GROUP Effort

SQL features group functions, i.e., functions that operate across multiple rows.
SELECT Title, AVG (PayRate) AS AveragePayRate FROM Employees GROUP
BY Title;

COUNT (), MAX (), MIN (), SUM () are also group functions.

HAVING a good time yet?

To restrict the groups that we select, we use the HAVING clause. (The WHERE
clause is not permitted with GROUP BY.)
SELECT Type, AVG (Calories) AS AverageCalories FROM Menu GROUP BY
Type HAVING Type IN ('Coffee','Espresso','Iced','Hot Tea');

ManYee Yu Brittani Kellogg


President Vice President
Dan Sui
Greta Xiong Cullen Im Annie Thornton
Director of Professional
Corporate Liaison Corporate Liaison Director of Finance
Development
Bruin Actuarial Society | University of California, Los Angeles | 2017

That’s all for tonight, folks.

Part II of the Microsoft Access/SQL Tutorial will cover the following topics:
1. Inner, left, right, and outer joins
2. Switch statements
3. Subqueries
4. How to create, update and delete tables, columns and rows

See you on Thursday!

ManYee Yu Brittani Kellogg


President Vice President
Dan Sui
Greta Xiong Cullen Im Annie Thornton
Director of Professional
Corporate Liaison Corporate Liaison Director of Finance
Development
Bruin Actuarial Society | University of California, Los Angeles | 2017

Microsoft Access / SQL


Tutorial
Part II
Presented by Bruin Actuarial Society in Winter 2017 as
part of the Technical Series Workshops at UCLA

ManYee Yu Brittani Kellogg


President Vice President
Dan Sui
Greta Xiong Cullen Im Annie Thornton
Director of Professional
Corporate Liaison Corporate Liaison Director of Finance
Development
Bruin Actuarial Society | University of California, Los Angeles | 2017

JOINing Together

Use joins to display data from more than one data table.
Let’s say that we need employee names together with their location names
(The Employee table contains location numbers, but not location names)
SELECT * FROM Employees INNER JOIN Locations ON Employees.LocationNo
= Locations.LocationNo;

Why do we use dot notation? To prevent ambiguity, the table name must accompany
column names that appear in multiple tables.

ManYee Yu Brittani Kellogg


President Vice President
Dan Sui
Greta Xiong Cullen Im Annie Thornton
Director of Professional
Corporate Liaison Corporate Liaison Director of Finance
Development
Bruin Actuarial Society | University of California, Los Angeles | 2017

Putting the Cartesian Product before the Horse

Why do we have to use INNER JOIN? Let’s see what happens if we don’t.
SELECT * FROM Employees, Locations;

Whoa! What happened? To the server, FROM Employees, Locations denotes the
Cartesian Product of these two tables. Every row in Employee is matched with every
row in Location, but we only need the rows where LocationNo matches.

Venn Aquí

In a normal inner join, what gets excluded? Locations without managers and
employees without a location.

How do we show the new Location for which we don’t have a manager yet?
SELECT * FROM Locations LEFT JOIN Employees on Employees.EmpNo =
Locations.Manager;

ManYee Yu Brittani Kellogg


President Vice President
Dan Sui
Greta Xiong Cullen Im Annie Thornton
Director of Professional
Corporate Liaison Corporate Liaison Director of Finance
Development
Bruin Actuarial Society | University of California, Los Angeles | 2017

LEFT JOIN means that Locations without corresponding Employees (Managers) will
still be included. RIGHT JOIN also works.

For a full outer join:


SELECT * FROM Locations LEFT JOIN Employees on Employees.EmpNo =
Locations.Manager UNION SELECT * FROM Locations RIGHT JOIN
Employees on Employees.EmpNo = Locations.Manager;

If you’re confused as to why these joins are called LEFT, RIGHT, INNER and outer,
imagine a Venn diagram.

Switching it Up

The SWITCH statement works like an IF-ELSE statement.


Let’s say there has been upheaval in the commodities markets. Coffee prices must
rise by 5%, and baked goods prices by 7%.
SELECT ItemName, Type, Price AS OldPrice, SWITCH(Type = 'Coffee', Price*1.05,
Type = 'Iced', Price*1.05, Type = 'Baked', Price*1.07) AS NewPrice FROM Menu;

ManYee Yu Brittani Kellogg


President Vice President
Dan Sui
Greta Xiong Cullen Im Annie Thornton
Director of Professional
Corporate Liaison Corporate Liaison Director of Finance
Development
Bruin Actuarial Society | University of California, Los Angeles | 2017

SUBQUERY

Subqueries are SELECT statements in the WHERE clause of another SELECT


statement. What drinks have higher prices than a Danish? Is a query. Well, what’s
the price of a Danish? <— This is the subquery.
SELECT ItemName, Price FROM Menu WHERE Price >= (SELECT Price
FROM Menu WHERE ItemName = 'Danish');

ManYee Yu Brittani Kellogg


President Vice President
Dan Sui
Greta Xiong Cullen Im Annie Thornton
Director of Professional
Corporate Liaison Corporate Liaison Director of Finance
Development
Bruin Actuarial Society | University of California, Los Angeles | 2017

Back to Square One: Creating Database Objects

CREATE TABLE Employees2(


EmpNo INTEGER,
FirstName TEXT,
LastName TEXT,
Title TEXT,
PayRate CURRENCY,
Location BYTE,
CONSTRAINT Employees_EmpNo_pk PRIMARY KEY (EmpNo),
CONSTRAINT Employees_LocationNo_fk FOREIGN KEY (Location)
REFERENCES Locations (LocationNo)
);

DML Statements: Database Manipulation Language

We’re opening a new location! Let’s add it to our Locations table.


INSERT INTO Locations (LocationNo, LocationName, County, State, ZIP)
VALUES (9, "San_Francisco" , "San_Francisco" , "CA" , 94103);

ManYee Yu Brittani Kellogg


President Vice President
Dan Sui
Greta Xiong Cullen Im Annie Thornton
Director of Professional
Corporate Liaison Corporate Liaison Director of Finance
Development
Bruin Actuarial Society | University of California, Los Angeles | 2017

We hired a manager for this new location. We need to add him to our roster of
employees.
INSERT INTO Employees VALUES (1031, “John” , “Doe” , “Manager” , 22 , 9);

UPDATE Locations SET Manager = 1031 WHERE LocationNo = 9;

A Philz Coffee opens next door and takes all of our customers, and we have to close.
DELETE FROM Locations WHERE LocationNo = 9;

FIN

ManYee Yu Brittani Kellogg


President Vice President
Dan Sui
Greta Xiong Cullen Im Annie Thornton
Director of Professional
Corporate Liaison Corporate Liaison Director of Finance
Development

You might also like