Anda di halaman 1dari 12

CS 338: Computer Applications in Business: Databases (Fall 2014)

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. )

CS 338: Computer Applications in Business: Databases


Rice University Data Center

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. )

CS 338: Computer Applications in Business: Databases (Fall 2014)

Basic Retrieval Queries in SQL


SELECT statement
Basic statement for retrieving information from a database
Unlike relational model, SQL allows a table to have two or more tuples
that are identical in all their attribute values (multiset or bag behavior)

The SELECT-FROM-WHERE Structure


Basic form of the SELECT statement:

Basic Retrieval Queries in SQL


SELECT-FROM-WHERE Structure
Basic logical comparison operators for comparing attribute values
=

<

<=

>

>=

<>

SELECT clause specifies attributes whose values are to be retrieved


Also known as projection attributes

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. )

CS 338: Computer Applications in Business: Databases (Fall 2014)

Basic Retrieval Queries in SQL


SELECT-FROM-WHERE Structure

join condition: combines two tuples (one from


DEPARTMENT and one from EMPLOYEE)

Basic Retrieval Queries in SQL


SELECT-FROM-WHERE Structure

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. )

CS 338: Computer Applications in Business: Databases (Fall 2014)

Ambiguous Attribute Names


In SQL, the same name can be used for two (or more) attributes

As long as the attributes are in different relations


Must qualify the attribute name with the relation name to prevent ambiguity

Suppose in Figure 3.7 we have the columns names as follows:

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:

When names are


not ambiguous,
though, do not
need to specify

Fully qualified attribute names can be used for clarity even if there is no ambiguity in
attribute names

Aliasing and Renaming


Ambiguity of attribute names also arises in the case of queries
that refer to the same relation twice
Query 8: For each employee, retrieve the employees first and last name
and the first and last name of his or her immediate supervisor

aliases or tuple variables

It is also possible to rename the relation attributes within the


query in SQL by giving them aliases
alias for Fname

alias for Bdate

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. )

CS 338: Computer Applications in Business: Databases (Fall 2014)

Unspecified WHERE Clause


Missing WHERE clause
Indicates no condition on tuple selection
All tuples will be selected

If more than one relation is specified in the FROM clause and


there is no WHERE clause, then the CROSS PRODUCT of
these relations is selected
All possible tuple combinations

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. )

CS 338: Computer Applications in Business: Databases (Fall 2014)

Selecting Columns
How to Select Specific Columns
To retrieve specific columns,
each column name must be
specified after the SELECT
keyword

CarRentals Table
City

multiple columns are


separated by commas

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. )

CS 338: Computer Applications in Business: Databases (Fall 2014)

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)

The WHERE clause has a condition which is a logical expression.

The WHERE condition consists of:


Comparison Operators
Logical Operators
Arithmetic Operators
Other SQL constructs
14

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. )

CS 338: Computer Applications in Business: Databases (Fall 2014)

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

A BETWEEN includes the endpoints of


range.
A NOT BETWEEN includes all values that
are not part of the range (and endpoints)

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. )

CS 338: Computer Applications in Business: Databases (Fall 2014)

Selecting Rows
Selecting Specific Rows
Pattern Matching Search (%..% and LIKE)
LIKE comparison operator

Used for string pattern matching


% replaces an arbitrary number of zero or more characters
underscore (_) replaces a single character

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

Example 2: SELECT DISTINCT City


FROM CarRentals
City
Chicago
Los Angles
Miami
New Jersey
New York

DISTINCT removes duplicate rows


from the result-set
18

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. )

CS 338: Computer Applications in Business: Databases (Fall 2014)

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)

Keyword DESC to see result in a descending order of values


Keyword ASC to specify ascending order explicitly (default order)

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

CS 338: Computer Applications in Business: Databases (Fall 2014)

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

Using UNION ALL

City

Los Angles
Miami
New Jersey
Miami

If ALL specified, result can include duplicate rows

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

CS 338: Computer Applications in Business: Databases (Fall 2014)

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

Anda mungkin juga menyukai