in this session, you will learn to: query data by using joins query data by using subqueries...

32
In this session, you will learn to: Query data by using joins Query data by using subqueries Objectives

Upload: dwayne-marshall

Post on 18-Jan-2018

238 views

Category:

Documents


0 download

DESCRIPTION

A B CB D E A B C D E INNER JOIN Table XTable Y COLUMNS OUTPUT COMMON ROWS Using an Inner Join

TRANSCRIPT

Page 1: In this session, you will learn to: Query data by using joins Query data by using subqueries Objectives

In this session, you will learn to:Query data by using joinsQuery data by using subqueries

Objectives

Page 2: In this session, you will learn to: Query data by using joins Query data by using subqueries Objectives

JoinsAllow to retrieve data from multiple tablesCan be of the following types:

Inner joinOuter joinCross joinEqui joinSelf join

Querying Data by Using Joins

Page 3: In this session, you will learn to: Query data by using joins Query data by using subqueries Objectives

A B C B D E

A B C D E

INNER JOIN

Table X Table Y

COLUMNS COLUMNS

OUTPUT

COMMON ROWS

Using an Inner Join

Page 4: In this session, you will learn to: Query data by using joins Query data by using subqueries Objectives

Inner Join:Retrieves data from multiple tables by using a comparison operator on a common columnRetrieves only those rows that satisfy the join conditionSyntax:SELECT column_name, column_name [,column_name]FROM table1_name JOIN table2_name ON table1_name.ref_column_name join_operatortable2_name.ref_column_name

Let’s see how…

Using an Inner Join (Contd.)

Page 5: In this session, you will learn to: Query data by using joins Query data by using subqueries Objectives

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

Page 6: In this session, you will learn to: Query data by using joins Query data by using subqueries Objectives

Outer Join:Displays the result set containing all rows from one table and the matching rows from another tableDisplays NULL for the columns of the related table where it does not find matching recordsIs of three types:

Left outer joinRight outer join Full outer join

Using an Outer Join

Page 7: In this session, you will learn to: Query data by using joins Query data by using subqueries Objectives

A B C B D E

A B C D E

LEFT OUTER JOIN

Table X Table Y

COLUMNS COLUMNS

OUTPUT

ALL ROWS FROM TABLE X AND COMMON ROWS FROM TABLE Y

Using an Outer Join (Contd.)

Page 8: In this session, you will learn to: Query data by using joins Query data by using subqueries Objectives

A B C B D E

A B C D E

RIGHT OUTER JOIN

Table X Table Y

COLUMNS COLUMNS

OUTPUT

ALL ROWS FROM TABLE Y AND COMMON ROWS FROM TABLE X

Using an Outer Join (Contd.)

Page 9: In this session, you will learn to: Query data by using joins Query data by using subqueries Objectives

Using an Outer Join (Contd.)

A B C B D E

A B C D E

FULL OUTER JOIN

Table X Table Y

COLUMNS COLUMNS

OUTPUT

ALL ROWS FROM TABLE Y AND TABLE Y AND COMMON ROWS ONLY ONCE

Page 10: In this session, you will learn to: Query data by using joins Query data by using subqueries Objectives

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…

Using an Outer Join (Contd.)

Page 11: In this session, you will learn to: Query data by using joins Query data by using subqueries Objectives

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

Page 12: In this session, you will learn to: Query data by using joins Query data by using subqueries Objectives

A B C B D E

A B C D E

CROSS JOIN

Table X Table Y

COLUMNS COLUMNS

OUTPUT

n ROWS m ROWS

ALL ROWS (n X m)EACH ROW OF TABLE X JOINED WITH EACH ROW OF TABLE Y

Using a Cross Join

Page 13: In this session, you will learn to: Query data by using joins Query data by using subqueries Objectives

Cross Join:Displays each row from the first table joined with each row from the second tableProduces 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…

Using a Cross Join (Contd.)

Page 14: In this session, you will learn to: Query data by using joins Query data by using subqueries Objectives

A B C B D E

A B C D B E

EQUI JOIN

Table X Table Y

COLUMNS COLUMNS

OUTPUT

COMMON ROWS

Using an Equi Join

Page 15: In this session, you will learn to: Query data by using joins Query data by using subqueries Objectives

Equi Join:Is same as an inner joinDisplays all the columns from both the tablesDisplays redundant column data in the result set

Let’s see how…

Using an Equi Join (Contd.)

Page 16: In this session, you will learn to: Query data by using joins Query data by using subqueries Objectives

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

Page 17: In this session, you will learn to: Query data by using joins Query data by using subqueries Objectives

Self Join:Is used when one row in a table correlates with other rows in the same tableUses alias name to differentiate the two copies of the same table

Let’s see how…

Using a Self Join

Page 18: In this session, you will learn to: Query data by using joins Query data by using subqueries Objectives

Problem Statement:The HR manager of AdventureWorks, Inc. requires a report containing the following details:

Employee IDEmployee NameDepartment NameDate of JoiningEmployee Address

How will you generate this report?

Demo: Using Joins

Page 19: In this session, you will learn to: Query data by using joins Query data by using subqueries Objectives

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.

Demo: Using Joins (Contd.)

Page 20: In this session, you will learn to: Query data by using joins Query data by using subqueries Objectives

Subqueries:Are used when the result set of one query needs to be taken as input for another querySyntax:SELECT column, column [,column]FROM table_nameWHERE column = ( SELECT column FROM table_name [WHERE conditional_expression] )

Querying Data by Using Subqueries

Page 21: In this session, you will learn to: Query data by using joins Query data by using subqueries Objectives

IN keyword:Is used to retrieve rows in a subquery based on the match of values given in a listSyntax:SELECT column, column [,column]FROM table_nameWHERE column [ NOT ] IN ( SELECT column FROM table_name [WHERE conditional_expression] )

Let’s see how…

Using the IN and EXISTS Keyword

Page 22: In this session, you will learn to: Query data by using joins Query data by using subqueries Objectives

EXISTS keyword:Is used to check the existence of the data and returns true or falseSyntax:SELECT column, column [,column]FROM table_nameWHERE EXISTS ( SELECT column FROM table_name [WHERE

conditional_expression] )

Let’s see how…

Using the IN and EXISTS Keyword (Contd.)

Page 23: In this session, you will learn to: Query data by using joins Query data by using subqueries Objectives

Comparison operators are modified using:ALL keyword that returns TRUE, if all the values retrieved by the subquery satisfy the comparison operatorANY keyword that returns TRUE, if any value retrieved by the subquery satisfies the comparison operator

Let’s see how…

Using Modified Comparison Operators

Page 24: In this session, you will learn to: Query data by using joins Query data by using subqueries Objectives

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

Page 25: In this session, you will learn to: Query data by using joins Query data by using subqueries Objectives

Aggregate functions:Can be used in the subquery to generate aggregated values from the inner query

Let’s see how…

Using Aggregate Functions

Page 26: In this session, you will learn to: Query data by using joins Query data by using subqueries Objectives

Nested subqueries:Contain one or more subqueriesCan be used when the condition of a query is dependent on the result of another query

Let’s see how…

Using Nested Subqueries

Page 27: In this session, you will learn to: Query data by using joins Query data by using subqueries Objectives

Correlated subqueries:Can be defined as a query that depends on the outer query for its evaluationUses the WHERE clause to refer to the table specified in the FROM clause

Let’s see how…

Using Correlated Subqueries

Page 28: In this session, you will learn to: Query data by using joins Query data by using subqueries Objectives

Just 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)

Page 29: In this session, you will learn to: Query data by using joins Query data by using subqueries Objectives

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?

Demo: Using Subqueries

Page 30: In this session, you will learn to: Query data by using joins Query data by using subqueries Objectives

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.

Demo: Using Subqueries (Contd.)

Page 31: In this session, you will learn to: Query data by using joins Query data by using subqueries Objectives

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.

Summary

Page 32: In this session, you will learn to: Query data by using joins Query data by using subqueries Objectives

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.

Summary (Contd.)