Professional Documents
Culture Documents
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
The MENU table contains information on the items and drinks we’ll be selling.
The LOCATIONS table contains info on each of the locations we have opened
across the country.
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;
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;
A FOREIGN concept
Tables may also have Foreign Keys; these make references to rows in other tables
SELECT * FROM Locations;
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!)
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’);
For all employees whose last names start with the letter J
SELECT FirstName, LastName FROM Employees where LastName LIKE ‘J*’;
Functions
Et cetera:
INSTR(‘MISSISSIPPI’,’S’) = 3
ROUND (45.926 , 2) = 45.93 1600 MOD 300 = 100
On a Case-by-case Basis
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.
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');
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
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.
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;
LEFT JOIN means that Locations without corresponding Employees (Managers) will
still be included. RIGHT JOIN also works.
If you’re confused as to why these joins are called LEFT, RIGHT, INNER and outer,
imagine a Venn diagram.
Switching it Up
SUBQUERY
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);
A Philz Coffee opens next door and takes all of our customers, and we have to close.
DELETE FROM Locations WHERE LocationNo = 9;
FIN