Introduction to Data Manipulation in SQL CIS 4301 Lecture Notes Lecture /03/2006
Lecture 16© CIS Spring Simple Movie Database Schema Movie(Title,Year,length,inColor,studioName,producerC#) StarsIn(MovieTitle,MovieYear,StarName) MovieStar(Name,address,gender,birthdate) MovieExec(name,address,Cert#,netWorth) Studio(Name,address,presC#)
Lecture 16© CIS Spring Simple Queries in SQL SELECT FROM WHERE ORDER BY Asks for those tuples of one or more relation that satisfy a condition, order the output (default ascending) Note, most SQL implementations allow duplicate tuples, i.e., operate on multisets (bags) rather than sets
Lecture 16© CIS Spring SQL vs. Rel. Algebra SELECT A1, A2,..., A n FROM R1, R2,..., R m WHERE condition is equivalent to relational algebra query: A1, A2,..., An ( condition (R1 X R2 X... X R m )) Returns unnamed relation whose schema is (A1, A2,..., A n ) SQL is case insensitive Case makes a difference inside quotes!
Lecture 16© CIS Spring Selection and Projection Selection: Find all movies produced by Disney in 1990 SELECT * FROM Movie WHERE studioName = ‘Disney’ and year = ‘1990’; Projection: Find all movies produced by Disney in 1990 and return their title and length SELECT title, length FROM Movie WHERE studioName = ‘Disney’ and year = ‘1990’;
Lecture 16© CIS Spring Duplicates Retained unless use keyword DISTINCT Duplicate removal is an expensive operation This is a significant difference between SQL and “pure” relational algebra
Lecture 16© CIS Spring String Comparison Use any of the “usual” string comparison operators such as ‘ ’, ‘= ’, … More useful, pattern matching using “ like ” Two wildcards: ‘_’ and ‘%’ Find all movies whose title starts with ‘Star’ SELECT title FROM Movie WHERE title LIKE ‘Star%’; Find all movies with possessive (‘s) in their title SELECT title FROM Movie WHERE title LIKE ‘%’’s%’;
Lecture 16© CIS Spring Ordering Output Return tuples in sorted order (by default in ascending order) Use desc if you want descending order Find all movies produced by Universal in 1973 and return their title and length; sort movies in ascend. order by length then by title SELECT title, length FROM Movie WHERE studioName = ‘Universal’ AND year = ‘1973’ ORDER BY length, title;
Lecture 16© CIS Spring Renaming Attributes Use keyword AS in SELECT clause SELECT title AS Movie_Title, length AS Movie_Length FROM Movie WHERE studioName = ‘Disney’ and year = ‘1990’; SELECT title AS Title, length* AS Length, ‘hrs.’ AS inHours FROM Movie WHERE studioName = ‘Disney’ and year = ‘1990’;
Lecture 16© CIS Spring Cartesian Product Much of the power of rel. query language comes for the ability to combine two or more relations through cartesian product Form cartesian product by listing each relation in the FROM clause Consider pairs of tuples satisfying conditions in WHERE clause Find the name of the producer of ‘Star Wars’
Lecture 16© CIS Spring Disambiguating Attributes Method 1: Prefixing the relation name Find stars and movie executives with the same address SELECT MovieStar.name, MovieExec.name FROM MovieStar, MovieExec WHERE MovieStar.address = MovieExec.address; Output: MovieStar.nameMovieExec.name Jane FondaTed Turner
Lecture 16© CIS Spring Disambiguating Attributes Method 2: Tuple variable Find two stars with the same address SELECT Star1.name, Star2.name FROM MovieStar Star1, MovieStar Star2 WHERE Star1.address = Star2.address AND Star1.name< Star2.name; Output: Star1.nameStar2.name Alec BaldwinKim Basinger tuple variable Can use tuple variables anytime for convenience and readability!
Lecture 16© CIS Spring Disambiguating Attributes What would be different if we re-wrote the query as: SELECT Star1.name, Star2.name FROM MovieStar Star1, MovieStar Star2 WHERE Star1.address = Star2.address AND Star1.name <> Star2.name; Output: Star1.nameStar2.name Alec BaldwinKim Basinger Alec Baldwin
Lecture 16© CIS Spring Tuple Variables Tuples variables are always used (implicitly) in SELECT and WHERE clause If relation appears only once in the FROM clause, we use the relation name as its own tuple variable Relation name R in FROM clause is shorthand for R R
Lecture 16© CIS Spring Set Operators Union: union Intersection: intersect Set Difference: minus Eliminate duplicates Use ALL to retain duplicates: e.g., UNION ALL Find the names and addresses of movie stars who are not also movie execs
Lecture 16© CIS Spring Sample Queries Find the address of MGM studios. Find all stars that appeared either in a movie made in 1988 or a movie with “Dollar” in the title.
Lecture 16© CIS Spring Sample Queries Find the names and addresses of all female movie stars who are also movie executives with a net worth over $10M.
Lecture 16© CIS Spring Sample Queries Which movies are longer than “Star Wars?”
Lecture 16© CIS Spring Sample Queries What is the result of the following query if T is empty? SELECT * FROM Movie WHERE length 120;