Database Management Systems,

Similar documents
Week 4 & 5: SQL. SQL as a Query Language. Example Database. SQL Queries

ASSIGNMENT NO Computer System with Open Source Operating System. 2. Mysql

Database Management Systems,

Aggregation. Lecture 7 Section Robb T. Koether. Hampden-Sydney College. Wed, Jan 29, 2014

What Are Group Functions? Reporting Aggregated Data Using the Group Functions. Objectives. Types of Group Functions

Database Systems CSE 303. Outline. Lecture 06: SQL. What is Sub-query? Sub-query in WHERE clause Subquery

Subquery: There are basically three types of subqueries are:

Aggregate Functions. Eng. Mohammed Alokshiya. Islamic University of Gaza. Faculty of Engineering. Computer Engineering Dept. Database Lab (ECOM 4113)

CSC Web Programming. Introduction to SQL

Reporting Aggregated Data Using the Group Functions. Copyright 2004, Oracle. All rights reserved.

CMP-3440 Database Systems

INDEX. 1 Basic SQL Statements. 2 Restricting and Sorting Data. 3 Single Row Functions. 4 Displaying data from multiple tables

Set theory is a branch of mathematics that studies sets. Sets are a collection of objects.

Advanced SQL GROUP BY Clause and Aggregate Functions Pg 1

STRUCTURED QUERY LANGUAGE (SQL)

Intermediate SQL: Aggregated Data, Joins and Set Operators

KORA. RDBMS Concepts II

Slicing and Dicing Data in CF and SQL: Part 1

WHAT IS SQL. Database query language, which can also: Define structure of data Modify data Specify security constraints

CS 582 Database Management Systems II

SQL Data Query Language

Database Management Systems,

Optimizing SQL. Kate Amorella Proff. AKA SQL Glitter Bombs. Research Analyst

MANAGING DATA(BASES) USING SQL (NON-PROCEDURAL SQL, X401.9)

Ian Kenny. November 28, 2017

Why Relational Databases? Relational databases allow for the storage and analysis of large amounts of data.

Computing for Medicine (C4M) Seminar 3: Databases. Michelle Craig Associate Professor, Teaching Stream

A subquery is a nested query inserted inside a large query Generally occurs with select, from, where Also known as inner query or inner select,

Structured Query Language Continued. Rose-Hulman Institute of Technology Curt Clifton

Institute of Aga. Network Database LECTURER NIYAZ M. SALIH

An Introduction to Structured Query Language

An Introduction to Structured Query Language

SQL Queries. COSC 304 Introduction to Database Systems SQL. Example Relations. SQL and Relational Algebra. Example Relation Instances

An Introduction to Structured Query Language

SQL Recursion, Window Queries

Database Management Systems. Chapter 5

Database 2: Slicing and Dicing Data in CF and SQL

CS 464/564 Introduction to Database Management System Instructor: Abdullah Mueen

An Introduction to Structured Query Language

Institute of Aga. Microsoft SQL Server LECTURER NIYAZ M. SALIH

Data Manipulation Language (DML)

An Introduction to Structured Query Language

Database Management Systems. Chapter 5

Retrieving Data Using the SQL SELECT Statement. Copyright 2009, Oracle. All rights reserved.

Querying Data with Transact SQL

COMP102: Introduction to Databases, 5 & 6

COSC 304 Introduction to Database Systems SQL. Dr. Ramon Lawrence University of British Columbia Okanagan

Structure Query Language (SQL)

Introduction to Oracle9i: SQL

CS3DB3/SE4DB3/SE6M03 TUTORIAL

CMP-3440 Database Systems

SQL. Rodrigo García Carmona Universidad San Pablo-CEU Escuela Politécnica Superior

Introduction to SQL (Structured Query Language)

Oracle Database 11g: SQL and PL/SQL Fundamentals

Based on the following Table(s), Write down the queries as indicated: 1. Write an SQL query to insert a new row in table Dept with values: 4, Prog, MO

LECTURE10: DATA MANIPULATION IN SQL, ADVANCED SQL QUERIES

Chapter 6. SQL Data Manipulation

Database Technology. Topic 3: SQL. Olaf Hartig.

CSE 530A SQL. Washington University Fall 2013

DATABASE MANAGEMENT SYSTEMS PREPARED BY: ENGR. MOBEEN NAZAR

SQL Aggregate Functions

SIT772 Database and Information Retrieval WEEK 6. RELATIONAL ALGEBRAS. The foundation of good database design

Lab # 6. Using Subqueries and Set Operators. Eng. Alaa O Shama

ORACLE VIEWS ORACLE VIEWS. Techgoeasy.com

GIFT Department of Computing Science. CS-217: Database Systems. Lab-4 Manual. Reporting Aggregated Data using Group Functions

Chapter 3: Introduction to SQL. Chapter 3: Introduction to SQL

Data about data is database Select correct option: True False Partially True None of the Above

Oracle Database 10g: Introduction to SQL

Advanced SQL Tribal Data Workshop Joe Nowinski

Database Laboratory Note (MS SQL Server 2000)

Concepts of Database Management Eighth Edition. Chapter 2 The Relational Model 1: Introduction, QBE, and Relational Algebra

EECS 647: Introduction to Database Systems

Introduction to SQL Server 2005/2008 and Transact SQL

Oracle Database: SQL and PL/SQL Fundamentals NEW

Unit Assessment Guide

Database Management Systems. Chapter 5

T-SQL Training: T-SQL for SQL Server for Developers

Database design process

Oracle Database: SQL and PL/SQL Fundamentals Ed 2

Retrieving Data from Multiple Tables

SQL Recursion, Window Queries

COSC 304 Introduction to Database Systems. Views and Security. Dr. Ramon Lawrence University of British Columbia Okanagan

Relational Algebra. Procedural language Six basic operators

INTERMEDIATE SQL GOING BEYOND THE SELECT. Created by Brian Duffey

NESTED QUERIES AND AGGREGATION CHAPTER 5 (6/E) CHAPTER 8 (5/E)

1) Introduction to SQL

Introduction to SQL. ECE 650 Systems Programming & Engineering Duke University, Spring 2018

Chapter 3: Introduction to SQL

DUE: 9. Create a query that will return the average order total for all Global Fast Foods orders from January 1, 2002, to December 21, 2002.

Fundamentals of Database Systems

Fundamentals of Database Systems

SQL. Char (30) can store ram, ramji007 or 80- b

In This Lecture. Yet More SQL SELECT ORDER BY. SQL SELECT Overview. ORDER BY Example. ORDER BY Example. Yet more SQL

The SQL data-definition language (DDL) allows defining :

Who is the borrower whose id is 12345? σ borrower borrower_id = select * from borrower where borrower_id = '12345'

Sql Server Syllabus. Overview

NESTED QUERIES AND AGGREGATION CHAPTER 5 (6/E) CHAPTER 8 (5/E)

Structure Query Language (SQL)

SQL Data Manipulation Language. Lecture 5. Introduction to SQL language. Last updated: December 10, 2014

Assignment 6: SQL III Solution

Transcription:

Database Management Systems SQL Query Language (3) 1

Topics Aggregate Functions in Queries count sum max min avg Group by queries Set Operations in SQL Queries Views 2

Aggregate Functions Tables are collections of records with a schema. Aggregate functions are used to get information about the records of a table such as: The maximum value in a column of a table The number of records in a table Average of the values in a column of a table 3

Aggregate Functions Most important aggregate functions are: count sum max min avg 4

Count Function Count returns the number of rows or distinct values. Count is used with SELECT query Count can be used with distinct option Syntax Select count( [distinct] attribute list ) From table_name where condition 5

Example Find the number of employees: Select count(*) From Employee Find the number of different values on the attribute Salary for all the rows in Employee: Select count(distinct Salary) From Employee 6

Example Query Results Query 1 : 8 Query 2 : 6 7

Sum Function Sum finds the total sum of the values in an attribute. Sum cannot be used with non-numeric attributes. e.g. Find the sum of the salaries of the Administration department select sum(salary) as SumSalary from Employee where Dept = Administration Result: 8

Min, Max and Avg functions Min, Max, and Avg functions are used to find the minimum, maximum and average of the values in an attribute. e.g. Find the maximum salary among the employees who work in a department based in London. 9

Example Query Select max(salary) as MaxLondonSal From Employee, Department Where Employee.DeptCode = Department.DeptCode AND Department.City = London 10

11

Group By Aggregate functions can also be used with sub-sets of the table records. Group By puts the records in subsets (groups) Aggregate functions can be used with Group By 12

Group By Example Find the sum of salaries of all the employees of each department: Select Dept, sum(salary)as TotSal From Employee, Department Where Employee.DeptCode = Department.DeptCode Group by Dept 13

How Group By Works? First the query is executed Then the query result is divided into subsets Next aggregate function is executed for each group 14

Conditions on Aggregate Function Result When conditions are on the result of an aggregate operator, it is necessary to use the having clause e.g. Find which departments spend more than 100 on salaries Select Dept, sum(salary) as TotSal from Employee, Department Where Employee.DeptCode = Department.DeptCode group by Dept having sum(salary) > 100 Result: 15

Example Find the departments in which the average salary of employees older than 30 is higher than 25: Select Dept, avg(salary) from Employee, Department Where Employee.DeptCode = Department.DeptCode AND Age > 30 group by Dept having avg(salary) > 25 16

Set Operations in SQL Queries Union, Intersection and Set Difference operations can be carried out with SQL e.g. Find the first names and surnames of the employees This query tries to find the union of two sets 1- Set of first names 2- Set of surnames 17

Union in SQL Queries Select FirstName as Name from Employee union Select Surname from Employee Duplicates are removed by UNION 18

Union Query Result 19

Intersection in SQL Find the surnames of employees that are also first names: select FirstName as Name from Employee intersect select Surname from Employee 20

Intersect Query Result 21

Set Difference in SQL Find the first names of employees that are not also surnames: select FirstName as Name from Employee except select Surname from Employee 22

Set Difference Result 23

Views A view is a virtual table. It does not physically exist. Rather, it is created by a query joining one or more tables A view can be a subset of a table Syntax: create view ViewName [ (AttributeList) ] as SelectSQL 24

Creating a Sample View Create a view for the employees of Administration department using their Name, Surname, and Salary. The view is the result of a select query on the employee table 25

Example Creating Views Create view AdminEmployee (FirstName,Surname,Salary) as select FirstName,Surname,Salary from Employee where Dept = Administration 26

Example View 27

Summary Aggregate functions are used to get information about the records of a table Set operations are also used in SQL tables A virtual table named view can be created using some of the records or attributes of one or more tables. 28

Questions? 29