SQL © 1 פרופ' יהושע שגיב ,האוניברסיטה העברית Basic SQL Query SELECT [Distinct] target-list FROM from-list WHERE condition; •from-list: A list of relation names (possibly with a rangevariable after each name) •target-list: A list of fields of relations in relation-list •condition: A boolean condition •DISTINCT: Optional keyword to delete duplicates 2 Basic SQL Query (continues) SELECT [Distinct] target-list FROM from-list WHERE condition; The result evaluation: 1. 2. 3. 4. Compute the cross product of the tables in from-list. Delete all rows that do not satisfy condition. Delete all columns that do not appear in target-list. If Distinct is specified eliminate duplicate rows. 3 Basic SQL Query (continues) SELECT Distinct A1,…,An FROM R1,…,Rm WHERE C; This translates to the expression in relational algebra: A1,…,An (C(R1 x…x Rm)) 4 Example Tables Used Sailors sid sname rating age 22 31 58 Dustin 7 Lubber 8 Rusty 10 45.0 55.5 35.0 bid 101 Nancy 103 Gloria sid Reserves bid Day 22 58 101 103 Key Boats bname color 10/10/02 11/12/02 red green Key 5 Boat Names and Reservation Dates SELECT bname,day FROM Boats,Reserves WHERE Boats.bid = Reserves.bid 6 Boat Names and Reservation Dates SELECT bname,day FROM Boats,Reserves WHERE Boats.bid = Reserves.bid sid Reserves bid day Boats bname color bid 22 22 101 101 10/10/02 10/10/02 101 Nancy 103 Gloria red green 58 58 103 103 11/12/02 11/12/02 101 Nancy 103 Gloria red green 7 Boat Names and Reservation Dates SELECT bname,day FROM Boats,Reserves WHERE Boats.bid = Reserves.bid Reserves Boats sid bid day bid bname color 22 101 10/10/02 101 Nancy red 58 103 11/12/02 103 Gloria green 8 Boat Names and Reservation Dates SELECT bname,day FROM Boats,Reserves WHERE Boats.bid = Reserves.bid Reserves Boats day bname 10/10/02 Nancy 11/12/02 Gloria 9 Sailors Who Reserved Boat 103 SELECT sname FROM Sailors, Reserves WHERE Sailors.sid = Reserves.sid and bid = 103; sname(Sailors.sid = Reserves.sid bid = 103 (Sailors x Reserves)) 10 Sailors Who Reserved Boat 103 Sailors x Reserves Sailors Reserves sid sname rating age sid bid day 22 Dustin 7 45.0 22 101 10/10/02 22 31 31 58 58 Dustin Lubber Lubber Rusty Rusty 7 8 8 10 10 45.0 55.5 55.5 35.0 35.0 58 22 58 22 58 103 101 103 101 103 11/12/02 10/10/02 11/12/02 10/10/02 11/12/02 11 Sailors Who Reserved Boat 103 Sailors.sid = Reserves.sid bid = 103 Sailors Reserves sid sname rating age sid bid day 22 Dustin 7 45.0 22 101 10/10/02 22 31 31 58 58 Dustin Lubber Lubber Rusty Rusty 7 8 8 10 10 45.0 55.5 55.5 35.0 35.0 58 22 58 22 58 103 101 103 101 103 11/12/02 10/10/02 11/12/02 10/10/02 11/12/02 12 Sailors Who Reserved Boat 103 sname Sailors Reserves sid sname rating age sid bid day 22 Dustin 7 45.0 22 101 10/10/02 22 31 31 58 58 Dustin Lubber Lubber Rusty Rusty 7 8 8 10 10 45.0 55.5 55.5 35.0 35.0 58 22 58 22 58 103 101 103 101 103 11/12/02 10/10/02 11/12/02 10/10/02 11/12/02 13 Range Variables SELECT S.sname FROM Sailors S, Reserves R WHERE S.sid = R.sid and R.bid = 103; • Range variables are good style • They are necessary if the same relation appears twice in the FROM clause 14 A Few SELECT Options Select all columns: SELECT * FROM Sailors S; Rename selected columns: SELECT S.sname AS Sailors_Name FROM Sailors S; 15 A Few SELECT Options Applying functions: Mathematical manipulations: SELECT (age-5)*2 FROM Sailors S; Aggregate functions: SELECT COUNT(*) FROM Sailors S; SELECT MAX(age) FROM Sailors S; 16 The WHERE Clause Numerical and string comparison: !=,<>,=, <, >, >=, <=, between(between val1 AND val2) String comparisson is according to the alphabetical order! Logical components: AND, OR Null verification: IS NULL, IS NOT NULL Example: SELECT sname FROM Sailors WHERE age>=40 AND rating IS NOT NULL ; 17 The WHERE Clause (continues) The LIKE operator: A pattern matching operator Basic format: colname LIKE pattern Example: SELECT sid FROM Sailors WHERE sname LIKE ‘B_%g’; _ is a single character % is 0 or more characters 18 Sailors Who’ve Reserved a Boat SELECT sname FROM Sailors S, Reserves R WHERE S.sid = R.sid; Would adding DISTINCT give a different result? 19 Exercise Formulate a query that finds the names of sailors who reserved a yellow boat. sid 22 31 58 Sailors sname rating Dustin 7 Lubber 8 Rusty 10 Boats age 45.0 55.5 35.0 bid bname color 101 103 Nancy Gloria red green sid Reserves bid Day 22 58 101 103 10/10/02 11/12/02 20 A Harder Exercise 1. Formulate a query that finds the bid of boats that are reserved in at least two different days 2. Fix the query you formulated to find the names of these boats Hint: A relation may appear more than once in the FROM list… 21 Union, Intersect and Except Sqlplus supports the union, intersection and difference operators The syntax: query1 UNION query2; query1 INTERSECT query2; query1 MINUS query2; 22 Sailors who’ve reserved a red or green boat SELECT S.sname FROM Sailors S, Boats B, Reserves R WHERE S.sid = R.sid and R.bid = B.bid and (B.color = ‘red’ or B.color = ‘green’); sname(color = ‘red’ color = ‘green’ (Sailors ⋈ Reserves ⋈ Boats)) What would happen if we replaced or by and ? 23 Sailors who’ve reserved red or green boat SELECT S.sname FROM Sailors S, Boats B, Reserves R WHERE S.sid = R.sid and R.bid = B.bid and B.color = ‘red’ UNION SELECT S.sname FROM Sailors S, Boats B, Reserves R WHERE S.sid = R.sid and R.bid = B.bid and B.color = ‘green’; 24 The Second Version in Relational Algebra sname(color = ‘red’ (Sailors ⋈ Reserves ⋈ Boats)) sname(color = ‘green’ (Sailors ⋈ Reserves ⋈ Boats)) 25 Nested Queries A query may be nested within another through the following operators: (NOT) IN (NOT) EXISTS ANY ALL A query with nested queries is computed using nested loops 26 The IN Operator Names of sailors who’ve reserved boat 103: SELECT S.sname FROM Sailors S WHERE S.sid IN (SELECT FROM WHERE What would happen if we wrote NOT IN? R.sid Reserves R R.bid = 103); 27 The EXISTS Operator Names of sailors who’ve reserved boat 103: SELECT S.sname FROM Sailors S WHERE EXISTS (SELECT FROM WHERE Notice the correlation between the examined row and the inner query What would happen if we wrote NOT EXISTS? * Reserves R R.bid = 103 and S.sid = R.sid); 28 Set-Comparison Queries: ANY,ALL Sailors who are not the youngest: SELECT * FROM Sailors S1 WHERE S1.age > ANY (SELECT S2.age FROM Sailors S2); We can also use op ALL (op is >, <, =, >=, <=, or <>) 29 Another Exercise What does the following query compute? SELECT S.sname FROM Sailors S, Boats B, Reserves R WHERE S.sid = R.sid and R.bid = B.bid and B.color = ‘red’ INTERSECT SELECT S.sname FROM Sailors S, Boats B, Reserves R WHERE S.sid = R.sid and R.bid = B.bid and B.color = ‘green’; Using IN and INTERSECT operators, formulate a query that finds the names of the sailors who reserved both green and red boats. 30 Another Exercise (continues) What does the following query compute? SELECT S.sname FROM Sailors S WHERE NOT EXISTS ( (SELECT B.bid FROM Boats B) MINUS (SELECT R.bid FROM Reserves R WHERE R.sid=S.sid) ) ; 31 For The Brave Ones… 1. 2. Formulate a query that computes the sid of all sailors who have not reserved a green boat. Prove formally that in the Sailors-Reserves-Boats database, the query of question 1 cannot be as simple as a query of the form: SELECT Vj.sid FROM R1 V1, R2 V2…Rn Vn WHERE C Where C is a simple condition (contains only comparison clauses, AND and OR, and does not contain nested queries) Notice that the records in each table are arbitrary! 32
© Copyright 2026 Paperzz