12
1 CS 338: Computer Applications in Business: Databases (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 Basic SQL Retrieval Queries ©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. ) Rice University Data Center Fall 2014 Chapter 4 Announcements Assignment 1 Due Monday October 6 th , 2014 Solutions will be posted on midnight Lecture 5 Exercise Solutions posted LEARN Æ Content Æ Lecture 5 … 2

06 Basic SQL - Retrieval Queries

Embed Size (px)

Citation preview

Page 1: 06 Basic SQL - Retrieval Queries

1

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

©1992-2014 by Addison Wesley & Pearson Education, Inc., McGraw Hill, Cengage LearningSlides adapted and modified from Fundamentals of Database Systems (5/6) (Elmasri et al.), Database SystemConcepts (5/6) (Silberschatz et al.), Database Systems (Coronel et al.), Database Systems (4/5) (Connolly et al. )

CS 338: Computer Applications in Business: Databases

Basic SQLRetrieval Queries

©1992-2014 by Addison Wesley & Pearson Education, Inc., McGraw Hill, Cengage LearningSlides adapted and modified from Fundamentals of Database Systems (5/6) (Elmasri et al.), Database SystemConcepts (5/6) (Silberschatz et al.), Database Systems (Coronel et al.), Database Systems (4/5) (Connolly et al. )

Rice University Data Center

Fall 2014

Chapter 4

Announcements

• Assignment 1

• Due Monday October 6th, 2014

• Solutions will be posted on midnight

• Lecture 5 Exercise

• Solutions posted LEARN Æ Content Æ Lecture 5 …

2

Page 2: 06 Basic SQL - Retrieval Queries

2

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

©1992-2014 by Addison Wesley & Pearson Education, Inc., McGraw Hill, Cengage LearningSlides adapted and modified from Fundamentals of Database Systems (5/6) (Elmasri et al.), Database SystemConcepts (5/6) (Silberschatz et al.), Database Systems (Coronel et al.), Database Systems (4/5) (Connolly et al. )

Basic Retrieval Queries in SQL

Basic statement for retrieving information from a database

SELECT statement

3

Unlike relational model, SQL allows a table to have two or more tuplesthat are identical in all their attribute values (multiset or bag behavior)

Basic form of the SELECT statement:

The SELECT-FROM-WHERE Structure

Basic Retrieval Queries in SQLÆ SELECT-FROM-WHERE Structure

= < <= > >= <>Basic logical comparison operators for comparing attribute values

4

Also known as projection attributes

SELECT clause specifies attributes whose values are to be retrieved

Â

Also known as selection condition

WHERE clause specifies the Boolean condition that must be true for any retrieved tuple

Page 3: 06 Basic SQL - Retrieval Queries

3

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

©1992-2014 by Addison Wesley & Pearson Education, Inc., McGraw Hill, Cengage LearningSlides adapted and modified from Fundamentals of Database Systems (5/6) (Elmasri et al.), Database SystemConcepts (5/6) (Silberschatz et al.), Database Systems (Coronel et al.), Database Systems (4/5) (Connolly et al. )

Basic Retrieval Queries in SQLÆ SELECT-FROM-WHERE Structure

5

Â

join condition: combines two tuples (one from DEPARTMENT and one from EMPLOYEE)

Basic Retrieval Queries in SQLÆ SELECT-FROM-WHERE Structure

6

Â

Page 4: 06 Basic SQL - Retrieval Queries

4

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

©1992-2014 by Addison Wesley & Pearson Education, Inc., McGraw Hill, Cengage LearningSlides adapted and modified from Fundamentals of Database Systems (5/6) (Elmasri et al.), Database SystemConcepts (5/6) (Silberschatz et al.), Database Systems (Coronel et al.), Database Systems (4/5) (Connolly et al. )

Ambiguous Attribute Names

• As long as the attributes are in different relations• Must qualify the attribute name with the relation name to prevent ambiguity

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

7

• 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:

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

8

Â

Query 8: For each employee, retrieve the employee’s 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

Nathan Wilson
When names are not ambiguous, though, do not need to specify
Nathan Wilson
Nathan Wilson
Page 5: 06 Basic SQL - Retrieval Queries

5

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

©1992-2014 by Addison Wesley & Pearson Education, Inc., McGraw Hill, Cengage LearningSlides adapted and modified from Fundamentals of Database Systems (5/6) (Elmasri et al.), Database SystemConcepts (5/6) (Silberschatz et al.), Database Systems (Coronel et al.), Database Systems (4/5) (Connolly et al. )

Unspecified WHERE Clause

• Indicates no condition on tuple selection• All tuples will be selected

Missing WHERE clause

9

• All possible tuple combinations

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

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

City Year CarsRentedChicago 2011 567

Los Angles 2010 1540Los Angles 2011 1320

Miami 2010 512Miami 2011 987Miami 2009 1054

New Jersey 2010 987New York 2011 1021

CarRentals Table

10

Page 6: 06 Basic SQL - Retrieval Queries

6

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

©1992-2014 by Addison Wesley & Pearson Education, Inc., McGraw Hill, Cengage LearningSlides adapted and modified from Fundamentals of Database Systems (5/6) (Elmasri et al.), Database SystemConcepts (5/6) (Silberschatz et al.), Database Systems (Coronel et al.), Database Systems (4/5) (Connolly et al. )

• To retrieve specific columns, each column name must be specified after the SELECT keyword

• multiple columns are separated by commas

• Example

SELECT City, CarsRentedFROM CarRentals

City CarsRentedChicago 567

Los Angles 1540Los Angles 1320

Miami 512Miami 987Miami 1054

New Jersey 987New York 1021

CarRentals Table

Selecting ColumnsÆ How to Select Specific Columns

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 TableCity Year CarsRented CarsRented * 10

Chicago 2011 567 5670Los Angles 2010 1540 15400Los Angles 2011 1320 13200

Miami 2010 512 5120Miami 2011 987 9870Miami 2009 1054 10540

New Jersey 2010 987 9870New York 2011 1021 10210

12

Page 7: 06 Basic SQL - Retrieval Queries

7

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

©1992-2014 by Addison Wesley & Pearson Education, Inc., McGraw Hill, Cengage LearningSlides adapted and modified from Fundamentals of Database Systems (5/6) (Elmasri et al.), Database SystemConcepts (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

• Example

SELECT *FROM CarRentals

City Year CarsRentedChicago 2011 567

Los Angles 2010 1540Los Angles 2011 1320

Miami 2010 512Miami 2011 987Miami 2009 1054

New Jersey 2010 987New 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

Page 8: 06 Basic SQL - Retrieval Queries

8

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

©1992-2014 by Addison Wesley & Pearson Education, Inc., McGraw Hill, Cengage LearningSlides adapted and modified from Fundamentals of Database Systems (5/6) (Elmasri et al.), Database SystemConcepts (5/6) (Silberschatz et al.), Database Systems (Coronel et al.), Database Systems (4/5) (Connolly et al. )

• Partial Matching Search (WHERE)

• Example 1SELECT *FROM CarRentalsWHERE City = ‘Miami’

• Example 2SELECT *FROM CarRentalsWHERE City = ‘Miami’AND Year > 2010

City Year CarsRentedMiami 2011 987

City Year CarsRentedMiami 2010 512Miami 2011 987Miami 2009 1054

Selecting RowsÆ Selecting Specific Rows

Â

15

• Range Search (BETWEEN)

• Example 1SELECT *FROM CarRentalsWHERE Year >= 2009AND YEAR <= 2010

• Example 2SELECT *FROM CarRentalsWHERE YearBETWEEN 2009 AND 2010

City Year CarsRentedLos Angles 2010 1540

Miami 2010 512Miami 2009 1054

New Jersey 2010 987

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

Selecting RowsÆ Selecting Specific Rows

Â

16

Page 9: 06 Basic SQL - Retrieval Queries

9

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

©1992-2014 by Addison Wesley & Pearson Education, Inc., McGraw Hill, Cengage LearningSlides adapted and modified from Fundamentals of Database Systems (5/6) (Elmasri et al.), Database SystemConcepts (5/6) (Silberschatz et al.), Database Systems (Coronel et al.), Database Systems (4/5) (Connolly et al. )

• 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 1SELECT *FROM CarRentalsWHERE City LIKE ‘N%’

• Example 2SELECT *FROM CarRentalsWHERE City LIKE ‘%es’

City Year CarsRentedNew Jersey 2010 987New York 2011 1021

City Year CarsRentedLos Angles 2010 1540Los Angles 2011 1320

Selecting RowsÆ Selecting Specific Rows

Â

17

• SQL does not automatically eliminate duplicate tuples in query results � Use the keyword DISTINCT in the SELECT clause only

Selecting RowsÆ Removing Duplicate Rows

18

CityChicago

Los AnglesLos Angles

MiamiMiamiMiami

New JerseyNew York

Example 1: SELECT CityFROM CarRentals

City

Chicago

Los Angles

Miami

New Jersey

New York

Example 2: SELECT DISTINCT CityFROM CarRentals

DISTINCT removes duplicate rowsfrom the result-set

Nathan Wilson
Nathan Wilson
Page 10: 06 Basic SQL - Retrieval Queries

10

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

©1992-2014 by Addison Wesley & Pearson Education, Inc., McGraw Hill, Cengage LearningSlides adapted and modified from Fundamentals of Database Systems (5/6) (Elmasri et al.), Database SystemConcepts (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 CarsStatsFROM CarRentals

CarRentals Table

City Year Rented CarsStatsChicago 2011 567 5670

Los Angles 2010 1540 15400Los Angles 2011 1320 13200

Miami 2010 512 5120Miami 2011 987 9870Miami 2009 1054 10540

New Jersey 2010 987 9870New York 2011 1021 10210

Sorting RowsÆ 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)

ORDER BY clause

20

City Year CarsRentedChicago 2011 567

Los Angles 2010 1540Los Angles 2011 1320

Miami 2010 512Miami 2011 987Miami 2009 1054

New Jersey 2010 987New York 2011 1021

Example 1: SELECT *FROM CarRentalsORDER BY City ASC

City Year CarsRentedChicago 2011 567

Los Angles 2011 1320Los Angles 2010 1540

Miami 2011 987Miami 2010 512Miami 2009 1054

New Jersey 2010 987New York 2011 1021

Example 2: SELECT *FROM CarRentalsORDER BY City ASC, Year DESC

Page 11: 06 Basic SQL - Retrieval Queries

11

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

©1992-2014 by Addison Wesley & Pearson Education, Inc., McGraw Hill, Cengage LearningSlides adapted and modified from Fundamentals of Database Systems (5/6) (Elmasri et al.), Database SystemConcepts (5/6) (Silberschatz et al.), Database Systems (Coronel et al.), Database Systems (4/5) (Connolly et al. )

SET OperationsÆ UNION

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

UNION of two tables

21

City Year CarsRentedChicago 2011 567

Los Angles 2010 1540Los Angles 2011 1320

Miami 2010 512Miami 2011 987Miami 2009 1054

New Jersey 2010 987New York 2011 1021

SELECT CityFROM CarRentalsWHERE Year = 2009UNIONSELECT CityFROM CarRentalsWHERE Year = 2010

If ALL specified, result can include duplicate rows

ALL

Example 1: List the cities that rented cars for years 2009 or 2010

CityLos Angles

MiamiNew Jersey

Using UNION

CityLos Angles

MiamiNew Jersey

Miami

Using UNION ALL

SET OperationsÆ EXCEPT (DIFFERENCE or MINUS)

is a table containing all rows in A but not in B

EXCEPT of two tables

22

SELECT CityFROM CarRentalsEXCEPTSELECT CityFROM CarRentalsWHERE Year = 2010

Example 1: List the cities that rented cars for any year but (except) 2010

City Year CarsRentedChicago 2011 567

Los Angles 2010 1540Los Angles 2011 1320

Miami 2010 512Miami 2011 987Miami 2009 1054

New Jersey 2010 987New York 2011 1021

CityChicago

New York

Using EXCEPT

Page 12: 06 Basic SQL - Retrieval Queries

12

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

©1992-2014 by Addison Wesley & Pearson Education, Inc., McGraw Hill, Cengage LearningSlides adapted and modified from Fundamentals of Database Systems (5/6) (Elmasri et al.), Database SystemConcepts (5/6) (Silberschatz et al.), Database Systems (Coronel et al.), Database Systems (4/5) (Connolly et al. )

SET OperationsÆ INTERSECT

is table containing all rows common to both A and B.

INTERSECT of two tables

23

SELECT CityFROM CarRentalsWHERE Year = 2009INTERSECTSELECT CityFROM CarRentalsWHERE Year = 2010

Example 1: List the cities that rented cars for in both 2009 and 2010

City Year CarsRentedChicago 2011 567

Los Angles 2010 1540Los Angles 2011 1320

Miami 2010 512Miami 2011 987Miami 2009 1054

New Jersey 2010 987New York 2011 1021

CityMiami

Using INTERSECT