SQL: Queries.

Slides:



Advertisements
Similar presentations
Life Satisfaction in Past, Present, and Future
Advertisements

SQL: The Query Language Part 2
SQL: The Query Language Part 1 R &G - Chapter 5 Life is just a bowl of queries. -Anon (not Forrest Gump)
Complex Integrity Constraints in SQL. Constraints over a Single Table Table Constraint: Create TABLE Sailors (sid INTEGER, sname CHAR(10), rating INTEGER,
1 Relational Algebra & Calculus Chapter 4, Part A (Relational Algebra) Query Formulation Exercise.
1 Database Systems 10.8, 2008 Lecture #4. 2 Course Administration Assignment #1 is due next Wed (outside 336/338). Course slides will now have black background.
TURKISH STATISTICAL INSTITUTE 1 /34 SQL FUNDEMANTALS (Muscat, Oman)
1 Advanced SQL Queries. 2 Example Tables Used Reserves sidbidday /10/04 11/12/04 Sailors sidsnameratingage Dustin Lubber Rusty.
1 TRC vs DRC 한욱신. 2 Queries in TRC and DRC  TRC Q = {t| f(t)} where t is a (free) tuple variable and f(t) is a well-formed formula  DRC.
Chapter (7): Advanced SQL
Relational Algebra Rohit Khokher. Relational Algebra Set Oriented Operations UnionIntersectionDifference Cartesian Product Relation Oriented Operations.
Database Management Systems 3ed, R. Ramakrishnan and J. Gehrke1 SQL: Queries, Programming, Triggers Chapter 5 Modified by Donghui Zhang.
1 Query-by-Example (QBE). 2 v A “GUI” for expressing queries. –Based on the Domain Relational Calulus (DRC)! –Actually invented before GUIs. –Very convenient.
Introduction to Database Systems 1 SQL: The Query Language Relation Model : Topic 4.
1 SQL: Structured Query Language (‘Sequel’) Chapter 5.
Schema BankWorld Customer(C_Name, C_Street, C_City) Depositor(Account_#, C_Name) Loan(Loan_#, B_Name, Amount) Borrower(C_Name, Loan_#)
CS 166: Database Management Systems
Database Management Systems 3ed, R. Ramakrishnan and J. Gehrke1 SQL: Queries, Constraints, Triggers Chapter 5.
SQL and Relational Algebra Zaki Malik September 02, 2008.
SQL.
SQL: The Query Language Jianlin Feng School of Software SUN YAT-SEN UNIVERSITY courtesy of Joe Hellerstein and etc for some slides.
SQL Part II: Advanced Queries. 421B: Database Systems - SQL Queries II 2 Aggregation q Significant extension of relational algebra q “Count the number.
Database Management Systems, R. Ramakrishnan and J. Gehrke1 SQL: Queries, Programming, Triggers Chapter 5.
CS 405G: Introduction to Database Systems
Database Management Systems, R. Ramakrishnan and J. Gehrke1 Query-by-Example (QBE) Chapter 6 Example is the school of mankind, and they will learn at no.
SQL (2).
CMPT 258 Database Systems SQL: Queries, Constraints, Triggers (Chapter 5) Part II home.manhattan.edu/~tina.tian.
SQL Review.
Database Management Systems, R. Ramakrishnan and J. Gehrke1 SQL: Queries, Programming, Triggers Chapter 5.
1 SQL: Structured Query Language (‘Sequel’) Chapter 5.
1 SQL (Simple Query Language). 2 Query Components A query can contain the following clauses –select –from –where –group by –having –order by Only select.
FALL 2004CENG 351 File Structures and Data Management1 SQL: Structured Query Language Chapter 5.
Rutgers University SQL: Queries, Constraints, Triggers 198:541 Rutgers University.
1 Rewriting Minus Queries Using Not In SELECT S.sname FROM Sailors S, Boats B, Reserves R WHERE S.sid = R.sid and R.bid = B.bid and B.color = ‘red’ MINUS.
Database Management Systems 3ed, R. Ramakrishnan and J. Gehrke1 SQL: Queries, Constraints, Triggers Chapter 5.
1 Chapter 5 SQL: QUERIES, CONSTRAINTS, TRIGGERS. 2 INTRODUCTION - The current presentation is consistent with both SQL-92 and SQL: 99 (differences will.
1 The Oracle Database System Querying the Data Database Course The Hebrew University of Jerusalem.
1 SQL: Structured Query Language Chapter 5. 2 SQL and Relational Calculus relationalcalculusAlthough relational algebra is useful in the analysis of query.
1 Rewriting Intersect Queries Using In SELECT S.sid FROM Sailors S, Boats B, Reserves R WHERE S.sid = R.sid and R.bid = B.bid and B.color = ‘red’ INTERSECT.
Chapter 5.  Data Manipulation Language (DML): subset of SQL which allows users to create queries and to insert, delete and modify rows.  Data Definition.
CSC 411/511: DBMS Design Dr. Nan WangCSC411_L6_SQL(1) 1 SQL: Queries, Constraints, Triggers Chapter 5 – Part 1.
Instructor: Jinze Liu Fall Basic Components (2) Relational Database Web-Interface Done before mid-term Must-Have Components (2) Security: access.
SQL Examples CS3754 Class Note 11 CS3754 Class Note 11, John Shieh,
SQL 2 Introduction Structured Query Language (SQL): the most widely used commercial relational database language Originally.
ICS 321 Fall 2009 SQL: Queries, Constraints, Triggers Asst. Prof. Lipyeow Lim Information & Computer Science Department University of Hawaii at Manoa 9/8/20091Lipyeow.
More SQL Today:  Nested Queries  More SQL Nested Queries in SQL  Queries containing other queries  Inner query:  Can appear in FROM or WHERE clause.
1 Database Systems ( 資料庫系統 ) October 24, 2005 Lecture #5.
1 SQL: Queries, Constraints, Triggers Chapter 5. 2 Example Instances R1 S1 S2  We will use these instances of the Sailors and Reserves relations in our.
ICS 321 Spring 2011 The Database Language SQL (iii) Asst. Prof. Lipyeow Lim Information & Computer Science Department University of Hawaii at Manoa 3/14/20111Lipyeow.
Introduction to SQL ; Christoph F. Eick & R. Ramakrishnan and J. Gehrke 1 Using SQL as a Query Language COSC 6340.
CMPT 258 Database Systems SQL Queries (Chapter 5).
Database Management Systems 3ed, R. Ramakrishnan and J. Gehrke1 Database Management Systems Chapter 5 SQL.
1 SQL: The Query Language (Part II). 2 Expressions and Strings v Illustrates use of arithmetic expressions and string pattern matching: Find triples (of.
1 SQL: Structured Query Language (‘Sequel’) Chapter 5.
CSCI 4333 Database Design and Implementation – Exercise (2) Xiang Lian The University of Texas Rio Grande Valley Edinburg, TX 78539
1 SQL: The Query Language. 2 Example Instances R1 S1 S2 v We will use these instances of the Sailors and Reserves relations in our examples. v If the.
Database Management Systems 1 Raghu Ramakrishnan SQL: Queries, Programming, Triggers Chpt 5 Jianping Fan.
1 CS122A: Introduction to Data Management Lecture 9 SQL II: Nested Queries, Aggregation, Grouping Instructor: Chen Li.
DataBase - Check 01 DataBase 2 nd year Computer Science & Engineer 1.
COP Introduction to Database Structures
© פרופ' יהושע שגיב, האוניברסיטה העברית
01/31/11 SQL Examples Edited by John Shieh CS3754 Classnote #10.
COP Introduction to Database Structures
Database Systems October 14, 2009 Lecture #5.
Database Systems (資料庫系統)
Database Applications (15-415) SQL-Part II Lecture 9, February 04, 2018 Mohammad Hammoud.
CSCI 6315 Applied Database Systems – Exercise (3)
CS4222 Principles of Database System
SQL: Structured Query Language
Textbook Chapter 9 (incl. example queries at end)
Presentation transcript:

SQL: Queries

Relational Schema Sailors (sid, sname, rating, age) Boats (bid, bname, color) Reserves (sid, bid, day)

Q11 Find the sailors with a rating above 7. SELECT S.sid, S.sname, S.rating, S.age FROM Sailors S WHERE S.rating > 7;

Q16 Find the sids of sailors who have reserved a red boat. SELECT R.sid FROM Boats B, Reserves R WHERE B.bid = R.bid AND B.color = ‘red’;

Q3 Find the colors of boats reserved by Lubber. SELECT B.color FROM Sailors S, Reserves R, Boats B WHERE S.sid = R.sid AND R.bid = B.bid AND S.sname = ‘Lubber’;

Q4 Find the names of sailors who have reserved at least one boat. SELECT S.sname FROM Sailors S, Reserves R WHERE S.sid = R.sid;

Q17 Compute increments for the ratings of persons who have sailed two different boats on the same day. SELECT S.sname, S.rating + 1 AS rating FROM Sailors S, Reserves R1, Reserves R2 WHERE S.sid = R1.sid AND S.sid = R2.sid AND R1.day = R2.day AND R1.bid <> R2.bid;

Q18 Find the ages of sailors whose name begins and ends with B and has at least three characters. SELECT S.age FROM Sailors S WHERE S.sname LIKE ‘B_%B’;

Q20 Find all sids of sailors who have a rating of 10 or have reserved boat 104. SELECT S.sid FROM Sailors S WHERE S.rating = 10 UNION SELECT R.sid FROM Reserves R WHERE R.bid = 104

Q21 Find the names of sailors who have not reserved a red boat. SELECT S.sname FROM Sailors S WHERE S.sid NOT IN (SELECT R.sid FROM Reserves R WHERE R.bid IN (SELECT B.bid FROM Boats B WHERE B.color = ‘red’));

Q9 Find the names of sailors who have reserved all boat. SELECT S.sname FROM Sailors S WHERE NOT EXISTS ((SELECT B.bid FROM Boats B) EXCEPT (SELECT R.bid FROM Reserves R WHERE R.sid = S.sid));

Q9 : Second Solution Find the names of sailors who have reserved all boats. SELECT S.sname FROM Sailors S WHERE NOT EXISTS ((SELECT B.bid FROM Boats B) WHERE NOT EXISTS (SELECT R.bid FROM Reserves R WHERE R.bid = B.bid AND R.sid = S.sid));

Q28 Count the number of sailors. SELECT COUNT (*) FROM Sailors S;

Q29 Count the number of different sailor names. SELECT COUNT (DISTINCT S.sname) FROM Sailors S;

Q30 Find the names of sailors who are older than the oldest sailor with a rating of 10. SELECT S.sname FROM Sailors S WHERE S.age > (SELECT MAX (S2.age) FROM Sailors S2 WHERE S2.rating = 10);

Q30 : Second Solution Find the names of sailors who are older than the oldest sailor with a rating of 10. SELECT S.sname FROM Sailors S WHERE S.age > ALL (SELECT S2.age) FROM Sailors S2 WHERE S2.rating = 10); Note: error prone (“any” could be mistakenly used instead of “all”)

Q33 For each red boat, find the number of reservations for this boat. SELECT B.bid, COUNT (*) AS sailorcount FROM Boats B, Reserves R WHERE R.bid = B.bid AND B.color = ‘red’ GROUP BY B.bid;

Q33: Second Solution For each red boat, find the number of reservations for this boat. SELECT B.bid, COUNT (*) AS sailorcount FROM Boats B, Reserves R WHERE R.bid = B.bid GROUP BY B.bid HAVING B.color = ‘red’; Note: This is incorrect because only columns that appears in the GROUP BY clause can appear in the HAVING clause, unless they appear as arguments to an aggregate operator in the HAVING clause.

Q34 Find the average age of sailors for each rating level that has at least two sailors. SELECT S.rating, AVG (S.age) AS averageAge FROM Sailors S GROUP BY S.rating HAVING COUNT (*) >1;

Q34: Second Solution Find the average age of sailors for each rating level that has at least two sailors. SELECT S.rating, AVG (S.age) AS averageAge FROM Sailors S GROUP BY S.rating HAVING 1 < (SELECT COUNT (*) FROM Sailors S2 WHERE S.rating = S2.rating);

Q35 Find the average age of sailors who are of voting age (i.e. at least 18 years old) for each rating level that has at least two sailors. SELECT S.rating, AVG (S.age) AS averageAge FROM Sailors S WHERE S.age >= 18 GROUP BY S.rating HAVING 1 < (SELECT COUNT (*) FROM Sailors S2 WHERE S.rating = S2.rating);

Q35: Second Solution? Find the average age of sailors who are of voting age (i.e. at least 18 years old) for each rating level that has at least two sailors. SELECT S.rating, AVG (S.age) AS averageAge FROM Sailors S WHERE S.age >= 18 GROUP BY S.rating HAVING COUNT (*) > 1; INCORRECT

Q36 Find the average age of sailors who are of voting age (i.e. at least 18 years old) for each rating level that has at least two such sailors. SELECT S.rating, AVG (S.age) AS averageAge FROM Sailors S WHERE S.age >= 18 GROUP BY S.rating HAVING COUNT (*) > 1;

Q36: Second Solution Find the average age of sailors who are of voting age (i.e. at least 18 years old) for each rating level that has at least two such sailors. SELECT S.rating, AVG (S.age) AS averageAge FROM Sailors S WHERE S.age >= 18 GROUP BY S.rating HAVING 1 < (SELECT COUNT (*) FROM Sailors S2 WHERE S.rating = S2.rating AND S2.age >= 18);

Q36: Third Solution Find the average age of sailors who are of voting age (i.e. at least 18 years old) for each rating level that has at least two such sailors. SELECT Temp.rating, Temp.averageAge FROM (SELECT S.rating, AVG (S.age) AS averageAge, COUNT (*) AS ratingcount FROM Sailors S WHERE S.age >= 18 GROUP BY S.rating) AS Temp) WHERE Temp.ratingcount > 1;

Q37 Find those ratings for which the average age of sailors is the minimum over all ratings. SELECT Temp.rating, Temp.averageAge FROM (SELECT S.rating, AVG (S.age) AS averageAge, FROM Sailors S GROUP BY S.rating) AS Temp) WHERE Temp.averageAge = (SELECT MIN (Temp.averageAge) FROM TEMP);

Q37: Second Solution? Find those ratings for which the average age of sailors is the minimum over all ratings. SELECT Temp.rating, MIN (Temp.averageAge) FROM (SELECT S.rating, AVG (S.age) AS averageAge, FROM Sailors S GROUP BY S.rating) AS Temp) GROUP BY Temp.rating;