Presentation is loading. Please wait.

Presentation is loading. Please wait.

+ Complex SQL Week 9. + Today’s Objectives TOP GROUP BY JOIN Inner vs. Outer Right vs. Left.

Similar presentations


Presentation on theme: "+ Complex SQL Week 9. + Today’s Objectives TOP GROUP BY JOIN Inner vs. Outer Right vs. Left."— Presentation transcript:

1 + Complex SQL Week 9

2 + Today’s Objectives TOP GROUP BY JOIN Inner vs. Outer Right vs. Left

3 + TOP Clause specify how many rows to return. This can be useful on very large tables when there are thousands of records. Returning thousands of records can impact on performance, and if you are working with a production database, this could have an adverse impact on the users. SELECT TOP (3) EmpName FROM EMPLOYEE Particularly useful with ORDER BY

4 + GROUP BY Sample: SELECT Department, COUNT(*) AS NumOfEmployees FROM EMPLOYEE GROUP BY Department; Allows a function (COUNT) and column names in the SELECT clause to be together

5 + GROUP BY SELECT Department, COUNT(*) AS NumOfEmployees FROM EMPLOYEE GROUP BY Department; Note this is information about departments, but is querying the EMPLOYEE table!

6 + GROUP BY GROUP BY (with HAVING) Further restrict the results obtained Sample: SELECT Department, COUNT(*) AS NumOfEmployees FROM EMPLOYEE GROUP BY Department HAVING COUNT(*) > 1;

7 + GROUP BY SELECT Department, COUNT(*) AS NumOfEmployees FROM EMPLOYEE GROUP BY Department HAVING COUNT(*) > 1;

8 + SQL for Data Retrieval: Retrieving Information from Multiple Tables Joins Another way of combining data is by using a join. Join [also called an Inner Join, default is Inner] Left Outer Join Right Outer Join KROENKE and AUER - DATABASE CONCEPTS (6th Edition) Copyright © 2013 Pearson Education, Inc. Publishing as Prentice Hall

9 + SQL for Data Retrieval: Join Example Employee Names are stored in the EMPLOYEE table. Department Names are stored in the DEPARTMENT table. We want a list of employee names from the Accounting Department. SELECT EmpName FROM EMPLOYEE AS E, DEPARTMENT AS D WHERE E.DeptID = D.DeptID AND D.DeptName LIKE 'Account%'; KROENKE and AUER - DATABASE CONCEPTS (6th Edition) Copyright © 2013 Pearson Education, Inc. Publishing as Prentice Hall

10 + SQL for Data Retrieval: JOIN…ON Example The JOIN…ON syntax can be used in joins. It has the advantage of moving the JOIN syntax into the FROM clause. SELECT EmpName FROM EMPLOYEE AS E JOIN DEPARTMENT AS D ON E.DeptID = D.DeptID WHERE D.DeptName LIKE 'Account%'; KROENKE and AUER - DATABASE CONCEPTS (6th Edition) Copyright © 2013 Pearson Education, Inc. Publishing as Prentice Hall

11 + Inner vs. Outer An “Outer” join would include the data that met part of the where clause from each of the joined tables example of a “Left” outer join. SELECT E.EmpName, D.DeptName FROM EMPLOYEE AS E LEFT JOIN DEPARTMENT AS D ON E.DeptID = D.DeptID; Results are included if they meet the LEFT side of the FROM clause (they exist in EMPLOYEE and meet the WHERE clause) This query will give us all employee names, even if they have no department assigned (NULL will be in the DeptName field)

12 + Right vs. Left A right outer join would be one in which you included results that matched on the RIGHT side of the FROM clause example of a “Right” outer join. SELECT E.EmpName, D.DeptName FROM EMPLOYEE AS E RIGHT JOIN DEPARTMENT AS D ON E.DeptID = D.DeptID; Results are included if they exist in DEPARTMENT and meet the WHERE clause) In this case a department with no employees would be listed, the fields in the EmpName Column would be NULL

13 + SQL for Data Retrieval: LEFT OUTER JOIN Example The OUTER JOIN syntax can be used to obtain data that exists in one table without matching data in the other table. SELECT E.EmpLastName, E.EmpFirstName, D.DeptName FROM EMPLOYEE AS E LEFT JOIN DEPARTMENT AS D ON E.DeptID = D.DeptID WHERE E.EmpLastName LIKE ’S%'; Get all the Employee first and last names and the Department name, include all employees, even if they have no department, but only get those who’s Last name starts with “S”. KROENKE and AUER - DATABASE CONCEPTS (6th Edition) Copyright © 2013 Pearson Education, Inc. Publishing as Prentice Hall

14 + SQL for Data Retrieval: RIGHT OUTER JOIN Example The unmatched data displayed can be from either table, depending on whether RIGHT JOIN or LEFT JOIN is used. SELECT E.EmpName, D.DeptName FROM EMPLOYEE AS E RIGHT JOIN DEPARTMENT AS D ON E.DeptID = D.DeptID WHERE D.DeptName LIKE 'Account%'; Get the Employee Names and Department Names, of all employees who are in a department starting with “Account”. If no employees are in a department starting with “Account” still include the department name and show NULL for EmpName. KROENKE and AUER - DATABASE CONCEPTS (6th Edition) Copyright © 2013 Pearson Education, Inc. Publishing as Prentice Hall

15 + Multiple Joins You can use multiple joins together. Here an inner join is used in ( ) then that result is right outer joined to EMPLOYEE (so you could get employees listed who have no projects or assignments) example of multiple joins. SELECT P.ProjectName, A.HoursWorked, E.FirstName, E.LastName FROM (PROJECT AS P JOIN ASSIGNMENT AS A ON P.ProjectID = A.ProjectID) RIGHT JOIN EMPLOYEE AS E ON A.EmployeeNumber = E.EmployeeNumber ORDER BY P.ProjectID, A.EmployeeNumber;

16 + Class Activity Write an SQL statement to list the Golf Course Name, Par for hole, and Yards for hole for all holes that are greater than 100 yards. Write an SQL statement to list the players names and their handicap (even if they don’t have one) for players that have a played at least 1 round of golf.

17 + Class Activity Find all the players that have not golf any rounds yet List any holes and comments for it, that have special comments in the database Who has played the most holes so far?


Download ppt "+ Complex SQL Week 9. + Today’s Objectives TOP GROUP BY JOIN Inner vs. Outer Right vs. Left."

Similar presentations


Ads by Google