Final Review. put the following items in their correct order where order by select having group by...

55
Final Review

Transcript of Final Review. put the following items in their correct order where order by select having group by...

Page 1: Final Review. put the following items in their correct order where order by select having group by from select from where group by having order by.

Final Review

Page 2: Final Review. put the following items in their correct order where order by select having group by from select from where group by having order by.

put the following items in their correct order

whereorder byselecthavinggroup byfrom

selectfromwheregroup byhavingorder by

Page 3: Final Review. put the following items in their correct order where order by select having group by from select from where group by having order by.

Table_A has 3 records and Table_B has 4 records

How many records will the following query return?

select *from Table_A, Table_B

Page 4: Final Review. put the following items in their correct order where order by select having group by from select from where group by having order by.

What does DDL stand for?

• Stands for Data Definition Language– create statements– drop statements– alter statements

Page 5: Final Review. put the following items in their correct order where order by select having group by from select from where group by having order by.

List reasons for using views

Page 6: Final Review. put the following items in their correct order where order by select having group by from select from where group by having order by.

Tell me about indexes

Page 7: Final Review. put the following items in their correct order where order by select having group by from select from where group by having order by.

Using SQL, how do you add records to a table

Page 8: Final Review. put the following items in their correct order where order by select having group by from select from where group by having order by.

Using SQL, how do you remove data from a table?

Page 9: Final Review. put the following items in their correct order where order by select having group by from select from where group by having order by.

Using SQL, how do you change data in a table?

Page 10: Final Review. put the following items in their correct order where order by select having group by from select from where group by having order by.

Using SQL, how do you change your default database?

Page 11: Final Review. put the following items in their correct order where order by select having group by from select from where group by having order by.

CREATE TABLE a_marina( MARINA_NUM char(4) NOT NULL, NAME char(20), ADDRESS char(15), CITY char(15), STATE char(2), ZIP)

What will this SQL statement do?

ERROR

We did not specify a datatype for the column ZIP

Page 12: Final Review. put the following items in their correct order where order by select having group by from select from where group by having order by.

Field Type Null Key Default ExtraORDER_NUM char(5) NO PRI PART_NUM char(4) NO PRI NUM_ORDERED decimal(3,0) YES QUOTED_PRICE decimal(6,2) YES

CREATE TABLE p_order_line( ORDER_NUM char(5) NOT NULL, PART_NUM char(4) NOT NULL, NUM_ORDERED decimal(3,0) NULL, QUOTED_PRICE decimal(6,2) NULL, PRIMARY KEY (ORDER_NUM,PART_NUM))

Write the SQL that will create an P_ORDER_LINE table with the following

structure:

Page 13: Final Review. put the following items in their correct order where order by select having group by from select from where group by having order by.

Field Type Null Key Default ExtraBOOK_CODE char(4) NO PRI BRANCH_NUM decimal(2,0) NO PRI 0 ON_HAND decimal(2,0) YES

CREATE TABLE h_inventory( BOOK_CODE char(4) NOT NULL, BRANCH_NUM decimal(2,0) NOT NULL default 0, ON_HAND decimal(2,0) NULL, PRIMARY KEY (BOOK_CODE,BRANCH_NUM))

Write the SQL that will create an H_INVENTORY table with the following

structure:

Page 14: Final Review. put the following items in their correct order where order by select having group by from select from where group by having order by.

What is a database?

structure containing categories of information and relationships between these categories

Page 15: Final Review. put the following items in their correct order where order by select having group by from select from where group by having order by.

What is a Database Management System (DBMS)?

software that lets you create a database and maintain the structures and data within

Page 16: Final Review. put the following items in their correct order where order by select having group by from select from where group by having order by.

What is open source software?

software whose source code is freely and publicly available

Page 17: Final Review. put the following items in their correct order where order by select having group by from select from where group by having order by.

Describe the relationship

17

Page 18: Final Review. put the following items in their correct order where order by select having group by from select from where group by having order by.

18

A sales rep has one or many customers

Page 19: Final Review. put the following items in their correct order where order by select having group by from select from where group by having order by.

19

A sales rep has one or many customers

A customer has one and only one sales rep

Page 20: Final Review. put the following items in their correct order where order by select having group by from select from where group by having order by.

What does SQL stand for?

Structured Query Language

Page 21: Final Review. put the following items in their correct order where order by select having group by from select from where group by having order by.

• What is another name for a table?– Entity

• What is a relationship– An association between entities

Page 22: Final Review. put the following items in their correct order where order by select having group by from select from where group by having order by.

What are some reasons for database normalization?

• minimize duplication of data• help safeguard against data anomalies• improve performance for certain activities

Page 23: Final Review. put the following items in their correct order where order by select having group by from select from where group by having order by.

What is a primary key?

• A primary key uniquely identifies a row in a table. A primary key can be a single column, or a group of columns. When a primary key is made up of multiple columns it may also be referred to as a composite key or a concatenated key, both of which mean “consisting of more than one column.”

Page 24: Final Review. put the following items in their correct order where order by select having group by from select from where group by having order by.

Tell me about null values

Page 25: Final Review. put the following items in their correct order where order by select having group by from select from where group by having order by.

shop_id shop_name1 Wisconsin Coffee Shop2 College Coffee Shop3 Northland Coffee Shop

worker_id shop_id name pay_rt weekly_hrs1 1 Kris 8 252 2 Jill 8.5 403 2 Erika 9 404 1 Kim 8.5 305 1 Bill 9 256 3 Bill 9 15

shop_id year quarter revenues1 2008 1 100001 2008 2 75001 2008 3 63002 2008 1 48002 2008 2 73002 2008 3 null

Shops

Workers

Revenues

Page 26: Final Review. put the following items in their correct order where order by select having group by from select from where group by having order by.

shop_id shop_name1 Wisconsin Coffee Shop2 College Coffee Shop3 Northland Coffee Shop

What will the following query return?

select*

fromShops

shop_id shop_name1 Wisconsin Coffee Shop2 College Coffee Shop3 Northland Coffee Shop

Results

Page 27: Final Review. put the following items in their correct order where order by select having group by from select from where group by having order by.

shop_id shop_name1 Wisconsin Coffee Shop2 College Coffee Shop3 Northland Coffee Shop

Write a query that will return all records that have the letter “L” in their shop name

shop_id shop_name2 College Coffee Shop3 Northland Coffee Shop

Results

select*

from Shopswhere shop_name like '%L%'

Page 28: Final Review. put the following items in their correct order where order by select having group by from select from where group by having order by.

shop_id shop_name1 Wisconsin Coffee Shop2 College Coffee Shop3 Northland Coffee Shop

Write a query that will return a count of all records in the shops table

Count(*)3

Results

select count(*)from Shops

Page 29: Final Review. put the following items in their correct order where order by select having group by from select from where group by having order by.

shop_id shop_name1 Wisconsin Coffee Shop2 College Coffee Shop3 Northland Coffee Shop

worker_id shop_id name pay_rt weekly_hrs1 1 Kris 8 252 2 Jill 8.5 403 2 Erika 9 404 1 Kim 8.5 305 1 Bill 9 256 3 Bill 9 15

What will the following query return?

select shop_namefrom Shops sjoin workers w on w.shop_id = s.shop_id and w.name = 'Kris'

shop_nameWisconsin Coffee Shop

Results

Page 30: Final Review. put the following items in their correct order where order by select having group by from select from where group by having order by.

shop_id shop_name1 Wisconsin Coffee Shop2 College Coffee Shop3 Northland Coffee Shop

worker_id shop_id name pay_rt weekly_hrs1 1 Kris 8 252 2 Jill 8.5 403 2 Erika 9 404 1 Kim 8.5 305 1 Bill 9 256 3 Bill 9 15

Write a query that will return average pay rate by shop name

Results

select s.shop_name, avg(w.pay_rt) as 'Avg Pay Rt'from Shops sjoin workers w on w.shop_id = s.shop_idgroup by s.shop_name

shop_name Avg Pay RtCollege Coffee Shop 8.75

Northland Coffee Shop 9Wisconsin Coffee Shop 8.5

Page 31: Final Review. put the following items in their correct order where order by select having group by from select from where group by having order by.

shop_id shop_name1 Wisconsin Coffee Shop2 College Coffee Shop3 Northland Coffee Shop

worker_id shop_id name pay_rt weekly_hrs1 1 Kris 8 252 2 Jill 8.5 403 2 Erika 9 404 1 Kim 8.5 305 1 Bill 9 256 3 Bill 9 15

Write a query that will return average pay rate by shop name. Only display shops with an average pay rate over 8.50

Results

select s.shop_name, avg(w.pay_rt) as 'Avg Pay Rt'from Shops sjoin workers w on w.shop_id = s.shop_idgroup by s.shop_namehaving avg(w.pay_rt) > 8.5

shop_name Avg Pay RtCollege Coffee Shop 8.75

Northland Coffee Shop 9

Page 32: Final Review. put the following items in their correct order where order by select having group by from select from where group by having order by.

shop_id shop_name1 Wisconsin Coffee Shop2 College Coffee Shop3 Northland Coffee Shop

worker_id shop_id name pay_rt weekly_hrs1 1 Kris 8 252 2 Jill 8.5 403 2 Erika 9 404 1 Kim 8.5 305 1 Bill 9 256 3 Bill 9 15

Write a query that will return average pay rate by shop name. Don’t include workers making less then 8.50 and only display shops with an average pay rate over 8.50

Results

select s.shop_name, avg(w.pay_rt) as 'Avg Pay Rt'from Shops sjoin workers w on w.shop_id = s.shop_id and w.pay_rt >= 8.5group by s.shop_namehaving avg(w.pay_rt) > 8.5

shop_name Avg Pay RtCollege Coffee Shop 8.75

Northland Coffee Shop 9Wisconsin Coffee Shop 8.75

Page 33: Final Review. put the following items in their correct order where order by select having group by from select from where group by having order by.

worker_id shop_id name pay_rt weekly_hrs1 1 Kris 8 252 2 Jill 8.5 403 2 Erika 9 404 1 Kim 8.5 305 1 Bill 9 256 3 Bill 9 15

What will the following query return?

Results

select distinct namefrom workers

nameKrisJill

ErikaKimBill

Page 34: Final Review. put the following items in their correct order where order by select having group by from select from where group by having order by.

worker_id shop_id name pay_rt weekly_hrs1 1 Kris 8 252 2 Jill 8.5 403 2 Erika 9 404 1 Kim 8.5 305 1 Bill 9 256 3 Bill 9 15

What will the following query return?

Results

select distinct namefrom workerssort by

1

ERROR

Sort by isn’t a valid clause

Page 35: Final Review. put the following items in their correct order where order by select having group by from select from where group by having order by.

worker_id shop_id name pay_rt weekly_hrs1 1 Kris 8 252 2 Jill 8.5 403 2 Erika 9 404 1 Kim 8.5 305 1 Bill 9 256 3 Bill 9 15

What will the following query return?

Results

select distinct namefrom workersorder by

1

nameBill

ErikaJill

KimKris

Page 36: Final Review. put the following items in their correct order where order by select having group by from select from where group by having order by.

put the following items in their correct order

whereorder byselecthavinggroup byfrom

selectfromwheregroup byhavingorder by

Page 37: Final Review. put the following items in their correct order where order by select having group by from select from where group by having order by.

shop_id shop_name1 Wisconsin Coffee Shop2 College Coffee Shop3 Northland Coffee Shop

shop_id year quarter revenues1 2008 1 100001 2008 2 75001 2008 3 63002 2008 1 48002 2008 2 73002 2008 3 null

What will the following query return?

select s.shop_name, sum(r.revenues)from shops sleft outer join revenues r on r.shop_id = s.shop_id

Results

ERROR

Query is missing a group by

Page 38: Final Review. put the following items in their correct order where order by select having group by from select from where group by having order by.

shop_id shop_name1 Wisconsin Coffee Shop2 College Coffee Shop3 Northland Coffee Shop

shop_id year quarter revenues1 2008 1 100001 2008 2 75001 2008 3 63002 2008 1 48002 2008 2 73002 2008 3 null

What will the following query return?

select s.shop_name, sum(r.revenues)from shops sleft outer join revenues r on r.shop_id = s.shop_idgroup by s.shop_name

Results

shop_name sum(r.revenues)College Coffee Shop 12100

Northland Coffee Shop Wisconsin Coffee Shop 23800

Page 39: Final Review. put the following items in their correct order where order by select having group by from select from where group by having order by.

Explain left outer joins

Left outer join:

Returns data from the table on the left regardless if it has a match in the table on the right. If there is not a match on the right, null values are returned.

Page 40: Final Review. put the following items in their correct order where order by select having group by from select from where group by having order by.

Explain right outer joins

right outer join:

Returns data from the table on the right regardless if it has a match in the table on the left. If there is not a match on the left, null values are returned.

Page 41: Final Review. put the following items in their correct order where order by select having group by from select from where group by having order by.

All operator

condition is true only if it satisfies all values

Page 42: Final Review. put the following items in their correct order where order by select having group by from select from where group by having order by.

Any operator

condition is true only if it satisfies any value

Page 43: Final Review. put the following items in their correct order where order by select having group by from select from where group by having order by.

shop_id shop_name1 Wisconsin Coffee Shop2 College Coffee Shop3 Northland Coffee Shop

shop_id year quarter revenues1 2008 1 100001 2008 2 75001 2008 3 63002 2008 1 48002 2008 2 73002 2008 3 null

Write a query that will list shops that do not have any recorded revenues (use IN in your query)

select shop_namefrom shopswhere shop_id not in ( select shop_id from revenues )

Results

shop_nameNorthland Coffee Shop

Page 44: Final Review. put the following items in their correct order where order by select having group by from select from where group by having order by.

shop_id shop_name1 Wisconsin Coffee Shop2 College Coffee Shop3 Northland Coffee Shop

shop_id year quarter revenues1 2008 1 100001 2008 2 75001 2008 3 63002 2008 1 48002 2008 2 73002 2008 3 null

Write a query that will list shops that do not have any recorded revenues (use EXISTS in your query)

select shop_namefrom shops swhere not exists ( select shop_id from revenues r

wherer.shop_id = s.shop_id

)Results

shop_nameNorthland Coffee Shop

Page 45: Final Review. put the following items in their correct order where order by select having group by from select from where group by having order by.

What is a correlated subquery?

A subquery that involves a table listed in outer query

Page 46: Final Review. put the following items in their correct order where order by select having group by from select from where group by having order by.

46

Car_Dealer_ID Name1 Kris Windorski2 Al Pacino3 Paris Hilton4 Jessica Simpson5 A.J. Hawk

Sale_ID Car Cost Car_Dealer_ID1 Porsche 40,000 12 Cavalier 60,000 13 Mustang 30,000 34 Vibe 19,000 45 BMW 42,000 46 Ferrari 65,000 27 Lexus 61,000 2

select name, carfrom car_dealer c

left outer join sales s on s.car_dealer_id = c.car_dealer_id and s.cost > 50000

What will the following query return?

name carKris Windorski Cavalier

Al Pacino FerrariAl Pacino Lexus

Paris Hilton nullJessica Simpson null

A.J. Hawk null

Results

Page 47: Final Review. put the following items in their correct order where order by select having group by from select from where group by having order by.

47

Car_Dealer_ID Name1 Kris Windorski2 Al Pacino3 Paris Hilton4 Jessica Simpson5 A.J. Hawk

Sale_ID Car Cost Car_Dealer_ID1 Porsche 40,000 12 Cavalier 60,000 13 Mustang 30,000 34 Vibe 19,000 45 BMW 42,000 46 Ferrari 65,000 27 Lexus 61,000 2

select name, carfrom car_dealer c

left outer join sales s on s.car_dealer_id = c.car_dealer_id

where s.cost > 50000

What will the following query return?

name carKris Windorski Cavalier

Al Pacino FerrariAl Pacino Lexus

Results

Page 48: Final Review. put the following items in their correct order where order by select having group by from select from where group by having order by.

Why do these queries produce different results?

48

select name, carfrom car_dealer c

left outer join sales s on s.car_dealer_id = c.car_dealer_id and s.cost > 50000

select name, carfrom car_dealer c

left outer join sales s on s.car_dealer_id = c.car_dealer_id

where s.cost > 50000

When a query only has inner joins a filter placed in the “where” clause will yield the same result as placing the filter in the “from” clause.

However, the same cannot be said when dealing with outer joins

Let’s take a closer look

Page 49: Final Review. put the following items in their correct order where order by select having group by from select from where group by having order by.

49

Car_Dealer_ID Name1 Kris Windorski2 Al Pacino3 Paris Hilton4 Jessica Simpson5 A.J. Hawk

Sale_ID Car Cost Car_Dealer_ID1 Porsche 40,000 12 Cavalier 60,000 13 Mustang 30,000 34 Vibe 19,000 45 BMW 42,000 46 Ferrari 65,000 27 Lexus 61,000 2

select name, carfrom car_dealer c

left outer join sales s on s.car_dealer_id = c.car_dealer_id and s.cost > 50000

A left outer join returns data from the table on the left regardless if it has a match in the table on the right.

In this query in order for there to be a match between the two tables the join criteria must be met, which includes the following :

- Matching car dealer ids- A cost greater then 50,000

Page 50: Final Review. put the following items in their correct order where order by select having group by from select from where group by having order by.

50

Car_Dealer_ID Name1 Kris Windorski2 Al Pacino3 Paris Hilton4 Jessica Simpson5 A.J. Hawk

Sale_ID Car Cost Car_Dealer_ID1 Porsche 40,000 12 Cavalier 60,000 13 Mustang 30,000 34 Vibe 19,000 45 BMW 42,000 46 Ferrari 65,000 27 Lexus 61,000 2

select name, carfrom car_dealer c

left outer join sales s on s.car_dealer_id = c.car_dealer_id and s.cost > 50000

name carKris Windorski Cavalier

Al Pacino FerrariAl Pacino Lexus

Paris Hilton nullJessica Simpson null

A.J. Hawk null

Results

The car_dealer table is the table on the left, so we know every car dealer’s name will be returned regadless if it satisfies the join criteria

Page 51: Final Review. put the following items in their correct order where order by select having group by from select from where group by having order by.

51

Car_Dealer_ID Name1 Kris Windorski2 Al Pacino3 Paris Hilton4 Jessica Simpson5 A.J. Hawk

Sale_ID Car Cost Car_Dealer_ID1 Porsche 40,000 12 Cavalier 60,000 13 Mustang 30,000 34 Vibe 19,000 45 BMW 42,000 46 Ferrari 65,000 27 Lexus 61,000 2

select name, carfrom car_dealer c

left outer join sales s on s.car_dealer_id = c.car_dealer_id and s.cost > 50000

name carKris Windorski Cavalier

Al Pacino FerrariAl Pacino Lexus

Paris Hilton nullJessica Simpson null

A.J. Hawk null

Results

If the join condition is satisfied the information from the right (in this case the sales table) will also be returned. If the condition is not met null values will be returned from the right.

Page 52: Final Review. put the following items in their correct order where order by select having group by from select from where group by having order by.

52

Car_Dealer_ID Name1 Kris Windorski2 Al Pacino3 Paris Hilton4 Jessica Simpson5 A.J. Hawk

Sale_ID Car Cost Car_Dealer_ID1 Porsche 40,000 12 Cavalier 60,000 13 Mustang 30,000 34 Vibe 19,000 45 BMW 42,000 46 Ferrari 65,000 27 Lexus 61,000 2

select name, carfrom car_dealer c

left outer join sales s on s.car_dealer_id = c.car_dealer_id

where s.cost > 50000

A left outer join returns data from the table on the left regardless if it has a match in the table on the right.

In this query in order for there to be a match between the two tables the join criteria must be met, which includes the following :

- Matching car dealer ids

Note: the “where” clause also contains join/filter criteria, but logically it is applied after the join/filter criteria in the “from” clause have been applied

Page 53: Final Review. put the following items in their correct order where order by select having group by from select from where group by having order by.

Union vs. Union all

Page 54: Final Review. put the following items in their correct order where order by select having group by from select from where group by having order by.

Instructor_ID Name

1 Doug Waterman

2 Terri Keane

3 Glen Orsburn

4 Kris Windorski

Class_NBR Instructor_ID Name

1111 4 SQL

4444 1 VB.net

2222 3 SQL

5555 2 C++

99999 4 Accounting

7777 4 SQL

What will the following query return?

54

select i.name as 'Instructor Name', c.name as 'Class Name'from instructor i, class c

Instructor Name Class NameDoug Waterman SQL

Terri Keane SQLGlen Orsburn SQLKris Windorski SQL

Doug Waterman VB.netTerri Keane VB.net

Glen Orsburn VB.netKris Windorski VB.net

Doug Waterman SQLTerri Keane SQL

Glen Orsburn SQLKris Windorski SQL

Doug Waterman C++Terri Keane C++

Glen Orsburn C++Kris Windorski C++

Doug Waterman AccountingTerri Keane Accounting

Glen Orsburn AccountingKris Windorski Accounting

Doug Waterman SQLTerri Keane SQL

Glen Orsburn SQLKris Windorski SQL

Results

A join is not specified for the tables

SQL returns every possible combination

Referred to as a Cartesian Product

Page 55: Final Review. put the following items in their correct order where order by select having group by from select from where group by having order by.

put the following items in their correct order

whereorder byselecthavinggroup byfrom

selectfromwheregroup byhavingorder by