Lecture#7: Fun with SQL (Part 2)

Slides:



Advertisements
Similar presentations
SQL: The Query Language Part 2
Advertisements

Copyright © 2011 Pearson Education, Inc. Publishing as Pearson Addison-Wesley Chapter 5 More SQL: Complex Queries, Triggers, Views, and Schema Modification.
TURKISH STATISTICAL INSTITUTE 1 /34 SQL FUNDEMANTALS (Muscat, Oman)
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.
CMU SCS Carnegie Mellon Univ. Dept. of Computer Science /615 - DB Applications C. Faloutsos – A. Pavlo Lecture#6: Fun with SQL (part2)
CS 405G: Introduction to Database Systems
M ATH IN SQL. 222 A GGREGATION O PERATORS Operators on sets of tuples. Significant extension of relational algebra. SUM ( [DISTINCT] A): the sum of all.
Copyright © 2011 Pearson Education, Inc. Publishing as Pearson Addison-Wesley Chapter 5 More SQL: Complex Queries, Triggers, Views, and Schema Modification.
Introduction to Relational Databases Obtained from Portland State University.
CMU SCS Carnegie Mellon Univ. Dept. of Computer Science Database Applications C. Faloutsos Lecture#7 (cont’d): Rel. model - SQL part3.
SQL 2 – The Sequel R&G, Chapter 5 Lecture 10. Administrivia Homework 2 assignment now available –Due a week from Sunday Midterm exam will be evening of.
1 Query Languages: How to build or interrogate a relational database Structured Query Language (SQL)
Database Management Systems 3ed, R. Ramakrishnan and J. Gehrke1 SQL: Queries, Constraints, Triggers Chapter 5.
Relational Operators, SQL, and Access Query ISYS 562.
Carnegie Mellon Carnegie Mellon Univ. Dept. of Computer Science Database Applications C. Faloutsos Rel. model - SQL part1.
The Relational Model. Review Why use a DBMS? OS provides RAM and disk.
Oracle Database Administration Lecture 2 SQL language.
1 ICS 184: Introduction to Data Management Lecture Note 10 SQL as a Query Language (Cont.)
1 TAC2000/ Protocol Engineering and Application Research Laboratory (PEARL) Structured Query Language Introduction to SQL Structured Query Language.
Instructor: Jinze Liu Fall Basic Components (2) Relational Database Web-Interface Done before mid-term Must-Have Components (2) Security: access.
Introduction to Relational Databases
CS146 References: ORACLE 9i PROGRAMMING A Primer Rajshekhar Sunderraman
MIS 3053 Database Design & Applications The University of Tulsa Professor: Akhilesh Bajaj RM/SQL Lecture 5 © Akhilesh Bajaj, 2000, 2002, 2003, All.
Database Management COP4540, SCS, FIU Structured Query Language (Chapter 8)
Database Fundamental & Design by A.Surasit Samaisut Copyrights : All Rights Reserved.
AL-MAAREFA COLLEGE FOR SCIENCE AND TECHNOLOGY INFO 232: DATABASE SYSTEMS CHAPTER 7 (Part II) INTRODUCTION TO STRUCTURED QUERY LANGUAGE (SQL) Instructor.
CS 405G: Introduction to Database Systems Instructor: Jinze Liu Fall 2009.
CMPT 258 Database Systems The Relationship Model (Chapter 3)
CMU SCS Carnegie Mellon Univ. Dept. of Computer Science /615 - DB Applications C. Faloutsos – A. Pavlo Lecture#7: Fun with SQL (Part 2)
Database Management Systems 3ed, R. Ramakrishnan and J. Gehrke1 Database Management Systems Chapter 5 SQL.
CS 405G: Introduction to Database Systems Instructor: Jinze Liu.
CS 405G: Introduction to Database Systems Instructor: Jinze Liu Fall 2007.
7 1 Database Systems: Design, Implementation, & Management, 7 th Edition, Rob & Coronel 7.6 Advanced Select Queries SQL provides useful functions that.
SQL: Interactive Queries (2) Prof. Weining Zhang Cs.utsa.edu.
SQL SQL Ayshah I. Almugahwi Maryam J. Alkhalifa
More SQL: Complex Queries, Triggers, Views, and Schema Modification
Structured Query Language (SQL) DML
More SQL: Complex Queries,
SQL – Part 2.
Lecture#6: Fun with SQL (Part 1)
COP Introduction to Database Structures
01/31/11 SQL Examples Edited by John Shieh CS3754 Classnote #10.
SQL The Query Language R & G - Chapter 5
Concept of Aggregation in SQL
SQL: Advanced Options, Updates and Views Lecturer: Dr Pavle Mogin
SQL: Structured Query Language DML- Queries Lecturer: Dr Pavle Mogin
Introduction to Relational Databases
CS 405G: Introduction to Database Systems
CS 405G: Introduction to Database Systems
Database Applications (15-415) SQL-Part II Lecture 9, February 04, 2018 Mohammad Hammoud.
CS 405G: Introduction to Database Systems
Chapter # 7 Introduction to Structured Query Language (SQL) Part II.
CSC 453 Database Systems Lecture
Faloutsos/Pavlo C. Faloutsos – A. Pavlo Lecture#13: Query Evaluation
More SQL: Complex Queries, Triggers, Views, and Schema Modification
Agenda SQL Queries Join. MGS 4020 Business Intelligence Relational Algebra and Structured Query Language Jul 3, 2018.
CMPT 354: Database System I
CS4222 Principles of Database System
CMPT 354: Database System I
SQL: Structured Query Language
Contents Preface I Introduction Lesson Objectives I-2
CSC 453 Database Systems Lecture
Views 1.
SQL: Structured Query Language
SQL: Structured Query Language
CSC 453 Database Systems Lecture
CSC 453 Database Systems Lecture
Introduction to SQL Server and the Structure Query Language
Intermediate Query Structure and Development
Recap – Relational languages
Presentation transcript:

Lecture#7: Fun with SQL (Part 2) Faloutsos/Pavlo CMU 15-415/615 Carnegie Mellon Univ. Dept. of Computer Science 15-415/615 - DB Applications C. Faloutsos – A. Pavlo Lecture#7: Fun with SQL (Part 2)

Last Class Basic Queries Table Definition (DDL) NULLs Faloutsos/Pavlo CMU - 15-415/615 Last Class Basic Queries Table Definition (DDL) NULLs String/Date/Time/Set/Bag Operations Output Redirection/Control Faloutsos/Pavlo CMU SCS 15-415/615

Today's Class: OLAP Views Joins Aggregations + Group By Nested Queries Faloutsos/Pavlo CMU - 15-415/615 Today's Class: OLAP Views Joins Aggregations + Group By Nested Queries Window Functions Common Table Expressions Faloutsos/Pavlo CMU SCS 15-415/615

Example Database STUDENT ENROLLED COURSE sid name login age gpa 53666 Faloutsos/Pavlo CMU - 15-415/615 Example Database STUDENT ENROLLED sid name login age gpa 53666 Kayne kayne@cs 39 4.0 53688 Bieber jbieber@cs 22 3.9 53655 Tupac shakur@cs 26 3.5 sid cid grade 53666 15-415 C 53688 15-721 A 15-826 B 53655 COURSE cid name 15-415 Database Applications 15-721 Database Systems 15-826 Data Mining 15-823 Advanced Topics in Databases Faloutsos/Pavlo CMU SCS 15-415/615

Views Creates a “virtual” table containing the output from a SELECT query. Mechanism for hiding data from view of certain users. Can be used to simplify a complex query that is executed often. Won’t make it faster though! Faloutsos/Pavlo CMU SCS 15-415/615

View Example Create a view of the CS student records with just their id, name, and login. CREATE VIEW CompSciStudentInfo AS SELECT sid, name, login FROM student WHERE login LIKE ‘%@cs’; sid name login age gpa 53666 Kayne kayne@cs 45 4.0 53688 Bieber jbieber@cs 21 3.9 Original Table sid name login 53666 Kayne kayne@cs 53688 Bieber jbieber@cs View

View Example Create a view with the average age of the students enrolled in each course. CREATE VIEW CourseAge AS SELECT cid, AVG(age) AS avg_age FROM student, enrolled WHERE student.sid = enrolled.sid GROUP BY enrolled.cid; cid avg_age 15-415 45.0 15-721 15-826 21.0 Faloutsos/Pavlo

Views vs. SELECT INTO CREATE VIEW AvgGPA AS SELECT AVG(gpa) AS avg_gpa FROM student WHERE login LIKE ‘%@cs’ SELECT AVG(gpa) AS avg_gpa INTO AvgGPA FROM student WHERE login LIKE ‘%@cs’ INTO→Creates static table that does not get updated when student gets updated. VIEW→Dynamic results are only materialized when needed.

Materialized Views Creates a view containing the output from a SELECT query that is automatically updated when the underlying tables change. CREATE MATERIALIZED VIEW AvgGPA AS SELECT AVG(gpa) AS avg_gpa FROM student WHERE login LIKE ‘%@cs’ Faloutsos/Pavlo CMU SCS 15-415/615

Today's Class: OLAP Views Joins Aggregations + Group By Nested Queries Faloutsos/Pavlo CMU - 15-415/615 Today's Class: OLAP Views Joins Aggregations + Group By Nested Queries Window Functions Common Table Expressions Faloutsos/Pavlo CMU SCS 15-415/615

Join Query Grammar Join-Type: The type of join to compute. Faloutsos/Pavlo CMU - 15-415/615 Join Query Grammar SELECT ... FROM table-name1 join-type table-name2 ON qualification [WHERE ...] Join-Type: The type of join to compute. Qualification: Expression that determines whether a tuple from table1 can be joined with table2. Comparison of attributes or constants using operators =, ≠, <, >, ≤, and ≥. Faloutsos/Pavlo CMU SCS 15-415/615

INNER JOIN SELECT name, cid, grade FROM student, enrolled sid cid grade 53666 15-415 C 53688 15-721 A 15-826 B 53655 sid name login age gpa 53666 Kayne kayne@cs 39 4.0 53688 Bieber jbieber@cs 22 3.9 53655 Tupac shakur@cs 26 3.5 53800 Drake drake@cs 29 3.7 SELECT name, cid, grade FROM student, enrolled WHERE student.sid = enrolled.sid SELECT name, cid, grade FROM student INNER JOIN enrolled ON student.sid = enrolled.sid SELECT name, cid, grade FROM student NATURAL JOIN enrolled

OUTER JOIN SELECT name, cid, grade sid cid grade 53666 15-415 C 53688 15-721 A 15-826 B 53655 sid name login age gpa 53666 Kayne kayne@cs 39 4.0 53688 Bieber jbieber@cs 22 3.9 53655 Tupac shakur@cs 26 3.5 53800 Drake drake@cs 29 3.7 SELECT name, cid, grade FROM student LEFT OUTER JOIN enrolled ON student.sid = enrolled.sid name cid grade Kayne 15-415 C Bieber 15-721 A 15-826 B Tupac Drake NULL SELECT name, cid, grade FROM student NATURAL JOIN enrolled

OUTER JOIN SELECT name, cid, grade sid cid grade 53666 15-415 C 53688 15-721 A 15-826 B 53655 sid name login age gpa 53666 Kayne kayne@cs 39 4.0 53688 Bieber jbieber@cs 22 3.9 53655 Tupac shakur@cs 26 3.5 53800 Drake drake@cs 29 3.7 SELECT name, cid, grade FROM enrolled RIGHT OUTER JOIN student ON student.sid = enrolled.sid name cid grade Kayne 15-415 C Bieber 15-721 A 15-826 B Tupac Drake NULL SELECT name, cid, grade FROM student NATURAL JOIN enrolled

Join Types SELECT * FROM A JOIN B ON A.id = B.id Join Type Description Faloutsos/Pavlo CMU - 15-415/615 Join Types SELECT * FROM A JOIN B ON A.id = B.id Join Type Description INNER JOIN Join where A and B have same value LEFT OUTER JOIN Join where A and B have same value AND where only A has a value RIGHT OUTER JOIN Join where A and B have same value AND where only B has a value FULL OUTER JOIN Join where A and B have same value AND where A or B have unique values CROSS JOIN Cartesian Product Faloutsos/Pavlo CMU SCS 15-415/615

Today's Class: OLAP Views Joins Aggregations + Group By Nested Queries Faloutsos/Pavlo CMU - 15-415/615 Today's Class: OLAP Views Joins Aggregations + Group By Nested Queries Window Functions Common Table Expressions Faloutsos/Pavlo CMU SCS 15-415/615

Aggregates Functions that return a single value from a bag of tuples: Faloutsos/Pavlo CMU - 15-415/615 Aggregates Functions that return a single value from a bag of tuples: AVG(col)→ Return the average col value. MIN(col)→ Return minimum col value. MAX(col)→ Return maximum col value. SUM(col)→ Return sum of values in col. COUNT(col) → Return # of values for col. CMU SCS 15-415/615

Faloutsos/Pavlo CMU - 15-415/615 Aggregates Functions can only be used in the SELECT attribute output list. Get the # of students with a “@cs” login: cnt 12 SELECT COUNT(login) AS cnt FROM student WHERE login LIKE ‘%@cs’ SELECT COUNT(*) AS cnt FROM student WHERE login LIKE ‘%@cs’ CMU SCS 15-415/615

Aggregates Can use multiple functions together at the same time. Faloutsos/Pavlo CMU - 15-415/615 Aggregates Can use multiple functions together at the same time. Get the number of students and their average GPA that have a “@cs” login. AVG(gpa) COUNT(sid) 3.25 12 SELECT AVG(gpa), COUNT(sid) FROM student WHERE login LIKE ‘%@cs’ CMU SCS 15-415/615

Aggregates COUNT, SUM, AVG support DISTINCT Faloutsos/Pavlo CMU - 15-415/615 Aggregates COUNT, SUM, AVG support DISTINCT Get the number of unique students that have an “@cs” login. COUNT(DISTINCT login) 10 SELECT COUNT(DISTINCT login) FROM student WHERE login LIKE ‘%@cs’ CMU SCS 15-415/615

Faloutsos/Pavlo CMU - 15-415/615 Aggregates Output of other columns outside of an aggregate is undefined: Unless… AVG(s.gpa) e.cid 3.5 ??? SELECT AVG(s.gpa), e.cid FROM enrolled AS e, student AS s WHERE e.sid = s.sid CMU SCS 15-415/615

Faloutsos/Pavlo CMU - 15-415/615 GROUP BY Project tuples into subsets and calc aggregates against each subset. SELECT AVG(s.gpa), e.cid FROM enrolled AS e, student AS s WHERE e.sid = s.sid GROUP BY e.cid e.sid s.sid s.gpa e.cid 53435 2.25 15-721 53439 2.70 56023 2.75 15-826 59439 3.90 53961 3.50 58345 1.89 15-415 AVG(s.gpa) e.cid 2.46 15-721 3.39 15-826 1.89 15-415

Faloutsos/Pavlo CMU - 15-415/615 GROUP BY Non-aggregated values in SELECT output clause must appear in GROUP BY clause. X SELECT AVG(s.gpa), e.cid, s.name FROM enrolled AS e, student AS s WHERE e.sid = s.sid GROUP BY e.cid CMU SCS 15-415/615

Faloutsos/Pavlo CMU - 15-415/615 GROUP BY Non-aggregated values in SELECT output clause must appear in GROUP BY clause. ✔ SELECT AVG(s.gpa), e.cid, s.name FROM enrolled AS e, student AS s WHERE e.sid = s.sid GROUP BY e.cid, s.name CMU SCS 15-415/615

HAVING Filters output results Like a WHERE clause for a GROUP BY Faloutsos/Pavlo CMU - 15-415/615 HAVING Filters output results Like a WHERE clause for a GROUP BY SELECT AVG(s.gpa) AS avg_gpa, e.cid FROM enrolled AS e, student AS s WHERE e.sid = s.sid GROUP BY e.cid HAVING avg_gpa > 2.75; AVG(s.gpa) e.cid 2.46 15-721 3.39 15-826 1.89 15-415 avg_gpa e.cid 3.39 15-826

Today's Class: OLAP Views Joins Aggregations + Group By Nested Queries Faloutsos/Pavlo CMU - 15-415/615 Today's Class: OLAP Views Joins Aggregations + Group By Nested Queries Window Functions Common Table Expressions Faloutsos/Pavlo CMU SCS 15-415/615

Nested Queries Queries containing other queries Inner query: Can appear in FROM or WHERE clause “outer query” “inner query” SELECT name FROM student WHERE sid IN (SELECT sid FROM enrolled) Think of this as a function that returns the result of the inner query

“sid in the set of people that take 15-415” Nested Queries Find the names of students in ‘15-415’ SELECT name FROM student WHERE ... “sid in the set of people that take 15-415” Faloutsos/Pavlo CMU SCS 15-415/615

Nested Queries Find the names of students in ‘15-415’ SELECT name FROM student WHERE ... SELECT sid FROM enrolled WHERE cid = ‘15-415’ Faloutsos/Pavlo CMU SCS 15-415/615

Nested Queries Find the names of students in ‘15-415’ SELECT name FROM student WHERE sid IN ( SELECT sid FROM enrolled WHERE cid = ‘15-415’ ) Faloutsos/Pavlo CMU SCS 15-415/615

Nested Queries ALL →Must satisfy expression for all rows in sub-query ANY →Must satisfy expression for at least one row in sub-query. IN → Equivalent to ‘=ANY()’. EXISTS → At least one row is returned. Nested queries are difficult to optimize. Try to avoid them if possible. Faloutsos/Pavlo CMU SCS 15-415/615

Nested Queries Find the names of students in ‘15-415’ SELECT name FROM student WHERE sid = ANY( SELECT sid FROM enrolled WHERE cid = ‘15-415’ ) Faloutsos/Pavlo CMU SCS 15-415/615

X Nested Queries Find student record with the highest id. This won’t work in SQL-92: Runs in MySQL, but you get wrong answer: X SELECT MAX(sid), name FROM student; sid name 53688 Tupac Faloutsos/Pavlo CMU SCS 15-415/615

“is greater than every other sid” Nested Queries Find student record with the highest id. SELECT sid, name FROM student WHERE ... “is greater than every other sid” Faloutsos/Pavlo CMU SCS 15-415/615

Nested Queries Find student record with the highest id. SELECT sid, name FROM student WHERE sid SELECT sid FROM enrolled is greater than every Faloutsos/Pavlo CMU SCS 15-415/615

Nested Queries Find student record with the highest id. SELECT sid, name FROM student WHERE sid => ALL( SELECT sid FROM enrolled ) sid name 53688 Bieber Faloutsos/Pavlo CMU SCS 15-415/615

Nested Queries Find student record with the highest id. SELECT sid, name FROM student WHERE sid IN ( SELECT MAX(sid) FROM enrolled ) SELECT sid, name FROM student WHERE sid IN ( SELECT sid FROM enrolled ORDER BY sid DESC LIMIT 1 )

“with no tuples in the ‘enrolled’ table” Nested Queries Find all courses that nobody is enrolled in. SELECT * FROM course WHERE ... “with no tuples in the ‘enrolled’ table” sid cid grade 53666 15-415 C 53688 15-721 A 15-826 B 53655 cid name 15-415 Database Applications 15-721 Database Systems 15-826 Data Mining 15-823 Advanced Topics in Databases Faloutsos/Pavlo CMU SCS 15-415/615

tuples in the ‘enrolled’ table Nested Queries Find all courses that nobody is enrolled in. SELECT * FROM course WHERE NOT EXISTS( ) tuples in the ‘enrolled’ table Faloutsos/Pavlo CMU SCS 15-415/615

Nested Queries Find all courses that nobody is enrolled in. SELECT * FROM course WHERE NOT EXISTS( SELECT * FROM enrolled WHERE course.cid = enrolled.cid ) cid name 15-823 Advanced Topics in Databases Faloutsos/Pavlo CMU SCS 15-415/615

Today's Class: OLAP Views Joins Aggregations + Group By Nested Queries Faloutsos/Pavlo CMU - 15-415/615 Today's Class: OLAP Views Joins Aggregations + Group By Nested Queries Window Functions Common Table Expressions Faloutsos/Pavlo CMU SCS 15-415/615

Aggregation Functions Special Functions Window Functions Performs a calculation across a set of tuples that related to a single row. Like an aggregation but tuples are not grouped into a single output tuples. SELECT ... FUNC-NAME(...) OVER (...) FROM tableName Aggregation Functions Special Functions How to “slice” up data Can also sort Faloutsos/Pavlo CMU SCS 15-415/615

Window Functions Aggregation functions: Special window functions: Anything that we discussed earlier Special window functions: ROW_NUMBER()→ Number of the current row RANK()→ Order position of the current row. SELECT *, ROW_NUMBER() OVER () FROM enrolled Faloutsos/Pavlo CMU SCS 15-415/615

Window Function The OVER keyword specifies how to group together tuples when computing the window function. Use PARTITION BY to specify group. SELECT *, ROW_NUMBER() OVER (PARTITION BY cid) FROM enrolled ORDER BY cid Faloutsos/Pavlo CMU SCS 15-415/615

Window Function You can also include an ORDER BY in the window grouping. SELECT *, ROW_NUMBER() OVER (ORDER BY cid) FROM enrolled ORDER BY cid Faloutsos/Pavlo CMU SCS 15-415/615

Window Functions Find the student with the highest grade for each course. SELECT *, RANK() OVER (PARTITION BY cid ORDER BY grade ASC) FROM enrolled Faloutsos/Pavlo CMU SCS 15-415/615

Faloutsos/Pavlo CMU - 15-415/615 Window Functions Get the name of the student with the second highest grade for each course. SELECT * FROM ( ) AS R WHERE R.grade_rank = 2; SELECT C.name, S.name, E.grade, RANK() OVER (PARTITION BY E.cid ORDER BY E.grade ASC ) AS grade_rank FROM student S, course C, enrolled E WHERE S.sid = E.sid AND C.cid = E.cid SELECT * FROM ( SELECT C.name, S.name, E.grade, RANK() OVER (PARTITION BY E.cid ORDER BY E.grade ASC) AS grade_rank FROM student S, course C, enrolled E WHERE S.sid = E.sid AND C.cid = E.cid ) AS R WHERE R.grade_rank = 2; Faloutsos/Pavlo CMU SCS 15-415/615

Today's Class: OLAP Views Joins Aggregations + Group By Nested Queries Faloutsos/Pavlo CMU - 15-415/615 Today's Class: OLAP Views Joins Aggregations + Group By Nested Queries Window Functions Common Table Expressions Faloutsos/Pavlo CMU SCS 15-415/615

Common Table Expressions Provides a way to write auxiliary statements for use in a larger query. Think of it like a temp table just for one query. Alternative to nested queries and views. WITH cteName AS ( SELECT 1 ) SELECT * FROM cteName Faloutsos/Pavlo CMU SCS 15-415/615

Common Table Expressions You can bind output columns to names before the AS keyword. WITH cteName (col1, col2) AS ( SELECT 1, 2 ) SELECT col1 + col2 FROM cteName Faloutsos/Pavlo CMU SCS 15-415/615

Common Table Expressions Faloutsos/Pavlo CMU - 15-415/615 Common Table Expressions Find student record with the highest id that is enrolled in at least one course. SELECT name FROM student WHERE sid = ANY( SELECT sid FROM enrolled WHERE cid = ‘15-415’ ) WITH cteSource (maxId) AS ( SELECT MAX(sid) FROM enrolled ) SELECT name FROM student, cteSource WHERE student.sid = cteSource.maxId DROP TABLE IF EXISTS student; CREATE TABLE student ( sid INT PRIMARY KEY, name VARCHAR(16), login VARCHAR(32) UNIQUE, age SMALLINT CHECK (age > 0), gpa FLOAT ); DROP TABLE IF EXISTS course; CREATE TABLE course ( cid VARCHAR(32) NOT NULL PRIMARY KEY, name VARCHAR(32) NOT NULL DROP TABLE IF EXISTS enrolled; CREATE TABLE enrolled ( sid INT REFERENCES student (sid), cid VARCHAR(32) NOT NULL REFERENCES course (cid), grade CHAR(1) INSERT INTO student VALUES (53666, 'Kayne', 'kayne@cs', 39, 4.0); INSERT INTO student VALUES (53688, 'Bieber', 'jbiever@cs', 22, 3.9); INSERT INTO student VALUES (53655, 'Tupac', 'shakur@cs', 26, 3.5); INSERT INTO course VALUES ('15-415', 'Database Applications'); INSERT INTO course VALUES ('15-721', 'Database Systems'); INSERT INTO course VALUES ('15-826', 'Data Mining'); INSERT INTO course VALUES ('15-823', 'Advanced Topics in Databases'); INSERT INTO enrolled VALUES (53666, '15-415', 'C'); INSERT INTO enrolled VALUES (53688, '15-721', 'A'); INSERT INTO enrolled VALUES (53688, '15-826', 'B'); INSERT INTO enrolled VALUES (53655, '15-415', ‘B'); INSERT INTO enrolled VALUES (53666, '15-721', 'C'); Faloutsos/Pavlo CMU SCS 15-415/615

CTEs – Recursion Print 1 to 10. Postgres CTE Demo! Faloutsos/Pavlo CMU - 15-415/615 CTEs – Recursion Print 1 to 10. Postgres CTE Demo! WITH RECURSIVE cteSource (counter) AS ( (SELECT 1) UNION ALL (SELECT counter + 1 FROM cteSource WHERE counter < 10) ) SELECT * FROM cteSource -- Postgres -- Infinite loop with recursion SET statement_timeout = ‘10s’; WITH RECURSIVE source AS ( (SELECT 1) UNION ALL (SELECT 1 FROM source) ) SELECT * FROM source; -- For Loop WITH RECURSIVE source (counter) AS ( (SELECT 1) UNION ALL (SELECT counter + 1 FROM source WHERE counter < 10) ) SELECT * FROM source; Faloutsos/Pavlo CMU SCS 15-415/615

Next Class We begin discussing storage internals. This material will be important for helping you pick up dates at parties. Faloutsos/Pavlo CMU SCS 15-415/615