Joins and other advanced Queries

Slides:



Advertisements
Similar presentations
Relational Algebra, Join and QBE Yong Choi School of Business CSUB, Bakersfield.
Advertisements

Concepts of Database Management Seventh Edition
Concepts of Database Management Sixth Edition
Concepts of Database Management Seventh Edition
Using Relational Databases and SQL Steven Emory Department of Computer Science California State University, Los Angeles Chapter 6 Set Functions.
Chapter 11 Group Functions
The University of Akron Dept of Business Technology Computer Information Systems The Relational Model: Query-By-Example (QBE) 2440: 180 Database Concepts.
Using Relational Databases and SQL Steven Emory Department of Computer Science California State University, Los Angeles Lecture 6: Set Functions.
Computer Science 101 Web Access to Databases SQL – Extended Form.
DATA, DATABASES, AND QUERIES Managing Data in Relational Databases CS1100Microsoft Access - Introduction1.
DATA, DATABASES, AND QUERIES Managing Data in Relational Databases CS1100Microsoft Access - Introduction1 Created By Martin Schedlbauer
Advanced Tables Lesson 9. Objectives Creating a Custom Table When a table template doesn’t suit your needs, you can create a custom table in Design view.
Concepts of Database Management, Fifth Edition
SQL 資料庫查詢語言 取材自 EIS, 3 rd edition By Dunn et al..
©Silberschatz, Korth and Sudarshan4.1Database System Concepts Chapter 4: SQL Basic Structure Set Operations Aggregate Functions Null Values Nested Subqueries.
Database System Concepts, 6 th Ed. ©Silberschatz, Korth and Sudarshan See for conditions on re-usewww.db-book.com Chapter 3: Introduction.
SQL Unit 5 Aggregation, GROUP BY, and HAVING Kirk Scott 1.
HAP 709 – Healthcare Databases SQL Data Manipulation Language (DML) Updated Fall, 2009.
CSC 405: Web Application And Engineering II7.1 Database Programming with SQL Aggregation and grouping with GROUP BY Aggregation and grouping with GROUP.
M Taimoor Khan Course Objectives 1) Basic Concepts 2) Tools 3) Database architecture and design 4) Flow of data (DFDs)
1 Single Table Queries. 2 Objectives  SELECT, WHERE  AND / OR / NOT conditions  Computed columns  LIKE, IN, BETWEEN operators  ORDER BY, GROUP BY,
Concepts of Database Management Seventh Edition
 Agenda 2/20/13 o Review quiz, answer questions o Review database design exercises from 2/13 o Create relationships through “Lookup tables” o Discuss.
Database Systems Microsoft Access Practical #3 Queries Nos 215.
CS1100: Microsoft Access Managing Data in Relational Databases Created By Martin Schedlbauer CS11001Microsoft Access - Introduction.
CREATING COMPLEX QUERIES WITH NESTED QUERIES CS1100: Data, Databases, and Queries CS1100Microsoft Access1.
Access 2007 ® Use Databases How can Microsoft Access 2007 help you structure your database?
Intro to SQL Management Studio. Please Be Sure!! Make sure that your access is read only. If it isn’t, you have the potential to change data within your.
CREATING COMPLEX QUERIES WITH NESTED QUERIES CS1100: Data, Databases, and Queries CS1100Microsoft Access1.
Views, Algebra Temporary Tables. Definition of a view A view is a virtual table which does not physically hold data but instead acts like a window into.
IS201 Agenda: 09/19  Modify contents of the database.  Discuss queries: Turning data stored in a database into information for decision making.  Create.
T7-1 LEARNING OUTCOMES – ACCESS PROBLEM SOLVING 1.Describe the process of using the Simple Query Wizard using Access 2.Describe the process of using the.
Course title: Database-ii Chap No: 03 “Advanced SQL” Course instructor: ILTAF MEHDI.
Structured Query Language SQL Unit 4 Solving Problems with SQL.
PeopleSoft Financials Advanced Query Training Financial Information Systems and Reporting Controller’s Division
QUERY CONSTRUCTION CS1100: Data, Databases, and Queries CS1100Microsoft Access1.
In-Class SQL Query Exercises For the Plumbing Supply Store Database.
Computer Science & Engineering 2111 Inner Joins and Advanced Queries 1CSE 2111 Lecture-Advanced Queries.
Concepts of Database Management, Fifth Edition Chapter 3: The Relational Model 2: SQL.
Relational Databases Today we will look at: Different ways of searching a database Creating queries Aggregate Queries More complex queries involving different.
Prof: Dr. Shu-Ching Chen TA: Hsin-Yu Ha
Querying in Access Objectives: Learn how to use the Access Query Design Tool manipulate data in Access: Sorting data Aggregating Data Performing Calculations.
Chapter # 6 The Relational Algebra and Calculus
Relational Algebra - Part 1
Using Relational Databases and SQL
The Database Exercises Fall, 2009.
CS 440 Database Management Systems
Chapter 2: Intro to Relational Model
Chapter 2: Intro to Relational Model
MENAMPILKAN DATA DARI SATU TABEL (Chap 2)
Instructor: Mohamed Eltabakh
SQL – Entire Select.
Aggregations Various Aggregation Functions GROUP BY HAVING.
Chapter 4 Summary Query.
Creating and Maintaining
Microsoft Official Academic Course, Access 2016
Access: SQL Participation Project
Access Tutorial 5 Creating Advanced Queries and Enhancing Table Design
Exploring Microsoft® Office 2016 Series Editor Mary Anne Poatsy
Creating Noninput Items
Query Functions.
Access: Queries III Participation Project
Chapter 2: Intro to Relational Model
Section 4 - Sorting/Functions
Creating complex queries using nesting
Building Queries using the Principle of Simplest Query (POSQ)
Microsoft Access Understanding Relationships
Topic 12 Lesson 2 – Retrieving Data with Queries
Database Management System
Mathematical Formulas and Excel
Presentation transcript:

Joins and other advanced Queries Topic 13 Lesson 1 – Joins, Aggregation, Group By

Joining Tables A “join” is a query operation in which rows are selected that have a common value for some row. Two columns (each from a different table) can be related if their values represent the same thing To find contacts that actually placed an order1, the ContactID column value must be the same in the Contacts and the Orders table. This essentially finds all contacts who placed at least one order. 1 There may be contacts in the Contacts table that are not linked to any order, i.e., they never placed an order.

Table Joins In this example, the ContactID in the Contact table and the ContactID in the Orders table represent the same thing so the records with the same corresponding value are related. Contacts Table Orders Table

Steps for Joining Tables For every possible combination, take a row from the first table and a row from the second table Remove all the rows that do not have equal values in the related columns Merge the related columns into one column

Step 1 – all combinations Input Tables: Contacts and Order Tables Output result – Contact values from the order and contacts table not necessarily the same

Step 2 – remove rows with unequal values in related columns

Step 3 – merge related columns

Bad Joins No Output: Attributes are never equal. Meaningless Output: Attributes are equal but there is no real relationship between the fields.

Example: Group By and Join Find the first name, last name, and zip code of all contacts that placed an order. Here’s the result without a Group By: Note the duplicate rows

Example: Group By and Join Find the first name, last name, and zip code of all contacts that placed an order. Here’s the result with a Group By: All rows with the same first name, last name, and zip code have been collapsed into a single “group”

Unrelated Tables It is possible to join tables without relating columns in those tables. Such a join is called a “Cartesian product.” The Cartesian product contains all possible combinations of rows. For new database users, the Cartesian product is almost always the wrong thing.

Who ordered which products? No relationship lines so the tables are unrelated – wrong, misleading result set Every contact is paired with every product.

Who ordered which products? Correct: Need relationship lines between tables

Example List the OrderDates where the Quantity of an item ordered was at least 2. Each date should only appear once in the result.

Correct: Use a WHERE Clause Exercise: rewrite without WHERE using POSQ. Can’t be shown, may be more than one quantity >=2 per date

Incorrect: To use a GROUP BY instead of WHERE Exercise: rewrite without WHERE POSQ. Can be shown, some dates appear more than once with several quantities >=2

Renaming Attributes In query design view, attributes can be renamed with a colon : newname: oldname

Calculated Fields A query can also use calculated expressions. Example: List the extended prices for each line item. Note the user defined field name (ExtPrice) and the use of brackets for the field names, e.g., [Quantity].

Aggregate Functions Aggregate functions work on multiple rows. If used within a Group By, they work on the rows within each group. If used on an entire table or query, they work on all of the rows in the table or query.

Aggregate Functions Aggregate Function Description COUNT Counts the rows in a table or a group if used in a Group By SUM Adds the values of the summed field in a table or a group if used in a Group By AVG Averages the values of the field in a table or a group if used in a Group By MIN Finds the minimum value of the field in a table or a group if used in a Group By MAX Finds the maximum value of the field in a table or a group if used in a Group By STDEV Finds the standard deviation of the field in a table or a group if used in a Group By

Example: SUM Exercise: rewrite without mixing widening and aggregation using POSQ. Find the total of each order, i.e., the sum of the extended prices for each line item within an order. Note the user defined field name (Total) and the use of brackets for the field names, e.g., [Quantity]. The SUM applies to the values of the [Quantity]*[UnitPrice] within each group.

Example: SUM and Filter Exercise: rewrite without mixing widening, aggregation and selection operations using POSQ. Find all orders which have a total value of more than $1000.

Counting Items in a Group Exercise: Rewrite using the POSQ. To count the rows that were collapsed into a group, use the COUNT(*) function. The Group By collects identical rows into a group. Only the group is displayed, but COUNT counts the number of rows that are in each group. So the above query tells us how many orders each contact placed.

Expressions Note the use of the “Expression” in the Total row for COUNT(*). Expression is often required when an aggregate function is used. Aggregate functions work on the items within a group of the Group By clause: COUNT – counts items in a group SUM – adds the items in a group (numbers only) Beware when Access rewrites your query with an expression, this typically means there is an error in the query

Query Example 1 Which orders have totals less than $2,000?

Solution for Query Example 1 Exercise: rewrite query without mixing widening, aggregation and selection in one query. Which orders are for less than $2,000?

TRY FOR YOURSELF…

Question 1 How many orders were placed from each state?

Question 2 How often was each product ordered?

Question 3 Which contacts placed three or more orders?

Question 4 Which products have a total sales volume of more than $10,000, i.e., for which products did the total amount sold across all orders exceed $10,000?