Comp 5311 Database Management Systems. 3. Structured Query Language 1

Similar documents
Simple SQL Queries (3)

Advance DBMS. Structured Query Language (SQL)

Chapter 4: SQL. Schema Used in Examples

Introduction to SQL ( )

COMP 5138 Relational Database Management Systems. Week 5 : Basic SQL. Today s Agenda. Overview. Basic SQL Queries. Joins Queries

Chapter 6: Integrity Constraints

Chapter 4: SQL. Schema Used in Examples. Basic Structure. The select Clause. modifications and enhancements! A typical SQL query has the form:

You can use command show database; to check if bank has been created.

SUBQUERIES AND VIEWS. CS121: Introduction to Relational Database Systems Fall 2015 Lecture 6

More on SQL. Juliana Freire. Some slides adapted from J. Ullman, L. Delcambre, R. Ramakrishnan, G. Lindstrom and Silberschatz, Korth and Sudarshan

SQL is capable in manipulating relational data SQL is not good for many other tasks

Lesson 8: Introduction to Databases E-R Data Modeling

Chapter 1: Introduction. Database Management System (DBMS)

Introduction to database management systems

UNIT 6. Structured Query Language (SQL) Text: Chapter 5

Basic Concepts. Chapter A: Network Model. Cont.) Data-Structure Diagrams (Cont( Data-Structure Diagrams. General Relationships. The DBTG CODASYL Model

Chapter 7: Relational Database Design

Relational Databases

3.GETTING STARTED WITH ORACLE8i

Chapter 14: Query Optimization

Chapter 6: Integrity and Security. Domain Constraints

Comp 3311 Database Management Systems. 2. Relational Model Exercises

Relational Database: Additional Operations on Relations; SQL

Chapter 5. SQL: Queries, Constraints, Triggers

Introduction to Microsoft Jet SQL

SQL: Queries, Programming, Triggers

Chapter 1: Introduction

Oracle SQL. Course Summary. Duration. Objectives

Example Instances. SQL: Queries, Programming, Triggers. Conceptual Evaluation Strategy. Basic SQL Query. A Note on Range Variables

Boyce-Codd Normal Form

Introdução às Bases de Dados

A Comparison of Database Query Languages: SQL, SPARQL, CQL, DMX

Chapter 5 More SQL: Complex Queries, Triggers, Views, and Schema Modification

Evaluation of Expressions

Using Multiple Operations. Implementing Table Operations Using Structured Query Language (SQL)

SQL: Queries, Programming, Triggers

Oracle Database 11g SQL

B2.2-R3: INTRODUCTION TO DATABASE MANAGEMENT SYSTEMS

Oracle Database 10g: Introduction to SQL

We know how to query a database using SQL. A set of tables and their schemas are given Data are properly loaded

Chapter 1: Introduction

Information Systems SQL. Nikolaj Popov

The SQL Query Language. Creating Relations in SQL. Referential Integrity in SQL. Basic SQL Query. Primary and Candidate Keys in SQL

Oracle Database: SQL and PL/SQL Fundamentals

? Database Management

Oracle Database: SQL and PL/SQL Fundamentals

Part A: Data Definition Language (DDL) Schema and Catalog CREAT TABLE. Referential Triggered Actions. CSC 742 Database Management Systems

Introduction to SQL: Data Retrieving

Oracle Database 12c: Introduction to SQL Ed 1.1

SQL DATA DEFINITION: KEY CONSTRAINTS. CS121: Introduction to Relational Database Systems Fall 2015 Lecture 7

Introduction to SQL C H A P T E R3. Exercises

Introduction to SQL and SQL in R. LISA Short Courses Xinran Hu

Chapter 8. SQL-99: SchemaDefinition, Constraints, and Queries and Views

Introduction to database design

Chapter 9 Joining Data from Multiple Tables. Oracle 10g: SQL

MOC 20461C: Querying Microsoft SQL Server. Course Overview

Oracle Database: SQL and PL/SQL Fundamentals NEW

ICAB4136B Use structured query language to create database structures and manipulate data

SQL - QUICK GUIDE. Allows users to access data in relational database management systems.

Information and Computer Science Department ICS 324 Database Systems Lab#11 SQL-Basic Query

Exercise 1: Relational Model

Chapter 7: Relational Database Design

Functional Dependencies

SQL Server. 1. What is RDBMS?

Converting E-R Diagrams to Relational Model. Winter Lecture 17

A Brief Introduction to MySQL

Chapter 1 Overview of the SQL Procedure

Structured Query Language (SQL)

Database Query 1: SQL Basics

Chapter 2: Entity-Relationship Model. Entity Sets. " Example: specific person, company, event, plant

Performing Queries Using PROC SQL (1)

SQL Simple Queries. Chapter 3.1 V3.0. Napier University Dr Gordon Russell

Programming with SQL

2874CD1EssentialSQL.qxd 6/25/01 3:06 PM Page 1 Essential SQL Copyright 2001 SYBEX, Inc., Alameda, CA

A Novel Approach of Query Optimization for Distributed Database Systems

Databases 2011 The Relational Model and SQL

Query Processing C H A P T E R12. Practice Exercises

Duration Vendor Audience 5 Days Oracle End Users, Developers, Technical Consultants and Support Staff

Chapter 2: Entity-Relationship Model. E-R R Diagrams

History of SQL. Relational Database Languages. Tuple relational calculus ALPHA (Codd, 1970s) QUEL (based on ALPHA) Datalog (rule-based, like PROLOG)

Instant SQL Programming

SQL. Short introduction

Chapter 13: Query Processing. Basic Steps in Query Processing

Summary on Chapter 4 Basic SQL

Object-Oriented Query Languages: Object Query Language (OQL)

Oracle Database: Introduction to SQL

Oracle Database: Introduction to SQL

Boats bid bname color 101 Interlake blue 102 Interlake red 103 Clipper green 104 Marine red. Figure 1: Instances of Sailors, Boats and Reserves

David M. Kroenke and David J. Auer Database Processing: Fundamentals, Design and Implementation

Foundations of Information Management

Oracle Database: Introduction to SQL


Relational Algebra and SQL

Effective Use of SQL in SAS Programming

3. Relational Model and Relational Algebra

4. SQL. Contents. Example Database. CUSTOMERS(FName, LName, CAddress, Account) PRODUCTS(Prodname, Category) SUPPLIERS(SName, SAddress, Chain)

Chapter 13: Query Optimization

Netezza SQL Class Outline

Oracle 10g PL/SQL Training

Financial Data Access with SQL, Excel & VBA

Transcription:

Comp 5311 Database Management Systems 3. Structured Query Language 1 1

Aspects of SQL Most common Query Language used in all commercial systems Discussion is based on the SQL92 Standard. Commercial products have different features of SQL, but the basic structure is the same Data Manipulation Language Data Definition Language Constraint Specification Embedded SQL Transaction Management Security Management... 2

Basic Structure SQL is based on set and relational operations with certain modifications and enhancements A typical SQL query has the form: select A 1, A 2,, A n from R 1, R 2,, R m where P - A i represent attributes - R i represent relations - P is a predicate. This query is equivalent to the relational algebra expression: A1, A2,, An ( P (R 1 R 2 R m )) The result of an SQL query is a relation (but may contain duplicates). SQL statements can be nested. 3

Projection The select clause corresponds to the projection operation of the relational algebra. It is used to list the attributes desired in the result of a query. Find the names of all branches in the loan relation select branch-name from loan Equivalent to: branch-name (loan) An asterisk in the select clause denotes all attributes select * from loan Note: for our examples we use the tables: Branch (branch-name, branch-city, assets) Customer (customer-name, customer-street, customer-city) Loan (loan-number, amount, branch-name) Account (account-number, balance, branch-name) Borrower (customer-name, loan-number) Depositor (customer-name, account-number) 4

Duplicate Removal SQL allows duplicates in relations as well as in query results. Use select distinct to force the elimination of duplicates. Find the names of all branches in the loan relation, and remove duplicates select distinct branch-name from loan The keyword all specifies that duplicates are not removed. select all branch-name from loan force the DBMS to remove duplicates force the DBMS not to remove duplicates 5

Arithmetic Operations on Retrieved Results The select clause can contain arithmetic expressions involving the operators,,, and, and operating on constants or attributes of tuples. The query: select branch-name, loan-number, amount * 100 from loan would return a relation which is the same as the loan table, except that the attribute amount is multiplied by 100 6

The where Clause The where clause specifies conditions that tuples in the relations in the from clause must satisfy. Find all loan numbers for loans made at the Perryridge branch with loan amounts greater than $1200. select loan-number from loan where branch-name= Perryridge and amount >1200 SQL allows logical connectives and, or, and not. Arithmetic expressions can be used in the comparison operators. Note: attributes used in a query (both select and where parts) must be defined in the relations in the from clause. 7

The where Clause (Cont.) SQL includes the between operator for convenience. Find the loan number of those loans with loan amounts between $90,000 and $100,000 (that is, $90,000 and $100,000) select loan-number from loan where amount between 90000 and 100000 8

The from Clause The from clause corresponds to the Cartesian product operation of the relational algebra. Find the Cartesian product borrower loan select * from borrower, loan It is rarely used without a where clause. Find the name and loan number of all customers having a loan at the Perryridge branch. select distinct customer-name, borrower.loan-number from borrower, loan where borrower.loan-number = loan.loan-number and branch-name = Perryridge 9

The Rename Operation Renaming relations and attributes using the as clause: old-name as new-name Find the name and loan number of all customers having a loan at the Perryridge branch; replace the column name loan-number with the name loan-id. select distinct customer-name, borrower.loan-number as loan-id from borrower, loan where borrower.loan-number = loan.loan-number and branch-name = Perryridge 10

Tuple Variables/Alias Tuple variables are defined in the from clause via the use of the as clause. Find the customer names and their loan numbers for all customers having a loan at some branch. select distinct customer-name, T.loan-number from borrower as T, loan as S where T.loan-number = S.loan-number Tuple variable/alias can be used as short hand, but it is more than just a short hand (see next slide) 11

Tuple Variables/Alias Find the names of all branches that have greater assets than some branch located in Brooklyn. select distinct T.branch-name from branch as T, branch as S where T.assets > S.assets and S.branch-city= Brooklyn branch T branch S: a branch in Brooklyn branches in Brooklyn Does it returns branches within Brooklyn? 12

String Operations Character attributes can be compared to a pattern: % matches any substring. _ matches any single character. Find the name of all customers whose street includes the substring Main. (Eg Mainroad, Smallmain Road, AMainroad, ) select customer-name from customer where customer-street like %Main% 13

Ordering the Display of Tuples List in alphabetic order the names of all customers having a loan at Perryridge branch select distinct customer-name from borrower, loan where borrower.loan-number = loan.loan-number and branch-name = Perryridge order by customer-name order by customer-name desc, amount asc desc for descending order; asc for ascending order (default) SQL must perform a sort to fulfill an order by request. Since sorting a large number of tuples may be costly, it is desirable to sort only when necessary. 14

Set Operations The set operation union, intersect, and except operate on relations and correspond to the relational algebra operations, and. Each of the above operations automatically eliminates duplicates; to retain all duplicates use union all, intersect all and except all. Suppose a tuple occurs m times in r and n times in s, then, it occurs: m + n times in r union all s min(m,n) times in r intersect all s max(0,m-n) times in r except all s 15

Set operations Find all customers who have a loan, an account, or both: (select customer-name from depositor) union (select customer-name from borrower) Find all customers who have both a loan and an account. (select customer-name from depositor) intersect (select customer-name from borrower) Find all customers who have an account but no loan. (select customer-name from depositor) except (select customer-name from borrower) 16

SQL - Nested Subqueries Every SQL statement returns a relation/set in the result; remember a relation could be null or merely contain a single atomic value You can replace a value or set of values with a SQL statement (ie., a subquery) select * select * from loan from loan where amount > 1200 where amount > select avg(amount) from loan Illegal if the subquery returns the wrong type for the comparison 17

Example Query - IN Find all customers who have both an account and a loan in the bank. select distinct customer-name from borrower where customer-name in (select customer-name from depositor) Check for each borrower if he/she is also a depositor Return the set of depositors 18

Example Query NOT IN Find all customers who have a loan at the bank but do not have an account at the bank. select distinct customer-name from borrower where customer-name not in (select customer-name from depositor) 19

The Some Clause Find all branches that have greater assets than some branch located in Brooklyn Equivalent to find all branches that have greater assets than the minimum assets of any branch located in Brooklyn select branch-name from branch where assets > some (select assets from branch where branch-city = Brooklyn ) Assets of all branches in Brooklyn 20

Some Semantics 0 (5 < some 5 ) returns true (5 < 6) 6 (5 < some 0 ) returns false 5 0 (5 = some ) = true 5 Note: (= some) is equivalent to in However, ( some) is not equivalent to not in 0 (5 some ) = true (since 0 5) 5 21

The All Clause Find the names of all branches that have greater assets than all branches located in Brooklyn. Equivalent to find all branches that have greater assets than the maximum assets of any branch located in Brooklyn select branch-name from branch where assets > all (select assets from branch where branch-city= Brooklyn ) Assets of all branches in Brooklyn 22

All Semantics 0 (5 < all 5 ) = false 6 (5 < all ) = true 4 (5 = all ) = false 5 6 6 10 (5 all ) = true 10 Note: ( all) is equivalent to not in However, (= all) is not equivalent to in 23

Test for Empty Relations exists returns true if the argument subquery is nonempty. Find all customer names who have both a loan and an account. select customer-name from depositor as D where exists (select * from borrower as B where D.customer-name = B.customer-name) Find all customer names who have an account but no loan. select customer-name from depositor as D where not exists (select * from borrower as B where D.customer-name = B.customer-name) 24

Test for Absence of Duplicate Tuples unique tests whether a subquery has any duplicate tuples in its result. Find all customers who have only one account at the Perryridge branch. select T.customer-name For each depositor T, check... from depositor as T where unique ( Find depositors with select R.customer-name same name as T from account, depositor as R where T.customer-name = R.customer-name and R.account-number = account.account-number and account.branch-name = Perryridge ) Customers at Perryridge with same name as T 25

Example Query NOT UNIQUE Find all customers with at least 2 accounts at the Perryridge branch. select T.customer-name from depositor as T where not unique( select R.customer-name from account, depositor as R where T.customer-name = R.customer-name and R.account-number = account.account-number and account.branch-name = Perryridge ) 26

Division in SQL Find all customers with an account at all branches located in Brooklyn. For each customer S, check... Branches where customer S has an account select distinct S.customer-name from depositor as S where not exist ( (select branch-name Branches in Brooklyn where customer S doesn t have an account from branch where branch-city= Brooklyn ) except (select R.branch-name from depositor as T, account as R where T.account-number = R.account-number and S.customer-name = T.customer-name) ) X Y = X 27