Introduction To Structured Query Language (SQL) Topics: IOE 373 - Data Processing

You might also like

Download as pdf or txt
Download as pdf or txt
You are on page 1of 19

IOE 

373 – Data Processing

Topics
Introduction to
Structured Query Language  SELECT Queries
(SQL)  WHERE Clause
 ORDER BY
 GROUP BY
IOE 373 Lecture 04

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

Intro to SQL
Getting Information Out of Database SQL
 Now we have the database  SQL - Structured Query Language
 How do we actually use the data?  It can be pronounced “Sequel” or “S-Q-L”.
 How can we retrieve what we want from  It was originally developed in the early 1970s
database? by IBM as a way to manipulate and retrieve
data stored in IBM’s relational DBMS
 Now the standard query language for
relational databases
 SQL became a standard of the American National
Standards Institute (ANSI) in 1986, and of the
International Organization for Standardization
(ISO) in 1987

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

What SQL Can Do SELECT Statement


 Basic Syntax:
 Most commonly used to retrieve data from a  SELECT [Fields] FROM [Table(s)]
database (SELECT)  SELECT [Fields] FROM [Table(s)] WHERE
 Also commonly used to modify data. [Conditions] ORDER BY [Fields] ASC/DESC
(INSERT, UPDATE, DELETE)  Example:
 SQL also contains commands for creating  SELECT ItemID, ItemName FROM ItemList
tables and other database objects (CREATE)  Use (*) to select all fields
 And for maintaining data security through  SELECT * FROM Students
granting and denying privileges to users.

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

Using SQL How SELECT Statements Work…


• SELECT * FROM Students  SELECT UMID, StName FROM Students2
Students
UMID StName Email Gender
10000001 Steve steve@notexist.com M
UMID StName Email Gender UMID StName
10000002 John john@notexist.com M
10000001 Steve steve@notexist.com M 10000001 Steve
10000003 Mary mary@notexist.com F
10000002 John john@notexist.com M 10000002 John
10000004 Emily emily@notexist.com F
10000003 Mary mary@notexist.com F 10000003 Mary
10000005 Mike mike@notexist.com M
10000004 Emily emily@notexist.com F 10000004 Emily
10000006 James james@notexist.com M
UMID StName Email Gender 10000005 Mike mike@notexist.com M 10000005 Mike
10000001 Steve steve@notexist.com M 10000006 James james@notexist.com M 10000006 James
10000002 John john@notexist.com M
10000003 Mary mary@notexist.com F
10000004 Emily emily@notexist.com F
10000005 Mike mike@notexist.com M
10000006 James james@notexist.com M

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

SELECT Statement SELECT Statement


 Using alias in Fields • Concatenates two fields
SELECT StName AS StudentName SELECT StName + ' ' + StLastName AS Name
FROM Students2
FROM Students2
Students2 Students2
UMID StName StLastName Email Gender UMID StName StLastName Email Gender
10000001 Steve Martin steve@notexist.co M 10000001 Steve Martin steve@notexist.co M
m m
10000002 John Legend john@notexist.com M 10000002 John Legend john@notexist.com M
10000003 Mary Smith mary@notexist.co F 10000003 Mary Smith mary@notexist.co F
m m
10000004 Emily Lake emily@notexist.co F 10000004 Emily Lake emily@notexist.co F
m m
10000005 Mike Jones mike@notexist.co M 10000005 Mike Jones mike@notexist.co M
m m
10000006 James Dee james@notexist.co M 10000006 James Dee james@notexist.co M
m m

10

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

SELECT Statement SELECT Statement


• Calculated fields:  SELECT * FROM table is a good way to look at
SELECT CarID, KM/1.61 AS Miles
the entire table
FROM CarData  However, it will take long time if the database is
CarData large
 When writing SQL query, use specific Field
names rather than * whenever it’s possible

11

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

WHERE Clause How SELECT Statements Work…


 Use WHERE clause to only select rows that satisfy
some conditions  SELECT UMID, StName FROM Students2
WHERE Gender = "F"
 Syntax:
 SELECT [Fields] FROM [Table(s)] WHERE [Conditions] UMID StName Email Gender

 Example 10000001
10000002
Steve
John
steve@notexist.com
john@notexist.com
M
M UMID StName
 SELECT * FROM Students WHERE Gender = 'F' 10000003 Mary mary@notexist.com F 10000003 Mary
 SELECT Name FROM students WHERE Gender = 'F' 10000004 Emily emily@notexist.com F 10000004 Emily
PersonID Name Gender Email 10000005 Mike mike@notexist.com M

1 Tom M tom@umich.edu 10000006 James james@notexist.com M

2 Amy F amy@umich.edu
3 Kate F kate@umich.edu
4 Edward M edward@umich.edu
5 Matt M matt@umich.edu

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

SELECT Statement WHERE Clause


1. Pick the columns you specified right after  WHERE Clause can also be used in other SQL
commands, such as DELETE, UPDATE, INSERT
SELECT
 You can use AND, OR, NOT, and parentheses to
2. For each row, check conditions in WHERE change the order of operation.
clause  Examples:
WHERE PlayerID = 7
Display results that show up in both 1 and 2

3.
 WHERE TeamID=2 AND Age>24
 WHERE NOT (TeamID=1 AND Age<=30)
UMID StName Email Gender
 WHERE Age>20 OR Age<30 [Will affect all records]
10000001 Steve steve@notexist.com M
 WHERE Age <20 AND Age>30 [Won’t affect any
10000002 John john@notexist.com M record]
10000003 Mary mary@notexist.com F
10000004 Emily emily@notexist.com F
10000005 Mike mike@notexist.com M
10000006 James james@notexist.com M

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

BETWEEN and IN in WHERE


clauses ORDER BY Clause
 BETWEEN and IN can be used to simplify WHERE clauses.  By default, the query result is not sorted
BETWEEN combines two inequalities:

 To sort the result, use ORDER BY Clause:
WHERE Age BETWEEN 24 AND 27
Is the same as SELECT * FROM Players ORDER BY Age ASC
WHERE Age>=24 AND Age<=27  ASC stands for “Ascending” order, DESC is
BETWEEN is always inclusive; that is, it includes the the opposite
endpoints.
 Now youngest players is on top of the list,
IN combines multiple Ors:
with the oldest at the bottom

WHERE TeamID IN (2,3,5,8)


Is the same as  ASC is the default if you don’t specify ASC or
WHERE TeamID=2 OR TeamID=3 OR TeamID=5 OR DESC
TeamID=8

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

ORDER BY Clause ORDER BY Clause


 You can have more than one fields in ORDER  You can also sort two fields with different
BY Clause orders
 SELECT LastName, FirstName FROM Players  SELECT * FROM Players ORDER BY
ORDER BY LastName, FirstName TeamID ASC, Age DESC
 First sorted by LastName, then FirstName
(tie-breaker)  This query will display all members of team 1
before all members of team 2, but within
 Players with the same last names, those with
first names beginning with A will be listed each team, the oldest players will be listed
before those beginning with B first

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

TOP Clause UNION Operator


 The UNION operator combines the results of multiple
 Use TOP clause to select only the first of SELECT statements into one result.
several records selected  List all employees and managers with names starting
SELECT TOP 5 LastName, FirstName, GPA FROM with 'Z'
Students ORDER BY GPA DESC (SELECT Name, Salary FROM Employees
WHERE name LIKE 'Z*')
 This will return the best 5 students in class UNION
 Don’t use this: (SELECT Name, Bonus FROM Managers
WHERE name LIKE 'Z*')
SELECT TOP 5 LastName, FirstName, GPA FROM  List all cities with names starting with 'Z' or 'Y'
Students ORDER BY GPA (SELECT CityName, Population FROM City WHERE CityName LIKE 'Z*')
 This will only select the worst 5 students. UNION
(SELECT CityName, Population FROM City WHERE CityName LIKE 'Y*')

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

UNION Operator Aggregate Data


 Remark: one important rule for UNION  SELECT MAX(Age) AS MaxAge FROM
operation is that the queries’ fields must Players
match.  Returns the age of oldest players (one row, one
 Each query must return the same number of column result)
fields and corresponding fields must have  SELECT AVG(Age) AS AvgAge FROM
compatible data types Players
 In previous example, the first column (of both  Average age of players.
queries) is text type, and the second column  SELECT COUNT(*) AS NumOldPlayer
is number type, so the two queries are FROM Players WHERE Age >= 30
compatible  Number of players age 30 or older

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

Aggregate Data AAA: Always Alias Aggregates


 MIN: Find the minimum value (or the earliest date for dates) in  If your query says
the specified field in the rows meeting the conditions.
 MAX: Find the maximum value (or the latest date for dates) in  SELECT MAX(Age) FROM Players
the specified field in the rows meeting the conditions.
 SUM: Add the values in the specified field in the rows meeting
 Access will return the following:
the conditions.
 AVG: Find the mean value of the specified field in the rows
meeting the conditions.
 STDEV: Find the standard deviation of the values of the
specified field in the rows meeting the conditions.  Access doesn’t have a name for MAX(Age), so it makes
 COUNT: Find out how many rows there are which meet the up one: “Expr1000”
conditions. For this course, always use COUNT(*), not
COUNT(SomeField). They generally return the same numbers,  You should always alias aggregate with a meaningful
but doing COUNT on a specific field may return a smaller name.
number if that field contains NULLs  SELECT MAX(Age) AS MaxAge FROM Players

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

GROUP BY GROUP BY
 Consider this table called Workers
 The GROUP BY clause tells the database how
to group results contained in aggregate
functions

•If you want to know the maximum age of all workers, you use this query:
•SELECT MAX(Age) AS MaxAge FROM Workers
•which will return
MaxAge
64

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

GROUP BY Another GROUP BY Example


 But if you want to know the maximum age by
JobType, use GROUP BY:  If you want to know how many men and
women there are in the list:
 SELECT JobType, MAX(Age) AS MaxAge
FROM Workers GROUP BY JobType  SELECT Sex, COUNT(*) AS Num FROM
Workers GROUP BY Sex
 Which returns
JobType MaxAge
Clerical 44
Sex Num
Management 51
F 5
Other 55
Professional 64 M 14
Sales 22
Service 24

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

And Another One MAX/MIN vs. TOP


 SELECT JobType, Sex, COUNT(*) AS Num
 Above, we saw the results of this query:
FROM Workers GROUP BY JobType, Sex
 SELECT MAX(Age) AS MaxAge
JobType Sex Num FROM Workers
Clerical F 2
MaxAge
Clerical M 1
Management M 2 64
Other F 1 However, we can get the same result using TOP:
Other M 4 SELECT TOP 1 Age FROM Workers ORDER BY Age DESC
Professional F 1 In addition, we can find out everything about the oldest worker using TOP,
Professional M 3 something we can’t do with MAX:
Sales M 2 SELECT TOP 1 * FROM Workers ORDER BY Age DESC
Service F 1
WorkerID YearsOfSchool Sex YearsWorking UnionMember Wage Age JobType
Service M 2 1485 18 F 40 No 22.2 64 Professional

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

Combining TOP and Aggregates Using SQL In Access


If you want to find one maximum or minimum and want to

know something about the record that possesses that value, use  Open the EmployeeInfo Database
TOP. In most cases, it will be preferable to MAX or MIN. (downloadable from Canvas)
 However, if you want to calculate maximum or minimums by
categories, use MAX and MIN along with GROUP BY. You can
even combine TOP with GROUP BY:
 SELECT TOP 3 JobType, AVG(Wage) AS AvgWage FROM
Workers GROUP BY JobType ORDER BY AVG(Wage) DESC

JobType AvgWage
Management 18.695
Professional 16.2225
Other 9.388

 This tells us the three highest paying job categories


and the corresponding average wages.

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

SQL In Access Enter SQL Mode


 Click “Query Design” , Select tables  Click “SQL” Button at the bottom right

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

Type In SQL Query


 Type in your SQL query and click Run  You will see the result of the query

Luis Guzman, University of Michigan 19

You might also like