354.SQLQueries

354.SQLQueries - Database Systems I SQL Queries CMPT 354,...

Info iconThis preview shows pages 1–5. Sign up to view the full content.

View Full Document Right Arrow Icon
CMPT 354, Simon Fraser University, Fall 2008, Martin Ester 90 Database Systems I SQL Queries CMPT 354, Simon Fraser University, Fall 2008, Martin Ester 91 Introduction We now introduce SQL, the standard query language for relational DBS. As RA, an SQL query takes one or two input tables and returns one output table. Any RA query can also be formulated in SQL, but in a more user-friendly manner. In addition, SQL contains certain features of great practical importance that go beyond the expressiveness of RA, e.g. sorting and aggregation functions.
Background image of page 1

Info iconThis preview has intentionally blurred sections. Sign up to view the full version.

View Full DocumentRight Arrow Icon
CMPT 354, Simon Fraser University, Fall 2008, Martin Ester 92 Example Instances sid sname rating age 22 dustin 7 45.0 31 lubber 8 55.5 58 rusty 10 35.0 sid sname rating age 28 yuppy 9 35.0 31 lubber 8 55.5 44 guppy 5 35.0 58 rusty 10 35.0 sid bid day 22 101 10/10/96 58 103 11/12/96 R1 S1 S2 We will use these instances of the Sailors and Reserves tables in our examples. CMPT 354, Simon Fraser University, Fall 2008, Martin Ester 93 Basic SQL Query relation-list : list of relation names (possibly with a tuple-variable after each name). target-list : list of attributes of relations in relation-list qualification : comparisons (“Attr op const” or “Attr1 op Attr2”, where op is one of ) combined using AND, OR and NOT . DISTINCT is an optional keyword indicating that the answer should not contain duplicates. Default is that duplicates are not eliminated! SELECT [DISTINCT] target-list FROM relation-list WHERE qualification < = ≤ ≥ ≠ , , , , ,
Background image of page 2
CMPT 354, Simon Fraser University, Fall 2008, Martin Ester 94 Conceptual Evaluation Strategy Semantics of an SQL query defined in terms of the following conceptual evaluation strategy: Compute the Cartesian product of relation-list . Selection of the tuples satisfying qualifications . Projection onto the attributes that are in target-list . If DISTINCT is specified, eliminate duplicate rows. This strategy is not an efficient way to process a query! An optimizer will find more efficient strategies to compute the same answers . It is often helpful to write an SQL query in the same order (FROM, WHERE, SELECT). CMPT 354, Simon Fraser University, Fall 2008, Martin Ester 95 Example Conceptual Evaluation SELECT S.sname FROM Sailors S, Reserves R WHERE S.sid=R.sid AND R.bid=103 (sid) sname rating age (sid) bid day 22 dustin 7 45.0 22 101 10/10/96 22 dustin 7 45.0 58 103 11/12/96 31 lubber 8 55.5 22 101 10/10/96 31 lubber 8 55.5 58 103 11/12/96 58 rusty 10 35.0 22 101 10/10/96 58 rusty 10 35.0 58 103 11/12/96
Background image of page 3

Info iconThis preview has intentionally blurred sections. Sign up to view the full version.

View Full DocumentRight Arrow Icon
CMPT 354, Simon Fraser University, Fall 2008, Martin Ester 96 Projection Expressed through the SELECT clause. Can specify any subset of the set of all attributes. SELECT sname, age FROM Sailors; * ” selects all attributes. SELECT
Background image of page 4
Image of page 5
This is the end of the preview. Sign up to access the rest of the document.

This note was uploaded on 10/07/2009 for the course CS 12601 taught by Professor Kewang during the Spring '09 term at Simon Fraser.

Page1 / 23

354.SQLQueries - Database Systems I SQL Queries CMPT 354,...

This preview shows document pages 1 - 5. Sign up to view the full document.

View Full Document Right Arrow Icon
Ask a homework question - tutors are online