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.

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.
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.
IS698: Database Management Min Song IS NJIT. Overview  Query processing  Query Optmization  SQL.
Winter 2002Arthur Keller – CS 1809–1 Schedule Today: Jan. 31 (TH) u Constraints. u Read Sections , Project Part 3 due. Feb. 5 (T) u Triggers,
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.
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.
DB Modifications Modification = insert + delete + update. Insertion of a Tuple INSERT INTO relation VALUES (list of values). Inserts the tuple = list of.
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.
SCUHolliday - coen 1789–1 Schedule Today: u Constraints, assertions, triggers u Read Sections , 7.4. Next u Triggers, PL/SQL, embedded SQL, JDBC.
SQL 2014, Fall Pusan National University Ki-Joune Li These slides are made from the materials that Prof. Jeffrey D. Ullman distributes via his course web.
SCUHolliday6–1 Schedule Today: u SQL Queries. u Read Sections Next time u Subqueries, Grouping and Aggregation. u Read Sections And then.
Databases : SQL-Introduction 2007, Fall Pusan National University Ki-Joune Li These slides are made from the materials that Prof. Jeffrey D. Ullman distributes.
1 IT 244 Database Management System Lecture 11 SQL Select-From-Where Statements Meaning of queries Subqueries Ref : -A First Course in Database System.
Constraints on Relations Foreign Keys Local and Global Constraints Triggers Following lecture slides are modified from Jeff Ullman’s slides
Winter 2006Keller, Ullman, Cushing9–1 Constraints Commercial relational systems allow much more “fine-tuning” of constraints than do the modeling languages.
1 Introduction to SQL. 2 Why SQL? SQL is a very-high-level language, in which the programmer is able to avoid specifying a lot of data-manipulation details.
Onsdag The concepts in a relation data model SQL DDL DML.
SQL 2015, Fall Pusan National University Ki-Joune Li These slides are made from the materials that Prof. Jeffrey D. Ullman distributes via his course web.
Computational Biology Dr. Jens Allmer Lecture Slides Week 6.
Databases 1 Second lecture.
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 Introduction to SQL. 2 Why SQL? SQL is a very-high-level language, in which the programmer is able to avoid specifying a lot of data-manipulation details.
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.
603 Database Systems Senior Lecturer: Laurie Webster II, M.S.S.E.,M.S.E.E., M.S.BME, Ph.D., P.E. Lecture 18 A First Course in Database Systems.
SCUHolliday - coen 1789–1 Schedule Today: u Constraints, assertions, triggers u Read Sections , 7.4. Next u Embedded SQL, JDBC. u Read Sections.
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 SQL Select-From-Where Statements Subqueries Grouping and Aggregation.
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
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
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
CPSC-310 Database Systems
IST 210: Organization of Data
CPSC-310 Database Systems
IT 244 Database Management System
2018, Fall Pusan National University Ki-Joune Li
CMSC-461 Database Management Systems
CPSC-608 Database Systems
CPSC-608 Database Systems
More SQL Extended Relational Algebra Outerjoins, Grouping/Aggregation
CPSC-608 Database Systems
CPSC-608 Database Systems
CMSC-461 Database Management Systems
Instructor: Zhe He Department of Computer Science
Select-From-Where Statements Multirelation Queries Subqueries
Presentation transcript:

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 a lot of data-manipulation details that would be necessary in languages like C, Java. What makes SQL viable is that its queries are “optimized” quite well, yielding efficient query executions.

Himanshu GuptaCSE 532-SQL-3 SQL Queries Principal form: SELECT desired attributes FROM tables (or tuple variables) WHERE condition over tuple variables; [Bag Semantics, by default]

Himanshu GuptaCSE 532-SQL-4 SQL Semantics [Important to understand, esp. to write sub-queries correctly] Consider a tuple variable t i for each relation R i in the FROM clause. Then, execute the following: for t 1 in R 1 for t 2 in R 2 ….. If satisfies the WHERE condition, then output the SELECT attributes of

Himanshu GuptaCSE 532-SQL-5 Example Likes(drinker, beer); Frequents(drinker, bar) Find the beers that the frequenters of Joe's Bar like. SELECT beer FROM Frequents, Likes WHEREbar = 'Joe''s Bar' AND Frequents.drinker = Likes.drinker; [Here, technically, Frequents and Likes are tuple variables. The “bar” is an attribute of the implicit tuple variable.]

Himanshu GuptaCSE 532-SQL-6 Star as List of All Attribute Beers(name, manf) SELECT * FROM Beers WHERE manf = 'Anheuser-Busch'; namemanf BudAnheuser-Busch Bud LiteAnheuser-Busch MichelobAnheuser-Busch

Himanshu GuptaCSE 532-SQL-7 Renaming columns Beers(name, manf) SELECT name AS beer FROM Beers WHERE manf = 'Anheuser-Busch'; beer Bud Bud Lite Michelob

Himanshu GuptaCSE 532-SQL-8 Example Sells(bar, beer, price) Find the price Joe's Bar charges for Bud. SELECT price FROM Sells WHERE bar = 'Joe''s Bar' AND beer = 'Bud'; Note: two single-quotes in a character string represent one single quote. Conditions in WHERE clause can use logical operators.

Himanshu GuptaCSE 532-SQL-9 Explicit Tuple Variables Sometimes we need to refer to two or more copies of a relation. Use explicit tuple variables as aliases of the relations. Example Beers(name, manf) Find pairs of beers by the same manufacturer. SELECT b1.name, b2.name FROM Beers b1, Beers b2 WHERE b1.manf = b2.manf AND b1.name < b2.name; Why do we need ( b1.name < b2.name) ?

Himanshu GuptaCSE 532-SQL-10 Subqueries A query result can be used in the where-clause of another query. Example: Sells(bar, beer, price) Find bars that serve Miller at the same price Joe charges for Bud. SELECT bar FROM Sells WHERE beer = 'Miller' AND price = (SELECT price FROM Sells WHERE bar = 'Joe''s Bar' AND beer = 'Bud'); Scoping: An attribute refers to the most closely nested relation. Parentheses around subquery are essential. NEXT: Using subqueries with IN, EXISTS, ANY, ALL operators.

Himanshu GuptaCSE 532-SQL-11 Subqueries: The IN Operator “Tuple IN relation” is true iff the tuple is in the relation. Example Find the name and manufacturer of beers that Fred likes. Beers(name, manf) Likes(drinker, beer) SELECT * FROM Beers WHERE name IN (SELECT beer FROMLikes WHERE drinker = 'Fred’); Also: NOT IN.

Himanshu GuptaCSE 532-SQL-12 EXISTS “ EXISTS(relation)” is true iff the relation is nonempty. Example: Beers(name, manf) Find the beers that are the unique beer by their manufacturer. SELECT name FROM Beers b1 WHERE NOT EXISTS (SELECT * FROM Beers WHERE manf = b1.manf AND name <> b1.name); Scoping: To refer to outer Beers in the inner subquery, we need to create an explicit tuple variable b1. A subquery that refers to values from a surrounding query is called a correlated subquery.

Himanshu GuptaCSE 532-SQL-13 Quantifiers – ANY, ALL ANY and ALL behave as existential and universal quantifiers, respectively. Example Sells(bar, beer, price) Find the beer(s) sold for the highest price. SELECT beer FROM Sells WHERE price >= ALL(SELECT price FROM Sells); Class Problem Find the beer(s) not sold for the lowest price.

Himanshu GuptaCSE 532-SQL-14 Union, Intersection, Difference “(subquery) UNION (subquery)” produces the union. Similarly, INTERSECT, EXCEPT.  Oracle uses MINUS instead of EXCEPT. Example Likes(drinker, beer); Sells(bar, beer, price); Frequents(drinker, bar) Find the drinkers and beers such that the drinker likes the beer and frequents a bar that serves it. (SELECT * FROM Likes) INTERSECT (SELECT drinker, beer FROM Sells, Frequents WHERE Frequents.bar = Sells.bar );

Himanshu GuptaCSE 532-SQL-15 Forcing Set/Bag Semantics Default for select-from-where is bag;  Force set semantics using ‘ SELECT DISTINCT’ Default for union, intersection, or difference is set.  Force bag semantics using ‘ UNION ALL ’ etc. Example: Sells(bar, beer, price) Find the different prices for beers. SELECT DISTINCT price FROM Sells;

Himanshu GuptaCSE 532-SQL-16 Aggregations Recall the aggregate operator  A, F(B) (R). Equivalent SQL: SELECT A, F(B) FROM R GROUP BYA Example Sells(bar, beer, price) Find the average sales price for each beer. SELECT beer, AVG(price) FROM Sell GROUP BY beer;

Himanshu GuptaCSE 532-SQL-17 Aggregation Example Sells(bar, beer, price); Frequents(drinker, bar) Find, for each drinker, the average price of Bud at the bars they frequent. SELECT drinker, AVG(price) FROM Frequents, Sells WHERE beer = 'Bud' AND Frequents.bar = Sells.bar GROUP BY drinker; Note: grouping occurs after the ,  operations.

Himanshu GuptaCSE 532-SQL-18 Illegal Aggregation – 1 Sells(bar, beer, price) SELECT bar, beer SUM(price) FROM Sells WHERE beer = 'Bud‘ GroupBybar; Illegal. Why?

Himanshu GuptaCSE 532-SQL-19 Illegal Aggregation – 2 Sells(bar, beer, price) Find the bar that sells Bud the cheapest. SELECT bar, MIN(price) FROM Sells WHERE beer = 'Bud'; Illegal. Why? Rule: Each element of a SELECT clause must either be aggregated or appear in a group-by clause. Problem : How would we find that bar?

Himanshu GuptaCSE 532-SQL-20 HAVING clause HAVING clauses are selections on groups, after grouping and aggregation has been done. Beers(name, manf); Sells(bar, beer, price) Find the average price of those beers that are either served in at least 3 bars or manufactured by Busch. SELECT beer, AVG(price) FROM Sells GROUP BY beer HAVING COUNT(*) >= 3 OR beer IN (SELECT name FROM Beers WHERE manf = 'Busch');

Order of Evaluation FROM and WHERE (to get an intermediate table) GROUP BY HAVING SELECT Himanshu GuptaCSE 532-SQL-21

Himanshu GuptaCSE 532-SQL-22 DB Modifications Modification = insert, delete, or update. Syntax INSERT INTO relation VALUES (list of values). INSERT INTO relation (subquery). DELETE FROM relation WHERE condition UPDATE relation SET assignments WHERE condition.

Himanshu GuptaCSE 532-SQL-23 Defining a Database Schema CREATE TABLE name (list of elements). Elements: attributes and their types; key declarations; constraints. CREATE X for views, indexes, assertions, triggers. DROP X name deletes the element of kind X of that name. CREATE TABLE Sells ( bar CHAR(20), beer CHAR(20), price REAL ); DROP TABLE Sells;