Presentation is loading. Please wait.

Presentation is loading. Please wait.

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.

Similar presentations


Presentation on theme: "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."— Presentation transcript:

1 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

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

3 3

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? 4

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

6 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 * FROMxemp, xjobtitle

7 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 * FROMxemp emp INNER JOINxjobtitle jt ONemp.jobtitleID = jt.jobtitleID

8 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 * FROMxemp emp LEFT OUTER JOINxjobtitle jt ONemp.jobtitleID = jt.jobtitleID

9 Limit the columns to get the desired result 9 SELECT emp.lastname, emp.firstname, emp.hiredate, emp.billingrate, emp.jobtitleid, ISNULL(jt.title, 'No Matching Title') title FROMxemp emp LEFT OUTER JOINxjobtitle jt ONemp.jobtitleID = jt.jobtitleID ORDER BYemp.lastname

10 What do the two other outer joins do?? 10 SELECT emp.lastname, emp.firstname, emp.hiredate, emp.billingrate, emp.jobtitleid, ISNULL(jt.title, 'No Matching Title') title FROMxemp emp RIGHT OUTER JOINxjobtitle jt ONemp.jobtitleID = jt.jobtitleID ORDER BYemp.lastname SELECT emp.lastname, emp.firstname, emp.hiredate, emp.billingrate, emp.jobtitleid, ISNULL(jt.title, 'No Matching Title') title FROMxemp emp FULL OUTER JOINxjobtitle jt ONemp.jobtitleID = jt.jobtitleID ORDER BYemp.lastname

11 Self-join What if you want to add the last name of the manager for each employee? 11

12 12 Become familiar with the xEmp table. selectempid, lastname, firstname, managerid fromxEmp; 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.

13 SQL Self-Join 13 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' FROMxemp emp LEFT OUTER JOINxjobtitle jt ONemp.jobtitleID = jt.jobtitleID INNER JOIN xemp manager ON emp.managerid = manager.empid ORDER BY emp.lastname DOES IT WORK???

14 Let’s join some more tables! 14 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 FROMxemp emp LEFT OUTER JOINxjobtitle jt ONemp.jobtitleID = jt.jobtitleID LEFT OUTER JOIN xemp manager ON emp.managerid = manager.empid INNER JOIN xtimeworked tw ONtw.empid = emp.empid WHERE emp.lastname IN ('Chu', 'Jenkins') ORDER BY emp.lastname

15 15 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 FROMxemp emp LEFT OUTER JOINxjobtitle jt ONemp.jobtitleID = jt.jobtitleID LEFT OUTER JOIN xemp manager ON emp.managerid = manager.empid INNER JOIN xtimeworked tw ONtw.empid = emp.empid INNER JOIN xwork work ON tw.worktypeid = work.worktypeid WHERE emp.lastname IN ('Chu', 'Jenkins') ORDER BYemp.lastname

16 16 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 FROMxemp emp LEFT OUTER JOINxjobtitle jt ONemp.jobtitleID = jt.jobtitleID LEFT OUTER JOIN xemp manager ON emp.managerid = manager.empid INNER JOIN xtimeworked tw ONtw.empid = emp.empid INNER JOIN xwork work ON tw.worktypeid = work.worktypeid INNER JOINxcontract contract ONtw.contractid = contract.contractid WHERE emp.lastname IN ('Chu', 'Jenkins') ORDER BYemp.lastname DOES IT WORK???

17 17 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' FROMxemp emp LEFT OUTER JOINxjobtitle jt ONemp.jobtitleID = jt.jobtitleID LEFT OUTER JOIN xemp manager ON emp.managerid = manager.empid INNER JOIN xtimeworked tw ONtw.empid = emp.empid INNER JOIN xwork work ON tw.worktypeid = work.worktypeid LEFT OUTER JOINxcontract contract ONtw.contractid = contract.contractid LEFT OUTER JOIN xclient client ONcontract.clientid = client.clientid WHERE emp.lastname IN ('Chu', 'Jenkins') ORDER BYemp.lastname

18 Summarize time worked by contract 18

19 19 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' FROMxemp emp LEFT OUTER JOINxjobtitle jt ONemp.jobtitleID = jt.jobtitleID LEFT OUTER JOIN xemp manager ON emp.managerid = manager.empid INNER JOIN xtimeworked tw ONtw.empid = emp.empid LEFT OUTER JOINxcontract contract ONtw.contractid = contract.contractid LEFT OUTER JOIN xclient client ONcontract.clientid = client.clientid WHERE emp.lastname IN ('Chu', 'Jenkins') GROUP BY emp.lastname, emp.firstname, emp.billingrate, jt.title, manager.lastname, contract.datesigned, contract.datedue, client.name ORDER BYemp.lastname

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

21 21 SELECT* FROMxEMP WHERE empid NOT IN (SELECT empid FROM xtimeworked) Find the employees who are not in the TIMEWORKED table via a subquery. Outer Query Inner Query

22 22 SELECT* FROMxEMP WHERE billingrate = (SELECT max(billingrate) FROM xemp) Find the employee who has the highest billing rate via a subquery. Outer Query Inner Query

23 23 Find which employees have a billing rate higher than the average billing rate using a subquery. SELECTempID, lastname, billingrate FROMxemp WHEREbillingrate > (SELECT AVG(billingrate) FROMxemp) The preceding sub-queries are called “non-correlated” sub-queries.

24 24 Example of a correlated sub-query Which employees have a higher BillingRate than the average BillingRate for their job title? SELECTjobtitleID, AVG(billingrate) AverageBillRate FROMxemp GROUP BY jobtitleID; Let’s start by understanding the query requirements:

25 Now put together a query using a correlated sub-query: SELECTempID, lastname, billingrate, jobtitleID FROMxemp empOuter WHEREbillingrate > (SELECT AVG(billingrate) FROM xemp empInner WHERE empOuter.jobtitleID = empInner.jobtitleID) Query passes the jobtitleID from the outer query to the inner query 25 Must alias the tables since two tables of the same name will be in main memory concurrently.

26 To get the job title from the job title table requires the use of a join SELECT empID, lastname, billingrate, empOuter.jobtitleID, title FROMxemp empOuter LEFT JOIN xjobtitle ON empOuter.jobtitleID = xjobtitle.jobtitleID WHERE billingrate > (SELECT AVG(billingrate) FROM xemp empInner WHERE empOuter.jobtitleID = empInner.jobtitleID) 26

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

28 Example of a sub-query in the SELECT list: SELECTempID, lastname, empOuter.jobtitleID, title, billingrate "Employee Billing Rate", (SELECTAVG(billingrate) FROMxemp empSelect WHEREempOuter.jobtitleID = empSelect.jobtitleID) "Average Billing Rate" FROMxemp empOuter LEFT OUTER JOINxjobtitle ONempOuter.jobtitleID = xjobtitle.jobtitleID WHEREbillingrate > (SELECT AVG(billingrate) FROMxemp empInner WHEREempOuter.jobtitleID = empInner.jobtitleID) 28 How many copies of the EMP table are in memory when this query runs?


Download ppt "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."

Similar presentations


Ads by Google