28
While you are waiting for class to start... (1)Login to SQL Server 2012 Management Studio (2) Execute the file called “SQLLab4.sql”. It is located on the k: drive in the IS475\ LabFiles folder. You will see errors, and then the tables will create and populate. There are six tables created and populated with this file. (3) Look at the content of the tables

While you are waiting for class to start

  • Upload
    clare

  • View
    27

  • Download
    0

Embed Size (px)

DESCRIPTION

While you are waiting for class to start. Login to SQL Server 2012 Management Studio (2) Execute the file called “ SQLLab4.sql ”. It is located on the k: drive in the IS475\ LabFiles folder. - PowerPoint PPT Presentation

Citation preview

Page 1: While you are waiting for class to start

While you are waiting for class to start...

(1) Login to SQL Server 2012 Management Studio

(2) Execute the file called “SQLLab4.sql”. It is located on the k: drive in the IS475\LabFiles folder.You will see errors, and then the tables will create and populate. There are six tables created and populated with this file.

(3) Look at the content of the tables

Page 2: While you are waiting for class to start

2

Topics for today – 03/13/2014

Review and reinforce knowledge of joins. Discuss another twist on the join – a self-join. Practice multiple-table joins. Review non-correlated sub-queries. Learn about correlated sub-queries. Work on HW#7.

Page 3: While you are waiting for class to start
Page 4: While you are waiting for class to start

4

Know your tables

How many relationships in the database? Which table is the child and which is the parent

in each relationship? What are the data types of the primary keys? How many rows are in each table? Are there any concatenated keys in the

database?

Page 5: While you are waiting for class to start

This is the result table I want to produce. Which underlying tables do I need to use?

Page 6: While you are waiting for class to start

Review joins

Purpose is to combine more than one underlying table into a single result table.

A join operation multiplies the two underlying tables: the rows are multiplied and the columns are added together to create a single result table.

Cross join demonstration:

SELECT *FROM xemp,

xjobtitle

Page 7: While you are waiting for class to start

Types of joins: INNER JOIN

Inner join: Combines the rows from two tables where the foreign key in the child table equals the primary key in the parent table.

Result table: Consists only of those rows from both tables where the primary key = foreign key.

Example:

SELECT *FROM xemp emp INNER JOIN xjobtitle jtON emp.jobtitleID = jt.jobtitleID

Page 8: While you are waiting for class to start

Types of joins: OUTER JOIN

Outer join: Combines the rows from two tables where the foreign key in the child table equals the primary key in the parent table. In addition, an outer join will include those rows in one (left, right) or both (full) tables that do not match the other table.

Result table: Consists of those rows from both tables where the primary key = foreign key plus those rows from one or both tables where the primary key does not have a direct match.

Example:

SELECT *FROM xemp empLEFT OUTER JOIN xjobtitle jtON emp.jobtitleID = jt.jobtitleID

Page 9: While you are waiting for class to start

Limit the columns to get the desired result

SELECT emp.lastname,emp.firstname,emp.hiredate,emp.billingrate,emp.jobtitleid,ISNULL(jt.title, 'No Matching Title') title

FROM xemp emp LEFT OUTER JOIN xjobtitle jtON emp.jobtitleID = jt.jobtitleIDORDER BY emp.lastname

Page 10: While you are waiting for class to start

What do the two other outer joins do??SELECT emp.lastname,

emp.firstname,emp.hiredate,emp.billingrate,emp.jobtitleid,ISNULL(jt.title, 'No Matching Title') title

FROM xemp emp RIGHT OUTER JOIN xjobtitle jtON emp.jobtitleID = jt.jobtitleIDORDER BY emp.lastname

SELECT emp.lastname,emp.firstname,emp.hiredate,emp.billingrate,emp.jobtitleid,ISNULL(jt.title, 'No Matching Title') title

FROM xemp emp FULL OUTER JOIN xjobtitle jtON emp.jobtitleID = jt.jobtitleIDORDER BY emp.lastname

Page 11: While you are waiting for class to start

11

Self-join

What if you want to add the last name of the manager for each employee?

Page 12: While you are waiting for class to start

Become familiar with the xEmp table.

select empid,lastname,firstname,managerid

from xEmp;

Realize that the ManagerID in the EMP table represents the EmpID of the person who is the manager for that employee.

To get the name of the manager (look at previous slide for the desired output from the query) requires that you search the EMP table twice:1) Reading sequentially for each employee in the database;2) Reading again to find the name of the manager for each employee.

SQL does not let you read a table twice in a single query, so you must have two copies of the EMP table in memory in order to accomplish this operation. Those two copies must have table name aliases so that SQL Server (our DBMS) can differentiate them.

In order to identify the name of the manager for a given employee, we need to join the two tables. There must be a foreign key to self-join two tables. In this sample, the foreign key is the ManagerID. Type in the code on the next slide to join two copies of the EMP table on the foreign key of ManagerID and primary key of EmpID.

Page 13: While you are waiting for class to start

SQL Self-Join

Type in this code to join two copies of the same table:

SELECT emp.lastname,emp.firstname,emp.hiredate,emp.billingrate,emp.jobtitleid,ISNULL(jt.title, 'No Matching Title') title,ISNULL(manager.lastname, '**No Manager**') 'Manager

Last Name'FROM xemp emp LEFT OUTER JOIN xjobtitle jtON emp.jobtitleID = jt.jobtitleIDINNER JOIN xemp managerON emp.managerid = manager.empidORDER BY emp.lastname

DOES IT WORK???

Page 14: While you are waiting for class to start

Let’s join some more tables!

SELECT emp.lastname,emp.firstname,emp.hiredate,emp.billingrate,emp.jobtitleid,ISNULL(jt.title, 'No Matching Title') title,ISNULL(manager.lastname, '**No Manager**') 'Manager Last

Name',tw.StartWork,tw.Minutes

FROM xemp emp LEFT OUTER JOIN xjobtitle jtON emp.jobtitleID = jt.jobtitleIDLEFT OUTER JOIN xemp managerON emp.managerid = manager.empidINNER JOIN xtimeworked twON tw.empid = emp.empidWHERE emp.lastname IN ('Chu', 'Jenkins')ORDER BY emp.lastname

Page 15: While you are waiting for class to start

SELECT emp.lastname,emp.firstname,emp.billingrate,ISNULL(jt.title, 'No Matching Title') title,ISNULL(manager.lastname, '**No Manager**') 'Manager Last

Name',tw.StartWork,tw.Minutes,work.stdbillrate,work.description

FROM xemp emp LEFT OUTER JOIN xjobtitle jtON emp.jobtitleID = jt.jobtitleIDLEFT OUTER JOIN xemp managerON emp.managerid = manager.empidINNER JOIN xtimeworked twON tw.empid = emp.empidINNER JOIN xwork workON tw.worktypeid = work.worktypeidWHERE emp.lastname IN ('Chu', 'Jenkins')ORDER BY emp.lastname

Page 16: While you are waiting for class to start

SELECT emp.lastname,emp.firstname,emp.billingrate,ISNULL(jt.title, 'No Matching Title') title,ISNULL(manager.lastname, '**No Manager**') 'Manager Last Name',tw.StartWork,tw.Minutes,work.stdbillrate,work.description,contract.datesigned,contract.datedue

FROM xemp emp LEFT OUTER JOINxjobtitle jtON emp.jobtitleID = jt.jobtitleIDLEFT OUTER JOIN xemp managerON emp.managerid = manager.empidINNER JOIN xtimeworked twON tw.empid = emp.empidINNER JOIN xwork workON tw.worktypeid = work.worktypeidINNER JOIN xcontract contractON tw.contractid = contract.contractidWHERE emp.lastname IN ('Chu', 'Jenkins')ORDER BY emp.lastname

DOES IT WORK???

Page 17: While you are waiting for class to start

SELECT emp.lastname,emp.firstname,emp.billingrate,ISNULL(jt.title, 'No Matching Title') title,ISNULL(manager.lastname, '**No Manager**') 'Manager Last Name',tw.StartWork,tw.Minutes,work.stdbillrate,work.description,contract.datesigned,contract.datedue,client.name 'Client Name'

FROM xemp emp LEFT OUTER JOIN xjobtitle jtON emp.jobtitleID = jt.jobtitleIDLEFT OUTER JOIN xemp managerON emp.managerid = manager.empidINNER JOIN xtimeworked twON tw.empid = emp.empidINNER JOIN xwork workON tw.worktypeid = work.worktypeidLEFT OUTER JOIN xcontract contractON tw.contractid = contract.contractidLEFT OUTER JOIN xclient clientON contract.clientid = client.clientidWHERE emp.lastname IN ('Chu', 'Jenkins')ORDER BY emp.lastname

Page 18: While you are waiting for class to start

Summarize time worked by contract

Page 19: While you are waiting for class to start

SELECT emp.lastname,emp.firstname,emp.billingrate,ISNULL(jt.title, 'No Matching Title') title,ISNULL(manager.lastname, '**No Manager**') 'Manager Last Name',contract.datesigned,contract.datedue,client.name 'Client Name',SUM(tw.Minutes) 'Total Minutes Worked'

FROM xemp emp LEFT OUTER JOIN xjobtitle jtON emp.jobtitleID = jt.jobtitleIDLEFT OUTER JOIN xemp managerON emp.managerid = manager.empidINNER JOIN xtimeworked twON tw.empid = emp.empidLEFT OUTER JOIN xcontract contractON tw.contractid = contract.contractidLEFT OUTER JOIN xclient clientON contract.clientid = client.clientidWHERE emp.lastname IN ('Chu', 'Jenkins')GROUP BY emp.lastname,

emp.firstname,emp.billingrate,jt.title, manager.lastname, contract.datesigned,contract.datedue,client.name

ORDER BY emp.lastname

Page 20: While you are waiting for class to start

20

What is a sub-query?

A sub-query is a query embedded inside another query.

The sub-query is executed in the normal operation of the query in which it is embedded.

The sub-query will return an “answer” result table to the query in which it is embedded.

Page 21: While you are waiting for class to start

SELECT *FROM xEMPWHERE empid NOT IN

(SELECT empid FROM xtimeworked)

Find the employees who are not in the TIMEWORKED table via a subquery.

Outer Query

Inner Query

Page 22: While you are waiting for class to start

SELECT *FROM xEMPWHERE billingrate =

(SELECT max(billingrate) FROM xemp)

Find the employee who has the highest billing rate via a subquery.

Outer Query

Inner Query

Page 23: While you are waiting for class to start

Find which employees have a billing rate higher than the average billing rate using a subquery.

SELECT empID,lastname,billingrate

FROM xempWHERE billingrate >

(SELECT AVG(billingrate)FROM xemp)

The preceding sub-queries are called “non-correlated” sub-queries.

Page 24: While you are waiting for class to start

24

Example of a correlated sub-query

Which employees have a higher BillingRate than the average BillingRate for their job title?

SELECT jobtitleID,AVG(billingrate) AverageBillRate

FROM xempGROUP BY jobtitleID;

Let’s start by understanding the query requirements:

Page 25: While you are waiting for class to start

Now put together a query using a correlated sub-query:

SELECT empID, lastname,

billingrate, jobtitleIDFROM xemp empOuterWHERE billingrate >

(SELECT AVG(billingrate) FROM xemp empInner WHERE empOuter.jobtitleID = empInner.jobtitleID)

Query passes the jobtitleID from the outer query to the

inner query

Must alias the tables since two tables of the same name will be in main memory concurrently.

Must alias the tables since two tables of the same name will be in main memory concurrently.

Page 26: While you are waiting for class to start

To get the job title from the job title table requires the use of a join

SELECT empID, lastname, billingrate, empOuter.jobtitleID, title

FROM xemp empOuterLEFT JOIN xjobtitleON empOuter.jobtitleID = xjobtitle.jobtitleIDWHERE billingrate >

(SELECT AVG(billingrate) FROM xemp empInner WHERE empOuter.jobtitleID = empInner.jobtitleID)

Page 27: While you are waiting for class to start

27

Where are sub-queries placed in SQL code?

Sub-queries can be embedded in the SELECT list, FROM, WHERE or HAVING clauses. So far, all I’ve shown are sub-queries in the WHERE

clause… Sub-queries can be used in INSERT, UPDATE

or DELETE statements.

Page 28: While you are waiting for class to start

Example of a sub-query in the SELECT list:

SELECT empID,lastname,empOuter.jobtitleID,title,billingrate "Employee Billing Rate",(SELECT AVG(billingrate)

FROM xemp empSelect WHERE empOuter.jobtitleID = empSelect.jobtitleID)

"Average Billing Rate" FROM xemp empOuterLEFT OUTER JOIN xjobtitleON empOuter.jobtitleID = xjobtitle.jobtitleIDWHERE billingrate >

(SELECT AVG(billingrate) FROM xemp empInner WHERE empOuter.jobtitleID = empInner.jobtitleID)

How many copies of the EMP table are in memory when this query runs?