sql assignment yong choi school of business csu, bakersfield

36
SQL Assignment Yong Choi School of Business CSU, Bakersfield

Upload: dexter-mobbs

Post on 31-Mar-2015

216 views

Category:

Documents


2 download

TRANSCRIPT

Page 1: SQL Assignment Yong Choi School of Business CSU, Bakersfield

SQL Assignment

Yong Choi

School of Business

CSU, Bakersfield

Page 2: SQL Assignment 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 Assignment 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 Assignment Yong Choi School of Business CSU, Bakersfield

4

Introduction to 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 Assignment Yong Choi School of Business CSU, Bakersfield

5

Introduction to SQL (con’t)

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

Page 6: SQL Assignment Yong Choi School of Business CSU, Bakersfield

6

More about SQL

• Three basic data functions by SQL, and their basic SQL commands:1. Data definition through the use of CREATE2. Data manipulation through INSERT, UPDATE, and

DELETE3. Data querying through the use of SELECT AND MANY

OTHERS, which is the basis for all SQL queries.– We will try this first since we just completed all Access

queries.

Page 7: SQL Assignment Yong Choi School of Business CSU, Bakersfield

7

Basic Structure of SQL Queries

• SQL relation is not a set of tuples because a set does not allow two identical members; rather it is a multi-set (a bag) of tuples.

• A SQL query has the form:

SELECT <attribute list>

FROM <table list>

WHERE <condition>

GROUP BY < grouping attribute(s)>

HAVING <group condition>

ORDER BY <attribute list>

Page 8: SQL Assignment Yong Choi School of Business CSU, Bakersfield

8

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 9: SQL Assignment Yong Choi School of Business CSU, Bakersfield

9

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 10: SQL Assignment Yong Choi School of Business CSU, Bakersfield

10

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 11: SQL Assignment Yong Choi School of Business CSU, Bakersfield

11

SQL Assignment

• Assignment questions start from the next slide.

• Answer Table is given as a hint for each question.

Page 12: SQL Assignment Yong Choi School of Business CSU, Bakersfield

12

SQL

• Assignment Question 2: Save as Assignment Question 2– List the number, name, and balance of all

customers.• Assignment Question 3: Save as Assignment

Question 3– List the complete Part table.

Page 13: SQL Assignment Yong Choi School of Business CSU, Bakersfield

13

Assignment Question 2

Page 14: SQL Assignment Yong Choi School of Business CSU, Bakersfield

14

Assignment Question 3

Page 15: SQL Assignment Yong Choi School of Business CSU, Bakersfield

15

SQL – WHERE clause

• Assignment Question 4: Save as Assignment Question 4– List the name of every customers with $10,000

credit limit.• Assignment Question 5: Save as Assignment

Question 5– Find the name of customer 148.

Page 16: SQL Assignment Yong Choi School of Business CSU, Bakersfield

16

Assignment Question 4SQL Query with Where Condition

Page 17: SQL Assignment Yong Choi School of Business CSU, Bakersfield

17

Assignment Question 5SQL Query to Find Customer 148

Page 18: SQL Assignment Yong Choi School of Business CSU, Bakersfield

18

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 19: SQL Assignment Yong Choi School of Business CSU, Bakersfield

19

SQL

• Assignment Question 6: Save as Assignment Question 6– Find the customer name for every customer

located in the city of Grove• Assignment Question 7: Save as Assignment

Question 7– List the number, name, credit limit, and

balance for customers with credit limits that exceed their balances.

Page 20: SQL Assignment Yong Choi School of Business CSU, Bakersfield

20

Assignment Question 6

Page 21: SQL Assignment Yong Choi School of Business CSU, Bakersfield

21

Assignment Question 7

Page 22: SQL Assignment Yong Choi School of Business CSU, Bakersfield

22

SQL – Compound Conditions

• Assignment Question 8: Save as Assignment Question 8– List the description of all parts that are located

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

• Assignment Question 9: Save as Assignment Question 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 23: SQL Assignment Yong Choi School of Business CSU, Bakersfield

23

Assignment Question 8SQL Query with Compound

Condition using ‘AND’

Page 24: SQL Assignment Yong Choi School of Business CSU, Bakersfield

24

Assignment Question 9SQL Query using ‘OR’

Page 25: SQL Assignment Yong Choi School of Business CSU, Bakersfield

25

SQL

• Assignment Question 10: Save as Assignment Question 10– List the description of all parts that are not in

warehouse 3. • Assignment Question 11: Save as

Assignment Question 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.

Page 26: SQL Assignment Yong Choi School of Business CSU, Bakersfield

26

Assignment Question 10SQL Query using ‘NOT’

Page 27: SQL Assignment Yong Choi School of Business CSU, Bakersfield

27

Assignment Question 11Query with ‘BETWEEN’ Operator

Page 28: SQL Assignment Yong Choi School of Business CSU, Bakersfield

28

SQL – Computed Field

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

• Assignment Question 12: Save as Assignment Question 12– List the number, name and available credit for

all customers. • Assignment Question 13: Save as

Assignment Question 13– List the number, name, and available credit for

all customers with credit limits that exceed their balances.

Page 29: SQL Assignment Yong Choi School of Business CSU, Bakersfield

29

Assignment Question 12SQL Query with Computed Field

Page 30: SQL Assignment Yong Choi School of Business CSU, Bakersfield

30

Assignment Question 13SQL Query with Computed

Field and Condition

Page 31: SQL Assignment Yong Choi School of Business CSU, Bakersfield

31

SQL – LIKE and IN

• Assignment Question 14: Save as Assignment Question 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• Customer names end with E

• Assignment Question 15: Save as Assignment Question 15– List the number, name, and credit limit for

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

Page 32: SQL Assignment Yong Choi School of Business CSU, Bakersfield

32

Assignment Question 14SQL Query with ‘LIKE’ Operator

Page 33: SQL Assignment Yong Choi School of Business CSU, Bakersfield

33

Assignment Question 15SQL Query with ‘IN’ Operator

Page 34: SQL Assignment Yong Choi School of Business CSU, Bakersfield

34

SQL

• Default value of ORDER BY: ascending• Assignment Question 16: Save as Assignment

Question 16– List the number, name, and credit limit of all customers.

Sort the customers by name in ascending order . • Assignment Question 17: Save as Assignment

Question 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 35: SQL Assignment Yong Choi School of Business CSU, Bakersfield

35

Assignment Question 16SQL Query to Sort Data

Page 36: SQL Assignment Yong Choi School of Business CSU, Bakersfield

36

Assignment Question 17SQL Query to Sort on Multiple Fields