© Jalal Kawash 2010 4 Database Queries Peeking into Computer Science.

Slides:



Advertisements
Similar presentations
Copyright © 2011 Pearson Education, Inc. Publishing as Pearson Addison-Wesley Chapter 5 More SQL: Complex Queries, Triggers, Views, and Schema Modification.
Advertisements

Copyright © 2007 Ramez Elmasri and Shamkant B. Navathe Chapter 6 (Continued) The Relational Algebra and Calculus.
Chapter 6 Additional Relational Operations Dr. Bernard Chen Ph.D. University of Central Arkansas Fall 2008.
5 Copyright © 2007, Oracle. All rights reserved. Reporting Aggregated Data Using the Group Functions.
Data Warehousing/Mining 1 Data Warehousing/Mining Comp 150 Aggregation in SQL (not in book) Instructor: Dan Hebert.
4 การใช้ SQL Functions. Copyright © 2007, Oracle. All rights reserved What Are Group Functions? Group functions operate on sets of rows to give.
LECTURE 10.  Group functions operate on sets of rows to give one result per group.
The University of Akron Dept of Business Technology Computer Information Systems The Relational Model: Query-By-Example (QBE) 2440: 180 Database Concepts.
Session 4 SQL Structured Query Language. SQL Modes of use –Interactive –Embedded Purpose –Create database –Create, Read, Update, Delete.
Structured Query Language Part I Chapter Three CIS 218.
Database Systems More SQL Database Design -- More SQL1.
A Guide to SQL, Seventh Edition. Objectives Retrieve data from a database using SQL commands Use compound conditions Use computed columns Use the SQL.
Microsoft Access 2010 Chapter 7 Using SQL.
Computer Science 101 Web Access to Databases SQL – Extended Form.
SQL Operations Aggregate Functions Having Clause Database Access Layer A2 Teacher Up skilling LECTURE 5.
SQL for Data Retrieval. Running Example IST2102 Data Preparation Login to SQL server using your account Download three SQL script files from wiki page.
Database Programming Sections 5– GROUP BY, HAVING clauses, Rollup & Cube Operations, Grouping Set, Set Operations 11/2/10.
© Jalal Kawash 2010 Databases & Data Modelling Peeking into Computer Science.
Chapter 3 Single-Table Queries
Chapter 3 Section 3.4 Relational Database Operators
1 Chapter 7 Query-By-Example by Monica Chan CS157B Professor Lee.
© Jalal Kawash Databases & Data Modelling Peeking into Computer Science.
1 CS 430 Database Theory Winter 2005 Lecture 12: SQL DML - SELECT.
1 Single Table Queries. 2 Objectives  SELECT, WHERE  AND / OR / NOT conditions  Computed columns  LIKE, IN, BETWEEN operators  ORDER BY, GROUP BY,
1 TAC2000/ Protocol Engineering and Application Research Laboratory (PEARL) Structured Query Language Introduction to SQL Structured Query Language.
Using Special Operators (LIKE and IN)
Concepts of Database Management Seventh Edition
DATABASE TRANSACTION. Transaction It is a logical unit of work that must succeed or fail in its entirety. A transaction is an atomic operation which may.
Structured Query Language. Group Functions What are group functions ? Group Functions Group functions operate on sets of rows to give one result per group.
 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.
SQL queries ordering and grouping and joins
SQL for Data Retrieval. Running Example IST2102 Data Preparation Login to SQL server using your account Select your database – Your database name is.
© Jalal Kawash Database Queries Peeking into Computer Science.
Copyright © Curt Hill Queries in SQL More options.
1 CSE 480: Database Systems Lecture 12: SQL (Nested queries and Aggregate functions)
Structured Query Language Introduction. Basic Select SELECT lname, fname, phone FROM employees; Employees Table LNAMEFNAMEPHONE JonesMark SmithSara
1 Querying a Single Table Structured Query Language (SQL) - Part II.
DATA RETRIEVAL WITH SQL Goal: To issue a database query using the SELECT command.
Lecture 8 – SQL Joins – assemble new views from existing tables INNER JOIN’s The Cartesian Product Theta Joins and Equi-joins Self Joins Natural Join.
Concepts of Database Management Seventh Edition Chapter 3 The Relational Model 2: SQL.
A Guide to SQL, Eighth Edition Chapter Four Single-Table Queries.
IST 210 More SQL Todd Bacastow IST 210: Organization of Data.
Agenda for Class - 03/04/2014 Answer questions about HW#5 and HW#6 Review query syntax. Discuss group functions and summary output with the GROUP BY statement.
Aggregating Data Using Group Functions. What Are Group Functions? Group functions operate on sets of rows to give one result per group.
1 Chapter 3 Single Table Queries. 2 Simple Queries Query - a question represented in a way that the DBMS can understand Basic format SELECT-FROM Optional.
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.
Chapter 11 – Data Manipulation: Relational Algebra and SQL1 Unit 9: Data Manipulation: Relational Algebra and SQL IT238: Data Modeling and Database Design.
More SQL: Complex Queries, Triggers, Views, and Schema Modification
More SQL: Complex Queries,
CS580 Advanced Database Topics
6/22/2018.
Chapter 3 Introduction to SQL(3)
CS580 Advanced Database Topics
Database Queries.
Aggregating Data Using Group Functions
SQL – Entire Select.
More SQL: Complex Queries, Triggers, Views, and Schema Modification
Access: SQL Participation Project
Structured Query Language
CS4222 Principles of Database System
CS122 Using Relational Databases and SQL
Reporting Aggregated Data Using the Group Functions
Query Functions.
Reporting Aggregated Data Using the Group Functions
Reporting Aggregated Data Using the Group Functions
Chapter Name SQL: Data Manipulation
Group Operations Part IV.
Presentation transcript:

© Jalal Kawash Database Queries Peeking into Computer Science

© Jalal Kawash 2010Peeking into Computer Science Reading Assignment Mandatory: Chapter 4 – Sections 4.6 &4.7 2

Join Queries 3

© Jalal Kawash 2010Peeking into Computer Science Objectives By the end of this section, you will be able to: 1. Formulate queries on multiple tables 2. Understand how natural joins work 3. Determine the result of a multi-table query

© Jalal Kawash 2010Peeking into Computer Science JT’s Extra: Set Multiplication Recall: set multiplication ‘determines all possible combinations of elements from each set’. In actual databases not all combinations may occur. ◦E.g., an employee typically is just a member of one department not all departments. The database implementation of a set multiplication is ‘join’.

© Jalal Kawash 2010Peeking into Computer Science JT’s Extra: Join Example

© Jalal Kawash 2010Peeking into Computer Science JT’s Extra: Query For Join Example SELECT * FROM Employees, Departments;

© Jalal Kawash 2010Peeking into Computer Science JT’s Extra: Result Of The Query James Tam is only in Department 1 but the query yields all possible combinations (whether they actually occur in the data or not)

© Jalal Kawash 2010Peeking into Computer Science PROJECT x DEPARTMENT 9

© Jalal Kawash 2010Peeking into Computer Science QBE 10

© Jalal Kawash 2010Peeking into Computer Science JT’s Extra: Join Of Actual Cases From The Database In the previous example this would only include the cases where the department number of the Employees table matched the department number of the Departments table. ◦(It should exclude non-existent combinations of employees and departments.

© Jalal Kawash 2010Peeking into Computer Science JT’s Extra: MS-Access SQL Query SELECT Departments.*, Employees.* FROM Departments INNER JOIN Employees ON ◦Departments.[Department Number] = Employees.[Department number];

© Jalal Kawash 2010Peeking into Computer Science JT’s Extra: Query Results Results Employees table

© Jalal Kawash 2010Peeking into Computer Science Where Dnumbers Match 14

© Jalal Kawash 2010Peeking into Computer Science Natural Join 15

© Jalal Kawash 2010Peeking into Computer Science Natural Join Condition 16

© Jalal Kawash 2010Peeking into Computer Science Natural Join Result 17

© Jalal Kawash 2010Peeking into Computer Science Natural Join Result 18

© Jalal Kawash 2010Peeking into Computer Science Default QBE Joins in ACCESS Can be also done explicitly 19

© Jalal Kawash 2010Peeking into Computer Science QBE with Explicit Join 20

© Jalal Kawash 2010Peeking into Computer Science JT’s Extra: Effect Of QBE With Explicit Join QBE Query criteria Query result

© Jalal Kawash 2010Peeking into Computer Science Join Example 22 Retrieve the address of each employee of the IT department

© Jalal Kawash 2010Peeking into Computer Science Determining the Result of a Join Query 23

© Jalal Kawash 2010Peeking into Computer Science Reduce the size of the joined tables 24

© Jalal Kawash 2009Peeking into Computer Science 25

© Jalal Kawash 2010Peeking into Computer Science Multiply the resulting relations and project 26

© Jalal Kawash 2010Peeking into Computer Science Apply the join condition 27

Aggregate Functions, Ordering, & Grouping 28

© Jalal Kawash 2010Peeking into Computer Science Objectives By the end of this section, you will be able to: 1. Use aggregate functions 2. Group the calculations of aggregate functions 3. Formulate SQL queries with HAVING and ORDER BY clauses 29

© Jalal Kawash 2010Peeking into Computer Science JT’s Extra: Basic Parts Of An SQL Query ◦SELECT: Specifies the fields/columns shown in the query results e.g., SIN field. ◦FROM: Lists the tables from which the data is to be selected e.g., look in the Employees table. ◦WHERE: Provides the conditions to determine if rows/records are shown by the query. ◦ORDER BY: Specifies the order in which rows are to be returned by the query.

© Jalal Kawash 2010Peeking into Computer Science JT’s Extra: ‘Order By’ (Data and Query) Original table QBE SELECT Employees.Name, Employees.Salary FROM Employees ORDER BY Employees.Salary; SQL

© Jalal Kawash 2010Peeking into Computer Science JT’s Extra: Query Results

© Jalal Kawash 2010Peeking into Computer Science Order By Clause Orders the result of a query 33

© Jalal Kawash 2010Peeking into Computer Science Descending Order 34

© Jalal Kawash 2010Peeking into Computer Science Order By Clause 35

© Jalal Kawash 2010Peeking into Computer Science Aggregate Functions 1 Sum Average Minimum Maximum Count 36 1: JT’s extra, Aggregate functions allow you to perform calculations on multiple rows of data, but will only return a single value in the response.

© Jalal Kawash 2010Peeking into Computer Science JT’s Extra: SUM Original table QBESQL SELECT Sum(salary) AS Expr1 FROM Employees; Query result

© Jalal Kawash 2010Peeking into Computer Science Sum 38

© Jalal Kawash 2010Peeking into Computer Science COUNT 39

© Jalal Kawash 2010Peeking into Computer Science AVG 40

© Jalal Kawash 2010Peeking into Computer Science Exercise Retrieve the min salary of female employees who work for the Finance department Use function Min() 41

© Jalal Kawash 2010Peeking into Computer Science MIN 42

© Jalal Kawash 2010Peeking into Computer Science Exercise Retrieve the max salary of employees who work on projects located in Toronto Use function Max() 43

© Jalal Kawash 2010Peeking into Computer Science MAX 44

© Jalal Kawash 2010Peeking into Computer Science Grouping Calculations How to find the sum of salary per department SQL has the GROUP BY clause Groups calculations of an aggregate function 45

© Jalal Kawash 2010Peeking into Computer Science JT’s Extra: Group By (Small Example) Employees tableSELECT department_number, sum(salary) AS DEPT_TOTAL FROM Employees GROUP BY department_number; SQL QBE Query result

© Jalal Kawash 2010Peeking into Computer Science GROUP BY 47

© Jalal Kawash 2010Peeking into Computer Science HAVING Clause To show only some of the groups WHERE filters tuples HAVING filters groups 48

© Jalal Kawash 2010Peeking into Computer Science JT’s Extra: Group By (All Departments) Employees tableSELECT Department_number, COUNT(*) as DepartmentEmpTotals FROM Employees GROUP BY Department_number; SQL Query result

© Jalal Kawash 2010Peeking into Computer Science JT’s Extra: Having Clause (Bigger Dept.’s) Employees table Query result SELECT Department_number, COUNT(*) as DepartmentEmpTotals FROM Employees GROUP BY Department_number HAVING COUNT(*)>1 SQL

© Jalal Kawash 2010Peeking into Computer Science HAVING Clause 51

© Jalal Kawash 2010Peeking into Computer Science HAVING Clause 52

© Jalal Kawash 2010Peeking into Computer Science HAVING Clause 53

© Jalal Kawash 2010Peeking into Computer Science EXISTS – Optional Reading 54