SQL Query Examples Database Management COP4540, SCS, FIU.

Slides:



Advertisements
Similar presentations
Relational Calculus   Logic, like whiskey, loses its beneficial effect when taken in too large quantities. --Lord Dunsany.
Advertisements

16.3 Parser to Logical Query Plans SQL(not RAE) Figure 16.2 select distinct movietitle from starsIn where starname in (select name from moviestar.
Oracle Labs ECS 242, 342, 360 –You can connect from home to the machines in the lab. –E.g.: ssh u-knoppix.csc.uvic.ca Execute “sh” to use the proper shell.
SQL This presentation will cover: A Brief History of DBMS View in database MySQL installation.
Database Modifications CIS 4301 Lecture Notes Lecture /30/2006.
SQL reviews. Stored Procedures Create Procedure Triggers.
Relational Algebra (Continued)
Ex World War II ships Classes(class, type, country, numGuns, bore, displacement) Ships(name, class, launched) Battles (name, date) Outcomes (ship,
SQL Exercises – Part I April
Database Modifications, Data Types, Views. Database Modifications A modification command does not return a result as a query does, but it changes the.
Database Modifications, Data Types, Views. Database Modifications A modification command does not return a result as a query does, but it changes the.
Subqueries Example Find the name of the producer of ‘Star Wars’.
Indexes. An index on an attribute A of a relation is a data structure that makes it efficient to find those tuples that have a fixed value for attribute.
The Query Compiler Parses SQL query into parse tree Transforms parse tree into expression tree (logical query plan) Transforms logical query plan into.
Instructor: Amol Deshpande  Data Models ◦ Conceptual representation of the data  Data Retrieval ◦ How to ask questions of the database.
Query Compiler By:Payal Gupta Roll No:106(225) Professor :Tsau Young Lin.
CMSC424: Database Design Instructor: Amol Deshpande
The Query Compiler Section 16.3 DATABASE SYSTEMS – The Complete Book Presented By:Under the supervision of: Deepti KunduDr. T.Y.Lin.
SQL. 1.SQL is a high-level language, in which the programmer is able to avoid specifying a lot of data-manipulation details that would be necessary in.
CMSC424: Database Design Instructor: Amol Deshpande
CMSC424: Database Design Instructor: Amol Deshpande
SQL SQL is a very-high-level language, in which the programmer is able to avoid specifying a lot of data-manipulation details that would be necessary in.
Joins Natural join is obtained by: R NATURAL JOIN S; Example SELECT * FROM MovieStar NATURAL JOIN MovieExec; Theta join is obtained by: R JOIN S ON Example.
The Query Compiler 16.1 Parsing and Preprocessing Meghna Jain(205) Dr. T. Y. Lin.
CMSC424: Database Design Instructor: Amol Deshpande
Algebraic Laws. {P1,P2,…..} {P1,C1>...} parse convert apply laws estimate result sizes consider physical plans estimate costs pick best execute Pi answer.
CMSC424: Database Design Instructor: Amol Deshpande
SQL By: Toan Nguyen. Download Download the software at During the installation –Skip sign up for fast installation.
1 Relational Data Model CS 157B Nidhi Patel. 2 What is a Data Model? A notation for describing data or information A notation for describing data or information.
Chapter 6 The database Language SQL Spring 2011 Instructor: Hassan Khosravi.
Relational Algebra CIS 4301 Lecture Notes Lecture /28/2006.
CS 255: Database System Principles slides: From Parse Trees to Logical Query Plans By:- Arunesh Joshi Id:
Relational Algebra Spring 2012 Instructor: Hassan Khosravi.
Introduction to Data Manipulation in SQL CIS 4301 Lecture Notes Lecture /03/2006.
Introduction to Indexes. Indexes An index on an attribute A of a relation is a data structure that makes it efficient to find those tuples that have a.
CPS216: Advanced Database Systems Query Rewrite Rules for Subqueries Shivnath Babu.
THE DATABASE LANGUAGE SQL
Index Example From Garcia-Molina, Ullman, and Widom: Database Systems, the Complete Book pp
1 More SQL uDatabase Modification uDefining a Database Schema uViews.
Referential Integrity checks, Triggers and Assertions Examples from Chapter 7 of Database Systems: the Complete Book Garcia-Molina, Ullman, & Widom.
Advanced SQL Concepts - Checking of Constraints CIS 4301 Lecture Notes Lecture /6/2006.
© D. Wong Normalization  Purpose: process to eliminate redundancy in relations due to functional or multi-valued dependencies.  Decompose relation.
16.3 Parser to Logical Query Plans SQL(not RAE) Figure 16.2 select distinct movietitle from starsIn where starname in (select name from moviestar.
1 Algebra of Queries Classical Relational Algebra It is a collection of operations on relations. Each operation takes one or two relations as its operand(s)
CS 157B Database Systems Dr. T Y Lin. Updates 1.Red color denotes updated data (ppt) 2.Class participation will be part of “extra” credits to to “quiz.
1 Chapter 6 More SQL uDatabase Modification uDefining a Database Schema uViews.
SQL Exercises – Part I April
The Relational Model of Data Prof. Yin-Fu Huang CSIE, NYUST Chapter 2.
603 Database Systems Senior Lecturer: Laurie Webster II, M.S.S.E.,M.S.E.E., M.S.BME, Ph.D., P.E. Lecture 18 A First Course in Database Systems.
Practical Questions Movie (Title, Year, Length, inColor, StudioName, Producer) Give the SQL Statement that produced the outcome for the following query.
The Database Language SQL Prof. Yin-Fu Huang CSIE, NYUST Chapter 6.
CS 157B Database Systems Dr. T Y Lin. 1.2 Overview of a Database Management System Data-Definition Language Commands –Illustrated by three examples.
Databases : SQL Multi-Relations 2007, Fall Pusan National University Ki-Joune Li These slides are made from the materials that Prof. Jeffrey D. Ullman.
1 SQL: Concept and Usage. 2 SQL: an Overview SQL (Structured Query Language) –Also be pronounced as “sequel” –A relational database language –Consists.
1 Constraints and Triggers in SQL. 2 Constraints are conditions that must hold on all valid relation instances SQL2 provides a variety of techniques for.
Subqueries CIS 4301 Lecture Notes Lecture /23/2006.
Chap 5. The DB Language (SQL)
16.2.Algebraic Laws for Improving Query Plans
THE DATABASE LANGUAGE SQL
Introduction to Structured Query Language (SQL)
THE RELATIONAL MODEL OF DATA
Chap 2. The Relational Model of Data
The Query Compiler Parsing and Preprocessing. Meghna Jain(205)
THE RELATIONAL MODEL OF DATA
SQL: Concept and Usage.
SQL This presentation will cover: View in database MySQL installation
Algebraic Laws.
Deletion in AVL Tree There are 5 cases to consider.
CS4433 Database Systems SQL - Basics.
Query Compiler By:Payal Gupta Shirali Choksi Professor :Tsau Young Lin.
Presentation transcript:

SQL Query Examples Database Management COP4540, SCS, FIU

Exercise (1) Relations: Queries: 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#) Queries: a) Find the address of MGM studios. b) Find Sandra Bullock’s birthdate. c) Find all the stars that appear either in a movie made in 1980 or a movie with “Love” in the title. d) Find all executives worth at least $10,000,000. e) Find all the stars who either are male or live in Miami ( have Miami as a part of their address). Database Management COP4540, SCS, FIU

Answers (1) a) SELECT address FROM studio WHERE name = ‘MGM’; b) SELECT birthdate FROM moviestar WHERE name = ‘Sandra Bullock’; c) SELECT starName FROM StarsIn WHERE movieYear = 1980 OR movieTitle LIKE ‘%Love%’; SELECT starName FROM StarsIn WHERE movieYear = 1980 OR movieTitle LIKE 'Love %’ OR movieTitle LIKE '% Love %' OR movieTitle LIKE '% Love' OR movieTitle = 'Love'; d) SELECT name FROM MovieExec WHERE netWorth >= 10,000,000; e) SELECT name FROM MovieStar WHERE gender = ‘M’ OR address LIKE ’Miami %’ OR address LIKE ‘% Miami %’ OR address LIKE ‘% Miami’ OR address = ‘Miami’; Database Management COP4540, SCS, FIU

Exercise (2) Relations: Queries: 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#) Queries: a) Who were the male stars in Terms of Endearment. b) Which stars appeared in movies produced by MGM in 1995? c) Which movies are longer than Gone With the Wind? d) Which executives are worth more than Merv Griffin? Database Management COP4540, SCS, FIU

Answer (2) a) SELECT name FROM MovieStar, StarsIn WHERE gender = ‘M’ AND name = starName AND movieTitle = ‘Terms of Endearment’; b) SELECT starName FROM MovieStar, Movie WHERE title = movieTitle AND year = movieYear AND year = 1995 AND studioName = ‘MGM’; c) SELECT M1.title FROM Movie AS M1, Movie AS M2 WHERE M2.title = ‘Gone With the Wind’ AND M1.length > M2.length; SELECT title FROM Movie WHERE length > ANY ( SELECT length FROM Movie WHERE title = ‘Gone With the Wind’ ); d) SELECT M1.name FROM MovieExec AS M1, MovieExec AS M2 WHERE M2.name = ‘Mery Griffin’ AND M1.networth > M2.networth; Database Management COP4540, SCS, FIU

Exercise 3 Relations: Queries: Classes(class, type, country, numGuns, bore, displacement) Ships(name, class, launched) Battles(name, date) Outcomes(ship, battle, result) Queries: a) Find the countries whose ships had the largest number of guns. b) Find the classes of ships at least one of which was sunk in a battle. c) Find the names of the ships with a 16-inch bore. d) Find the battles in which ships of the Kongo class participated. e) Find the names of the ships whose number of guns was the largest for those ships of the same bore. Database Management COP4540, SCS, FIU

Answer (3-1) a) SELECT country FROM classes WHERE numGuns = (SELECT MAX(numGuns) from classes); SELECT country FROM classes WHERE numGuns >= ALL (SELECT numGuns from classes); b) SELECT DISTINCT class FROM Ships WHERE name IN (SELECT ship FROM Outcomes WHERE result = ‘sunk’); SELECT class FROM Ships WHERE EXISTS (SELECT * FROM Outcomes WHERE Ships.name = Outcomes.ship AND result = ‘sunk’); Database Management COP4540, SCS, FIU

Answer (3-2) c) SELECT name FROM ships WHERE class IN (SELECT class from classes where bore = 16); SELECT name FROM ships, classes WHERE ships.class = classes.class AND bore = 16; d) SELECT DISTINCT battle FROM ships, outcomes WHERE name = ship AND class = ‘Kongo’; SELECT DISTINCT battle FROM outcomes WHERE ship = ANY (SELECT name FROM ships WHERE class = ‘Kongo’); Database Management COP4540, SCS, FIU

Answer (3-3) e) SELECT name FROM ships, classes AS C1 WHERE ships.class = C1.class AND numGuns = (SELECT MAX(numGuns) FROM classes AS C2 WHERE C1.bore = C2.bore); Database Management COP4540, SCS, FIU

Exercise 4 Relations: Queries: Classes(class, type, country, numGuns, bore, displacement) Ships(name, class, launched) Battles(name, date) Outcomes(ship, battle, result) Queries: a) Find the number of battleship classes. b) Find the average number of guns of battleship classes. c) Find the average of guns of battleships. Note the difference between (b) and (c); do we weight a class by the number of ships of that class or not. d) Find for each class the year in which the first ship of that class was launched. e) Find for each class the number of ships of that class sunk in battle. Database Management COP4540, SCS, FIU

Answer (4-1) a) SELECT count(*) FROM classes WHERE type = ‘bc’; b) SELECT avg(numGuns) FROM classes c) SELECT avg(numGuns) FROM ships, classes WHERE ships.class =classes.class AND type = ‘bc’; d) SELECT class, launched FROM ships AS S1 WHRE launched <= ALL ( SELECT year FROM ships AS S2 WHERE S2.class = S1.class); Database Management COP4540, SCS, FIU

Answer (4-2) e) SELECT classes.class, count(*) FROM classes, ships, outcomes WHERE classes.class = ships.class AND ship = name AND result = ‘sunk’ GROUP BY classes.class; Database Management COP4540, SCS, FIU