SQL Basic format of the select command select[distinct] target_list fromtuple_variable_list wherequalification [order by target_list_subset]; Simple query.

Slides:



Advertisements
Similar presentations
Database Management Systems 3ed, R. Ramakrishnan and J. Gehrke1 SQL: Queries, Programming, Triggers Chapter 5 Modified by Donghui Zhang.
Advertisements

Introduction to Database Systems 1 SQL: The Query Language Relation Model : Topic 4.
1 SQL: Structured Query Language (‘Sequel’) Chapter 5.
1 Lecture 11: Basic SQL, Integrity constraints
CS 166: Database Management Systems
SQL: Queries, Constraints, Triggers
Database Management Systems 3ed, R. Ramakrishnan and J. Gehrke1 SQL: Queries, Constraints, Triggers Chapter 5.
SQL.
SQL: The Query Language Jianlin Feng School of Software SUN YAT-SEN UNIVERSITY courtesy of Joe Hellerstein and etc for some slides.
Database Management Systems, R. Ramakrishnan and J. Gehrke1 SQL: Queries, Programming, Triggers Chapter 5.
CS 405G: Introduction to Database Systems
SQL (2).
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.
1 Today’s Class  Relational Model  SQL CS F212 Database Systems.
SQL Review.
Database Management Systems, R. Ramakrishnan and J. Gehrke1 SQL: Queries, Programming, Triggers Chapter 5.
1 SQL: Structured Query Language (‘Sequel’) Chapter 5.
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.
CMPT 354, Simon Fraser University, Fall 2008, Martin Ester 90 Database Systems I SQL Queries.
1 SQL (Simple Query Language). 2 Query Components A query can contain the following clauses –select –from –where –group by –having –order by Only select.
FALL 2004CENG 351 File Structures and Data Management1 SQL: Structured Query Language Chapter 5.
Recap of Feb 11: SQL “*” is an abbreviation for all attributes in the from list implicit tuple variables; table names as tuple variables Set operators:
Rutgers University SQL: Queries, Constraints, Triggers 198:541 Rutgers University.
SQL: The Query Language Part 1
Database Management Systems 3ed, R. Ramakrishnan and J. Gehrke1 SQL: Queries, Constraints, Triggers Chapter 5.
1 Chapter 5 SQL: QUERIES, CONSTRAINTS, TRIGGERS. 2 INTRODUCTION - The current presentation is consistent with both SQL-92 and SQL: 99 (differences will.
1 The Oracle Database System Querying the Data Database Course The Hebrew University of Jerusalem.
1 SQL: Structured Query Language Chapter 5. 2 SQL and Relational Calculus relationalcalculusAlthough relational algebra is useful in the analysis of query.
Still More Operators: Outer Join outer join is an extension of the join operation to deal with missing information –three types: left outer join, right.
CSC343 – Introduction to Databases - A. Vaisman1 SQL: Queries, Programming, Triggers.
INFS614 - Lecture week 9 1 More on SQL Lecture Week 9 INFS 614, Fall 2008.
Database System Concepts, 6 th Ed. ©Silberschatz, Korth and Sudarshan See for conditions on re-usewww.db-book.com Chapter 3: Introduction.
1 IT420: Database Management and Organization SQL - Data Manipulation Language 27 January 2006 Adina Crăiniceanu
SQL Queries and Subqueries Zaki Malik September 04, 2008.
1 ICS 184: Introduction to Data Management Lecture Note 10 SQL as a Query Language (Cont.)
CSC 411/511: DBMS Design Dr. Nan WangCSC411_L6_SQL(1) 1 SQL: Queries, Constraints, Triggers Chapter 5 – Part 1.
Instructor: Jinze Liu Fall Basic Components (2) Relational Database Web-Interface Done before mid-term Must-Have Components (2) Security: access.
SQL Part I: Standard Queries. COMP-421: Database Systems - SQL Queries I 2 Example Instances sid sname rating age 22 debby debby lilly.
SQL Examples CS3754 Class Note 11 CS3754 Class Note 11, John Shieh,
Unit 5/COMP3300/ SQL: Queries, Programming, Triggers Chapter 5.
Database Management Systems 3ed, R. Ramakrishnan and J. Gehrke1 SQL: Queries, Constraints, Triggers Chapter 5.
SQL: Queries, Programming, Triggers. Example Instances We will use these instances of the Sailors and Reserves relations in our examples. If the key for.
ICS 321 Fall 2009 SQL: Queries, Constraints, Triggers Asst. Prof. Lipyeow Lim Information & Computer Science Department University of Hawaii at Manoa 9/8/20091Lipyeow.
1 Database Systems ( 資料庫系統 ) October 24, 2005 Lecture #5.
Database Management COP4540, SCS, FIU Structured Query Language (Chapter 8)
1 SQL: Queries, Constraints, Triggers Chapter 5. 2 Example Instances R1 S1 S2  We will use these instances of the Sailors and Reserves relations in our.
Introduction to SQL ; Christoph F. Eick & R. Ramakrishnan and J. Gehrke 1 Using SQL as a Query Language COSC 6340.
CS 405G: Introduction to Database Systems Instructor: Jinze Liu Fall 2009.
CMPT 258 Database Systems SQL Queries (Chapter 5).
Database Management Systems 3ed, R. Ramakrishnan and J. Gehrke1 Database Management Systems Chapter 5 SQL.
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 SQL: Structured Query Language (‘Sequel’) Chapter 5.
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.
Database Management Systems 1 Raghu Ramakrishnan SQL: Queries, Programming, Triggers Chpt 5 Jianping Fan.
SQL: The Query Language Part 1 R&G - Chapter 5 1.
Database Management Systems 3ed, R. Ramakrishnan and J. Gehrke1 Basic SQL Queries.
1 CS122A: Introduction to Data Management Lecture 9 SQL II: Nested Queries, Aggregation, Grouping Instructor: Chen Li.
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.
1 CS122A: Introduction to Data Management Lecture 8 Introduction to SQL Instructor: Chen Li.
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
Basic SQL Lecture 6 Fall
CS 405G: Introduction to Database Systems
SQL: Queries, Constraints, Triggers
CS4222 Principles of Database System
SQL: Structured Query Language
Textbook Chapter 9 (incl. example queries at end)
SQL: Queries, Constraints, Triggers
Presentation transcript:

SQL Basic format of the select command select[distinct] target_list fromtuple_variable_list wherequalification [order by target_list_subset]; Simple query examples use this relational schema: sailors(sid, sname, rating) boats(bid, bname, colour) reserve(sid, bid, date)

SQL: target list “*” is an abbreviation for all attributes in the from list select * from sailors s where order by s.rating Each item in the target list can be as general as attribute_name = expression, where the expression is any arithmetic or string expression over indexed tuple variables and constants. It can also contain some built-in functions like sqrt, sin, mod, etc. as well as aggregates (coming up later)

SQL: target list expression example With rating an integer from 1 to 10, this query gives a rating bonus to sailors who sailed two different boats on the same day. select s.sid, s.sname, rating=s.rating+2 from sailors s, reserve r, reserve r2 where s.sid=r.sid and s.sid=r2.sid and r.date=r2.date and r.bid != r2.bid What’s wrong with the above? –What happens if s.rating = 9 before this query? –Domain constraints might take care of this, but we need to be careful

SQL Qualifications: each item in a qualification (where clause) can be as general as expression=expression Example: selectname1 = s1.sname, name2 = s2.sname fromsailors s1, sailors s2 where2*s1.rating = s2.rating-1

SQL Further elaboration: tuple variables can be implicit if the system can figure out which relation each attribute belongs to table names can be used as tuple variables Example: find names, ages, and departments of employees who are over 40 and work on the first floor. selectename, age, emp.dname fromemp, dept whereage>40 and floor=1 and emp.dname=dept.dname

SQL SQL provides set operators: union, intersect, and minus Example: find the names of employees who work in the toy department and make at most 60K (selectename fromemp wheredname=“toy”) minus (selectename fromemp wheresal>60K)

SQL Note that it is usually possible to phrase a single query in multiple ways. The previous query could have also been written: (selectename fromemp wheredname=“toy”) intersect (selectename fromemp wheresal  60K)

SQL Or also (even simpler): selectename fromemp wheredname=“toy” and sal  60K Writing a query in different ways will usually change how efficient the query is -- the above query is very likely to be faster than the example using intersects, and that one is likely to be faster than the one using minus.

SQL SQL also provides set operators: contains (a set being a superset of another) and exists (a set not being empty). Both return Boolean results, so may be negated (using not).

SQL Example: find the names of employees who manage all the departments on the first floor. selectmgr fromdept d1 where(select d2.dname from dept d2 where d1.mgr=d2.mgr) contains (select dname from dept where floor=1)

SQL SQL allows nested queries using the keyword in Example: find the names of employees who work on the first floor. selectename fromemp wheredname in (select dname from dept where floor = 1) The same query in flat form is select dname from emp, dept where emp.dname=dept.dname and floor=1

SQL The connective in tests for set membership. Similar connectives are: –not in (set non membership) –op any (op relationship with some tuple in the set) –op all (op relationship with all tuples in the set) where op is one of (=, !=,, =) Example: find the names of employees who make more than everybody on the first floor. selectename fromemp wheresal > all (select sal from emp, dept where emp.dname=dept.dname and floor = 1)

SQL Scoping of variables works exactly as in Pascal or C Example: find the names of students who take a course from their advisor. selectsname fromstudent wheres# in (select s# from enroll where c# in (select c# from class where prof=student.advisor))

Recap: SQL Four basic commands –select –insert –delete –update

SQL Insert Insert command format: insert into relation_name values (value_list) or insert into relation_name select_statement Semantics of insert –format one: add the tuple corresponding to value_list into relation_name –format two: execute the select statement, then add all the resulting tuples into relation_name Example: insert into student values (1, “Carey”, “CS”, “Stonebraker”)

SQL Insert Example: relation register(S#, name, paid) in which registered students are recorded. After the end of registration week, we execute: insert into student selectr.s#, r.name from register r wherer.paid=“yes”

SQL Delete Delete command format: delete relation_name where qualification Semantics of delete: execute the corresponding select command: selectfull_target_list (or “*”) fromrelation_name wherequalification and then remove the resulting tuples from relation_name

SQL Delete Example: with the following schema student(s#, name, major, advisor) enroll(s#, c#, grade) course(c#, dept) The following command expels CS majors who received a grade of less than 2.5 in a CS course: deletestudent wheremajor=“CS” and s# in (selects# from enroll, course where enroll.s#=student.s# and grade<2.5 and enroll.c#=course.c# and dept=“CS”)

SQL Update Update format updaterelation_name settarget_list wherequalification Semantics of update: it is equivalent to executing: –insert into del_temp select * from relation_name where qualification

SQL Update Semantics of update (cont): … then executing –insert into app_temp select ext_target_list from relation_name where qualification –delete the tuples in del_temp from relation_name –add the tuples in app_temp to relation_name Ext_target_list is identical to target_list in the original update command, but augmented with tuple_variable.attribute_name for all attributes of the range of tuple_variable that don’t appear in target_list.

SQL Update Example: give a 10% grade raise to every CS major in CS564 updateenroll setgrade=1.1*grade wherec#=“CS564” and s# in (select s# from student where major=“CS”)

SQL Update Which is equivalent to: insert into del_temp select s#, c#, grade from enroll where c#=“CS564” and s# in (select s# from student where major=“CS”) insert into app_temp select s#, c#, grade=1.1*grade from enroll where c#=“CS564” and s# in (select s# from student where major=“CS”)

SQL Aggregates Aggregate functions are functions that take a collection of values as input and return a single value. SQL supports five built-in aggregate functions: –average: avg –minimum: min –maximum: max –total: sum –cardinality: count using distinct to aggregate only unique values is often important with avg, sum, and count

SQL Aggregates Example: find the number of students select num_of_students = count(s#) from student why do we not need to use distinct in this example? Example: find the number of employee records select count (*) from emp if an employee appears more than once in the emp relation, for example if he had switched jobs or had two jobs, then this command would count that employee once for each record

SQL Aggregates Qualified Aggregates: Example: find the average age of employees in the toy department select avg(age) from emp where dname=“toy”

SQL: Group By clause Group aggregates: groups of tuples are computed using the group by clause –the attributes given in the clause are used to form groups –typles with the same value on all attributes in the clause are placed in one group Example: in each department, find the minimum age of employees who make more than 50K select dname, min(age) from emp where sal>50K group bydname

SQL: Having clause Sometimes it is useful to state a condition that applies to groups in group by rather than to tuples. We do that in SQL with the having clause. SQL applies predicates of having after groups have been formed. Example: find the average salary for employees under 30 for each department having more than 10 such employees select dname, avg(sal) from emp where age<30 group bydname havingcount(*)>10