Você está na página 1de 29

Lecture 5:

Structured Query Language (SQL) II

ISOM3260
ISOM3260 Overview
Database environment and development process
database environment
steps to develop a database
Conceptual data modeling
entity-relationship (ER) diagram; enhanced ER
Logical database design
transforming ER diagram into relations; normalization
Physical database design
technical specifications of the database
Database implementation
Structured Query Language I, II & III
Advanced topics
data and database administration, data warehousing

2
Database development activities during SDLC

3
Structured Query Language (SQL)
What is SQL?
Creating Tables
Changing and Removing Tables
INSERT, DELETE, UPDATE
SELECT statement

4
SELECT Statement
Used for queries on single or multiple tables
Clauses of the SELECT statement
SELECT
list the columns (and expressions) that should be returned from the
query
FROM
indicate the table(s) or view(s) from which data will be obtained
WHERE
indicate the conditions under which a row will be included in the
result
GROUP BY
indicate categorization of results
HAVING
indicate the conditions under which a category (group) will be
included
ORDER BY
sorts the result according to specified criteria
5
Figure 6-10:
SQL statement
processing order

6
SELECT Example
Find products with standard price less than $275
SELECT Product_Description, Standard_Price
FROM Product_T
WHERE Standard_Price < 275;

To display all columns


SELECT *

To display without duplicate rows


SELECT DISTINCT STATE
FROM CUSTOMER_T;

7
SELECT: Comparison Operators
Which orders have been placed after 10/24/2010?
SELECT Order_ID, Order_Date
FROM Order_T
WHERE Order_Date > '24-OCT-2010';

What furniture does Pine Valley carry that isnt made


of Cherry?
SELECT Product_Description, Product_Finish
FROM Product_T
WHERE Product_Finish != 'Cherry';

8
SELECT: Alias
Alias is an alternative column name or table
name

SELECT CUST.Customer_Name AS NAME,


CUST.Customer_Address
FROM Customer_T CUST
WHERE Customer_Name = 'Home Furnishings';

Results:

NAME
CUSTOMER_ADDRESS
Home Furnishings 1900 Allard
Ave. 9
SELECT: Using Expressions
An expression has an operator acting on numeric
columns
Operators include: *, / , +,
Expressions in parentheses are executed first,
followed by * and / and then + and -, from left to
right
Suppose there are 100 units for each of the
products .What is the total value for each product?
SELECT Product_Description, Standard_Price,
Standard_Price * 100 AS VALUE
FROM Product_T;

10
SELECT: Using Functions
Functions include
COUNT, COUNT (*), MIN, MAX, SUM, and AVG
COUNT adds up the number of rows selected by a query
that do not contain NULL
COUNT (*) adds up all the rows selected by a query
SUM and AVG can only be used with numeric columns
Using functions will result in a one-row answer

How many different items were ordered on order


number 1004?
SELECT COUNT(*) FROM Order_Line_T
WHERE Order_ID = 1004;
11
SELECT: Using Wildcards
Wildcard used in SELECT clause
* (means all)
SELECT * FROM Product_T

Wildcards used in WHERE clause


% (means any collection of characters)
WHERE Product_Description LIKE '%Desk'
will find Computer Desk, 8-Drawer Desk, etc.

_ (means exactly one character)


WHERE Product_Description LIKE '_-Drawer'
will find 3-drawer, 5-drawer, etc.
12
SELECT: Boolean Operators
Include AND, OR, and NOT operators for customizing
conditions in WHERE clause
If multiple operators are used, NOT is evaluated first, then
AND, then OR

List product description, finish, and price for all desks and all
tables that cost more than $300.
SELECT Product_Description, Product_Finish,
Standard_Price
FROM Product_T
WHERE Product_Description LIKE '%Desk'
OR Product_Description LIKE '%Table'
AND Standard_Price > 300;
Note: All desks will be listed; even those that cost 300
13 or less.
SELECT: Boolean Operators
List product description, finish, and price for
all desks and tables that cost more than
$300.

SELECT Product_Description, Product_Finish,


Standard_Price
FROM Product_T
WHERE (Product_Description LIKE '%Desk'
OR Product_Description LIKE '%Table')
AND Standard_Price > 300;

14
SELECT: Ranges
Which products have a price between $200
and $300?
SELECT Product_Description, Standard_Price
FROM Product_T
WHERE Standard_Price > 199 AND
Standard_Price < 301;

SELECT Product_Description, Standard_Price


FROM Product_T
WHERE Standard_Price BETWEEN 200 AND 300;

15
SELECT: IN and NOT IN Lists
List all customers who live in warmer states.

SELECT Customer_Name, City, State


FROM Customer_T
WHERE State IN ('FL','TX','CA');

Note:
State IN ('FL','TX','CA')
is equivalent to
State = 'FL' OR State = TX' OR State = 'CA'

Using IN operator is more efficient than separate OR conditions.


16
Sorting Results: ORDER BY
Referring to the previous query, list the results
alphabetically by state, and alphabetically by
customer within each state.

SELECT Customer_Name, City, State


FROM Customer_T
WHERE State IN ('FL','TX','CA')
ORDER BY State, Customer_Name;
Note: (1) If sorting from high to low, use DESC as a keyword
placed after the column to sort.
(2) Oracle sorts NULLs last.
17
Categorizing Results : GROUP BY
GROUP BY is useful when paired with functions
divides a table into subsets (by groups)
a function can be used to provide summary information for
that group
using functions together with GROUP BY could result in
multiple rows answers

Count no. of customers with addresses in each state we ship.


SELECT State, COUNT(State)
FROM Customer_T
GROUP BY State;

Note: In Oracle, apart from the functions, we can include


columns in the HAVING, SELECT and ORDER BY clause only
if these columns are included in the GROUP BY clause.
18
Qualifying Results: HAVING
Acts like a WHERE clause
Use together with GROUP BY
Identifies groups that meet a criterion rather than
rows

Find only states with more than one customer.


SELECT State, COUNT(State)
FROM Customer_T
GROUP BY State
HAVING COUNT(State) > 1;

Note: Only groups with total number of customers greater


than 1 are included in final result. 19
Example of All Clauses
Based on Enrollment_T, find students with average scores
lower than 90, by excluding the OM4930 scores. Display
StudentID in descending order.
StudentID CourseCode Year Semester Score Grade Credit
1 IS2010 2015 Spring 80 B 3
1 IS3210 2015 Fall 72 C 4
1 IS3230 2015 Fall 85 B 3
2 IS2010 2014 Spring 88 B 3
2 MK2010 2014 Fall 90 A 3
2 IS3260 2015 Fall 90 A 4
3 IS2010 2014 Fall 80 B 3
3 OM2110 2014 Fall 71 C 3
3 OM4930 2015 Spring 70 Pass 4
4 IS2010 2014 Spring 98 A 3
4 OM2110 2014 Spring 94 A 3
4 IS3210 2015 Spring 95 A 4
4 OM3970 2015 Fall 95 A 4
4 OM4930 2015 Spring 88 Pass 4
20
Example of All Clauses
Based on Enrollment_T, find students with average scores
lower than 90, by excluding the OM4930 scores. Display
StudentID in descending order.

SELECT StudentID
FROM Enrollment_T
WHERE CourseCode != 'OM4930'
GROUP BY StudentID
HAVING AVG(Score) < 90
ORDER BY StudentID DESC;

21
Example: FROM
FROM Enrollment_T

StudentID CourseCode Year Semester Score Grade Credit


1 IS2010 2015 Spring 80 B 3
1 IS3210 2015 Fall 72 C 4
1 IS3230 2015 Fall 85 B 3
2 IS2010 2014 Spring 88 B 3
2 MK2010 2014 Fall 90 A 3
2 IS3260 2015 Fall 90 A 4
3 IS2010 2014 Fall 80 B 3
3 OM2110 2014 Fall 71 C 3
3 OM4930 2015 Spring 70 Pass 4
4 IS2010 2014 Spring 98 A 3
4 OM2110 2014 Spring 94 A 3
4 IS3210 2015 Spring 95 A 4
4 OM3970 2015 Fall 95 A 4
4 OM4930 2015 Spring 88 Pass 4
22
Example: WHERE
WHERE CourseCode != 'OM4930'

StudentID CourseCode Year Semester Score Grade Credit


1 IS2010 2015 Spring 80 B 3
1 IS3210 2015 Fall 72 C 4
1 IS3230 2015 Fall 85 B 3
2 IS2010 2014 Spring 88 B 3
2 MK2010 2014 Fall 90 A 3
2 IS3260 2015 Fall 90 A 4
3 IS2010 2014 Fall 80 B 3
3 OM2110 2014 Fall 71 C 3
4 IS2010 2014 Spring 98 A 3
4 OM2110 2014 Spring 94 A 3
4 IS3210 2015 Spring 95 A 4
4 OM3970 2015 Fall 95 A 4

23
Example: GROUP BY
GROUP BY StudentID
StudentID CourseCode Year Semester Score Grade Credit
1 IS2010 2015 Spring 80 B 3
1 IS3210 2015 Fall 72 C 4
1 IS3230 2015 Fall 85 B 3

2 IS2010 2014 Spring 88 B 3


2 MK2010 2014 Fall 90 A 3
2 IS3260 2015 Fall 90 A 4

3 IS2010 2014 Fall 80 B 3


3 OM2110 2014 Fall 71 C 3

4 IS2010 2014 Spring 98 A 3


4 OM2110 2014 Spring 94 A 3
4 IS3210 2015 Spring 95 A 4
4 OM3970 2015 Fall 95 A 4
24
Example: HAVING
HAVING AVG(Score) < 90

StudentID CourseCode Year Semester Score Grade Credit


1 IS2010 2015 Spring 80 B 3
1 IS3210 2015 Fall 72 C 4
1 IS3230 2015 Fall 85 B 3

2 IS2010 2014 Spring 88 B 3


2 MK2010 2014 Fall 90 A 3
2 IS3260 2015 Fall 90 A 4

3 IS2010 2014 Fall 80 B 3


3 OM2110 2014 Fall 71 C 3

25
Example: SELECT
SELECT StudentID

StudentID StudentID StudentID


1 2 3
2 OR 1 OR 2
3 3 1

26
Example: ORDER BY
ORDER BY StudentID DESC

StudentID
3
2
1

27
Example: Results
Based on Enrollment_T, find students with average scores
lower than 90, by excluding the OM4930 scores. Display
StudentID in descending order.
Results:
StudentID SELECT StudentID
3 FROM Enrollment_T
2
1
WHERE CourseCode != 'OM4930'
GROUP BY StudentID
HAVING AVG(Score) < 90
ORDER BY StudentID DESC;

28
Review Questions
How to retrieve data using SELECT statement?
What are the clauses of SELECT statement?

29

Você também pode gostar