Querying and Managing Data Using SQL Server 2005Objectives                In this session, you will learn to:             ...
Querying and Managing Data Using SQL Server 2005Querying Data by Using Joins                Joins                   Allow ...
Querying and Managing Data Using SQL Server 2005Using an Inner Join                  COLUMNS                 COLUMNS      ...
Querying and Managing Data Using SQL Server 2005Using an Inner Join (Contd.)                Inner Join:                   ...
Querying and Managing Data Using SQL Server 2005Just a minute                Why do you need a table alias with the column...
Querying and Managing Data Using SQL Server 2005Using an Outer Join                Outer Join:                   Displays ...
Querying and Managing Data Using SQL Server 2005Using an Outer Join (Contd.)                  COLUMNS                     ...
Querying and Managing Data Using SQL Server 2005Using an Outer Join (Contd.)                  COLUMNS                     ...
Querying and Managing Data Using SQL Server 2005Using an Outer Join (Contd.)                  COLUMNS                     ...
Querying and Managing Data Using SQL Server 2005Using an Outer Join (Contd.)                   Syntax:                    ...
Querying and Managing Data Using SQL Server 2005Just a minute                When do you use the right outer join?        ...
Querying and Managing Data Using SQL Server 2005Using a Cross Join                     COLUMNS                 COLUMNS    ...
Querying and Managing Data Using SQL Server 2005Using a Cross Join (Contd.)                Cross Join:                   D...
Querying and Managing Data Using SQL Server 2005Using an Equi Join                     COLUMNS                 COLUMNS    ...
Querying and Managing Data Using SQL Server 2005Using an Equi Join (Contd.)                Equi Join:                   Is...
Querying and Managing Data Using SQL Server 2005Just a minute                What is the difference between an equi and an...
Querying and Managing Data Using SQL Server 2005Using a Self Join                Self Join:                    Is used whe...
Querying and Managing Data Using SQL Server 2005Demo: Using Joins                Problem Statement:                   The ...
Querying and Managing Data Using SQL Server 2005Demo: Using Joins (Contd.)                Solution:                   To s...
Querying and Managing Data Using SQL Server 2005Querying Data by Using Subqueries                Subqueries:              ...
Querying and Managing Data Using SQL Server 2005Using the IN and EXISTS Keyword                IN keyword:                ...
Querying and Managing Data Using SQL Server 2005Using the IN and EXISTS Keyword (Contd.)                EXISTS keyword:   ...
Querying and Managing Data Using SQL Server 2005Using Modified Comparison Operators                Comparison operators ar...
Querying and Managing Data Using SQL Server 2005Just a minute                What is the use of EXISTS keyword in a subque...
Querying and Managing Data Using SQL Server 2005Using Aggregate Functions                Aggregate functions:             ...
Querying and Managing Data Using SQL Server 2005Using Nested Subqueries                Nested subqueries:                 ...
Querying and Managing Data Using SQL Server 2005Using Correlated Subqueries                Correlated subqueries:         ...
Querying and Managing Data Using SQL Server 2005Just a minute                Write a query to determine the Employee ID an...
Querying and Managing Data Using SQL Server 2005Demo: Using Subqueries               Problem Statement:                  T...
Querying and Managing Data Using SQL Server 2005Demo: Using Subqueries (Contd.)                Solution:                  ...
Querying and Managing Data Using SQL Server 2005Summary               In this session, you learned that:                  ...
Querying and Managing Data Using SQL Server 2005Summary (Contd.)               The IN clause in a subquery returns zero or...
Upcoming SlideShare
Loading in …5
×

04 qmds2005 session05

542 views

Published on

Published in: Technology
  • Be the first to comment

04 qmds2005 session05

  1. 1. Querying and Managing Data Using SQL Server 2005Objectives In this session, you will learn to: Query data by using joins Query data by using subqueries Ver. 1.0 Session 5 Slide 1 of 32
  2. 2. Querying and Managing Data Using SQL Server 2005Querying Data by Using Joins Joins Allow to retrieve data from multiple tables Can be of the following types: Inner join Outer join Cross join Equi join Self join Ver. 1.0 Session 5 Slide 2 of 32
  3. 3. Querying and Managing Data Using SQL Server 2005Using an Inner Join COLUMNS COLUMNS ABC BDE Table X Table Y INNER JOIN ABCDE COMMON ROWS OUTPUT Ver. 1.0 Session 5 Slide 3 of 32
  4. 4. Querying and Managing Data Using SQL Server 2005Using an Inner Join (Contd.) Inner Join: Retrieves data from multiple tables by using a comparison operator on a common column Retrieves only those rows that satisfy the join condition Syntax: SELECT column_name, column_name [,column_name] FROM table1_name JOIN table2_name ON table1_name.ref_column_name join_operator table2_name.ref_column_name Let’s see how… Ver. 1.0 Session 5 Slide 4 of 32
  5. 5. Querying and Managing Data Using SQL Server 2005Just a minute Why do you need a table alias with the column name? Answer: A table alias is required to uniquely identify columns in the SELECT query to avoid ambiguity that arises due to same column names in multiple tables. Ver. 1.0 Session 5 Slide 5 of 32
  6. 6. Querying and Managing Data Using SQL Server 2005Using an Outer Join Outer Join: Displays the result set containing all rows from one table and the matching rows from another table Displays NULL for the columns of the related table where it does not find matching records Is of three types: • Left outer join • Right outer join • Full outer join Ver. 1.0 Session 5 Slide 6 of 32
  7. 7. Querying and Managing Data Using SQL Server 2005Using an Outer Join (Contd.) COLUMNS COLUMNS ABC BDE Table X Table Y LEFT OUTER JOIN ABCDE ALL ROWS FROM TABLE X AND COMMON ROWS FROM TABLE Y OUTPUT Ver. 1.0 Session 5 Slide 7 of 32
  8. 8. Querying and Managing Data Using SQL Server 2005Using an Outer Join (Contd.) COLUMNS COLUMNS ABC BDE Table X Table Y RIGHT OUTER JOIN ABCDE ALL ROWS FROM TABLE Y AND COMMON ROWS FROM TABLE X OUTPUT Ver. 1.0 Session 5 Slide 8 of 32
  9. 9. Querying and Managing Data Using SQL Server 2005Using an Outer Join (Contd.) COLUMNS COLUMNS ABC BDE Table X Table Y FULL OUTER JOIN ABCDE ALL ROWS FROM TABLE Y AND TABLE Y AND COMMON ROWS ONLY ONCE OUTPUT Ver. 1.0 Session 5 Slide 9 of 32
  10. 10. Querying and Managing Data Using SQL Server 2005Using an Outer Join (Contd.) Syntax: SELECT column_name, column_name [,column_name] FROM table1_name [LEFT | RIGHT | FULL] OUTER JOIN table2_name ON table1_name.ref_column_name join_operator table2_name.ref_column_name Let’s see how… Ver. 1.0 Session 5 Slide 10 of 32
  11. 11. Querying and Managing Data Using SQL Server 2005Just a minute When do you use the right outer join? Answer: You can use the right outer join when you need all the records from the table at the right side of the outer join and only the matching records from the the table at the left side of the outer join. Ver. 1.0 Session 5 Slide 11 of 32
  12. 12. Querying and Managing Data Using SQL Server 2005Using a Cross Join COLUMNS COLUMNS ABC BDE Table X Table Y n ROWS CROSS JOIN m ROWS ABCDE ALL ROWS (n X m) EACH ROW OF TABLE X JOINED WITH EACH ROW OF TABLE Y OUTPUT Ver. 1.0 Session 5 Slide 12 of 32
  13. 13. Querying and Managing Data Using SQL Server 2005Using a Cross Join (Contd.) Cross Join: Displays each row from the first table joined with each row from the second table Produces the result set as the number of rows in the first table multiplied by the number of rows in the second table Let’s see how… Ver. 1.0 Session 5 Slide 13 of 32
  14. 14. Querying and Managing Data Using SQL Server 2005Using an Equi Join COLUMNS COLUMNS ABC BDE Table X Table Y EQUI JOIN ABCDBE COMMON ROWS OUTPUT Ver. 1.0 Session 5 Slide 14 of 32
  15. 15. Querying and Managing Data Using SQL Server 2005Using an Equi Join (Contd.) Equi Join: Is same as an inner join Displays all the columns from both the tables Displays redundant column data in the result set Let’s see how… Ver. 1.0 Session 5 Slide 15 of 32
  16. 16. Querying and Managing Data Using SQL Server 2005Just a minute What is the difference between an equi and an inner join? Answer: An equi join is used to retrieve all the columns from both the tables. An inner join is used to retrieve selected columns from tables. Ver. 1.0 Session 5 Slide 16 of 32
  17. 17. Querying and Managing Data Using SQL Server 2005Using a Self Join Self Join: Is used when one row in a table correlates with other rows in the same table Uses alias name to differentiate the two copies of the same table Let’s see how… Ver. 1.0 Session 5 Slide 17 of 32
  18. 18. Querying and Managing Data Using SQL Server 2005Demo: Using Joins Problem Statement: The HR manager of AdventureWorks, Inc. requires a report containing the following details: Employee ID Employee Name Department Name Date of Joining Employee Address How will you generate this report? Ver. 1.0 Session 5 Slide 18 of 32
  19. 19. Querying and Managing Data Using SQL Server 2005Demo: Using Joins (Contd.) Solution: To solve the preceding problem, you need to perform the following tasks: 1. Identify the join. 2. Create a query based on joins. 3. Execute the query to verify the result. Ver. 1.0 Session 5 Slide 19 of 32
  20. 20. Querying and Managing Data Using SQL Server 2005Querying Data by Using Subqueries Subqueries: Are used when the result set of one query needs to be taken as input for another query Syntax: SELECT column, column [,column] FROM table_name WHERE column = ( SELECT column FROM table_name [WHERE conditional_expression] ) Ver. 1.0 Session 5 Slide 20 of 32
  21. 21. Querying and Managing Data Using SQL Server 2005Using the IN and EXISTS Keyword IN keyword: Is used to retrieve rows in a subquery based on the match of values given in a list Syntax: SELECT column, column [,column] FROM table_name WHERE column [ NOT ] IN ( SELECT column FROM table_name [WHERE conditional_expression] ) Let’s see how… Ver. 1.0 Session 5 Slide 21 of 32
  22. 22. Querying and Managing Data Using SQL Server 2005Using the IN and EXISTS Keyword (Contd.) EXISTS keyword: Is used to check the existence of the data and returns true or false Syntax: SELECT column, column [,column] FROM table_name WHERE EXISTS ( SELECT column FROM table_name [WHERE conditional_expression] ) Let’s see how… Ver. 1.0 Session 5 Slide 22 of 32
  23. 23. Querying and Managing Data Using SQL Server 2005Using Modified Comparison Operators Comparison operators are modified using: ALL keyword that returns TRUE, if all the values retrieved by the subquery satisfy the comparison operator ANY keyword that returns TRUE, if any value retrieved by the subquery satisfies the comparison operator Let’s see how… Ver. 1.0 Session 5 Slide 23 of 32
  24. 24. Querying and Managing Data Using SQL Server 2005Just a minute What is the use of EXISTS keyword in a subquery? Answer: The EXISTS keyword is used to check the existence of rows in the result set of an inner query according to the condition specified in the inner query. Ver. 1.0 Session 5 Slide 24 of 32
  25. 25. Querying and Managing Data Using SQL Server 2005Using Aggregate Functions Aggregate functions: Can be used in the subquery to generate aggregated values from the inner query Let’s see how… Ver. 1.0 Session 5 Slide 25 of 32
  26. 26. Querying and Managing Data Using SQL Server 2005Using Nested Subqueries Nested subqueries: Contain one or more subqueries Can be used when the condition of a query is dependent on the result of another query Let’s see how… Ver. 1.0 Session 5 Slide 26 of 32
  27. 27. Querying and Managing Data Using SQL Server 2005Using Correlated Subqueries Correlated subqueries: Can be defined as a query that depends on the outer query for its evaluation Uses the WHERE clause to refer to the table specified in the FROM clause Let’s see how… Ver. 1.0 Session 5 Slide 27 of 32
  28. 28. Querying and Managing Data Using SQL Server 2005Just a minute Write a query to determine the Employee ID and the Department ID of all the employees whose Manager ID is 12. Answer: SELECT EmployeeID, DepartmentID FROM HumanResources.EmployeeDepartmentHistory WHERE EmployeeID=(SELECT EmployeeID FROM HumanResources.Employee WHERE ManagerID=12) Ver. 1.0 Session 5 Slide 28 of 32
  29. 29. Querying and Managing Data Using SQL Server 2005Demo: Using Subqueries Problem Statement: The management of AdventureWorks, Inc. is planning to revise the pay rate of the employees. For this, they want a report containing the EmployeeID and designation of those employees whose present pay rate is more than 40. How will you generate this report? Ver. 1.0 Session 5 Slide 29 of 32
  30. 30. Querying and Managing Data Using SQL Server 2005Demo: Using Subqueries (Contd.) Solution: To solve the preceding problem, you need to perform the following tasks: 1. Create a query. 2. Execute the query to verify the result. Ver. 1.0 Session 5 Slide 30 of 32
  31. 31. Querying and Managing Data Using SQL Server 2005Summary In this session, you learned that: Joins and subqueries are used to retrieve data from multiple tables. An inner join combines records from multiple tables by using a comparison operator on a common column. A left outer join returns all the rows from the left table and the matching rows from the right table. A right outer join returns all the rows from the right table and the matching rows from the left table. A full outer join returns all the matching and non-matching rows from both the tables on which join is applied. A cross join returns each row from the first table joined with each row from the second table. An equi join is used to list all the columns from the joining tables. Ver. 1.0 Session 5 Slide 31 of 32
  32. 32. Querying and Managing Data Using SQL Server 2005Summary (Contd.) The IN clause in a subquery returns zero or more values. The EXISTS clause in a subquery returns data in terms of a TRUE or FALSE value. The ALL and ANY keyword is used in a subquery to modify the existing comparison operator. Aggregate functions can also be used in subqueries to generate aggregated values from the inner query. Subqueries that contain one or more queries are specified as nested subqueries. A correlated subquery can be defined as a query that depends on the outer query for its evaluation. A self join correlates one row in a table with other rows in the same table. Ver. 1.0 Session 5 Slide 32 of 32

×