sql – part i yong choi school of business csu, bakersfield

48
SQL – Part I Yong Choi School of Business CSU, Bakersfield

Upload: diana-hubbard

Post on 24-Dec-2015

224 views

Category:

Documents


0 download

TRANSCRIPT

Page 1: SQL – Part I Yong Choi School of Business CSU, Bakersfield

SQL – Part I

Yong ChoiSchool of BusinessCSU, Bakersfield

Page 2: SQL – Part I Yong Choi School of Business CSU, Bakersfield

2

Study Objectives

• Understand the basic commands and functions of SQL

• Learn how SQL is used for data manipulation (to add, modify, delete, and retrieve data)

• Learn how to use SQL to query a database to extract useful information

• Learn how SQL is used for data administration (to create tables, indexes, and views)

• Practice SQL

Page 3: SQL – Part I Yong Choi School of Business CSU, Bakersfield

3

Ideal Database Language Requirements

• Create database and table structures. – SQL has a data definition component that gives us the

ability to meet this requirement.

• Manage the data component of the database.– SQL gives us a set of commands to add, update, and

delete data within the database tables.

• Provide detailed data query capability.– "Standard" SQL uses a set of approximately thirty

commands that allow us to retrieve data and to convert the raw data into useful information.

Page 4: SQL – Part I Yong Choi School of Business CSU, Bakersfield

4

Brief History of SQL

• Standard Query Language (SQL) is the relational model’s standard language.

• The original version of SQL was developed at IBM's San Jose Research Laboratory. This language, originally called Sequel. The Sequel language has evolved since then, and its name has changed to SQL (Structured Query Language).

• In 1986, the American National Standards Institute (ANSI) published an SQL standard.– In 1992, work was completed on a significantly

revised version of the SQL standard (SQL-92).

Page 5: SQL – Part I Yong Choi School of Business CSU, Bakersfield

5

Introduction to SQL

• SQL is relatively easy to learn– SQL commands set has a basic vocabulary of less than

100 words.

• SQL is a nonprocedural language. So, it is much easier to use. – Its user merely commands what is to be done without

having to worry about how it's to be done.– Procedural language: COBOL, C, or Pascal.

• See SQL by “Restaurant” DB on the class website

Page 6: SQL – Part I Yong Choi School of Business CSU, Bakersfield

6

Basic Structure of SQL Queries

• SELECT <attribute list>• FROM <table list>• WHERE <condition>• GROUP BY < grouping attribute(s)>• HAVING <group condition>• ORDER BY <attribute list>

Page 7: SQL – Part I Yong Choi School of Business CSU, Bakersfield

7

The SELECT and FROM Statement

• The SELECT statement is used to select data from a table. The tabular result is stored in a result table (called the result set). The FROM statement is used to select tables.

• Syntax:– SELECT column_name(s) – FROM table_name

• To select all columns from a table, use a * symbol instead of column names: – SELECT * FROM table_name

Page 8: SQL – Part I Yong Choi School of Business CSU, Bakersfield

8

The WHERE Statement

• To conditionally select data from a table, a WHERE clause can be added to the SELECT statement.

• Syntax:– SELECT column – FROM table – WHERE column operator value

Page 9: SQL – Part I Yong Choi School of Business CSU, Bakersfield

9

Typical Data Types

• INTEGER: Numbers without a decimal point• SMALLINT: Uses less space than INTEGER• DECIMAL(p,q): P number of digits; q number of

decimal places• CHAR(n): Character string n places long• DATE: Dates in DD-MON-YYYY or MM/DD/YYYY

Page 10: SQL – Part I Yong Choi School of Business CSU, Bakersfield

10

Semicolon after SQL Statements?

• Semicolon is the standard way to separate each SQL statement in database systems that allow more than one SQL statement to be executed in the same call to the server.

• MS Access and SQL Server do not require to put a semicolon after each SQL statement, but other database SQLs force you to use it such as Oracle. So, you must use a semicolon for this class.– Access SQL commands are not case sensitive (including

table and attribute names) but try to follow exact names for better readability.

– Download SQL data file form the class web site.

Page 11: SQL – Part I Yong Choi School of Business CSU, Bakersfield

11

CustomerNum CustomerName Street City State Zip Balance CreditLimit RepNum

148 Al's Appliance and Sport 2837 Greenway Fillmore FL 33336 $6,550 $7,500 20

OrderNum PartNum NumOrdered QuotedPrice

21608 AT94 11 $21.95

OrderNum OrderDate CustomerNum

21608 10/20/2003 148

Customer

OrderLine

Orders

PartNum Description OnHand Class Warehouse Price

AT94 Iron 50 HW 3 $24.95

Part

RepRepNum LastName FirstName Street City State Zip Commission Rate

20 Kaiser Valerie 624 Randall Grove FL 33321 $20,542.50 0.05

Page 12: SQL – Part I Yong Choi School of Business CSU, Bakersfield

12

Example 2

• Example 2: Save as example 2– List the number, name, and balance

of all customers.

Page 13: SQL – Part I Yong Choi School of Business CSU, Bakersfield

13

Example 2

Page 14: SQL – Part I Yong Choi School of Business CSU, Bakersfield

14

Example 2

SELECT Customernum, CustomerName, Balance

FROM Customer;

Page 15: SQL – Part I Yong Choi School of Business CSU, Bakersfield

15

Example 3

• Example 3: Save as example 3– List the complete Part table.– Use of “ * “

Page 16: SQL – Part I Yong Choi School of Business CSU, Bakersfield

16

Example 3

Page 17: SQL – Part I Yong Choi School of Business CSU, Bakersfield

17

Example 3

SELECT *FROM Part;

Page 18: SQL – Part I Yong Choi School of Business CSU, Bakersfield

18

Oracle 7.0 SQL ExampleSQL> select * from employee;

 EMP_ID EMP_LNAME EMP_FNAME EMP_SALARY EMP_DEPT_NO

--------- ---------- ---------- ---------- -----------

1 Kim John 1000 100

2 Johnson Steve 1200 100

3 Jonson Paul 1100 200

4 Lee Jim 1100 200

5 Basinger Jon 1300

6 Stone Sharon 1000

 6 rows selected.

Page 19: SQL – Part I Yong Choi School of Business CSU, Bakersfield

19

SQL Example – WHERE clause

• Example 4: Save as example 4– List the name of every customers with

$10,000 credit limit.• Credit limit must be equal to $10000

Page 20: SQL – Part I Yong Choi School of Business CSU, Bakersfield

20

Example 4SQL Query with Where Condition

Page 21: SQL – Part I Yong Choi School of Business CSU, Bakersfield

21

Example 4

SELECT CustomernameFROM CustomerWHERE CreditLimit=10000;

Page 22: SQL – Part I Yong Choi School of Business CSU, Bakersfield

22

SQL Example – WHERE clause

• Example 5: Save as example 5– Find the name of customer 148.

Page 23: SQL – Part I Yong Choi School of Business CSU, Bakersfield

23

Example 5SQL Query to Find Customer 148

Page 24: SQL – Part I Yong Choi School of Business CSU, Bakersfield

24

SQL Comparison Operators For WHERE clause

NOT Warehouse =‘3’

LIKE: LIKE ‘a*’, LIKE ‘*s’, Like ‘*Oxford*’

(NOT) BETWEEN 45000 AND 78000

(NOT) IN (123, 345)

Page 25: SQL – Part I Yong Choi School of Business CSU, Bakersfield

25

SQL Examples

• Example 6: Save as example 6– Find the customer name for every

customer located in the city of Grove

Page 26: SQL – Part I Yong Choi School of Business CSU, Bakersfield

26

Example 6

Page 27: SQL – Part I Yong Choi School of Business CSU, Bakersfield

27

SQL Examples

• Example 7: Save as example 7– List the number, name, credit limit,

and balance for customers with credit limits that exceed their balances.

Page 28: SQL – Part I Yong Choi School of Business CSU, Bakersfield

28

Example 7

Page 29: SQL – Part I Yong Choi School of Business CSU, Bakersfield

29

SQL Examples – Compound Conditions

• Example 8: Save as example 8– List the description of all parts that

are located in warehouse 3 and for which there are more than 20 units on hand.

Page 30: SQL – Part I Yong Choi School of Business CSU, Bakersfield

30

Example 8SQL Query with Compound Condition

using ‘AND’

Page 31: SQL – Part I Yong Choi School of Business CSU, Bakersfield

31

SQL Examples – Compound Conditions

• Example 9: Save as example 9– List the descriptions of all parts that

are located in warehouse 3 or for which there are more than 20 units on hand.

Page 32: SQL – Part I Yong Choi School of Business CSU, Bakersfield

32

Example 9SQL Query using ‘OR’

Page 33: SQL – Part I Yong Choi School of Business CSU, Bakersfield

33

SQL Examples

• Example 10: Save as example 10– List the description of all parts that

are not in warehouse 3. – Use “NOT” (i.e., where NOT A = 100;)

Page 34: SQL – Part I Yong Choi School of Business CSU, Bakersfield

34

Example 10SQL Query using ‘NOT’

Page 35: SQL – Part I Yong Choi School of Business CSU, Bakersfield

35

SQL Examples

• Example 11: Save as example 11– List the number, name, and balance

of all customers with balances greater than or equal to $1,000 and less than or equal to $5,000.

– (NOT) BETWEEN 45000 AND 78000

Page 36: SQL – Part I Yong Choi School of Business CSU, Bakersfield

36

Example 11Query with ‘BETWEEN’ Operator

Page 37: SQL – Part I Yong Choi School of Business CSU, Bakersfield

37

SQL Examples – Computed Field

• Computed field can involve:– addition(+), subtraction(-), Multiplication(*), or

division (/)

• Example 12: Save as example 12– List the number, name and available

credit for all customers. – Use “AS” for assigning a new field

name

Page 38: SQL – Part I Yong Choi School of Business CSU, Bakersfield

38

Example 12SQL Query with Computed Field

Page 39: SQL – Part I Yong Choi School of Business CSU, Bakersfield

39

SQL Examples – Computed Field

• Computed field can involve:– addition(+), subtraction(-), Multiplication(*), or

division (/)

• Example 13: Save as example 13– List the number, name, and available

credit for all customers with credit limits that exceed their balances.

Page 40: SQL – Part I Yong Choi School of Business CSU, Bakersfield

40

Example 13SQL Query with Computed

Field and Condition

Page 41: SQL – Part I Yong Choi School of Business CSU, Bakersfield

41

SQL Examples – LIKE and IN

• Example 14: Save as example 14– List the number, name, and complete

address of every customer located on a street that contain the letters “Oxford.” • Customer names begin with B: Like B*• Customer names end with E: Like *E• Fine exact customer last name: like

‘*Choi*’

Page 42: SQL – Part I Yong Choi School of Business CSU, Bakersfield

42

Example 14SQL Query with ‘LIKE’ Operator

Page 43: SQL – Part I Yong Choi School of Business CSU, Bakersfield

43

SQL Examples – LIKE and IN

• Example 15: Save as example 15– List the number, name, and credit

limit for every customer with a credit of $7,500, $10,000, or $15,000.

– IN (7500, 10000, 15000);

Page 44: SQL – Part I Yong Choi School of Business CSU, Bakersfield

44

Example 15SQL Query with ‘IN’ Operator

Page 45: SQL – Part I Yong Choi School of Business CSU, Bakersfield

45

SQL Examples

• Default value of ORDER BY: ascending• Example 16: Save as example 16

– List the number, name, and credit limit of all customers. Sort the customers by name in ascending order .

Page 46: SQL – Part I Yong Choi School of Business CSU, Bakersfield

46

Example 16SQL Query to Sort Data

Page 47: SQL – Part I Yong Choi School of Business CSU, Bakersfield

47

SQL Examples

• Default value of ORDER BY: ascending• Example 17: Save as example 17

– List the number, name, and credit limit of all customers. Sort the customers by name (minor) in ascending order within credit limit (major) in descending order.

– What about this case? • customer name (minor) and credit limit (major)

Page 48: SQL – Part I Yong Choi School of Business CSU, Bakersfield

48

Example 17SQL Query to Sort on Multiple Fields