IST 210: Organization of Data

Slides:



Advertisements
Similar presentations
Union, Intersection, Difference (subquery) UNION (subquery) produces the union of the two relations. Similarly for INTERSECT, EXCEPT = intersection and.
Advertisements

SQL Group Members: Shijun Shen Xia Tang Sixin Qiang.
SQL Queries Principal form: SELECT desired attributes FROM tuple variables –– range over relations WHERE condition about tuple variables; Running example.
Winter 2002Arthur Keller – CS 1806–1 Schedule Today: Jan. 22 (T) u SQL Queries. u Read Sections Assignment 2 due. Jan. 24 (TH) u Subqueries, Grouping.
SQL CSET 3300.
Algebraic and Logical Query Languages Spring 2011 Instructor: Hassan Khosravi.
CS411 Database Systems Kazuhiro Minami 06: SQL. Join Expressions.
1 Database Systems Relations as Bags Grouping and Aggregation Database Modification.
1 Introduction to SQL Multirelation Queries Subqueries Slides are reused by the approval of Jeffrey Ullman’s.
Relational Operations on Bags Extended Operators of Relational Algebra.
Structured Query Language – Continued Rose-Hulman Institute of Technology Curt Clifton.
Fall 2001Arthur Keller – CS 1806–1 Schedule Today (TH) Bags and SQL Queries. u Read Sections Project Part 2 due. Oct. 16 (T) Duplicates, Aggregation,
CPSC-608 Database Systems Fall 2011 Instructor: Jianer Chen Office: HRBB 315C Phone: Notes #3.
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.
CPSC-608 Database Systems Fall 2008 Instructor: Jianer Chen Office: HRBB 309B Phone: Notes #3.
Winter 2002Arthur Keller – CS 1807–1 Schedule Today: Jan. 24 (TH) u Subqueries, Grouping and Aggregation. u Read Sections Project Part 2 due.
1 More SQL Extended Relational Algebra Outerjoins, Grouping/Aggregation Insert/Delete/Update.
CPSC-608 Database Systems Fall 2011 Instructor: Jianer Chen Office: HRBB 315C Phone: Notes #2.
Chapter 6 Notes. 6.1 Simple Queries in SQL SQL is not usually used as a stand-alone language In practice there are hosting programs in a high-level language.
SCUHolliday6–1 Schedule Today: u SQL Queries. u Read Sections Next time u Subqueries, Grouping and Aggregation. u Read Sections And then.
1 ICS 184: Introduction to Data Management Lecture Note 10 SQL as a Query Language (Cont.)
Onsdag The concepts in a relation data model SQL DDL DML.
1 Single Table Queries. 2 Objectives  SELECT, WHERE  AND / OR / NOT conditions  Computed columns  LIKE, IN, BETWEEN operators  ORDER BY, GROUP BY,
SQL Part I: Standard Queries. COMP-421: Database Systems - SQL Queries I 2 Example Instances sid sname rating age 22 debby debby lilly.
Computational Biology Dr. Jens Allmer Lecture Slides Week 6.
Databases 1 Second lecture.
Databases : Relational Algebra - Complex Expression 2007, Fall Pusan National University Ki-Joune Li These slides are made from the materials that Prof.
1 CSCE Database Systems Anxiao (Andrew) Jiang The Database Language SQL.
1 Introduction to SQL Database Systems. 2 Why SQL? SQL is a very-high-level language, in which the programmer is able to avoid specifying a lot of data-manipulation.
1 Lecture 6 Introduction to SQL part 4 Slides from
Himanshu GuptaCSE 532-SQL-1 SQL. Himanshu GuptaCSE 532-SQL-2 Why SQL? SQL is a very-high-level language, in which the programmer is able to avoid specifying.
SCUHolliday - coen 1787–1 Schedule Today: u Subqueries, Grouping and Aggregation. u Read Sections Next u Modifications, Schemas, Views. u Read.
More SQL (and Relational Algebra). More SQL Extended Relational Algebra Outerjoins, Grouping/Aggregation Insert/Delete/Update.
IST 210 More SQL Todd Bacastow IST 210: Organization of Data.
Databases : SQL Multi-Relations 2007, Fall Pusan National University Ki-Joune Li These slides are made from the materials that Prof. Jeffrey D. Ullman.
1 Introduction to Database Systems, CS420 SQL JOIN, Aggregate, Grouping, HAVING and DML Clauses.
1 Database Design: DBS CB, 2 nd Edition SQL: Select-From-Where Statements & Multi-relation Queries & Subqueries Ch. 6.
Select-From-Where Statements Multirelation Queries Subqueries
More SQL: Complex Queries,
Relational Database Systems 1
Schedule Today: Jan. 28 (Mon) Jan. 30 (Wed) Next Week Assignments !!
Slides are reused by the approval of Jeffrey Ullman’s
CPSC-310 Database Systems
Computational Biology
Outerjoins, Grouping/Aggregation Insert/Delete/Update
Foreign Keys Local and Global Constraints Triggers
Databases : More about SQL
CPSC-310 Database Systems
Schedule Today: Next After that Subqueries, Grouping and Aggregation.
Introduction to Database Systems, CS420
CPSC-608 Database Systems
06a: SQL-1 The Basics– Select-From-Where
CS 440 Database Management Systems
CPSC-608 Database Systems
Database Design and Programming
Operators Expression Trees Bag Model of Data
CPSC-310 Database Systems
IT 244 Database Management System
More SQL: Complex Queries, Triggers, Views, and Schema Modification
Structured Query Language
Basic Operations Algebra of Bags
CMSC-461 Database Management Systems
Schedule Today: Next And then Relational Algebra.
CPSC-608 Database Systems
CPSC-608 Database Systems
More SQL Extended Relational Algebra Outerjoins, Grouping/Aggregation
CPSC-608 Database Systems
Instructor: Zhe He Department of Computer Science
Select-From-Where Statements Multirelation Queries Subqueries
Presentation transcript:

IST 210: Organization of Data SQL: Data Manipulation Language (DML) Todd Bacastow IST 210: Organization of Data 11/16/2018

Follow-up SQL provides two wildcard characters that you can use in LIKE clauses to match character strings. The percent sign (%) matches zero or more characters. The underscore ( _ ) matches any single character. 11/16/2018

SQL – Data Manipulation Language (DML) Think in terms of sets and not in terms of "one record at a time" processing Ask set-oriented questions For example: "What is in the set of students?" 11/16/2018

Principal Form SELECT desired attributes FROM relations (formally, tuple variables) WHERE condition about tuples in the relations 11/16/2018

Example What beers are made by A.B. Beers (name, addr) SELECT name FROM Beers WHERE manf = ’A.B.’; Note single quotes for strings. name Bud BudLite Michelob 11/16/2018

Use * As List of All Attributes Beers (name, manf) SELECT * FROM Beers WHERE manf = ’A.B.’; name manf Bud A.B. BudLite Michelob 11/16/2018

Notes Condition in WHERE clause can use logical operators AND, OR, NOT and parentheses in the usual way Remember: SQL is case insensitive. Keywords like SELECT or AND can be written upper/lower case as you like Only inside quoted strings does case matter ’Joe Johns’ != ’joe johns’ != ’Joe JOHNS’ 11/16/2018

Relational Operators There are six Relational Operators in SQL: = Equal <> or != Not Equal  < Less Than > Greater Than <= Less Than or Equal To >= Greater Than or Equal To  11/16/2018

Multirelation Queries List of relations in FROM clause Relation-dot-attribute disambiguates attributes from several relations Example: Find the beers that the frequenters of Joe’s Bar like. Likes (drinker, beer) Frequents (drinker, bar) SELECT beer FROM Frequents, Likes WHERE bar=’Joe’’s’ AND Frequent.drinker=Likes.drinker; 11/16/2018

Multirelation Query Semantics Same as for single relation, but start with the product of all the relations mentioned in the FROM clause Operational Semantics Consider a tuple variable for each relation in the FROM Imagine these tuple variables each pointing to a tuple of their relation, in all combinations (i.e., a cross product) If the current assignment of tuple-variables to tuples makes the WHERE true, then output the attributes of the SELECT. 11/16/2018

Explicit Tuple Variables Sometimes we need to refer to two or more copies of a relation Use tuple variables as aliases of the relations E.g. Find pairs of beers by the same manufacturer Beers (name, manf) SELECT b1.name, b2.name FROM Beers AS b1, Beers AS b2 WHERE b1.manf=b2.manf AND b1.name<b2.name; Note: b1.name<b2.name is needed to avoid producing (Bud,Bud) and to avoid producing in both orders. 11/16/2018

Subqueries Result of a SELECT-FROM-WHERE query can be used in the WHERE-clause of another query Simplest case: subquery returns a single, unary tuple E.g. Find bars that serve Miller at the same price Joe charges for Bud Sells (bar, beer, price) SELECT bar FROM Sells WHERE beer=’Miller’ AND price= (SELECT price WHERE bar=’Joe’’s’ AND beer=’Bud’); 11/16/2018

Subqueries Scoping rule: an attribute refers to the most closely nested relation with that attribute Parentheses rule: parentheses around subquery are essential 11/16/2018

Union, Intersection, Difference “Relation UNION relation” produces the union of the two relations Similarly for INTERSECTION, EXCEPT = intersection and set difference Deference renders the difference between two relations 11/16/2018

Bag Semantics of SQL An SQL relation is really a bag or multiset It may contain the same tuple more than once, although there is no specified order Example: {1,4,1,3} is a bag and not a set {1,4,3} is a set Deferent implementations of SQL handle this in different ways! 11/16/2018

Defaults for SQL Default for SELECT-FROM-WHERE is bag Default for UNION, INTERSECTION, and DIFFERENCE is set Force set semantics with DISTINCT after SELECT Example: Find the different prices charged for beers Sells (bar, beer, price) SELECT DISTINCT price FROM Sells; Force bag semantics with ALL after UNION, etc. 11/16/2018

More on Bag Semantics Bag Union: collective of the element in the two bags Example: {1,2,1} UNION {1,2,3} = {1,1,1,2,2,3} Intersection: take the minimum of the number of occurrences in each bag Example: {1,2,1} INTERSECT {1,2,3,3} = {1,2} Difference: proper-subtract the number of occurrences in the two bags Example: {1,2,1}-{1,2,3,3} = {1} NOTE: Frequently the default for UNION, INTERSECTION, and DIFFERENCE is set 11/16/2018

Example Find the drinkers and beers such that the drinker likes the beer and frequents a bar that serves it Likes (drinker, beer) Sells (bar, beer, price) Frequents (drinker, bar) Likes INTERSECT (SELECT drinker, beer FROM Sells, Frequents WHERE Frequents.bar = Sells.bar); 11/16/2018

Aggregations SUM, AVG, MIN, MAX, and COUNT apply to attributes/columns. COUNT(*) also applies to tuples Use these in lists following SELECT Example: Find the average price of Bud Sells (bar, beer, price) SELECT AVG (price) FROM Sells WHERE beer = ’Bud’; Counts each tuple once (including possible duplicates) 11/16/2018

Eliminating Duplicates before Aggregation Find the number of different prices at which Bud is sold Sells (bar, beer, price) SELECT COUNT(DISTINCT price) FROM Sells WHERE beer = ’Bud’; DISTINCT may be used in any aggregation, but typically only make sense with COUNT 11/16/2018

Grouping Follow SELECT-FROM-WHERE by GROUP BY and a list of attributes The relation that is the result of the FROM and WHERE clauses is grouped according to the values of these attributes, and aggregations take place only within a group Example: find the average sales price for each beer Sells (bar, beer, price) SELECT beer, AVG(price) FROM Sells GROUP BY beer; 11/16/2018

Simple Join Consider: Likes (drinker, beer) Sells (bar, beer, price) Chapter Name September 98 Simple Join Consider: Likes (drinker, beer) Sells (bar, beer, price) SELECT drinker, bar FROM Likes, Sells WHERE Likes.beer=Sells.beer; What’s happening here???? 11/16/2018 83

Other ‘Joins’ in SQL2 R NATURAL JOIN S R CROSS JOIN S R JOIN S ON condition R OUTER JOIN S 11/16/2018

Alternative JOIN Constructs Chapter Name September 98 Alternative JOIN Constructs SQL2 provides alternative ways to specify joins: FROM Likes.beer JOIN Sells.beer ON Likes.beer= Sells.beer FROM Likes JOIN Sells USING beer FROM Likes NATURAL JOIN Sells The first produces a table with two identical beer columns, remaining two produce table with single beer column. 11/16/2018 86

Outerjoin Outerjoin = natural join with dangling tuples padded with NULLs and included in the result A tuple is dangling if it doesn’t join with any other tuple A B C 1 2 5 6 3 4 NULL 7 8 B C 2 5 6 7 8 A B 1 2 3 4 = OUTERJOIN 11/16/2018

Modifiers of OUTERJOIN Optional NATURAL in front Optional ON condition at end Optional LEFT, RIGHT, FULL before OUTER LEFT = Pad dangling tuples of R ONLY RIGHT = Pad dangling tuples of S ONLY 11/16/2018

Other Examples Employees: Orders: 11/16/2018 Employee_ID Name 01 Hansen, Ola 02 Svendson, Tove 03 Svendson, Stephen 04 Pettersen, Kari Orders: Prod_ID Product Employee_ID 234 Printer 01 657 Table 03 865 Chair 11/16/2018

Who has ordered a product, and what did they order? SELECT Employees.Name, Orders.Product FROM Employees, Orders WHERE Employees.Employee_ID=Orders.Employee_ID Result Name Product Hansen, Ola Printer Svendson, Stephen Table Chair 11/16/2018

Who ordered a printer? SELECT Employees.Name FROM Employees, Orders WHERE Employees.Employee_ID=Orders.Employee_ID AND Orders.Product='Printer' Result Name Hansen, Ola 11/16/2018

Who has ordered a product, and what did they order? SELECT Employees.Name, Orders.Product FROM Employees INNER JOIN Orders ON Employees.Employee_ID=Orders.Employee_ID The INNER JOIN returns all rows from both tables where there is a match. If there are rows in Employees that do not have matches in Orders, those rows will not be listed. Result Name Product Hansen, Ola Printer Svendson, Stephen Table Chair 11/16/2018