Slide 1 of 32ASH-Training Querying and Managing Data Using SQL Server 2014 By: Segla In this session, you will learn to: Query data by using joins Query.

Slides:



Advertisements
Similar presentations
© 2007 by Prentice Hall (Hoffer, Prescott & McFadden) 1 Joins and Sub-queries in SQL.
Advertisements

Chapter 4 Joining Multiple Tables
A Guide to SQL, Seventh Edition. Objectives Use joins to retrieve data from more than one table Use the IN and EXISTS operators to query multiple tables.
Group functions cannot be used in the WHERE clause: SELECT type_code FROM d_songs WHERE SUM (duration) = 100; (this will give an error)
Writing Basic SQL SELECT Statements. Capabilities of SQL SELECT Statements A SELECT statement retrieves information from the database. Using a SELECT.
Introduction to Oracle9i: SQL1 Subqueries. Introduction to Oracle9i: SQL2 Chapter Objectives Determine when it is appropriate to use a subquery Identify.
Introduction to Structured Query Language (SQL)
Introduction to Oracle9i: SQL1 Basic SQL SELECT Statements.
Database Systems: Design, Implementation, and Management Eighth Edition Chapter 8 Advanced SQL.
Introduction to Structured Query Language (SQL)
WRITING BASIC SQL SELECT STATEMENTS Lecture 7 1. Outlines  SQL SELECT statement  Capabilities of SELECT statements  Basic SELECT statement  Selecting.
02 | Advanced SELECT Statements Brian Alderman | MCT, CEO / Founder of MicroTechPoint Tobias Ternstrom | Microsoft SQL Server Program Manager.
Objectives After completing this lesson, you should be able to do the following: Define subqueries Describe the types of problems that the subqueries.
SQL Joins.
Rationale Aspiring Database Developers should be able to efficiently query and maintain databases. This module will help students learn the Structured.
Introduction to Databases Chapter 7: Data Access and Manipulation.
Banner and the SQL Select Statement: Part Four (Multiple Connected Select Statements) Mark Holliday Department of Mathematics and Computer Science Western.
SQL/lesson 2/Slide 1 of 45 Retrieving Result Sets Objectives In this lesson, you will learn to: * Use wildcards * Use the IS NULL and IS NOT NULL keywords.
Lecture 2 of Advanced Databases Advanced SQL Instructor: Mr.Ahmed Al Astal.
Chapter 9 Joining Data from Multiple Tables
SQL advanced select using Oracle 1 7. Multiple Tables: Joins and Set Operations 8. Subqueries: Nested Queries.
SQL/Lesson 4/Slide 1 of 45 Using Subqueries and Managing Databases Objectives In this lesson, you will learn to: *Use subqueries * Use subqueries with.
A Guide to MySQL 5. 2 Objectives Use joins to retrieve data from more than one table Use the IN and EXISTS operators to query multiple tables Use a subquery.
Database Programming Sections 6 –Subqueries, Single Row Subqueries, Multiple-column subqueries, Multiple-row Subqueries, Correlated Subqueries 11/2/10,
Joins & Sub-queries. Oracle recognizes that you may want data that resides in multiple tables drawn together in some meaningful way. One of the most important.
Chapter 4Introduction to Oracle9i: SQL1 Chapter 4 Joining Multiple Tables.
Unit 4 Queries and Joins. Key Concepts Using the SELECT statement Statement clauses Subqueries Multiple table statements Using table pseudonyms Inner.
Join, Subqueries and set operators. Obtaining Data from Multiple Tables EMPLOYEES DEPARTMENTS … …
6 1 Lecture 8: Introduction to Structured Query Language (SQL) J. S. Chou, P.E., Ph.D.
In this session, you will learn to: Use functions to customize the result set Summarize and group data Objectives.
5 Copyright © 2004, Oracle. All rights reserved. Displaying Data from Multiple Tables.
Chapter 12 Subqueries and Merge Statements
SQL Select Statement IST359.
Slide 1 of 19Session 13 Ver. 1.0 Querying and Managing Data Using SQL Server 2005 In this session, you will learn to: Implement stored procedures Implement.
A Guide to SQL, Eighth Edition Chapter Five Multiple-Table Queries.
In this session, you will learn to: Query data by using joins Query data by using subqueries Objectives.
Query Processing – Implementing Set Operations and Joins Chap. 19.
Manipulating Data Lesson 3. Objectives Queries The SELECT query to retrieve or extract data from one table, how to retrieve or extract data by using.
Database Programming Sections 6 –Subqueries, Single Row Subqueries, Multiple-row Subqueries, Correlated Subqueries.
Chapter 7 Subqueries. Chapter Objectives  Determine when it is appropriate to use a subquery  Identify which clauses can contain subqueries  Distinguish.
In this session, you will learn to: Create and manage views Implement a full-text search Implement batches Objectives.
Select Complex Queries Database Management Fundamentals LESSON 3.1b.
LEC-8 SQL. Indexes The CREATE INDEX statement is used to create indexes in tables. Indexes allow the database application to find data fast; without reading.
Advanced SQL Advanced Database Dr. AlaaEddin Almabhouh.
In this session, you will learn to: Manage databases Manage tables Objectives.
Module 5: Working with Subqueries. Writing Basic Subqueries Writing Correlated Subqueries Comparing Subqueries with Joins and Temporary Tables Using Common.
More SQL: Complex Queries,
Module 4: Joining Data from Multiple Tables
In this session, you will learn to:
Writing Basic SQL SELECT Statements
Chapter 12 Subqueries and MERGE Oracle 10g: SQL
02 | Advanced SELECT Statements
Displaying Data from Multiple Tables
Using the Set Operators
Displaying Data from Multiple Tables
Using Subqueries to Solve Queries
Writing Basic SQL SELECT Statements
Using the Set Operators
JOINS (Joinining multiple tables)
More SQL: Complex Queries, Triggers, Views, and Schema Modification
Using Subqueries to Solve Queries
Contents Preface I Introduction Lesson Objectives I-2
Using Subqueries to Solve Queries
Displaying Data from Multiple Tables
Using Subqueries to Solve Queries
Using the Set Operators
Displaying Data from Multiple Tables
Subqueries Schedule: Timing Topic 25 minutes Lecture
Manipulating Data Lesson 3.
JOINS (Joinining multiple tables)
Presentation transcript:

Slide 1 of 32ASH-Training Querying and Managing Data Using SQL Server 2014 By: Segla In this session, you will learn to: Query data by using joins Query data by using subqueries Objectives

Slide 2 of 32ASH-Training Querying and Managing Data Using SQL Server 2014 By: Segla Joins Allow to retrieve data from multiple tables Can be of the following types: Inner join Outer join Cross join Equi join Self join Querying Data by Using Joins

Slide 3 of 32ASH-Training Querying and Managing Data Using SQL Server 2014 By: Segla A B CB D E A B C D E INNER JOIN Table XTable Y COLUMNS OUTPUT COMMON ROWS Using an Inner Join

Slide 4 of 32ASH-Training Querying and Managing Data Using SQL Server 2014 By: Segla 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… Using an Inner Join (Contd.)

Slide 5 of 32ASH-Training Querying and Managing Data Using SQL Server 2014 By: Segla USE AdventureWorks2014; GO SELECT *FROM HumanResources.Employee AS e INNER JOIN Person.Person AS p ON e.BusinessEntityID = p.BusinessEntityID ORDER BY p.LastName SELECT DISTINCT p.ProductID, p.Name, p.ListPrice, sd.UnitPrice AS 'Selling Price' FROM Sales.SalesOrderDetail AS sd JOIN Production.Product AS p ON sd.ProductID = p.ProductID AND sd.UnitPrice < p.ListPrice WHERE p.ProductID = 718;

Slide 6 of 32ASH-Training Querying and Managing Data Using SQL Server 2014 By: Segla 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. Just a minute

Slide 7 of 32ASH-Training Querying and Managing Data Using SQL Server 2014 By: Segla 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 Using an Outer Join

Slide 8 of 32ASH-Training Querying and Managing Data Using SQL Server 2014 By: Segla A B C B D E A B C D E LEFT OUTER JOIN Table X Table Y COLUMNS OUTPUT ALL ROWS FROM TABLE X AND COMMON ROWS FROM TABLE Y Using an Outer Join (Contd.)

Slide 9 of 32ASH-Training Querying and Managing Data Using SQL Server 2014 By: Segla A B C B D E A B C D E RIGHT OUTER JOIN Table X Table Y COLUMNS OUTPUT ALL ROWS FROM TABLE Y AND COMMON ROWS FROM TABLE X Using an Outer Join (Contd.)

Slide 10 of 32ASH-Training Querying and Managing Data Using SQL Server 2014 By: Segla Using an Outer Join (Contd.) A B C B D E A B C D E FULL OUTER JOIN Table X Table Y COLUMNS OUTPUT ALL ROWS FROM TABLE Y AND TABLE Y AND COMMON ROWS ONLY ONCE

Slide 11 of 32ASH-Training Querying and Managing Data Using SQL Server 2014 By: Segla 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.)

Slide 12 of 32ASH-Training Querying and Managing Data Using SQL Server 2014 By: Segla SELECT p.Name, pr.ProductReviewID FROM Production.Product p LEFT OUTER JOIN Production.ProductReview pr ON p.ProductID = pr.ProductID SELECT st.Name AS Territory, sp.BusinessEntityID FROM Sales.SalesTerritory st RIGHT OUTER JOIN Sales.SalesPerson sp ON st.TerritoryID = sp.TerritoryID ; SELECT p.Name, sod.SalesOrderID FROM Production.Product p FULL OUTER JOIN Sales.SalesOrderDetail sod ON p.ProductID = sod.ProductID WHERE p.ProductID IS NULL OR sod.ProductID IS NULL ORDER BY p.Name ;

Slide 13 of 32ASH-Training Querying and Managing Data Using SQL Server 2014 By: Segla 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.

Slide 14 of 32ASH-Training Querying and Managing Data Using SQL Server 2014 By: Segla A B CB D E A B C D E CROSS JOIN Table XTable Y 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

Slide 15 of 32ASH-Training Querying and Managing Data Using SQL Server 2014 By: Segla 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… SELECT p.BusinessEntityID, t.Name AS Territory FROM Sales.SalesPerson p CROSS JOIN Sales.SalesTerritory t ORDER BY p.BusinessEntityID; Using a Cross Join (Contd.)

Slide 16 of 32ASH-Training Querying and Managing Data Using SQL Server 2014 By: Segla A B CB D E A B C D B E EQUI JOIN Table XTable Y COLUMNS OUTPUT COMMON ROWS Using an Equi Join

Slide 17 of 32ASH-Training Querying and Managing Data Using SQL Server 2014 By: Segla 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… Using an Equi Join (Contd.)

Slide 18 of 32ASH-Training Querying and Managing Data Using SQL Server 2014 By: Segla 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.

Slide 19 of 32ASH-Training Querying and Managing Data Using SQL Server 2014 By: Segla 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… SELECT st.Name AS TerritoryName, sp.BusinessEntityID, sp.SalesQuota, sp.SalesYTD FROM Sales.SalesPerson AS sp JOIN Sales.SalesTerritory AS st ON sp.TerritoryID = st.TerritoryID ORDER BY st.Name, sp.BusinessEntityID Using a Self Join

Slide 20 of 32ASH-Training Querying and Managing Data Using SQL Server 2014 By: Segla 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] )  SELECT Name FROM AdventureWorks2014.Production.Product WHERE ListPrice = (SELECT ListPrice FROM AdventureWorks2014.Production.Product WHERE Name = 'Chainring Bolts' ); Querying Data by Using Subqueries

Slide 21 of 32ASH-Training Querying and Managing Data Using SQL Server 2014 By: Segla 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… Using the IN and EXISTS Keyword

Slide 22 of 32ASH-Training Querying and Managing Data Using SQL Server 2014 By: Segla 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… Using the IN and EXISTS Keyword (Contd.)

Slide 23 of 32ASH-Training Querying and Managing Data Using SQL Server 2014 By: Segla 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… Using Modified Comparison Operators

Slide 24 of 32ASH-Training Querying and Managing Data Using SQL Server 2014 By: Segla 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.

Slide 25 of 32ASH-Training Querying and Managing Data Using SQL Server 2014 By: Segla Aggregate functions: Can be used in the subquery to generate aggregated values from the inner query Let’s see how… Using Aggregate Functions

Slide 26 of 32ASH-Training Querying and Managing Data Using SQL Server 2014 By: Segla 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… Using Nested Subqueries

Slide 27 of 32ASH-Training Querying and Managing Data Using SQL Server 2014 By: Segla 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… Using Correlated Subqueries

Slide 28 of 32ASH-Training Querying and Managing Data Using SQL Server 2014 By: Segla Just a minute Write a query to determine the Business Entity ID and the Department ID of all the employees whose National ID Number is Answer: select BusinessEntityID, DepartmentID from HumanResources.EmployeeDepartmentHistory where BusinessEntityID=(Select BusinessEntityID from HumanResources.Employee Where NationalIDNumber= )

Slide 29 of 32ASH-Training Querying and Managing Data Using SQL Server 2014 By: Segla 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

Slide 30 of 32ASH-Training Querying and Managing Data Using SQL Server 2014 By: Segla 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.)

Slide 31 of 32ASH-Training Querying and Managing Data Using SQL Server 2014 By: Segla 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

Slide 32 of 32ASH-Training Querying and Managing Data Using SQL Server 2014 By: Segla 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.)