You are on page 1of 19

IOE 

373 – Data Processing

Review
 SELECT statement
SELECT [Fields] FROM [Table(s)] WHERE
SQL Part II [Conditions]
 ORDER BY
 MAX(), MIN(), AVG(), COUNT()
IOE 373 Lecture 05
 GROUP BY

Luis Guzman, University of Michigan 1
IOE 373 – Data Processing

Topics INSERT Statement


 INSERT is used to add rows into a table
 INSERT
 Syntax:
 UPDATE INSERT INTO table (field1, field2, …) VALUES (value1, value2, …)
 DELETE  Example:
 Queries on multiple tables INSERT INTO Students (UMID, SSN, Name, Email) VALUES
(37339830, 334332190, ‘Joseph Woods’, ‘joew@umich.edu’)

UMID* SSN Name Email


37339822 344021945 Edward Jones edjones@umich.edu
37339823 342122843 Steven Hanks shanks@umich.edu
37339824 564231347 Edward Jones edjones2@umich.edu
37339829 473293828 Edward Jones edwardj@umich.edu

New Row 37339830 334332190 Joseph Woods joew@umich.edu

Luis Guzman, University of Michigan 2
IOE 373 – Data Processing

INSERT Statement INSERT Statement


 You can omit some fields
 You can omit the list of field names,
 But not for field(s) that belong to primary key!
 But in this case, you have to include a value
INSERT INTO Students (UMID, Name) VALUES for every field in the order defined by the
(37339830, ‘Joseph Woods’)
table
 This is equivalent to  For example
INSERT INTO Students (UMID, SSN, Name, Email)
INSERT INTO Students
VALUES (37339830, NULL, ‘Joseph Woods’, NULL)
VALUES (37339830, NULL, ‘Joseph Woods’,
UMID* SSN Name Email
NULL)
37339822 344-02-1945 Edward Jones edjones@umich.edu  Is equivalent to the query in previous slide
37339823 342-12-2843 Steven Hanks shanks@umich.edu
37339824 564-23-1347 Edward Jones edjones2@umich.edu
37339829 473-29-3828 Edward Jones edwardj@umich.edu
New Row 37339830 Joseph Woods

Luis Guzman, University of Michigan 3
IOE 373 – Data Processing

UPDATE Statement UPDATE Example


 The UPDATE statement modifies a record in a  Update a record in Student table:
table
UPDATE Students SET
 Syntax:
Name=‘Joseph Woodson’, SSN=‘333-23-3444’
UPDATE table SET field1=value1, WHERE UMID=37339830
field2=value2, … WHERE conditions  Result:
 Warning: UPDATE queries can change lots of
data very quickly, and the results may be UMID* SSN Name Email

irreversible! 37339822 344-02-1945 Edward Jones edjones@umich.edu


37339823 342-12-2843 Steven Hanks shanks@umich.edu
37339824 564-23-1347 Edward Jones edjones2@umich.edu
37339829 473-29-3828 Edward Jones edwardj@umich.edu
37339830 333-23-3444 Joseph Woodson

Luis Guzman, University of Michigan 4
IOE 373 – Data Processing

More Examples
 Without a WHERE clause, UPDATE  Add 5 points to everyone’s grade
commands will change ALL records in a table! UPDATE Grades SET Score = Score + 5
UPDATE Students SET Name=‘Amy’
will make EVERY student have the name ‘Amy’
 Increase the grade by 4% for all students
with grade less than 60
 Whereas
UPDATE Students SET Name=‘Amy’ WHERE  UPDATE Grades SET Score = Score*1.04
UMID=44403200 WHERE Score < 60
Will only change the name of one student

Luis Guzman, University of Michigan 5
IOE 373 – Data Processing

DELETE Statement DELETE Example


 The DELETE statement removes a record  Delete a student with UMID 37339824
from a table DELETE * FROM Students WHERE
UMID=37339824
 Syntax:
 This will delete only one record
DELETE * FROM table WHERE conditions
 But if you use
 Warning: DELETE queries can change lots of DELETE * FROM Students WHERE Name=‘Edward Jones’
data very quickly, and the results may be  This will delete all students named ‘Edward Jones’
irreversible! UMID* SSN Name Email
37339822 344-02-1945 Edward Jones edjones@umich.edu
37339823 342-12-2843 Steven Hanks shanks@umich.edu
37339824 564-23-1347 Edward Jones edjones2@umich.edu
37339829 473-29-3828 Edward Jones edwardj@umich.edu
37339830 333-23-3444 Joseph Woodson

Luis Guzman, University of Michigan 6
IOE 373 – Data Processing

DELETE Statement Query On Multiple Tables


 Use WHERE clause  Now we know how to select data from one
 This will remove all students records: table.
DELETE * FROM Students  But sometimes we need to get data from
 Use primary key as condition if possible multiple tables
 Use INNER JOIN clause to “join” two or
more tables

Luis Guzman, University of Michigan 7
IOE 373 – Data Processing

Example of Two Tables Query Example


• Suppose we want to list employees’ Name, Email
and Department Name with salary >=3000  Basically you need to match up records in
Employees two tables with same DeptID
PersonID PersonName Address Email Phone Salary DeptID

1 Steve 1234 Fuller steve@notexist.com 734-333- 2000 2


Employees
9999 PersonI PersonNam Salar DeptI
Address Email Phone
2 John 234 Huron St. john@notexist.com 734-233- 3000 1 D e y D
8777 1 Steve 1234 Fuller steve@notexist.com 734-333- 2000 2
3 Mary 2489 Stone Road mary@notexist.com 734-876- 4000 1 9999
8888 2 John 234 Huron St. john@notexist.com 734-233- 3000 1
4 Emily 1254 Green road emily@notexist.com 734-233- 2500 2 8777
9089 3 Mary 2489 Stone Road mary@notexist.com 734-876- 4000 1
5 Mike 333 Fifth mike@notexist.com 734-344- 2900 1 8888
0934 4 Emily 1254 Green road emily@notexist.com 734-233- Departments
2500 2
6 James 255 Plymouth james@notexist.co 734-333- 4500 3 9089 DeptI DeptNa Manager
m 4000 Departments 5 Mike 333 Fifth mike@notexist.com 734-344- D 2900 me 1
DeptPhone
ID
DeptID DeptName DeptPhone ManagerID 0934 1 Finance 222-222- 2
6 James 255 Plymouth james@notexist.co 734-333- 4500 3 2233
1 Finance 222-222-2233 2
m 4000 2 Marketing 222-222- 1
2 Marketing 222-222-2333 1
2333
3 IT 222-222-2345 6
3 IT 222-222- 6
2345

Luis Guzman, University of Michigan 8
IOE 373 – Data Processing

Example
 Syntax:
 SELECT Employees.PersonName,
Employees.Email, SELECT Employees.PersonName,
Departments.DepartmentName Employees.Email,
Departments.DeptName
FROM Employees, Departments
FROM Employees INNER JOIN Departments
WHERE
ON Employees.DeptID = Departments.DeptID
Employees.DeptID=Departments.DeptID
WHERE Employees.Salary >= 3000
AND Query Result
Employees.Salary>=3000 PersonName Email DeptName
John john@notexist.com Finance
Mary mary@notexist.com Finance
James james@notexist.com HumanResources

17

Luis Guzman, University of Michigan 9
IOE 373 – Data Processing

What if I want the name of the


Example manager also?
 Since an INNER JOIN query returns fields
from both tables, you must specify which
table each field comes from.
 You do this by prefixing each field name with
the name of the table it comes from, followed
by a period:
Employees.DeptID, Departments.DeptID

20

Luis Guzman, University of Michigan 10
IOE 373 – Data Processing

What if I want the name of the What if I want the name of the
manager also? manager also?
SELECT Employees.PersonName, SELECT Employees.PersonName,
Employees.Email, Departments.DeptName, Employees.Email, Departments.DeptName,
(Select Employees.PersonName from
Employees Where
Employees.PersonID=Departments.Mana
gerID) AS ManagerName

I need to figure out the name of the manager based


on the managerID
This expression in parenthesis is also known as a subquery
21 22

Luis Guzman, University of Michigan 11
IOE 373 – Data Processing

What if I want the name of the


manager also? Example
SELECT Employees.PersonName,  Using INNER JOIN is preferable (more
Employees.Email, Departments.DeptName, efficient) to using WHERE to join two tables:
(Select Employees.PersonName from Employees SELECT Employees.PersonName, Employees.Email,
Where Departments.DeptName
Employees.PersonID=Departments.ManagerID) FROM Employees, Departments
AS ManagerName WHERE Employees.DeptID = Departments.DeptID
AND Employees.Salary >= 3000
FROM Employees INNER JOIN Departments ON
Employees.DeptID = Departments.DeptID Although this query will give the same result, it
WHERE Salary >=3000; is much slower in most database systems

23

Luis Guzman, University of Michigan 12
IOE 373 – Data Processing

Example Other Ways of Joining Tables


 You can use INNER JOIN together with other  Sometimes INNER JOIN is not enough
clauses:
 What if there are “mismatches” between two
SELECT Departments.DeptName, tables?
AVG(Employees.Salary) AS AverageSalary
FROM Employees INNER JOIN Departments Ordinals SpelledValues

ON Employees.DeptID = Departments.DeptID Number OrdinalValue Number SpelledValue


1 First 1 One
GROUP BY Departments.DeptName 2 Second 2 Two
3 Third 5 Five
4 Fourth 6 Six

Luis Guzman, University of Michigan 13
IOE 373 – Data Processing

Joining Tables Other Ways of Joining Tables


 INNER JOIN can only join records with  LEFT JOIN
corresponding field:
 RIGHT JOIN
SELECT * FROM Ordinals INNER JOIN SpelledValues
ON Ordinals.Number = SpelledValues.Number  FULL JOIN
ORDER BY Ordinals.Number

Ordinals SpelledValues

Number OrdinalValue Number SpelledValue


Number Ordina Spelled
1 First 1 One
lValue Value
2 Second 2 Two 1 First One
3 Third 5 Five 2 Second Two
4 Fourth 6 Six

Luis Guzman, University of Michigan 14
IOE 373 – Data Processing

LEFT JOIN RIGHT JOIN


 LEFT JOIN select all records from the left  RIGHT JOIN select all records from the right
table even if there are no matching records in table even if there are no matching records in
the right table the left table
SELECT * FROM Ordinals LEFT JOIN SpelledValues SELECT * FROM Ordinals RIGHT JOIN SpelledValues
ON Ordinals.Number = SpelledValues.Number ON Ordinals.Number = SpelledValues.Number
ORDER BY Ordinals.Number ORDER BY Ordinals.Number
Ordinals SpelledValues Ordinals SpelledValues
Number Ordinal Spelled Number Ordinal Spelled
Number OrdinalValue Number SpelledValue Value Value Number OrdinalValue Number SpelledValue Value Value
1 First 1 One 1 First One 1 First 1 One 1 First One
2 Second 2 Two 2 Second Two 2 Second 2 Two 2 Second Two
3 Third 5 Five 3 Third NULL 3 Third 5 Five 5 NULL Five
4 Fourth 6 Six 4 Fourth NULL 4 Fourth 6 Six 6 NULL Six

Luis Guzman, University of Michigan 15
IOE 373 – Data Processing

FULL JOIN (not in access) Equivalent expression (for Access)

 FULL JOIN select all records from both SELECT Ordinals.*, SpelledValues.*
tables even if there are no matching records FROM Ordinals LEFT JOIN SpelledValues ON
in the other table Ordinals.NumberID = SpelledValues.NumberID
SELECT * FROM Ordinals FULL JOIN SpelledValues UNION
ON Ordinals.Number = SpelledValues.Number
ORDER BY Ordinals.NumberID SELECT Ordinals.*, SpelledValues.*
Ordinals SpelledValues
Number
ID
Ordinal
Value
Spelled
Value FROM SpelledValues LEFT JOIN Ordinals ON
Number OrdinalValue Number SpelledValue
1 First One
SpelledValues.NumberID = Ordinals.NumberID
ID ID 2 Second Two
1 First 1 One 3 Third NULL ORDER BY Ordinals.NumberID;
2 Second 2 Two 4 Fourth NULL
3 Third 5 Five 5 NULL Five
4 Fourth 6 Six 6 NULL Six
32

Luis Guzman, University of Michigan 16
IOE 373 – Data Processing

More Complex INNER JOIN


 If we want to select data from Employees
and EmployeeProject, we may need to join 3
tables together

33

Luis Guzman, University of Michigan 17
IOE 373 – Data Processing

Complex INNER JOIN Example Complex INNER JOIN Example


 If we want to see which employee is working on SELECT Employees.PersonName, Project.ProjectName,
which project, and how many hours they EmployeeProject.WorkHours
contribute to the project FROM (Employees INNER JOIN EmployeeProject
ON
Employees.PersonID=EmployeeProject.EmployeeID)
INNER JOIN Project
ON EmployeeProject.ProjectID=Project.ProjectID

Luis Guzman, University of Michigan 18
IOE 373 – Data Processing

 We first join the Employees and EmployeeProject  Result:


table:
(Employees INNER JOIN EmployeeProject
ON
Employees.PersonID=EmployeeProject.EmployeeID)

Then join the result with Project table:


(…) INNER JOIN Project
ON EmployeeProject.ProjectID=Project.ProjectID

Luis Guzman, University of Michigan 19

You might also like