Basic SQL Query SQL. SQL Query. Conceptual Evaluation Strategy. Example of Conceptual Evaluation. A Slightly Modified Query

Similar documents
Relational Algebra and SQL

SQL: Queries, Programming, Triggers

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

SQL: Queries, Programming, Triggers

Chapter 5. SQL: Queries, Constraints, Triggers

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

Θεµελίωση Βάσεων εδοµένων

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

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

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

Proposed solutions Project Assignment #1 (SQL)

Relational Algebra. Module 3, Lecture 1. Database Management Systems, R. Ramakrishnan 1

Query-by-Example (QBE)

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

The Relational Model. Why Study the Relational Model? Relational Database: Definitions. Chapter 3

SQL: QUERIES, CONSTRAINTS, TRIGGERS

The Relational Model. Why Study the Relational Model? Relational Database: Definitions

The Relational Model. Why Study the Relational Model?

Database Management Systems. Chapter 1

Lecture 6. SQL, Logical DB Design

Relational Database: Additional Operations on Relations; SQL

Oracle SQL. Course Summary. Duration. Objectives

Answer Key. UNIVERSITY OF CALIFORNIA College of Engineering Department of EECS, Computer Science Division

Introduction to Microsoft Jet SQL

SQL NULL s, Constraints, Triggers

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

6 QUERY-BY-EXAMPLE (QBE)

The Relational Model. Ramakrishnan&Gehrke, Chapter 3 CS4320 1

IT2305 Database Systems I (Compulsory)

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

Instant SQL Programming

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

IT2304: Database Systems 1 (DBS 1)

Outline. Data Modeling. Conceptual Design. ER Model Basics: Entities. ER Model Basics: Relationships. Ternary Relationships. Yanlei Diao UMass Amherst

Lecture 4: More SQL and Relational Algebra

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

Database Security. Chapter 21

Oracle Database: SQL and PL/SQL Fundamentals

Oracle Database: SQL and PL/SQL Fundamentals

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

Relational Databases

3. Relational Model and Relational Algebra

Introduction to SQL: Data Retrieving

Oracle Database: SQL and PL/SQL Fundamentals NEW

INFO/CS 330: Applied Database Systems

Why Is This Important? Database Application Development. SQL in Application Code. Overview. SQL in Application Code (Contd.

MOC 20461C: Querying Microsoft SQL Server. Course Overview

Oracle Database 10g: Introduction to SQL

Review: Participation Constraints

Advance DBMS. Structured Query Language (SQL)

CS2Bh: Current Technologies. Introduction to XML and Relational Databases. The Relational Model. The relational model

Databases and BigData

Introduction to database design

Netezza SQL Class Outline

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

Databases 2011 The Relational Model and SQL

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

Database Design Overview. Conceptual Design ER Model. Entities and Entity Sets. Entity Set Representation. Keys

There are five fields or columns, with names and types as shown above.

Review Entity-Relationship Diagrams and the Relational Model. Data Models. Review. Why Study the Relational Model? Steps in Database Design

Data Warehousing and Decision Support. Introduction. Three Complementary Trends. Chapter 23, Part A

DBMS / Business Intelligence, SQL Server


Security and Authorization. Introduction to DB Security. Access Controls. Chapter 21

SQL (structured query language)

SQL/PSM. Outline. Database Application Development Oracle PL/SQL. Why Stored Procedures? Stored Procedures PL/SQL. Embedded SQL Dynamic SQL

Introduction to tuple calculus Tore Risch

Introduction to Querying & Reporting with SQL Server

CSE132A Solutions HW 1

How To Create A Table In Sql (Ahem)

Relational Calculus. Module 3, Lecture 2. Database Management Systems, R. Ramakrishnan 1

Oracle Database 12c: Introduction to SQL Ed 1.1

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

Querying Microsoft SQL Server

Database Application Development. Overview. SQL in Application Code. Chapter 6

Week 4 & 5: SQL. SQL as a Query Language

1 Structured Query Language: Again. 2 Joining Tables

Data Structure: Relational Model. Programming Interface: JDBC/ODBC. SQL Queries: The Basic From

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

Oracle Database 11g SQL

Overview. Database Application Development. SQL in Application Code (Contd.) SQL in Application Code. Embedded SQL. Embedded SQL: Variables

unisys OS 2200 Relational Data Management System (RDMS 2200) and IPF SQL Interface End Use Guide imagine it. done. ClearPath OS

MOC QUERYING MICROSOFT SQL SERVER

EECS 647: Introduction to Database Systems

Ad Hoc Advanced Table of Contents

Lecture 6: Query optimization, query tuning. Rasmus Pagh

Course ID#: W 35 Hrs. Course Content

Querying Microsoft SQL Server 20461C; 5 days

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

Structured Query Language (SQL)

Oracle Database: SQL and PL/SQL Fundamentals NEW

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

Introducing Microsoft SQL Server 2012 Getting Started with SQL Server Management Studio

- Eliminating redundant data - Ensuring data dependencies makes sense. ie:- data is stored logically

CSE 530A Database Management Systems. Introduction. Washington University Fall 2013

types, but key declarations and constraints Similar CREATE X commands for other schema ëdrop X name" deletes the created element of beer VARCHARè20è,

Physical Database Design and Tuning

Data Modeling. Database Systems: The Complete Book Ch ,

1. Physical Database Design in Relational Databases (1)

Querying Microsoft SQL Server Course M Day(s) 30:00 Hours

Transcription:

Basic SQL Query SQL SELECT FROM [WHERE [DISTINCT] target-list relation-list condition] SELECT S.Name WHERE S.Age > 25 SELECT DISTINCT S.Name WHERE S.Age > 25 Default is that duplicates are not eliminated! Need to explicitly say DISTINCT Database Management Systems, R. Ramakrishnan and J. Gehrke 1 Database Management Systems, R. Ramakrishnan and J. Gehrke 2 SQL Query 31 lubber 8 55.5, Reseres R WHERE S.sid=R.sid AND R.bid=103 Sailors Reseres sid bid day 22 101 10/10/96 58 103 11/12/96 Database Management Systems, R. Ramakrishnan and J. Gehrke 3 Conceptual Ealuation Strategy Semantics of an SQL query defined in terms of the following conceptual ealuation strategy: Compute the cross-product of relation-list Discard resulting tuples if they fail condition. Delete attributes that are not in target-list If DISTINCT is specified, eliminate duplicate rows. This strategy is probably the least efficient way to compute a query! An optimizer will find more efficient strategies to compute the same answers. Database Management Systems, R. Ramakrishnan and J. Gehrke 4 Example of Conceptual Ealuation, Reseres R WHERE S.sid=R.sid AND R.bid=103 (sid) sname rating age (sid) bid day 22 101 10/10/96 58 103 11/12/96 31 lubber 8 55.5 22 101 10/10/96 31 lubber 8 55.5 58 103 11/12/96 22 101 10/10/96 58 103 11/12/96 A Slightly Modified Query, Reseres R WHERE S.sid=R.sid AND R.bid=103 Would adding DISTINCT to this query make a difference? Database Management Systems, R. Ramakrishnan and J. Gehrke 5 Database Management Systems, R. Ramakrishnan and J. Gehrke 6

Find sid s of sailors who e resered a red or a green boat What does this query compute?, Boats B, Reseres R AND (B.color= red OR B.color= green ), Boats B, Reseres R AND B.color= red UNION, Boats B, Reseres R AND B.color= green Database Management Systems, R. Ramakrishnan and J. Gehrke 7, Boats B1, Reseres R1, Boats B2, Reseres R2 WHERE S.sid=R1.sid AND R1.bid=B1.bid AND S.sid=R2.sid AND R2.bid=B2.bid AND B1.color= red AND B2.color= green Database Management Systems, R. Ramakrishnan and J. Gehrke 8 Find sid s of sailors who e resered a red and a green boat Key field!, Boats B, Reseres R AND B.color= red INTERSECT, Boats B, Reseres R AND B.color= green What if INTERSECT were replaced by EXCEPT? EXCEPT is set difference Database Management Systems, R. Ramakrishnan and J. Gehrke 9 Expressions and Strings SELECT S.age, S.age-5 AS age2, 2*S.age AS age2 WHERE S.sname LIKE B_%B Find triples (of ages of sailors and two fields defined by expressions) for sailors whose names begin and end with B and contain at least three characters. AS is used to name fields in result. LIKE is used for string matching `_ stands for any one character `% stands for 0 or more arbitrary characters. Database Management Systems, R. Ramakrishnan and J. Gehrke 10 Nested Queries (with Correlation) Nested Queries (with Correlation) Find names of sailors who hae resered boat #103: Find names of sailors who hae not resered boat #103: WHERE EXISTS (SELECT * FROM Reseres R WHERE R.bid=103 AND S.sid=R.sid) WHERE NOT EXISTS (SELECT * FROM Reseres R WHERE R.bid=103 AND S.sid=R.sid) Database Management Systems, R. Ramakrishnan and J. Gehrke 11 Database Management Systems, R. Ramakrishnan and J. Gehrke 12

Diision in SQL Find sailors who e resered all boats Diision in SQL (without Except!) Find sailors who e resered all boats. WHERE NOT EXISTS ((SELECT B.bid FROM Boats B) EXCEPT (SELECT R.bid FROM Reseres R WHERE R.sid=S.sid)) Database Management Systems, R. Ramakrishnan and J. Gehrke 13 WHERE NOT EXISTS (SELECT B.bid FROM Boats B WHERE NOT EXISTS (SELECT Sailors S such that... R.bid FROM Reseres R there is no boat B without... WHERE R.bid=B.bid AND R.sid=S.sid)) a Reseres tuple showing S resered B Database Management Systems, R. Ramakrishnan and J. Gehrke 14 More on Set-Comparison Operators op ANY, op ALL op can be >, <, =,,, Find sailors whose rating is greater than that of all sailors called Horatio: SELECT * WHERE S.rating > ALL (SELECT S2.rating 2 WHERE S2.sname= Horatio ) Database Management Systems, R. Ramakrishnan and J. Gehrke 15 Aggregate Operators Significant extension of relational algebra. COUNT (*) COUNT ( [DISTINCT] A) SUM ( [DISTINCT] A) AVG ( [DISTINCT] A) MAX (A) MIN (A) single column SELECT COUNT (*) SELECT AVG (S.age) WHERE S.rating=10 SELECT COUNT (DISTINCT S.rating) WHERE S.sname= Bob Database Management Systems, R. Ramakrishnan and J. Gehrke 16 Find name and age of the oldest sailor(s) with rating > 7 Find name and age of the oldest sailor(s) with rating > 7 This query is illegal!, MAX (S.age) WHERE S.Rating > 7 MAX (and other aggregate operators) can only return one tuple!, S.age WHERE S.Rating > 7 AND S.age = (SELECT MAX (S2.age) 2 WHERE S2.Rating > 7) Database Management Systems, R. Ramakrishnan and J. Gehrke 17 Database Management Systems, R. Ramakrishnan and J. Gehrke 18

Aggregate Operators GROUP BY So far, we e applied aggregate operators to all (qualifying) tuples Sometimes, we want to apply them to each of seeral groups of tuples. Consider: Find the age of the youngest sailor for each rating leel. If rating alues go from 1 to 10; we can write 10 queries that look like this: SELECT MIN (S.age) For i = 1, 2,..., 10: WHERE S.rating = i Database Management Systems, R. Ramakrishnan and J. Gehrke 19 SELECT [DISTINCT] target-list FROM relation-list [WHERE condition] GROUP BY grouping-list Find the age of the youngest sailor for each rating leel SELECT S.rating, MIN(S.Age) Database Management Systems, R. Ramakrishnan and J. Gehrke 20 Conceptual Ealuation Strategy Semantics of an SQL query defined as follows: Compute the cross-product of relation-list Discard resulting tuples if they fail condition. Delete attributes that are not in target-list Remaining tuples are partitioned into groups by the alue of the attributes in grouping-list One answer tuple is generated per group Note: Does not imply query will actually be ealuated this way! Database Management Systems, R. Ramakrishnan and J. Gehrke 21 Find the age of the youngest sailor with age 18, for each rating with at least one such sailor SELECT S.rating, MIN (S.age) 29 brutus 1 33.0 64 horatio 7 35.0 31 lubber 8 15.5 71 zorba 10 16.0 64 horatio 7 35.0 29 brutus 1 33.0 rating 1 33.0 7 35.0 10 35.0 Answer relation Database Management Systems, R. Ramakrishnan and J. Gehrke 22 What does this query compute? Find those ratings for which the aerage age is the minimum oer all ratings SELECT B.bid, COUNT (*) AS scount FROM Reseres R, Boats B WHERE R.bid=B.bid AND B.color= red GROUP BY B.bid SELECT Temp.rating, Temp.agage FROM (SELECT S.rating, AVG (S.age) AS agage ) AS Temp WHERE Temp.agage = (SELECT MIN (Temp2.agage) FROM (SELECT AVG(S.age) as agage )) Database Management Systems, R. Ramakrishnan and J. Gehrke 23 Database Management Systems, R. Ramakrishnan and J. Gehrke 24

Find those ratings for which the aerage age is the minimum oer all ratings What does this query compute? SELECT S.rating WHERE S.age = (SELECT MIN (AVG (S2.age)) 2) SELECT Temp.rating, Temp.minage FROM (SELECT S.rating, MIN (S.age) AS minage, COUNT(*) AS cnt ) AS Temp WHERE Temp.cnt >= 2 This is WRONG! Cannot nest aggregates Database Management Systems, R. Ramakrishnan and J. Gehrke 25 Database Management Systems, R. Ramakrishnan and J. Gehrke 26 Queries With GROUP BY and HAVING SELECT [DISTINCT] target-list FROM relation-list [WHERE qualification] GROUP BY grouping-list HAVING group-qualification Find the age of the youngest sailor with age >= 18 for each rating leel with at least 2 such sailors SELECT S.rating, MIN(S.Age) HAVING COUNT(*) >= 2 Database Management Systems, R. Ramakrishnan and J. Gehrke 27 Conceptual Ealuation Strategy Semantics of an SQL query defined as follows: Compute the cross-product of relation-list Discard resulting tuples if they fail condition. Delete attributes that are not in target-list Remaining tuples are partitioned into groups by the alue of the attributes in grouping-list The group-qualification is applied to eliminate some groups One answer tuple is generated per qualifying group Note: Does not imply query will actually be ealuated this way! Database Management Systems, R. Ramakrishnan and J. Gehrke 28 Find the age of the youngest sailor with age 18, for each rating with at least 2 such sailors SELECT S.rating, MIN (S.age) HAVING COUNT (*) > 1 Only S.rating and S.age are mentioned in the SELECT, GROUP BY or HAVING clauses; other attributes ùnnecessary. 2nd column of result is unnamed. (Use AS to name it.) 31 lubber 8 55.5 71 zorba 10 16.0 64 horatio 7 35.0 29 brutus 1 33.0 rating age 1 33.0 7 45.0 7 35.0 8 55.5 10 35.0 rating 7 35.0 Answer relation Database Management Systems, R. Ramakrishnan and J. Gehrke 29 Find the age of the youngest sailor with age >= 18, for each rating with at least 2 sailors (of any age) SELECT S.rating, MIN (S.age) HAVING 1 < (SELECT COUNT (*) 2 WHERE S.rating=S2.rating) Database Management Systems, R. Ramakrishnan and J. Gehrke 30

Null Values Field alues in a tuple are sometimes unknown e.g., a rating has not been assigned Field alues are sometimes inapplicable e.g., no spouse s name SQL proides a special alue null for such situations. Database Management Systems, R. Ramakrishnan and J. Gehrke 31 Queries and Null Values SELECT S.Name WHERE S.Age > 25 What if S.Age is NULL? S.Age > 25 returns NULL! Implies a predicate can return 3 alues True, false, NULL Three alued logic! Where clause eliminates rows that do not return true (i.e., which are false or NULL) Database Management Systems, R. Ramakrishnan and J. Gehrke 32 Three-alued Logic General Constraints SELECT S.Name WHERE NOT(S.Age > 25) OR S.rating > 7 What if one or both of S.age and S.rating are NULL? NOT Truth Table A NOT(A) True False False True NULL NULL OR Truth Table A/B True False NULL True True True True False True False NULL NULL True NULL NULL Useful when more general ICs than keys are inoled Can use queries to express constraint Constraints can be named CREATE TABLE Reseres ( sname CHAR(10), bid INTEGER, day DATE, PRIMARY KEY (bid,day), CONSTRAINT nointerlakeres CHECK ( Ìnterlake <> ( SELECT B.bname FROM Boats B WHERE B.bid=bid))) Database Management Systems, R. Ramakrishnan and J. Gehrke 33 Database Management Systems, R. Ramakrishnan and J. Gehrke 34 Constraints Oer Multiple Relations Number of boats plus number of sailors is < 100 CREATE ASSERTION smallclub CHECK ( (SELECT COUNT (S.sid) ) + (SELECT COUNT (B.bid) FROM Boats B) < 100 ) Database Management Systems, R. Ramakrishnan and J. Gehrke 35