M.P. Johnson, DBMS, Stern/NYU, Spring 20061 C20.0046: Database Management Systems Lecture #13 M.P. Johnson Stern School of Business, NYU Spring, 2005.

Slides:



Advertisements
Similar presentations
พีชคณิตแบบสัมพันธ์ (Relational Algebra) บทที่ 3 อ. ดร. ชุรี เตชะวุฒิ CS (204)321 ระบบฐานข้อมูล 1 (Database System I)
Advertisements

Database Management Systems 3ed, R. Ramakrishnan and J. Gehrke1 SQL: Queries, Programming, Triggers Chapter 5 Modified by Donghui Zhang.
COMP 3715 Spring 05. Working with data in a DBMS Any database system must allow user to  Define data Relations Attributes Constraints  Manipulate data.
SQL Subqueries Objectives of the Lecture : To consider the general nature of subqueries. To consider simple versus correlated subqueries. To consider the.
CS411 Database Systems Kazuhiro Minami 06: SQL. Join Expressions.
Copyright © 2011 Pearson Education, Inc. Publishing as Pearson Addison-Wesley Chapter 5 More SQL: Complex Queries, Triggers, Views, and Schema Modification.
Copyright  Oracle Corporation, All rights reserved. 6 Writing Correlated Subqueries.
Subqueries Example Find the name of the producer of ‘Star Wars’.
Instructor: Craig Duckett CASE, ORDER BY, GROUP BY, HAVING, Subqueries
M.P. Johnson, DBMS, Stern/NYU, Spring C : Database Management Systems Lecture #9 M.P. Johnson Stern School of Business, NYU Spring, 2008.
M.P. Johnson, DBMS, Stern/NYU, Spring C : Database Management Systems Lecture #16 M.P. Johnson Stern School of Business, NYU Spring, 2005.
Structured Query Language – Continued Rose-Hulman Institute of Technology Curt Clifton.
M.P. Johnson, DBMS, Stern/NYU, Spring C : Database Management Systems Lecture #11 M.P. Johnson Stern School of Business, NYU Spring, 2008.
M.P. Johnson, DBMS, Stern/NYU, Spring C : Database Management Systems Lecture #14 M.P. Johnson Stern School of Business, NYU Spring, 2005.
Sub Queries Pertemuan 5 Matakuliah: T0413/Current Popular IT II Tahun: 2007.
Nov 24, 2003Murali Mani SQL B term 2004: lecture 12.
1 Lecture 03: SQL Friday, January 7, Administrivia Have you logged in IISQLSRV yet ? HAVE YOU CHANGED YOUR PASSWORD ? Homework 1 is now posted.
M.P. Johnson, DBMS, Stern/NYU, Spring C : Database Management Systems Lecture #12 M.P. Johnson Stern School of Business, NYU Spring, 2005.
M.P. Johnson, DBMS, Stern/NYU, Spring C : Database Management Systems Lecture #10 M.P. Johnson Stern School of Business, NYU Spring, 2008.
M.P. Johnson, DBMS, Stern/NYU, Spring C : Database Management Systems Lecture #13 M.P. Johnson Stern School of Business, NYU Spring, 2005.
Joins Natural join is obtained by: R NATURAL JOIN S; Example SELECT * FROM MovieStar NATURAL JOIN MovieExec; Theta join is obtained by: R JOIN S ON Example.
Matthew P. Johnson, OCL4, CISDD CUNY, Sept OCL4 Oracle 10g: SQL & PL/SQL Session #5 Matthew P. Johnson CISDD, CUNY June, 2005.
M.P. Johnson, DBMS, Stern/NYU, Spring C : Database Management Systems Lecture #15 M.P. Johnson Stern School of Business, NYU Spring, 2005.
M.P. Johnson, DBMS, Stern/NYU, Spring C : Database Management Systems Lecture #11 M.P. Johnson Stern School of Business, NYU Spring, 2005.
M.P. Johnson, DBMS, Stern/NYU, Sp20041 C : Database Management Systems Lecture #14 Matthew P. Johnson Stern School of Business, NYU Spring, 2004.
1 Advanced SQL. 2 Consider the following relations: –pupil (pupil_name, address, class, birthyear) –subject (subject_name, class, teacher) –grades (pupil_name,
A Guide to SQL, Seventh Edition. Objectives Retrieve data from a database using SQL commands Use compound conditions Use computed columns Use the SQL.
SQL (almost end) April 26 th, Agenda HAVING clause Views Modifying views Reusing views.
Using Relational Databases and SQL Department of Computer Science California State University, Los Angeles Lecture 8: Subqueries.
1 SQL cont.. 2 Outline Unions, intersections, differences (6.2.5, 6.4.2) Subqueries (6.3) Aggregations (6.4.3 – 6.4.6) Hint for reading the textbook:
Relational Query Languages. Languages of DBMS  Data Definition Language DDL  define the schema and storage stored in a Data Dictionary  Data Manipulation.
Ashwani Roy Understanding Graphical Execution Plans Level 200.
1 ICS 184: Introduction to Data Management Lecture Note 10 SQL as a Query Language (Cont.)
1 CS 430 Database Theory Winter 2005 Lecture 12: SQL DML - SELECT.
CSC 411/511: DBMS Design Dr. Nan WangCSC411_L6_SQL(1) 1 SQL: Queries, Constraints, Triggers Chapter 5 – Part 1.
1 Agenda – 03/25/2014 Login to SQL Server 2012 Management Studio. Answer questions about HW#7 – display answers. Exam is 4/1/2014. It will be in the lab.
CSC271 Database Systems Lecture # 12. Summary: Previous Lecture  Row selection using WHERE clause  WHERE clause and search conditions  Sorting results.
1 Theory, Practice & Methodology of Relational Database Design and Programming Copyright © Ellis Cohen Basic Relational Algebra These slides.
Unit 4 Queries and Joins. Key Concepts Using the SELECT statement Statement clauses Subqueries Multiple table statements Using table pseudonyms Inner.
Subqueries Steve Perry 1.
NULL VALUES CHAPTER 5 (6/E) CHAPTER 8 (5/E) 1. LECTURE OUTLINE  Dealing with null values Three-valued logic Effects in WHERE clauses IS NULL Effects.
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.
CS 405G: Introduction to Database Systems Instructor: Jinze Liu Fall 2009.
CMPT 258 Database Systems SQL Queries (Chapter 5).
SqlExam1Review.ppt EXAM - 1. SQL stands for -- Structured Query Language Putting a manual database on a computer ensures? Data is more current Data is.
Copyright © 2011 Pearson Education, Inc. Publishing as Pearson Addison-Wesley Chapter 6 The Relational Algebra and Relational Calculus.
SQL: Sub-queries Single-value sub-queries Single-column sub-queries Sub-queries that produce tables Correlated sub-queries D. Christozov / G.Tuparov INF.
1 Theory, Practice & Methodology of Relational Database Design and Programming Copyright © Ellis Cohen Relational State Assertions These slides.
1 SY306 Web and Databases for Cyber Operations Set #13: SQL SELECT Grouping and sub-queries.
1 Theory, Practice & Methodology of Relational Database Design and Programming Copyright © Ellis Cohen Grouping These slides are licensed under.
A Guide to SQL, Eighth Edition Chapter Four Single-Table Queries.
1 Lecture 03: SQL Monday, January 9, Project t/Default.aspxhttp://iisqlsrv.cs.washington.edu/444/Projec.
Select Complex Queries Database Management Fundamentals LESSON 3.1b.
Subqueries CIS 4301 Lecture Notes Lecture /23/2006.
Slides are reused by the approval of Jeffrey Ullman’s
Lecture 04: SQL Monday, January 10, 2005.
Instructor: Craig Duckett Lecture 09: Tuesday, April 25th, 2017
Chapter 3 Introduction to SQL(3)
Using Subqueries to Solve Queries
CS 405G: Introduction to Database Systems
Introduction to Database Systems CSE 444 Lecture 03: SQL
January 19th – Subqueries 2 and relational algebra
The Relational Algebra and Relational Calculus
Introduction to Database Systems CSE 444 Lecture 03: SQL
CMPT 354: Database System I
SQL: Structured Query Language
Using Subqueries to Solve Queries
Lectures 6: Introduction to SQL 5
Lecture 04: SQL Monday, October 6, 2003.
Presentation transcript:

M.P. Johnson, DBMS, Stern/NYU, Spring C : Database Management Systems Lecture #13 M.P. Johnson Stern School of Business, NYU Spring, 2005

M.P. Johnson, DBMS, Stern/NYU, Spring Agenda Review G&A RegExps Live examples HW2 due next Tuesday MT next Thursday

M.P. Johnson, DBMS, Stern/NYU, Spring General form of G&A S = may contain attributes As and/or any aggregates but no other attributes C1 = condition on the attributes in R 1,…,R n C2 = condition on aggregations or attributes from As Why? NB: “Any attribute of relations in the FROM clause may be aggregated in the HAVING clause, but only those attributes that are in the GROUP BY list may appear unaggregated in the HAVING clause (the same rule as for the SELECT clause)” (Ullman, p283). Why? SELECTS FROMR1,…,Rn WHERE C1 GROUP BYAs HAVINGC2 SELECTS FROMR1,…,Rn WHERE C1 GROUP BYAs HAVINGC2

M.P. Johnson, DBMS, Stern/NYU, Spring Evaluation of G&A Evaluation steps: Compute the FROM-WHERE part as usual to obtain a table with all attributes in R 1,…,R n Group by the attributes a 1,…,a k Compute the aggregates in C 2 and keep only groups satisfying C 2 Compute aggregates in S and return the result SELECTS FROMR 1,…,R n WHERE C 1 GROUP BYa 1,…,a k HAVINGC 2 SELECTS FROMR 1,…,R n WHERE C 1 GROUP BYa 1,…,a k HAVINGC 2

M.P. Johnson, DBMS, Stern/NYU, Spring Summary: SQL queries Only SELECT, FROM required Can’t have HAVING without GROUP BY Can have GROUP BY without HAVING Any clauses used must appear in this order: SELECTL FROMRs WHEREs GROUP BYL2 HAVINGs2 ORDER BYL3 SELECTL FROMRs WHEREs GROUP BYL2 HAVINGs2 ORDER BYL3

M.P. Johnson, DBMS, Stern/NYU, Spring Web page examples Find all authors who wrote at least 10 documents  Authors(login, name)  Webpages(url, title, login) Attempt 1: with nested queries Bad! SELECT DISTINCT name FROM Authors WHERE (SELECT count(url) FROM Webpages WHERE Authors.login=Webpages.login) > 10 SELECT DISTINCT name FROM Authors WHERE (SELECT count(url) FROM Webpages WHERE Authors.login=Webpages.login) > 10

M.P. Johnson, DBMS, Stern/NYU, Spring Web page examples Find all authors who wrote at least 10 documents: Attempt 2: Simplify with GROUP BY Good! No need for DISTINCT - get for free from GROUP BY SELECT name FROM Authors, Webpages WHERE Authors.login= Webpages.login GROUP BY name HAVING count(Webpages.url) > 10 SELECT name FROM Authors, Webpages WHERE Authors.login= Webpages.login GROUP BY name HAVING count(Webpages.url) > 10

M.P. Johnson, DBMS, Stern/NYU, Spring Web page examples Find all authors who have a vocab > words:  Authors(login, name)  Webpages(url, title, login)  Mentions(url, word) SELECT name FROM Authors, Webpages, Mentions WHERE Authors.login=Wrote.login AND Webpages.url=Mentions.url GROUP BY name HAVING count(distinct word) > SELECT name FROM Authors, Webpages, Mentions WHERE Authors.login=Wrote.login AND Webpages.url=Mentions.url GROUP BY name HAVING count(distinct word) > 10000

M.P. Johnson, DBMS, Stern/NYU, Spring Live examples Inner joins require an ON clause  Like a where clause  Arbitrary boolean expression  If always true (1=1), reduces to cross join New compar op: BETWEEN  a between 5 and 10  a >= 5 and a <= 10 Q: produce a list of employees with their salary grades  emp, salgrade

M.P. Johnson, DBMS, Stern/NYU, Spring Live examples Q: produce list of bosses and underling- counts, for bosses with >1 underling 1. Ignoring non-bosses 2. Including them 3. Ignoring them again

M.P. Johnson, DBMS, Stern/NYU, Spring sqlzoo exercises 1. Select 2a: 2. Subquery 1d with a subquery: Subquery 1d with a join/cross product:

M.P. Johnson, DBMS, Stern/NYU, Spring Finally: R.A./SQL has limitations Can easily find Alice’s direct subordinates: But: find all of King’s underlings Cannot compute “transitive closure” Cannot express in R.A./SQL! SQL is not “Turing-Complete” NameJobBoss KingPresidentNULL JonesManagerKing BlakeManagerKing FordAnalystJones ScottAnalystJones

M.P. Johnson, DBMS, Stern/NYU, Spring Live examples Examples from sqlzoo.netsqlzoo.net  L (  C (R 1 x … R n ) SELECT L FROM R 1, …, R n WHERE C SELECT L FROM R 1, …, R n WHERE C