Presentation is loading. Please wait.

Presentation is loading. Please wait.

CS4222 Principles of Database System

Similar presentations


Presentation on theme: "CS4222 Principles of Database System"— Presentation transcript:

1 CS4222 Principles of Database System
1/11/2019 CS4222 Principles of Database System 11. SQL Huiping Guo Department of Computer Science California State University, Los Angeles

2 Outline Basic queries (only one table) Joins Subqueries Set operations
1/11/2019 Outline Basic queries (only one table) Joins Subqueries Set operations DML 11. SQL CS4222_Su17

3 Retrieval Queries in SQL
1/11/2019 Retrieval Queries in SQL SQL has one basic statement for retrieving information from a database; the SELECT statement This is not the same as the SELECT operation of the relational algebra Important distinction between SQL and the formal relational model: SQL allows a table (relation) to have two or more tuples that are identical in all their attribute values Hence, an SQL relation (table) is a multi-set (sometimes called a bag) of tuples; it is not a set of tuples SQL relations can be constrained to be sets by specifying PRIMARY KEY or UNIQUE attributes, or by using the DISTINCT option in a query 11. SQL CS4222_Su17

4 Retrieval Queries in SQL
1/11/2019 Retrieval Queries in SQL A bag or multi-set is like a set, but an element may appear more than once. Example: {A, B, C, A} is a bag. {A, B, C} is also a bag that also is a set. Bags also resemble lists, but the order is irrelevant in a bag. Example: {A, B, A} = {B, A, A} as bags However, [A, B, A] is not equal to [B, A, A] as lists 11. SQL CS4222_Su17

5 Retrieval Queries: SELECT-FROM-WHERE block
1/11/2019 Retrieval Queries: SELECT-FROM-WHERE block Basic form of the SQL SELECT statement is called a mapping or a SELECT-FROM-WHERE block SELECT <attribute list> FROM <table list> WHERE <condition> <attribute list> is a list of attribute names whose values are to be retrieved by the query <table list> is a list of the relation names required to process the query <condition> is a conditional (Boolean) expression that identifies the tuples to be retrieved by the query 11. SQL CS4222_Su17

6 Example database Sailors Boats Reserves
(sid:integer, sname:string, rating:integer, age:real) Boats (bid:integer, bname:string, color:string) Reserves (sid:integer, bid:integer, day:date) 11. SQL CS4222_Su17

7 Basic queries Basic format Distinct, Order by and Like
Aggregate functions Group by and Having 11. SQL CS4222_Su17

8 Basic format SELECT att1, att2,… FROM table1 WHERE conditions Ex1: Find the names of the sailors whose rating is greater than 5 11. SQL CS4222_Su17

9 Example Ex1: Find the names of the sailors
whose rating is greater than 5 SELECT sname FROM Sailors WHERE rating>5 11. SQL CS4222_Su17

10 1/11/2019 USE OF * To retrieve all the attribute values of the selected tuples, an * is used, which stands for all the attributes Examples: SELECT * FROM Boats WHERE color = 'red' 11. SQL CS4222_Su17

11 DISTINCT: remove duplicates
SQL does not treat a relation as a set; duplicate tuples can appear To eliminate duplicate tuples in a query result, the keyword DISTINCT is used Ex2: Find the names (without duplicates) of the sailors whose rating is greater than 5 SELECT DISTINCT sname FROM Sailors WHERE rating > 5 11. SQL CS4222_Su17

12 Order by: sort the results
Ex3: Find the names and the ratings of the sailors whose rating is greater than 2. Order the results by the rating SELECT sname, rating FROM Sailors WHERE rating>2 ORDER BY rating 11. SQL CS4222_Su17

13 More on ORDER BY Not SORT BY The default is ASC
Use DESC if you want descending order ORDER BY rating DESC Order by multiple attributes ORDER BY rating DESC, name DESC 11. SQL CS4222_Su17

14 LIKE: Pattern matching
Wildcards _: matches any single character %: matches any string of zero or more characters 'abc' LIKE 'abc' true 'abc' LIKE 'a%' true 'abc' LIKE '_b_' true 'abc' LIKE 'c' false 11. SQL CS4222_Su17

15 LIKE: example Ex4: Find the boats with ‘un’ anywhere in the boat’s name SELECT * FROM Boats WHERE bname LIKE '%un%' 11. SQL CS4222_Su17

16 LIKE: example Ex5: Find the boats with ‘i’ in the second left most character of the boat name SELECT * FROM Boats WHERE bname LIKE '_i%' Is the following query correct? FROM WHERE bname = '_i%' 11. SQL CS4222_Su17

17 Aggregate functions Max() Min() Avg() Count() Sum()
11. SQL CS4222_Su17

18 Aggregate functions: example
Ex6. Find the number of sailor’s , the maximum, minimum and average rating SELECT Count(*), Max(rating), Min(rating), Avg(rating) FROM Sailors 11. SQL CS4222_Su17

19 Group By Is the following query correct?
Select rating, Count(*) From Sailors When attributes and aggregation functions are listed together in the Select clause, Group by must be used. Group by rating 11. SQL CS4222_Su17

20 Having: remove groups Always used with Group by
Ex7. For each rating, report the number of sailors. Only Keep the rating with the number of sailors greater than 1 Select rating, Count(*) From Sailors Group by rating Having count(*) > 1 11. SQL CS4222_Su17

21 Having: remove groups The differences between WHERE and HAVING
Where: remove tuples to be aggregated Having: final filter, remove groups Ex8. For each rating, report the number of sailors whose sname contains ‘s’. Only Keep the rating with the number of such sailors greater than 1 Select rating, Count(*) From Sailors Where sname LIKE ‘%s%’ Group by rating Having count(*) > 1 11. SQL CS4222_Su17

22 Joins Equi Join Inner Join Natural Join Outer join Left join
Right join Produce the same results The syntax is different 11. SQL CS4222_Su17

23 Ambiguous attribute names
1/11/2019 Ambiguous attribute names In SQL, we can use the same name for two (or more) attributes as long as the attributes are in different relations A query that refers to two or more attributes with the same name must qualify the attribute name with the relation name by prefixing the relation name to the attribute name Example: Sailors.sid, Reserves.sid Can also use the AS keyword to specify aliases AS can be omitted 11. SQL CS4222_Su17

24 Equi Join Ex9. Find the names of sailors who have reserved boat number 10 SELECT S.sname FROM Sailors S, Reserves R WHERE S.sid=R.sid And R.bid=10 11. SQL CS4222_Su17

25 UNSPECIFIED WHERE-clause
1/11/2019 UNSPECIFIED WHERE-clause A missing WHERE-clause indicates no condition; hence, all tuples of the relations in the FROM-clause are selected This is equivalent to the condition WHERE TRUE If more than one relation is specified in the FROM-clause and there is no join condition, then the CARTESIAN PRODUCT of tuples is selected 11. SQL CS4222_Su17

26 UNSPECIFIED WHERE-clause
1/11/2019 UNSPECIFIED WHERE-clause Example: SELECT S.sname FROM Sailors S, Resvers R It is extremely important not to overlook specifying any selection and join conditions in the WHERE-clause; otherwise, incorrect and very large relations may result 11. SQL CS4222_Su17

27 Inner join Ex9. Find the names of sailors who have reserved boat number 10 SELECT S.sname FROM Sailors S INNER JOIN Reserves R ON S.sid=R.sid WHERE R.bid=10 11. SQL CS4222_Su17

28 Natural join Ex9. Find the names of sailors who have reserved boat number 10 SELECT S.sname FROM Sailors S NATURAL JOIN Reserves R WHERE R.bid=10 11. SQL CS4222_Su17

29 Outer join Left join Right join
Report all of the records of the first (left) of two tables plus matching records in the second (right) table. Right join Report all of the records of the second (right) of two tables plus matching records in the first (left) table 11. SQL CS4222_Su17

30 Left Join example Ex10. Report the names of all sailors and the boat ids (if any) they’ve reserved. Select S.sname, R.bid From Sailors S LEFT JOIN Reserves R ON S.sid = R.sid From Sailors S NATURAL LEFT JOIN Reserves R 11. SQL CS4222_Su17

31 Inner Join vs. Out Join Select sname, bid Select sname, bid
sid sname rating age 22 Dustin 7 45 29 Brutus 1 33 31 Lubber 8 55 sid bid day 22 101 1/1/98 102 31 103 1/8/98 sname bid Dustin 101 102 Brutus NULL Lubber 103 Select sname, bid From Sailors NATURAL LEFT JOIN Boats sname bid Dustin 101 102 Lubber 103 Select sname, bid From Sailors NATURAL JOIN Boats 11. SQL CS4222_Su17

32 Null Values Field values in a tuple are sometimes Test the NULL value
1/11/2019 Null Values Field values in a tuple are sometimes unknown (e.g., a rating has not been assigned) inapplicable (e.g., no spouse’s name). Test the NULL value IS NULL IS NOT NULL 11. SQL CS4222_Su17

33 What is a subquery? A complete SELECT query, called a subquery, can be specified within the WHERE-clause of another query, called the outer query Where to put a subquery? Where Having Select From 11. SQL CS4222_Su17

34 Subqueries in WHERE Ex.11 Find the name of the oldest sailor
Is this query correct? Select sname, max(age) From Sailors Select sname Where age = (Select max(age) From Sailors) 11. SQL CS4222_Su17

35 Another subquery example
In/NOT IN usually used with subqueries Ex.9 Find the name of sailors who have reserved boat 10 Select sname From Sailors S Where S.sid IN (Select R.sid From Reserves R Where R.bid=10) 11. SQL CS4222_Su17

36 Subqueries in Having Ex.12 Find the rating with the maximum number of sailors Select rating, count(*) From Sailors S Group by rating Having count(*) >=ALL (Select count(*) From Sailors Group by rating) 11. SQL CS4222_Su17


Download ppt "CS4222 Principles of Database System"

Similar presentations


Ads by Google