CMU SCS Carnegie Mellon Univ. Dept. of Computer Science 15-415/615 - DB Applications C. Faloutsos & A. Pavlo Lecture#6: Rel. model - SQL part1 (R&G, chapter.

Slides:



Advertisements
Similar presentations
SQL: The Query Language Part 1 R &G - Chapter 5 Life is just a bowl of queries. -Anon (not Forrest Gump)
Advertisements

CMU SCS Carnegie Mellon Univ. School of Computer Science /615 - DB Applications C. Faloutsos & A. Pavlo Lecture #4: Relational Algebra.
IMPLEMENTATION OF INFORMATION RETRIEVAL SYSTEMS VIA RDBMS.
CMU SCS Carnegie Mellon Univ. Dept. of Computer Science /615 – DB Applications Faloutsos & Pavlo Lecture#11 (R&G ch. 11) Hashing.
CMU SCS Carnegie Mellon Univ. Dept. of Computer Science /615 - DB Applications Lecture #16: Schema Refinement & Normalization - Functional Dependencies.
1 SQL: Structured Query Language (‘Sequel’) Chapter 5.
SQL (2).
Information Resources Management February 27, 2001.
CMU SCS Carnegie Mellon Univ. Dept. of Computer Science Database Applications C. Faloutsos Lecture#7 (cont’d): Rel. model - SQL part3.
Midterm Review Lecture 14b. 14 Lectures So Far 1.Introduction 2.The Relational Model 3.Disks and Files 4.Relational Algebra 5.File Org, Indexes 6.Relational.
1 SQL: Structured Query Language (‘Sequel’) Chapter 5.
FALL 2004CENG 351 File Structures and Data Management1 SQL: Structured Query Language Chapter 5.
SQL Structured Query Language Meizhen Huang. Content (4.1 – 4.4) Background Parts of SQL Basic Structure Set Operations Aggregate Functions.
Temple University – CIS Dept. CIS331– Principles of Database Systems V. Megalooikonomou Query by example (based on notes by Silberchatz,Korth, and Sudarshan.
Database Management Systems 3ed, R. Ramakrishnan and J. Gehrke1 SQL: Queries, Constraints, Triggers Chapter 5.
SQL I. SQL – Introduction  Standard DML/DDL for relational DB’s  DML = “Data Manipulation Language” (queries, updates)  DDL = “Data Definition Language”
CMU SCS Carnegie Mellon Univ. Dept. of Computer Science /615 - DB Applications C. Faloutsos & A. Pavlo Lecture#7 : Rel. model - SQL part2 (R&G, chapters.
Carnegie Mellon Carnegie Mellon Univ. Dept. of Computer Science Database Applications C. Faloutsos Rel. model - SQL part1.
Temple University – CIS Dept. CIS331– Principles of Database Systems V. Megalooikonomou Relational Model – SQL Part I (based on notes by Silberchatz,Korth,
©Silberschatz, Korth and Sudarshan4.1Database System Concepts Chapter 4: SQL Basic Structure Set Operations Aggregate Functions Null Values Nested Subqueries.
The Relational Model. Review Why use a DBMS? OS provides RAM and disk.
CS609 Introduction. Databases Current state? Future?
1 CS 430 Database Theory Winter 2005 Lecture 12: SQL DML - SELECT.
Temple University – CIS Dept. CIS331– Principles of Database Systems V. Megalooikonomou Relational Model – SQL Part II (based on notes by Silberchatz,Korth,
Relational Algebra - Chapter (7th ed )
SQL: Data Manipulation Presented by Mary Choi For CS157B Dr. Sin Min Lee.
FALL 2004CENG 351 File Structures and Data Management1 Relational Model Chapter 3.
CMU SCS Carnegie Mellon Univ. Dept. of Computer Science Database Applications Lecture#6: Relational calculus.
Database Management COP4540, SCS, FIU Structured Query Language (Chapter 8)
Carnegie Mellon Carnegie Mellon Univ. Dept. of Computer Science Database Applications C. Faloutsos Rel. model - SQL part3.
Carnegie Mellon Carnegie Mellon Univ. Dept. of Computer Science Database Applications C. Faloutsos Relational model.
Temple University – CIS Dept. CIS331– Principles of Database Systems V. Megalooikonomou Relational Model I (based on notes by Silberchatz,Korth, and Sudarshan.
Carnegie Mellon Carnegie Mellon Univ. Dept. of Computer Science Database Applications C. Faloutsos Relational domain calculus.
CMU SCS Carnegie Mellon Univ. Dept. of Computer Science /615 – DB Applications C. Faloutsos & A. Pavlo Lecture#5: Relational calculus.
CMU SCS Carnegie Mellon Univ. Dept. of Computer Science /615 - DB Applications Lecture #16: Schema Refinement & Normalization - Functional Dependencies.
1 CSCE Database Systems Anxiao (Andrew) Jiang The Database Language SQL.
1/18/00CSE 711 data mining1 What is SQL? Query language for structural databases (esp. RDB) Structured Query Language Originated from Sequel 2 by Chamberlin.
CMPT 258 Database Systems The Relationship Model (Chapter 3)
Carnegie Mellon Carnegie Mellon Univ. Dept. of Computer Science Database Applications C. Faloutsos Rel. model - SQL part2.
SqlExam1Review.ppt EXAM - 1. SQL stands for -- Structured Query Language Putting a manual database on a computer ensures? Data is more current Data is.
Temple University – CIS Dept. CIS616– Principles of Data Management V. Megalooikonomou Relational Model – SQL (based on notes by Silberchatz,Korth, and.
File Organizations and Indexing
1 SQL: The Query Language (Part II). 2 Expressions and Strings v Illustrates use of arithmetic expressions and string pattern matching: Find triples (of.
1 Dept. of CIS, Temple Univ. CIS616/661 – Principles of Data Management V. Megalooikonomou SQL (part 2) (based on slides by C. Faloutsos at CMU)
Query Processing – Implementing Set Operations and Joins Chap. 19.
SQL: The Query Language Part 1 R &G - Chapter 5 The important thing is not to stop questioning. Albert Einstein.
1 SQL: The Query Language. 2 Example Instances R1 S1 S2 v We will use these instances of the Sailors and Reserves relations in our examples. v If the.
Carnegie Mellon Carnegie Mellon Univ. Dept. of Computer Science Database Applications C. Faloutsos Relational tuple calculus.
SQL: Interactive Queries (2) Prof. Weining Zhang Cs.utsa.edu.
Chapter 3 The Relational Model. Why Study the Relational Model? Most widely used model. Vendors: IBM, Informix, Microsoft, Oracle, Sybase, etc. “Legacy.
1 CS122A: Introduction to Data Management Lecture 9 SQL II: Nested Queries, Aggregation, Grouping Instructor: Chen Li.
CENG 351 File Structures and Data Management1 Relational Model Chapter 3.
Concepts of Database Management, Fifth Edition Chapter 3: The Relational Model 2: SQL.
COP Introduction to Database Structures
SQL: Advanced Options, Updates and Views Lecturer: Dr Pavle Mogin
SQL: Structured Query Language DML- Queries Lecturer: Dr Pavle Mogin
Lecture #4: Relational Algebra
Lecture#7: Fun with SQL (Part 2)
CS 405G: Introduction to Database Systems
Database Applications (15-415) SQL-Part II Lecture 9, February 04, 2018 Mohammad Hammoud.
Database Applications (15-415) Relational Calculus Lecture 6, September 6, 2016 Mohammad Hammoud.
Faloutsos & Pavlo SCS /615 Carnegie Mellon Univ. Dept. of Computer Science /615 - DB Applications Lecture #16: Schema Refinement & Normalization.
Lecture#5: Relational calculus
Temple University – CIS Dept. CIS331– Principles of Database Systems
SQL: The Query Language Part 1
Relational Algebra Chapter 4, Sections 4.1 – 4.2
CSC 453 Database Systems Lecture
SQL: Structured Query Language
Relational Algebra & Calculus
Select-From-Where Statements Multirelation Queries Subqueries
Presentation transcript:

CMU SCS Carnegie Mellon Univ. Dept. of Computer Science /615 - DB Applications C. Faloutsos & A. Pavlo Lecture#6: Rel. model - SQL part1 (R&G, chapter 5)

CMU SCS Faloutsos, PavloCMU SCS /6152 General Overview - rel. model Formal query languages –rel algebra and calculi Commercial query languages –SQL –QBE, (QUEL)

CMU SCS Faloutsos, PavloCMU SCS /6153 Overview - detailed - SQL DML –select, from, where, renaming –set operations –ordering –aggregate functions –nested subqueries other parts: DDL, embedded SQL, auth etc

CMU SCS Faloutsos, PavloCMU SCS /6154 Relational Query Languages A major strength of the relational model: supports simple, powerful querying of data. Two sublanguages: DDL – Data Definition Language –define and modify schema (at all 3 levels) DML – Data Manipulation Language –Queries can be written intuitively.

CMU SCS Faloutsos, PavloCMU SCS /6155 Relational languages The DBMS is responsible for efficient evaluation. –Query optimizer: re-orders operations and generates query plan

CMU SCS Faloutsos, PavloCMU SCS /6156 The SQL Query Language The most widely used relational query language. – Major standard is SQL-1999 (=SQL3) Introduced “Object-Relational” concepts SQL 2003, SQL 2008 have small extensions – SQL92 is a basic subset

CMU SCS Faloutsos, PavloCMU SCS /6157 SQL (cont’d) –PostgreSQL has some “unique” aspects (as do most systems).

CMU SCS Faloutsos, PavloCMU SCS /6158 DML General form select a1, a2, … an from r1, r2, … rm where P [order by ….] [group by …] [having …]

CMU SCS Faloutsos, PavloCMU SCS /6159 Reminder: our Mini-U db

CMU SCS Faloutsos, PavloCMU SCS /61510 DML - eg: find the ssn(s) of everybody called “smith” select ssn from student where name=“smith”

CMU SCS Faloutsos, PavloCMU SCS /61511 DML - observation General form select a1, a2, … an from r1, r2, … rm where P equivalent rel. algebra query?

CMU SCS Faloutsos, PavloCMU SCS /61512 DML - observation General form select a1, a2, … an from r1, r2, … rm where P

CMU SCS Faloutsos, PavloCMU SCS /61513 DML - observation General form select distinct a1, a2, … an from r1, r2, … rm where P

CMU SCS Faloutsos, PavloCMU SCS /61514 select clause select [distinct | all ] name from student where address=“main”

CMU SCS Faloutsos, PavloCMU SCS /61515 where clause find ssn(s) of all “smith”s on “main” select ssn from student where address=“main” and name = “smith”

CMU SCS Faloutsos, PavloCMU SCS /61516 where clause boolean operators (and or not …) comparison operators (, =, …) and more…

CMU SCS Faloutsos, PavloCMU SCS /61517 What about strings? find student ssns who live on “main” (st or str or street - ie., “main st” or “main str” …)

CMU SCS Faloutsos, PavloCMU SCS /61518 What about strings? find student ssns who live on “main” (st or str or street) select ssn from student where address like “main%” %: variable-length don’t care _: single-character don’t care

CMU SCS Faloutsos, PavloCMU SCS /61519 from clause find names of people taking

CMU SCS Faloutsos, PavloCMU SCS /61520 from clause find names of people taking select name from student, takes where ???

CMU SCS Faloutsos, PavloCMU SCS /61521 from clause find names of people taking select name from student, takes where student.ssn = takes.ssn and takes.c-id = “15-415”

CMU SCS Faloutsos, PavloCMU SCS /61522 Overview - detailed - SQL DML –select, from, where, renaming –set operations –ordering –aggregate functions –nested subqueries other parts: DDL, embedded SQL, auth etc

CMU SCS Faloutsos, PavloCMU SCS /61523 renaming - tuple variables find names of people taking select name from ourVeryOwnStudent, studentTakingClasses where ourVeryOwnStudent.ssn = studentTakingClasses.ssn and studentTakingClasses.c-id = “15-415”

CMU SCS 2 reasons to rename: #1) shorthand #2) self-joins Faloutsos, PavloCMU SCS /61524

CMU SCS Faloutsos, PavloCMU SCS /61525 #1) renaming - tuple variables find names of people taking select name from ourVeryOwnStudent, studentTakingClasses where ourVeryOwnStudent.ssn =studentTakingClasses.ssn and studentTakingClasses.c-id = “15-415”

CMU SCS Faloutsos, PavloCMU SCS /61526 #1) renaming - tuple variables find names of people taking select name from ourVeryOwnStudent as S, studentTakingClasses as T where S.ssn =T.ssn and T.c-id = “15-415”

CMU SCS Faloutsos, PavloCMU SCS /61527 #2) renaming - self-join self -joins: find Tom’s grandparent(s)

CMU SCS Faloutsos, PavloCMU SCS /61528 #2) renaming - self-join find grandparents of “Tom” (PC(p-id, c-id)) select gp.p-id from PC as gp, PC where gp.c-id= PC.p-id and PC.c-id = “Tom”

CMU SCS Faloutsos, PavloCMU SCS /61529 #2) renaming - theta join find course names with more units than

CMU SCS Faloutsos, PavloCMU SCS /61530 #2) renaming - theta join find course names with more units than select c1.c-name from class as c1, class as c2 where c1.units > c2.units and c2.c-id = “15-415”

CMU SCS Faloutsos, PavloCMU SCS /61531 find course names with more units than select c1.name from class as c1, class as c2 where c1.units > c2.units and c2.c-id = “15-415”

CMU SCS Faloutsos, PavloCMU SCS /61532 find course names with more units than select c2.name from class as c1, class as c2 where c2.units > c1.units and c1.c-id = “15-415”

CMU SCS Faloutsos, PavloCMU SCS /61533 Overview - detailed - SQL DML –select, from, where –set operations –ordering –aggregate functions –nested subqueries other parts: DDL, embedded SQL, auth etc

CMU SCS Faloutsos, PavloCMU SCS /61534 set operations find ssn of people taking both and

CMU SCS Faloutsos, PavloCMU SCS /61535 set operations find ssn of people taking both and select ssn from takes where c-id=“15-415” and c-id=“15-413”

CMU SCS Faloutsos, PavloCMU SCS /61536 set operations find ssn of people taking both and (select ssn from takes where c-id=“15-415” ) intersect (select ssn from takes where c-id=“15-413” ) other ops: union, except

CMU SCS Faloutsos, PavloCMU SCS /61537 Overview - detailed - SQL DML –select, from, where –set operations –ordering –aggregate functions –nested subqueries other parts: DDL, embedded SQL, auth etc

CMU SCS Faloutsos, PavloCMU SCS /61538 Ordering find student records, sorted in name order select * from student where

CMU SCS Faloutsos, PavloCMU SCS /61539 Ordering find student records, sorted in name order select * from student order by name asc asc is the default

CMU SCS Faloutsos, PavloCMU SCS /61540 Ordering find student records, sorted in name order; break ties by reverse ssn select * from student order by name, ssn desc

CMU SCS Faloutsos, PavloCMU SCS /61541 Overview - detailed - SQL DML –select, from, where –set operations –ordering –aggregate functions –nested subqueries other parts: DDL, embedded SQL, auth etc

CMU SCS Faloutsos, PavloCMU SCS /61542 Aggregate functions find avg grade, across all students select ?? from takes

CMU SCS Faloutsos, PavloCMU SCS /61543 Aggregate functions find avg grade, across all students select avg(grade) from takes result: a single number Which other functions?

CMU SCS Faloutsos, PavloCMU SCS /61544 Aggregate functions A: sum count min max (std)

CMU SCS Faloutsos, PavloCMU SCS /61545 Aggregate functions find total number of enrollments select count(*) from takes

CMU SCS Faloutsos, PavloCMU SCS /61546 Aggregate functions find total number of students in select count(*) from takes where c-id=“15-415”

CMU SCS Faloutsos, PavloCMU SCS /61547 Aggregate functions find total number of students in each course select count(*) from takes where ???

CMU SCS Faloutsos, PavloCMU SCS /61548 Aggregate functions find total number of students in each course select c-id, count(*) from takes group by c-id

CMU SCS Faloutsos, PavloCMU SCS /61549 Aggregate functions find total number of students in each course select c-id, count(*) from takes group by c-id order by c-id

CMU SCS Faloutsos, PavloCMU SCS /61550 Aggregate functions find total number of students in each course, and sort by count, decreasing select c-id, count(*) as pop from takes group by c-id order by pop desc

CMU SCS Faloutsos, PavloCMU SCS /61551 Aggregate functions- ‘having’ find students with GPA > 3.0

CMU SCS Faloutsos, PavloCMU SCS /61552 Aggregate functions- ‘having’ find students with GPA > 3.0 select ???, avg(grade) from takes group by ???

CMU SCS Faloutsos, PavloCMU SCS /61553 Aggregate functions- ‘having’ find students with GPA > 3.0 select ssn, avg(grade) from takes group by ssn ???

CMU SCS Faloutsos, PavloCMU SCS /61554 Aggregate functions- ‘having’ find students with GPA > 3.0 select ssn, avg(grade) from takes group by ssn having avg(grade)>3.0 ‘having’ ‘where’ for groups

CMU SCS Faloutsos, PavloCMU SCS /61555 Aggregate functions- ‘having’ find students and GPA, for students with > 5 courses select ssn, avg(grade) from takes group by ssn having count(*) > 5

CMU SCS FaloutsosCMU SCS /61556 Overview - detailed - SQL DML –select, from, where, renaming –set operations –ordering –aggregate functions –nested subqueries other parts: DDL, embedded SQL, auth etc

CMU SCS FaloutsosCMU SCS /61557 DML General form select a1, a2, … an from r1, r2, … rm where P [order by ….] [group by …] [having …]

CMU SCS FaloutsosCMU SCS /61558 Reminder: our Mini-U db

CMU SCS FaloutsosCMU SCS /61559 DML - nested subqueries find names of students of select name from student where... “ssn in the set of people that take ”

CMU SCS FaloutsosCMU SCS /61560 DML - nested subqueries find names of students of select name from student where ………... select ssn from takes where c-id =“15-415”

CMU SCS FaloutsosCMU SCS /61561 DML - nested subqueries find names of students of select name from student where ssn in ( select ssn from takes where c-id =“15-415”)

CMU SCS FaloutsosCMU SCS /61562 DML - nested subqueries ‘in’ compares a value with a set of values ‘in’ can be combined other boolean ops it is redundant (but user friendly!): select name from student ….. where c-id = “15-415” ….

CMU SCS FaloutsosCMU SCS /61563 DML - nested subqueries ‘in’ compares a value with a set of values ‘in’ can be combined other boolean ops it is redundant (but user friendly!): select name from student, takes where c-id = “15-415” and student.ssn=takes.ssn

CMU SCS FaloutsosCMU SCS /61564 DML - nested subqueries find names of students taking and living on “main str” select name from student where address=“main str” and ssn in (select ssn from takes where c-id =“15-415”)

CMU SCS FaloutsosCMU SCS /61565 DML - nested subqueries ‘in’ compares a value with a set of values other operators like ‘in’ ??

CMU SCS FaloutsosCMU SCS /61566 DML - nested subqueries find student record with highest ssn select * from student where ssn is greater than every other ssn

CMU SCS FaloutsosCMU SCS /61567 DML - nested subqueries find student record with highest ssn select * from student where ssn greater than every select ssn from student

CMU SCS FaloutsosCMU SCS /61568 DML - nested subqueries find student record with highest ssn select * from student where ssn > all ( select ssn from student) almost correct

CMU SCS FaloutsosCMU SCS /61569 DML - nested subqueries find student record with highest ssn select * from student where ssn >= all ( select ssn from student)

CMU SCS FaloutsosCMU SCS /61570 DML - nested subqueries find student record with highest ssn - without nested subqueries? select S1.ssn, S1.name, S1.address from student as S1, student as S2 where S1.ssn > S2.ssn is not the answer (what does it give?) Puzzle

CMU SCS FaloutsosCMU SCS /61571 DML - nested subqueries S1 S2 S1 x S2 S1.ssn>S2.ssn Puzzle

CMU SCS FaloutsosCMU SCS /61572 DML - nested subqueries select S1.ssn, S1.name, S1.address from student as S1, student as S2 where S1.ssn > S2.ssn gives all but the smallest ssn - aha! Puzzle

CMU SCS FaloutsosCMU SCS /61573 DML - nested subqueries find student record with highest ssn - without nested subqueries? select S1.ssn, S1.name, S1.address from student as S1, student as S2 where S1.ssn < S2.ssn gives all but the highest - therefore…. Puzzle

CMU SCS FaloutsosCMU SCS /61574 DML - nested subqueries find student record with highest ssn - without nested subqueries? (select * from student) except (select S1.ssn, S1.name, S1.address from student as S1, student as S2 where S1.ssn < S2.ssn) Puzzle

CMU SCS FaloutsosCMU SCS /61575 DML - nested subqueries (select * from student) except (select S1.ssn, S1.name, S1.address from student as S1, student as S2 where S1.ssn < S2.ssn) select * from student where ssn >= all (select ssn from student) Puzzle

CMU SCS FaloutsosCMU SCS /61576 DML - nested subqueries Drill: Even more readable than select * from student where ssn >= all (select ssn from student)

CMU SCS FaloutsosCMU SCS /61577 DML - nested subqueries Drill: Even more readable than select * from student where ssn >= all (select ssn from student) select * from student where ssn in (select max(ssn) from student)

CMU SCS FaloutsosCMU SCS /61578 DML - nested subqueries Drill: find the ssn of the student with the highest GPA

CMU SCS FaloutsosCMU SCS /61579 DML - nested subqueries Drill: find the ssn and GPA of the student with the highest GPA select ssn, avg(grade) from takes where

CMU SCS FaloutsosCMU SCS /61580 DML - nested subqueries Drill: find the ssn and GPA of the student with the highest GPA select ssn, avg(grade) from takes group by ssn having avg( grade) …... greater than every other GPA on file

CMU SCS FaloutsosCMU SCS /61581 DML - nested subqueries Drill: find the ssn and GPA of the student with the highest GPA select ssn, avg(grade) from takes group by ssn having avg( grade) >= all ( select avg( grade ) from student group by ssn ) } all GPAs

CMU SCS FaloutsosCMU SCS /61582 DML - nested subqueries ‘in’ and ‘>= all’ compares a value with a set of values other operators like these?

CMU SCS FaloutsosCMU SCS /61583 DML - nested subqueries all()... ‘<>all’ is identical to ‘not in’ >some(), >= some ()... ‘= some()’ is identical to ‘in’ exists

CMU SCS FaloutsosCMU SCS /61584 DML - nested subqueries Drill for ‘exists’: find all courses that nobody enrolled in select c-id from class …. with no tuples in ‘takes’

CMU SCS FaloutsosCMU SCS /61585 DML - nested subqueries Drill for ‘exists’: find all courses that nobody enrolled in select c-id from class where not exists (select * from takes where class.c-id = takes.c-id)

CMU SCS FaloutsosCMU SCS /61586 DML - derived relations find the ssn with the highest GPA select ssn, avg(grade) from takes group by ssn having avg( grade) >= all ( select avg( grade ) from takes group by ssn )

CMU SCS FaloutsosCMU SCS /61587 DML - derived relations find the ssn with the highest GPA Query would be easier, if we had a table like: helpfulTable (ssn, gpa): then what?

CMU SCS FaloutsosCMU SCS /61588 DML - derived relations select ssn, gpa from helpfulTable where gpa in (select max(gpa) from helpfulTable)

CMU SCS FaloutsosCMU SCS /61589 DML - derived relations find the ssn with the highest GPA - Query for helpfulTable (ssn, gpa)?

CMU SCS FaloutsosCMU SCS /61590 DML - derived relations find the ssn with the highest GPA Query for helpfulTable (ssn, gpa)? select ssn, avg(grade) from takes group by ssn

CMU SCS FaloutsosCMU SCS /61591 DML - derived relations find the ssn with the highest GPA helpfulTable(ssn,gpa) select ssn, avg(grade) from takes group by ssn select ssn, gpa from helpfulTable where gpa = (select max(gpa) from helpfulTable)

CMU SCS FaloutsosCMU SCS /61592 DML - derived relations find the ssn with the highest GPA select ssn, gpa from (select ssn, avg(grade) from takes group by ssn) as helpfulTable(ssn, gpa) where gpa in (select max(gpa) from helpfulTable)

CMU SCS FaloutsosCMU SCS /61593 Overview - detailed - SQL DML –select, from, where, renaming –set operations –ordering –aggregate functions –nested subqueries other parts: DDL, embedded SQL, auth etc ✔ ✔ ✔ ✔ ✔

CMU SCS FaloutsosCMU SCS /61594 Next lecture: DML –select, from, where, renaming –set operations –ordering –aggregate functions –nested subqueries other parts: DDL, embedded SQL, auth etc ✔ ✔ ✔ ✔ ✔