Basic SQL
Retrieval Queries
Chapter 4
Fall 2014
1992-2014 by Addison Wesley & Pearson Education, Inc., McGraw Hill, Cengage Learning
Slides adapted and modified from Fundamentals of Database Systems (5/6) (Elmasri et al.), Database System
Concepts (5/6) (Silberschatz et al.), Database Systems (Coronel et al.), Database Systems (4/5) (Connolly et al. )
Announcements
Assignment 1
Due Monday October 6th, 2014
Solutions will be posted on midnight
Lecture 5 Exercise
Solutions posted LEARN
Content
Lecture 5
1992-2014 by Addison Wesley & Pearson Education, Inc., McGraw Hill, Cengage Learning
Slides adapted and modified from Fundamentals of Database Systems (5/6) (Elmasri et al.), Database System
Concepts (5/6) (Silberschatz et al.), Database Systems (Coronel et al.), Database Systems (4/5) (Connolly et al. )
<
<=
>
>=
<>
WHERE clause specifies the Boolean condition that must be true for
any retrieved tuple
Also known as selection condition
1992-2014 by Addison Wesley & Pearson Education, Inc., McGraw Hill, Cengage Learning
Slides adapted and modified from Fundamentals of Database Systems (5/6) (Elmasri et al.), Database System
Concepts (5/6) (Silberschatz et al.), Database Systems (Coronel et al.), Database Systems (4/5) (Connolly et al. )
1992-2014 by Addison Wesley & Pearson Education, Inc., McGraw Hill, Cengage Learning
Slides adapted and modified from Fundamentals of Database Systems (5/6) (Elmasri et al.), Database System
Concepts (5/6) (Silberschatz et al.), Database Systems (Coronel et al.), Database Systems (4/5) (Connolly et al. )
EMPLOYEE
Lname
Name
EMPLOYEE
Dno
Dnumber
DEPARTMENT
Dname
Name
To prevent ambiguity, Q1 would be rephrased. We must prefix attributes Name and
Dnumber as shown below:
Fully qualified attribute names can be used for clarity even if there is no ambiguity in
attribute names
1992-2014 by Addison Wesley & Pearson Education, Inc., McGraw Hill, Cengage Learning
Slides adapted and modified from Fundamentals of Database Systems (5/6) (Elmasri et al.), Database System
Concepts (5/6) (Silberschatz et al.), Database Systems (Coronel et al.), Database Systems (4/5) (Connolly et al. )
Selecting Columns
How to Select All Columns
To retrieve all the attribute
values of the selected tuples,
we use an asterisk (*)
Example
SELECT *
FROM CarRentals
CarRentals Table
City
Year
Chicago
2011
CarsRented
567
Los Angles
2010
1540
Los Angles
2011
1320
Miami
2010
512
Miami
2011
987
Miami
2009
1054
New Jersey
2010
987
New York
2011
1021
10
1992-2014 by Addison Wesley & Pearson Education, Inc., McGraw Hill, Cengage Learning
Slides adapted and modified from Fundamentals of Database Systems (5/6) (Elmasri et al.), Database System
Concepts (5/6) (Silberschatz et al.), Database Systems (Coronel et al.), Database Systems (4/5) (Connolly et al. )
Selecting Columns
How to Select Specific Columns
To retrieve specific columns,
each column name must be
specified after the SELECT
keyword
CarRentals Table
City
Example
SELECT City, CarsRented
FROM CarRentals
CarsRented
Chicago
567
Los Angles
1540
Los Angles
1320
Miami
512
Miami
987
Miami
1054
New Jersey
987
New York
1021
11
Selecting Columns
How to Select Computed Columns
To retrieve specific columns, each column name must be
specified after the SELECT keyword
multiple columns are separated by commas
Example
SELECT City, Year, CarsRented, (CarsRented * 10)
FROM CarRentals
CarRentals Table
City
Year
CarsRented
Chicago
2011
567
CarsRented * 10
5670
Los Angles
2010
1540
15400
Los Angles
2011
1320
13200
Miami
2010
512
5120
Miami
2011
987
9870
Miami
2009
1054
10540
New Jersey
2010
987
9870
New York
2011
1021
10210
12
1992-2014 by Addison Wesley & Pearson Education, Inc., McGraw Hill, Cengage Learning
Slides adapted and modified from Fundamentals of Database Systems (5/6) (Elmasri et al.), Database System
Concepts (5/6) (Silberschatz et al.), Database Systems (Coronel et al.), Database Systems (4/5) (Connolly et al. )
Selecting Rows
Selecting ALL Rows
A SELECT statement that does not have a WHERE clause
will return ALL rows
City
Example
SELECT *
FROM CarRentals
Year
CarsRented
Chicago
2011
567
Los Angles
2010
1540
Los Angles
2011
1320
Miami
2010
512
Miami
2011
987
Miami
2009
1054
New Jersey
2010
987
New York
2011
1021
CarRentals Table
13
Selecting Rows
Selecting Specific Rows
To select specific rows, the WHERE clause is required
WHERE filters which rows to be returned based on a specified
condition (i.e. search strategy)
1992-2014 by Addison Wesley & Pearson Education, Inc., McGraw Hill, Cengage Learning
Slides adapted and modified from Fundamentals of Database Systems (5/6) (Elmasri et al.), Database System
Concepts (5/6) (Silberschatz et al.), Database Systems (Coronel et al.), Database Systems (4/5) (Connolly et al. )
Selecting Rows
Selecting Specific Rows
Partial Matching Search (WHERE)
Example 1
SELECT *
FROM CarRentals
WHERE City = Miami
City
Year
CarsRented
Miami
2010
512
Miami
2011
987
Miami
2009
1054
Example 2
SELECT *
FROM CarRentals
WHERE City = Miami
AND Year > 2010
City
Year
CarsRented
Miami
2011
987
15
Selecting Rows
Selecting Specific Rows
Range Search (BETWEEN)
Example 1
SELECT *
FROM CarRentals
WHERE Year >= 2009
AND YEAR <= 2010
Example 2
SELECT *
FROM CarRentals
WHERE Year
BETWEEN 2009 AND 2010
City
Year
CarsRented
Los Angles
2010
1540
Miami
2010
512
Miami
2009
1054
New Jersey
2010
987
16
1992-2014 by Addison Wesley & Pearson Education, Inc., McGraw Hill, Cengage Learning
Slides adapted and modified from Fundamentals of Database Systems (5/6) (Elmasri et al.), Database System
Concepts (5/6) (Silberschatz et al.), Database Systems (Coronel et al.), Database Systems (4/5) (Connolly et al. )
Selecting Rows
Selecting Specific Rows
Pattern Matching Search (%..% and LIKE)
LIKE comparison operator
Example 1
SELECT *
FROM CarRentals
WHERE City LIKE N%
Example 2
SELECT *
FROM CarRentals
WHERE City LIKE %es
City
Year
New Jersey
2010
CarsRented
987
New York
2011
1021
City
Year
CarsRented
Los Angles
2010
1540
Los Angles
2011
1320
17
Selecting Rows
Removing Duplicate Rows
SQL does not automatically eliminate duplicate tuples in
query results
Use the keyword DISTINCT in the SELECT clause only
Example 1: SELECT City
FROM CarRentals
City
Chicago
Los Angles
Los Angles
Miami
Miami
Miami
New Jersey
New York
1992-2014 by Addison Wesley & Pearson Education, Inc., McGraw Hill, Cengage Learning
Slides adapted and modified from Fundamentals of Database Systems (5/6) (Elmasri et al.), Database System
Concepts (5/6) (Silberschatz et al.), Database Systems (Coronel et al.), Database Systems (4/5) (Connolly et al. )
Selecting Rows
Aliasing and Renaming Columns
Ambiguity of attribute names arises in the case of queries that
refer to the same relation twice
Solution: Use aliasing or renaming
Example
SELECT City, Year, CarsRented As Rented , (CarsRented * 10)
As CarsStats
FROM CarRentals
CarRentals Table
City
Year
Rented
Chicago
2011
567
CarsStats
5670
Los Angles
2010
1540
15400
Los Angles
2011
1320
13200
Miami
2010
512
5120
Miami
2011
987
9870
Miami
2009
1054
10540
New Jersey
2010
987
9870
New York
2011
1021
10210
Sorting Rows
ORDER BY Clause
ORDER BY clause
SQL allows the user to order the tuples in the result of a query by the
values of one or more of the attributes (that appear in the query result)
Example 1: SELECT *
FROM CarRentals
ORDER BY City ASC
City
Year
CarsRented
Chicago
2011
567
Los Angles
2010
1540
Los Angles
2011
1320
Miami
2010
512
Miami
2011
987
Miami
2009
1054
New Jersey
2010
987
New York
2011
1021
Example 2: SELECT *
FROM CarRentals
ORDER BY City ASC, Year DESC
City
Year
Chicago
2011
CarsRented
567
Los Angles
2011
1320
Los Angles
2010
1540
Miami
2011
987
Miami
2010
512
Miami
2009
1054
New Jersey
2010
987
New York
2011
1021
1992-2014 by Addison Wesley & Pearson Education, Inc., McGraw Hill, Cengage Learning
Slides adapted and modified from Fundamentals of Database Systems (5/6) (Elmasri et al.), Database System
Concepts (5/6) (Silberschatz et al.), Database Systems (Coronel et al.), Database Systems (4/5) (Connolly et al. )
20
10
SET Operations
UNION
UNION of two tables
Union of two tables, A and B, is table containing all rows in either A or B or
both
Two tables must be union compatible
Example 1: List the cities that rented cars for years 2009 or 2010
SELECT City
FROM CarRentals
WHERE Year = 2009
UNION ALL
SELECT City
FROM CarRentals
WHERE Year = 2010
City
Year
CarsRented
Chicago
2011
567
Los Angles
2010
1540
Miami
Los Angles
2011
1320
New Jersey
Miami
2010
512
Miami
2011
987
Miami
2009
1054
New Jersey
2010
987
New York
2011
1021
Using UNION
City
Los Angles
City
Los Angles
Miami
New Jersey
Miami
21
SET Operations
EXCEPT (DIFFERENCE or MINUS)
EXCEPT of two tables
is a table containing all rows in A but not in B
Example 1: List the cities that rented cars for any year but (except) 2010
SELECT City
FROM CarRentals
EXCEPT
SELECT City
FROM CarRentals
WHERE Year = 2010
City
Year
CarsRented
Chicago
2011
567
Los Angles
2010
1540
Los Angles
2011
1320
Miami
2010
512
Miami
2011
987
Miami
2009
1054
New Jersey
2010
987
New York
2011
1021
Using EXCEPT
City
Chicago
New York
22
1992-2014 by Addison Wesley & Pearson Education, Inc., McGraw Hill, Cengage Learning
Slides adapted and modified from Fundamentals of Database Systems (5/6) (Elmasri et al.), Database System
Concepts (5/6) (Silberschatz et al.), Database Systems (Coronel et al.), Database Systems (4/5) (Connolly et al. )
11
SET Operations
INTERSECT
INTERSECT of two tables
is table containing all rows common to both A and B.
Example 1: List the cities that rented cars for in both 2009 and 2010
SELECT City
FROM CarRentals
WHERE Year = 2009
INTERSECT
SELECT City
FROM CarRentals
WHERE Year = 2010
City
Year
CarsRented
Chicago
2011
567
Los Angles
2010
1540
City
Los Angles
2011
1320
Miami
Miami
2010
512
Miami
2011
987
Miami
2009
1054
New Jersey
2010
987
New York
2011
1021
Using INTERSECT
23
1992-2014 by Addison Wesley & Pearson Education, Inc., McGraw Hill, Cengage Learning
Slides adapted and modified from Fundamentals of Database Systems (5/6) (Elmasri et al.), Database System
Concepts (5/6) (Silberschatz et al.), Database Systems (Coronel et al.), Database Systems (4/5) (Connolly et al. )
12