Download presentation
Presentation is loading. Please wait.
Published byMelvyn Hopkins Modified over 9 years ago
1
CS 370 Database Systems Lecture 12 Introduction to SQL
2
Structured Query Language The most widely used relational query language The standard for relational database management systems (RDBMS) SQL Overview
3
Two sublanguages: –Data Manipulation Language (DML) – This subset of SQL allows users to pose queries and to insert, delete, and update rows. –Data Definition Language (DDL) – This subset of SQL supports the creation, deletion, and modification of definitions for tables and views. SQL Overview
4
1970 – E. Codd develops relational database concept 1974-1979 – IBM Sequel language developed as part of System R project at the IBM San Jose Research Laboratory Renamed Structured Query Language (SQL) 1986 – ANSI SQL standard released Current – SQL is supported by most major database vendors ANSI and ISO standard SQL: –SQL-86 –SQL-89 –SQL-92 –SQL:1999 –SQL:2003 History
5
Specify syntax/semantics for data definition and manipulation Define data structures Enable portability Allow for later growth/enhancement to standard Purpose of SQL Standard
6
The basic form of an SQL query is:- SELECT[DISTINCT] select-list FROMfrom-list WHEREqualification Clauses of the SELECT statement: –SELECT List the columns (and expressions) that should be returned from the query –FROM Indicate the table(s) or view(s) from which data will be obtained –WHERE Indicate the conditions under which a row will be included in the result Basic SQL Query
7
CS370 Spring 2007 SELECT DISTINCT S.sid FROM Sailors S, Reserves R WHERE S.sid=R.sid sidsnameratingage 1Frodo722 2Bilbo239 3Sam827 Sailors sidbidday 11029/12 21039/12 21029/13 Reserves sid 1 2 Find sailors that have reserved at least one boat SELECT DISTINCT
8
CS370 Spring 2007 How about: SELECT S.sid FROM Sailors S, Reserves R WHERE S.sid=R.sid sid 1 2 2 SELECT DISTINCT
9
CS370 Spring 2007 How about: SELECT S.sname FROM Sailors S, Reserves R WHERE S.sid=R.sid sidsnameratingage 1Frodo722 2Bilbo239 3Sam827 4Bilbo532 Sailors sidbidday 11029/12 21039/13 41059/13 Reserves sname Frodo Bilbo SELECT DISTINCT S.sname FROM Sailors S, Reserves R WHERE S.sid=R.sid sname Frodo Bilbo vs: SELECT DISTINCT
10
CS370 Spring 2007 Semantics of an SQL query are defined in terms of the following conceptual evaluation strategy: 1.FROM clause: compute cross-product of all tables 2.WHERE clause: Check conditions, discard tuples that fail. (called “selection”). 3. SELECT clause: Delete unwanted fields. (called “projection”). 4. If DISTINCT specified, eliminate duplicate rows. Query Semantics
11
SQL is based on set and relational operations with certain modifications and enhancements A typical SQL query has the form: select A 1, A 2, …, A n from R 1, R 2, …, R m where P - A i represent attributes - R i represent relations - P is a predicate. Basic Structure
12
Projection The select clause corresponds to the projection operation of the relational algebra. It is used to list the attributes desired in the result of a query. Find the names of all branches in the loan relation select branch-name from loan Equivalent to: branch-name (loan) An asterisk in the select clause denotes “all attributes” select * from loan
13
Duplicate Removal SQL allows duplicates in relations as well as in query results. Use select distinct to force the elimination of duplicates. Find the names of all branches in the loan relation, and remove duplicates select distinct branch-name from loan The keyword all specifies that duplicates not be removed. select all branch-name from loan force the DBMS to remove duplicates force the DBMS not to remove duplicates
14
Arithmetic Operations on Retrieved Results The select clause can contain arithmetic expressions involving the operators, , , and , and operating on constants or attributes of tuples. The query: select branch-name, loan-number, amount * 100 from loan would return a relation which is the same as the loan relations, except that the attribute amount is multiplied by 100
15
The where clause specifies conditions that tuples in the relations in the from clause must satisfy. Find all loan numbers for loans made at the Perryridge branch with loan amounts greater than $1200. select loan-number from loan where branch-name=“Perryridge” and amount >1200 SQL allows logical connectives and, or, and not. Arithmetic expressions can be used in the comparison operators. The where Clause
16
The where clause specifies conditions that tuples in the relations in the from clause must satisfy. Find all loan numbers for loans made at the Perryridge branch with loan amounts greater than $1200. select loan-number from loan where branch-name=“Perryridge” and amount >1200 SQL allows logical connectives and, or, and not. Arithmetic expressions can be used in the comparison operators. The where Clause
17
The where Clause (Cont.) SQL includes the between operator for convenience. Find the loan number of those loans with loan amounts between $90,000 and $100,000 (that is, $90,000 and $100,000) select loan-number from loan where amount between 90000 and 100000
18
The from Clause The from clause corresponds to the Cartesian product operation of the relational algebra. Find the Cartesian product borrower loan select * from borrower, loan It is rarely used without a where clause. Find the name and loan number of all customers having a loan at the Perryridge branch. select distinct customer-name, borrower.loan-number from borrower, loan where borrower.loan-number = loan.loan-number and branch-name = “Perryridge”
19
The Rename Operation Renaming relations and attributes using the as clause: old-name as new-name Find the name and loan number of all customers having a loan at the Perryridge branch; replace the column name loan-number with the name loan-id. select distinct customer-name, borrower.loan-number as loan-id from borrower, loan where borrower.loan-number = loan.loan-number and branch-name = “Perryridge”
20
Set Operations The set operation union, intersect, and except operate on relations and correspond to the relational algebra operations , and . Each of the above operations automatically eliminates duplicate; to retain all duplicates use the corresponding multiset versions union all, intersect all and except all. Suppose a tuple occurs m times in r and n times in s, then, it occurs: –m + n times in r union all s –min(m,n) times in r intersect all s –max(0,m-n) times in r except all s
21
Set operations Find all customers who have a loan, an account, or both: (select customer-name from depositor) union (select customer-name from borrower) Find all customers who have both a loan and an account. (select customer-name from depositor) intersect (select customer-name from borrower) Find all customers who have an account but no loan. (select customer-name from depositor) except (select customer-name from borrower)
22
CS370 Spring 2007 Find sids of sailors who’ve reserved a red or a green boat sidsnameratingage 1Frodo722 2Bilbo239 3Sam827 Sailors Reserves bidbnamecolor 101Ninared 102Pintablue 103Santared 105Titanicgreen Boats sidbidday 11029/12 21039/13 41059/13 X sid 2 4 SELECT R.sid FROM Boats B,Reserves R WHERE(B.color=‘red’ OR B.color=‘green’) AND R.bid=B.bid AND, OR, UNION & INTERSECT
23
CS370 Spring 2007 SELECT R.sid FROM Boats B,Reserves R WHERE(B.color=‘red’ AND B.color=‘green’) AND R.bid=B.bid Find sids of sailors who’ve reserved a red and a green boat sidsnameratingage 1Frodo722 2Bilbo239 3Sam827 Sailors Reserves sidbidday 11019/12 21039/13 11059/13 X bidbnamecolor 101Ninared 102Pintablue 103Santared 105Titanicgree n Boats AND & OR
24
CS370 Spring 2007 SELECT R.sid FROM Boats B,Reserves R WHERE B.color = ‘red’ AND R.bid=B.bid INTERSECT SELECT R.sid FROM Boats B,Reserves R WHERE B.color = ‘green’ AND R.bid=B.bid Reserves sidbidday 11019/12 21039/13 11059/13 bidbnamecolor 101Ninared 102Pintablue 103Santared 105Titanicgree n Boats sid 1 2 1 = 1 Use INTERSECT instead of AND
25
CS370 Spring 2007 Reserves sidbidday 11029/12 21039/13 41059/13 bidbnamecolor 101Ninared 102Pintablue 103Santared 105Titanicgree n Boats sid 2 4 = 2 4 SELECT R.sid FROM Boats B, Reserves R WHERE B.color = ‘red’ AND R.bid=B.bid UNION SELECT R.sid FROM Boats B, Reserves R WHERE B.color = ‘green’ AND R.bid=B.bid Could also use UNION for the OR query
26
SELECT S.sid FROM Sailors S EXCEPT SELECT S.sid FROM Sailors S, Reserves R WHERE S.sid=R.sid Find sids of sailors who have not reserved a boat sidsnameratingage 1Frodo722 2Bilbo239 3Sam827 Reserves sidbidday 11029/12 21039/13 11059/13 Sailors First find the set of sailors who have reserved a boat… and then compare it with the rest of the sailors sid 3 ESCEPT: Set Difference
Similar presentations
© 2025 SlidePlayer.com. Inc.
All rights reserved.