Joins, Subqueries, CTE and Indices Databases Basics SoftUni Team Technical Trainers Software University http://softuni.bg © Software University Foundation – http://softuni.org This work is licensed under the Creative Commons Attribution-NonCommercial-ShareAlike license.
Table of Content Joins Subqueries Common Table Expressions (CTE) Indices © Software University Foundation – http://softuni.org This work is licensed under the Creative Commons Attribution-NonCommercial-ShareAlike license.
Questions sli.do #SQL
Gathering Data From Multiple Tables JOINS Gathering Data From Multiple Tables
Data from Multiple Tables Sometimes you need data from several tables: Employees Departments EmployeeName DepartmentID Edward 3 John NULL DepartmentID DepartmentName 3 Sales 4 Marketing 5 Purchasing Data from Multiple Tables Sometimes you need to use data from more than one table. In the slide example, the report displays data from two separate tables. To produce the report, you need to link (join) the Employees and Departments tables and access data from both of them. EmployeeName DepartmentID DepartmentName Edward 3 Sales
Cartesian Product (1) This will produce Cartesian product: The result: SELECT LastName, Name AS DepartmentName FROM Employees, Departments LastName DepartmentName Gilbert Engineering Brown … Sales
Cartesian Product (2) A Cartesian product is formed when: A join condition is omitted A join condition is invalid All rows in the first table are joined to all rows in the second table To avoid a Cartesian product, always include a valid join condition
Types of Joins Inner joins Left, right and full outer joins Cross joins These are SQL99 compliant joins
INNER vs. OUTER Joins Inner join Left (or right) outer join A join of two tables returning only rows matching the join condition Left (or right) outer join Returns the results of the inner join as well as unmatched rows from the left (or right) table Full outer join Returns the results of an inner join along with all unmatched rows
Inner Join Departments Employees Result EmployeeID DepartmentID 263 3 270 NULL DepartmentID DepartmentName 3 Sales 4 Marketing 5 Purchasing Result Inner joins return only rows which exist in both tables. EmployeeID DepartmentID DepartmentName 263 3 Sales © Software University Foundation – http://softuni.org This work is licensed under the Creative Commons Attribution-NonCommercial-ShareAlike license.
Inner Join Syntax SELECT * FROM Employees AS e Table Employees SELECT * FROM Employees AS e INNER JOIN Departments AS d ON e.DepartmentID = d.DepartmentID Table Depatments Inner Join Join Conditions © Software University Foundation – http://softuni.org This work is licensed under the Creative Commons Attribution-NonCommercial-ShareAlike license.
Left Outer Join Departments Employees X Result EmployeeID DepartmentID 263 3 270 NULL DepartmentID DepartmentName 3 Sales 4 Marketing 5 Purchasing X Result Left outer joins return all the data in the first(left) table and all the data from the second(right) table that matches the join conditions. If the data in the right table doesn’t match any data in the left table the return value is NULL. EmployeeID DepartmentID DepartmentName 263 3 Sales 270 NULL © Software University Foundation – http://softuni.org This work is licensed under the Creative Commons Attribution-NonCommercial-ShareAlike license.
Left Outer Join Syntax SELECT * FROM Employees AS e Table Employees SELECT * FROM Employees AS e LEFT OUTER JOIN Departments AS d ON e.DepartmentID = d.DepartmentID Table Depatments Left Join Join Conditions © Software University Foundation – http://softuni.org This work is licensed under the Creative Commons Attribution-NonCommercial-ShareAlike license.
Right Outer Join Departments Employees X X Result EmployeeID DepartmentID 263 3 270 NULL DepartmentID DepartmentName 3 Sales 4 Marketing 5 Purchasing X X Result Right outer joins return all the data in the second(right) table and all the data from the first(left) table that matches the join conditions. If the data in the left table doesn’t match any data in the right table the return value is NULL. EmployeeID DepartmentID DepartmentName 263 3 Sales NULL 4 Marketing 5 Purchasing © Software University Foundation – http://softuni.org This work is licensed under the Creative Commons Attribution-NonCommercial-ShareAlike license.
Right Outer Join Syntax Table Employees SELECT * FROM Employees AS e RIGHT OUTER JOIN Departments AS d ON e.DepartmentID = d.DepartmentID Table Depatments Right Join Join Conditions © Software University Foundation – http://softuni.org This work is licensed under the Creative Commons Attribution-NonCommercial-ShareAlike license.
Full Join Departments Employees X X Result EmployeeID DepartmentID 263 270 NULL DepartmentID DepartmentName 3 Sales 4 Marketing 5 Purchasing X X Result Full joins match all the data in the left and the right table. If any of the values doesn’t the join conditions the return value is NULL. EmployeeID DepartmentID DepartmentName 263 3 Sales 270 NULL 4 Marketing 5 Purchasing © Software University Foundation – http://softuni.org This work is licensed under the Creative Commons Attribution-NonCommercial-ShareAlike license.
Full Join Syntax SELECT * FROM Employees AS e Table Employees SELECT * FROM Employees AS e FULL JOIN Departments AS d ON e.DepartmentID = d.DepartmentID Table Depatments Full Join Join Conditions © Software University Foundation – http://softuni.org This work is licensed under the Creative Commons Attribution-NonCommercial-ShareAlike license.
Cross Join Departments Employees Result EmployeeID DepartmentID 263 3 270 NULL DepartmentID DepartmentName 3 Sales 4 Marketing 5 Purchasing Result EmployeeID DepartmentID DepartmentName 263 3 Sales 4 Marketing 5 Purchasing 270 NULL Cross joins create Cartesian products. This means that all the rows in the left table are multiplied by all the rows in the right table. If table Employees has 2 rows and table Departments has 3 rows the result will return the multiplication – 6 rows. © Software University Foundation – http://softuni.org This work is licensed under the Creative Commons Attribution-NonCommercial-ShareAlike license.
Cross Join Syntax SELECT * FROM Employees AS e Table Employees SELECT * FROM Employees AS e CROSS JOIN Departments AS d Table Depatments Cross Join No Join Conditions © Software University Foundation – http://softuni.org This work is licensed under the Creative Commons Attribution-NonCommercial-ShareAlike license.
Join Overview Relation Sally 13 Accounting 18 John 10 Marketing 10 Michael 22 HR 12 Bob 11 Engineering 22 Robin 7 Sales 8 Jessica 15 Executive 7 Relation
Join Overview Accounting 18 Sally 13 John 10 Marketing 10 HR 12 Michael 22 Engineering 22 Sales 8 Bob 11 Robin 7 Executive 7 Jessica 15
Join Overview Inner Join Sally 13 Bob 11 Jessica 15 Sales 8 Accounting 18 HR 12 John 10 Marketing 10 Michael 22 Engineering 22 Robin 7 Executive 7
Join Overview Left Outer Join Sales 8 Accounting 18 HR 12 Sally 13 NULL John 10 Marketing 10 Michael 22 Engineering 22 Bob 11 Robin 7 Executive 7 Jessica 15
Join Overview Right Outer Join NULL Accounting 18 Sally 13 Bob 11 Jessica 15 John 10 Marketing 10 HR 12 Michael 22 Engineering 22 Sales 8 Robin 7 Executive 7
Join Overview Full Outer Join NULL Sales 8 Accounting 18 HR 12 Sally 13 NULL John 10 Marketing 10 Michael 22 Engineering 22 Bob 11 Robin 7 Executive 7 Jessica 15
Join Overview Negated Left Outer Join Sales 8 Accounting 18 HR 12 Sally 13 NULL John 10 Michael 22 Marketing Engineering Executive 7 Robin Bob 11 Jessica 15
Join Overview Negated Right Outer Join NULL Accounting 18 Sally 13 Bob 11 Jessica 15 John 10 Michael 22 Marketing Engineering Executive 7 Robin HR 12 Sales 8
Join Overview Negated Outer Join NULL Sales 8 Accounting 18 HR 12 Sally 13 NULL John 10 Michael 22 Marketing Engineering Executive 7 Robin Bob 11 Jessica 15
Problem: Addresses with Towns Display address information of all employees in "SoftUni" database. Select first 50 employees. The exact format of data is shown below. Order them by FirstName, then by LastName (ascending). Hint: Use three-way join. Three-Way Joins A three-way join is a join of three tables. In SQL: 1999 compliant syntax, joins are performed from left to right so the first join to be performed is Employees JOIN ADDRESS. The first join condition can reference columns in Employees and ADDRESS but cannot reference columns in STATEPROVINCE. The second join condition can reference columns from all three tables. This can also be written as a three-way equijoin: SELECT e.LastName, a.City, sp.Name SPName FROM employee e, address a, stateprovince sp WHERE e.AddressID = a.AddressID AND a.StateProvinceID = sp.StateProvinceID Check your solution here: https://judge.softuni.bg/Contests/Practice/Index/393#1
Solution: Addresses with Towns Cross Table Selection SELECT TOP 50 e.FirstName, e.LastName, t.Name as Town, a.AddressText FROM Employees e JOIN Addresses a ON e.AddressID = a.AddressID JOIN Towns t ON a.TownID = t.TownID ORDER BY e.FirstName ASC, e.LastName ASC Table Employees Three-Way Joins A three-way join is a join of three tables. In SQL: 1999 compliant syntax, joins are performed from left to right so the first join to be performed is Employees JOIN ADDRESS. The first join condition can reference columns in Employees and ADDRESS but cannot reference columns in STATEPROVINCE. The second join condition can reference columns from all three tables. This can also be written as a three-way equijoin: SELECT e.LastName, a.City, sp.Name SPName FROM employee e, address a, stateprovince sp WHERE e.AddressID = a.AddressID AND a.StateProvinceID = sp.StateProvinceID Table Towns Check your solution here: https://judge.softuni.bg/Contests/Practice/Index/393#1
Problem: Sales Employees Find all employees that are in the "Sales" department. Use "SoftUni" database. Follow the specified format: Order them by EmployeeID. The example shown performs a join on the Employees and Departments tables, and, in addition, displays only employees within the Sales department. Check your solution here: https://judge.softuni.bg/Contests/Practice/Index/393#2
Solution: Sales Employees Cross Table Selection SELECT e.EmployeeID, e.FirstName, e.LastName, d.Name AS DepartmentName FROM Employees e INNER JOIN Departments d ON e.DepartmentID = d.DepartmentID WHERE d.Name = 'Sales' ORDER BY e.EmployeeID Table Departments The example shown performs a join on the Employees and Departments tables, and, in addition, displays only employees within the Sales department. WHERE Precicate Check your solution here: https://judge.softuni.bg/Contests/Practice/Index/393#2
Problem: Employees Hired After * Problem: Employees Hired After 07/16/96 Show all employees that: Are hired after 1/1/1999 Are either in "Sales" or "Finance" department Sorted by HireDate (ascending). Check your solution here: https://judge.softuni.bg/Contests/Practice/Index/393#5 (c) 2006 National Academy for Software Development - http://academy.devbg.org*
Solution: Employees Hired After * Solution: Employees Hired After 07/16/96 Cross Table Selection SELECT e.FirstName, e.LastName, e.HireDate, d.Name as DeptName FROM Employees e INNER JOIN Departments d ON (e.DepartmentId = d.DepartmentId AND e.HireDate > '1/1/1999' AND d.Name IN ('Sales', 'Finance')) ORDER BY e.HireDate ASC Complex Join Condition Check your solution here: https://judge.softuni.bg/Contests/Practice/Index/393#5 (c) 2006 National Academy for Software Development - http://academy.devbg.org*
Problem: Employee Summary * Problem: Employee Summary 07/16/96 Display information about employee's manager and employee's department . Show only the first 50 employees. The exact format is shown below: Sort by EmployeeID (ascending). Check your solution here: https://judge.softuni.bg/Contests/Practice/Index/393#9 (c) 2006 National Academy for Software Development - http://academy.devbg.org*
Solution: Employee Summary SELECT TOP 50 e.EmployeeID, e.FirstName + ' ' + e.LastName AS EmployeeName, m.FirstName + ' ' + m. LastName AS ManagerName, d.Name AS DepartmentName FROM Employees AS e LEFT JOIN Employees AS m ON m.EmployeeID = e.ManagerID LEFT JOIN Departments AS d ON d.DepartmentID = e.DepartmentID ORDER BY e.EmployeeID ASC Cross Table Selection Self-join Table Departments Sort Check your solution here: https://judge.softuni.bg/Contests/Practice/Index/393#9
Query Manipulation on Multiple Levels ☰ ☰ ☰ Subqueries Query Manipulation on Multiple Levels
Subqueries Query Subquery Employees WHERE DepartmentID IN EmployeeID Salary 59 19,000 71 43,300 ... Subquery A subquery or nested query is a query within another SQL query and embedded within the WHERE clause. Its main purpose is to serve as a data filter for the main query. It can be used after any of the operators(>,<, =, !=, IN, BETWEEN). A subquery can return a single value or multiple values. WHERE DepartmentID IN DepartmentID Name 10 Finance © Software University Foundation – http://softuni.org This work is licensed under the Creative Commons Attribution-NonCommercial-ShareAlike license.
Subquery Syntax SELECT FROM Employees AS e WHERE e.DepartmentID IN ( Table Employees SELECT FROM Employees AS e WHERE e.DepartmentID IN ( SELECT d.DepartmentID FROM Deparments AS d WHERE d.Name = 'Finance' ) Table Depatments Subquery © Software University Foundation – http://softuni.org This work is licensed under the Creative Commons Attribution-NonCommercial-ShareAlike license.
Problem: Min Average Salary * Problem: Min Average Salary 07/16/96 Display lowest average salary of all departments. Calculate average salary for each department. Then show the value of smallest one. Check your solution here: https://judge.softuni.bg/Contests/Practice/Index/393#10 (c) 2006 National Academy for Software Development - http://academy.devbg.org*
Solution: Min Average Salary SELECT MIN(a.AverageSalary) AS MinAverageSalary FROM ( SELECT e.DepartmentID, AVG(e.Salary) AS AverageSalary FROM Employees AS e GROUP BY e.DepartmentID ) AS a Subquery Table Employees Check your solution here: https://judge.softuni.bg/Contests/Practice/Index/393#10 © Software University Foundation – http://softuni.org This work is licensed under the Creative Commons Attribution-NonCommercial-ShareAlike license.
Common Table Expressions Reusable Subqueries
Common Table Expressions Common Table Expressions (CTE) can be considered as "named subqueries". They could be used to improve code readability and code reuse. Usually they are positioned in the beginning of the query. WITH CTE_Name (ColumnA, ColumnB…) AS ( -- Insert subquery here. )
CTE Syntax WITH Employees_CTE (FirstName, LastName, DepartmentName) AS SELECT e.FirstName, e.LastName, d.Name FROM Employees AS e LEFT JOIN Departments AS d ON d.DepartmentID = e.DepartmentID ) SELECT FirstName, LastName, DepartmentName FROM Employees_CTE
Clustered and Non-Clustered Indexes Every table can have only one clustered indexes. They are stored on the table. Clustered indexes sorts the data physically in the table so the reads are much faster. The most common index structure are the B-trees. However, when you have an index inserts and deletes it takes more time to accomplish because indexes has to be updated as well. A non-clustered index has a duplicate of the data from the indexed columns kept ordered together with pointers to the actual data rows (pointers to the clustered index if there is one). This means that accessing data through a non-clustered index has to go through an extra layer of indirection. However, if you select only the data that's available in the indexed columns you can get the data back directly from the duplicated index data. Indexes Clustered and Non-Clustered Indexes
Indices Indices speed up searching of values in a certain column or group of columns. Usually implemented as B-trees. Indices can be built-in the table (clustered) or stored externally (non-clustered). Adding and deleting records in indexed tables is slower! Indices should be used for big tables only (e.g. 50 000 rows).
Clustered Indexes Clustered index is actually the data itself. Very useful for fast execution of WHERE, ORDER BY and GROUP BY clauses. Maximum 1 clustered index per table If a table has no clustered index, its data rows are stored in an unordered structure (heap). Keys 0-99 100-199 200-299 Data ☰
Non-Clustered Indexes (1) Useful for fast retrieving a single record or a range of records Maintained in a separate structure in the DB Tend to be much narrower than the base table Can locate the exact record(s) with less I/O Has at least one more intermediate level than the clustered index Much less valuable if table doesn’t have a clustered index
Non-Clustered Indexes (2) A non-clustered has pointers to the actual data rows (pointers to the clustered index if there is one). Keys Index 0-99 100-199 200-299 Range 2 Range 1 Range 3 Data ☰ Links ☰ © Software University Foundation – http://softuni.org This work is licensed under the Creative Commons Attribution-NonCommercial-ShareAlike license.
Indices Syntax Index Type CREATE NONCLUSTERED INDEX IX_Employees_FirstName_LastName ON Employees(FirstName, LastName) Table Name Columns © Software University Foundation – http://softuni.org This work is licensed under the Creative Commons Attribution-NonCommercial-ShareAlike license.
Demo: Index Performance Live Demo in Class
Databases Summary Joins Subqueries are used to nest queries. CTE's improve code reuse and readability. Indices improve SQL search performance if used properly. SELECT * FROM Employees AS e JOIN Departments AS d ON d.DepartmentId = e.DepartmentID Databases © Software University Foundation – http://softuni.org This work is licensed under the Creative Commons Attribution-NonCommercial-ShareAlike license.
Joins, Subqueries, CTE and Indices https://softuni.bg/courses/ © Software University Foundation – http://softuni.org This work is licensed under the Creative Commons Attribution-NonCommercial-ShareAlike license.
License This course (slides, examples, demos, videos, homework, etc.) is licensed under the "Creative Commons Attribution- NonCommercial-ShareAlike 4.0 International" license Attribution: this work may contain portions from "Databases" course by Telerik Academy under CC-BY-NC-SA license © Software University Foundation – http://softuni.org This work is licensed under the Creative Commons Attribution-NonCommercial-ShareAlike license.
Free Trainings @ Software University Software University Foundation – softuni.org Software University – High-Quality Education, Profession and Job for Software Developers softuni.bg Software University @ Facebook facebook.com/SoftwareUniversity Software University Forums forum.softuni.bg © Software University Foundation – http://softuni.org This work is licensed under the Creative Commons Attribution-NonCommercial-ShareAlike license.