1 Exercise 1  Given the following tables: employee (person_name, street, city)‏ works (person_name, company_name, salary)‏ company (company_name, city)‏

Slides:



Advertisements
Similar presentations
Advanced SQL (part 1) CS263 Lecture 7.
Advertisements

Review Session Monday, Oct 8 Shipra Agrawal. Announcements New Gradiance assignment deadline Wednesday, Oct 10 Please read FAQs for assignments.
Relational Algebra and SQL Exercises
TURKISH STATISTICAL INSTITUTE 1 /34 SQL FUNDEMANTALS (Muscat, Oman)
Ver 1,12/09/2012Kode :CCs 111,sistem basisdataFASILKOM Chapter 2: Relational Model Database System Concepts, 5th Ed. ©Silberschatz, Korth and Sudarshan.
Copyright © 2007 Ramez Elmasri and Shamkant B. Navathe Chapter 6 (Continued) The Relational Algebra and Calculus.
SQL Subqueries Objectives of the Lecture : To consider the general nature of subqueries. To consider simple versus correlated subqueries. To consider the.
CREATE VIEW SYNTAX CREATE VIEW name [(view_col [, view_col …])] AS [WITH CHECK OPTION];
4 การใช้ SQL Functions. Copyright © 2007, Oracle. All rights reserved What Are Group Functions? Group functions operate on sets of rows to give.
INTRODUCTION TO DATABASE MANAGEMENT SYSTEMS Dr. Adam Anthony Fall 2012.
Chapter 2 Relational Model (part II) Hankz Hankui Zhuo
SQL Neyha Amar CS 157A, Fall Inserting The insert statement is used to add a row of data into a table Strings should be enclosed in single quotes,
DB opgaven 2 Opgaven 2 Geert-Jan Houben.
SQL SQL stands for Structured Query Language SQL allows you to access a database SQL is an ANSI standard computer language SQL can execute queries against.
Slides adapted from A. Silberschatz et al. Database System Concepts, 5th Ed. SQL - part 2 - Database Management Systems I Alex Coman, Winter 2006.
Tutorial 5 Multi-table queries. Tutorial 5 objectives Displaying Data from Multiple Tables –[ ]Write SELECT statements to access data from more than one.
Correlated Queries SELECT title FROM Movie AS Old WHERE year < ANY (SELECT year FROM Movie WHERE title = Old.title); Movie (title, year, director, length)
Relational algebra SHIRAJ MOHAMED M | MIS 1. Relational algebra Notation SHIRAJ MOHAMED M | MIS 2.
©Silberschatz, Korth and Sudarshan4.1Database System Concepts Chapter 4: SQL Basic Structure Set Operations Aggregate Functions Null Values Nested Subqueries.
Chapter 3 Single-Table Queries
Relational Model Concepts. The relational model represents the database as a collection of relations. Each relation resembles a table of values. A table.
Oracle Database Administration Lecture 2 SQL language.
1 ICS 184: Introduction to Data Management Lecture Note 10 SQL as a Query Language (Cont.)
1 CS 430 Database Theory Winter 2005 Lecture 12: SQL DML - SELECT.
1 Single Table Queries. 2 Objectives  SELECT, WHERE  AND / OR / NOT conditions  Computed columns  LIKE, IN, BETWEEN operators  ORDER BY, GROUP BY,
Query Processing: Relational Algebra
Structured Query Language. Group Functions What are group functions ? Group Functions Group functions operate on sets of rows to give one result per group.
Oracle DML Dr. Bernard Chen Ph.D. University of Central Arkansas.
Advanced Database Systems
IS 230Lecture 6Slide 1 Lecture 7 Advanced SQL Introduction to Database Systems IS 230 This is the instructor’s notes and student has to read the textbook.
1Database Management Systems Exercise II  Consider the following relational schemas:  employee (person_name, street, city)  company (company_name, city)
1 CSCE Database Systems Anxiao (Andrew) Jiang The Database Language SQL.
COMP3030 Database Management System Final Review
Relational Algebra Sample Questions.
SqlExam1Review.ppt EXAM - 1. SQL stands for -- Structured Query Language Putting a manual database on a computer ensures? Data is more current Data is.
Copyright © 2011 Pearson Education, Inc. Publishing as Pearson Addison-Wesley Chapter 6 The Relational Algebra and Relational Calculus.
ORT Braude, CSE 61309, ©2004 Gary Schloss Exam Question #1: ER, RA & SQL Consider a DB schema with the following relations: - Student (s#, sname) - Professor.
IS6146 Databases for Management Information Systems Lecture 4: SQL IV – SQL Functions and Procedures Rob Gleasure robgleasure.com.
In this session, you will learn to: Query data by using joins Query data by using subqueries Objectives.
ASET Relational Algebra continues…. ASET Rename Operation Allows us to name, and therefore to refer to, the results of relational-algebra expressions.
1 CSE 480: Database Systems Lecture 16: Relational Algebra.
Chapter 3: Relational Model III Additional Relational Algebra Operations Additional Relational Algebra Operations Views Views.
1 Schema for Student Registration System Student Student (Id, Name, Addr, Status) Professor Professor (Id, Name, DeptId) Course Course (DeptId, CrsCode,
5-1 Copyright © 2004, Oracle. All rights reserved. DISPLAYING DATA FROM MULTIPLE TABLES OUTER JOIN.
1 Relational Algebra & SQL Query Formulation Exercise.
Database Systems. DataBase System Haichang Gao, Software School, Xidian University 2 Major Content & Grade  Introduction*  The Relational Model** 
Structured Query Language
More SQL: Complex Queries,
Structured Query Language (Data Manipulation Language)
CS 480: Database Systems Lecture 12 February 11, 2013.
Relational Model By Dr.S.Sridhar, Ph.D.(JNUD), RACI(Paris, NICE), RMR(USA), RZFM(Germany)
Chapter 3 Introduction to SQL(3)
Chapter 3: Relational Model III
Database Systems Subqueries, Aggregation
Relational Algebra - Select & Project
SQL: Structured Query Language DML- Queries Lecturer: Dr Pavle Mogin
Chapter 2: Intro to Relational Model
Chapter 2: Intro to Relational Model
SQL Structured Query Language 11/9/2018 Introduction to Databases.
Database Systems 10/13/2010 Lecture #4.
Lecture 4 of 42 Relational Joins Wednesday, 30 January 2008
Instructor: Mohamed Eltabakh
Chapter 4 Summary Query.
More SQL: Complex Queries, Triggers, Views, and Schema Modification
SQL: Structured Query Language
SQL Introduction Standard language for querying and manipulating data
CMPT 354: Database System I
Relational Algebra Sample Questions.
Lecture 4: SQL Thursday, January 11, 2001.
Lecture 3 Finishing SQL
Presentation transcript:

1 Exercise 1  Given the following tables: employee (person_name, street, city)‏ works (person_name, company_name, salary)‏ company (company_name, city)‏ manages (person_name, manager_name)‏

2  Find the name of employees who earn more than $10,000 and live in Hong Kong. select w.person_name from works as w where w.salary > and w.person_name in (select e.person_name from employee as e where e.city = “Hong Kong”)‏  Alternative solutions select w.person_name from works as w, employee as e where w.salary > and e.city = “Hong Kong” and w.person_name = e.person_name employee (person_name, street, city)‏ works (person_name, company_name, alary)‏ company (company_name, city)‏ manages (person_name, manager_name)‏

3  Find the name of the employees who are not managers. (select person_name from employee)‏ except (select manager_name from manages)‏ employee (person_name, street, city)‏ works (person_name, company_name, alary)‏ company (company_name, city)‏ manages (person_name, manager_name)‏

4  Alternative solutions select person_name from employee where not exists (select * from manages where employee.person_name = manages.manager_name)‏ select person_name from employee where person_name not in (select manager_name from manages)‏ employee (person_name, street, city)‏ works (person_name, company_name, alary)‏ company (company_name, city)‏ manages (person_name, manager_name)‏

5 RA SQL  Given the following tables: employee (person_name, street, city)‏ works (person_name, company_name, salary)‏ company (company_name, city)‏ manages (person_name, manager_name)‏  Find the names of all persons who work for “First Bank Corporation” and live in the city where the company is located.

6 select E.person_name from employee as E, works as W, company as C where E.person_name = W.person_name and W.company_name = C.company_name and C.company_name = “First Bank Corporation” and E.city = C.city employee (person_name, street, city)‏ works (person_name, company_name, alary)‏ company (company_name, city)‏ manages (person_name, manager_name)‏

7  Find the names, cities of employees who work for exactly ONE company select e.person_name, e.city from employee as e where unique (select * from works as w where w.person_name = e.person_name) employee (person_name, street, city)‏ works (person_name, company_name, salary)‏ company (company_name, city)‏ manages (person_naame, manager_name)‏

8  Find the names of all employees who earn more than SOME employee of Small Bank Corporation. select w1.person_name from works as w1 where w1.salary > some (select w2.salary from works as w2 where w2.company_name = “Small Bank Corporation”)‏  Alternative solution select w1.person_name from works as w1 where exists (select * from works as w2 where w2.company_name = “Small Bank Corporation” and w1.salary > w2.salary)‏ employee (person_name, street, city)‏ works (person_name, company_name, salary)‏ company (company_name, city)‏ manages (person_naame, manager_name)‏

9  Find the company located in Hong Kong that has the largest number of employees select temp.company_name from (select w.company_name, count(distinct w.person_name) as cnt from works as w, company as c where w.company_name = c.company_name and c.city = “Hong Kong” group by w.company_name) as temp where temp.cnt in (select max (cnt)‏ from temp)‏ employee (person_name, street, city)‏ works (person_name, company_name, salary)‏ company (company_name, city)‏ manages (person_naame, manager_name)‏ Can also be ‘=’

10  Find all companies located in Hong Kong and have total payroll less than 100,000 select company.company_name from works, company where works.company_name = company.company_name and company.city = “Hong Kong” group by company.company_name having sum(works.salary) < 100,000 employee (person_name, street, city)‏ works (person_name, company_name, salary)‏ company (company_name, city)‏ manages (person_naame, manager_name)‏

11  Question: Consider the following schemas. CUST (cust-id, name), and WITHDRAW (w-id, cust-id, acc-id, date, amount)  Write an SQL query to retrieve all the names of the customers who have withdrawn more than 1k dollars in a single withdrawal. If a customer made several such withdrawals, her/his name should be reported only once.

12  Answer: Consider the following schemas. CUST (cust-id, name), and WITHDRAW (w-id, cust-id, acc-id, date, amount)  Write an SQL query to retrieve all the names of the customers who have withdrawn more than 1k dollars in a single withdrawal. If a customer made several such withdrawals, her/his name should be reported only once. select distinct name from CUST as T1, WITHDRAW as T2 where T1.cust-id = T2.cust-id and T2.amount > 1k

13  Question: Consider the following schemas. CUST (cust-id, name), and WITHDRAW (w-id, cust-id, acc-id, date, amount)  Sometimes there may be a “shared” account, namely, an account with multiple owners.  Write an SQL query to return the acc-id of all the shared accounts. You may assume that all the owners of a shared account have made withdrawals from the account.

14  Answer: Consider the following schemas. CUST (cust-id, name), and WITHDRAW (w-id, cust-id, acc-id, date, amount)  Sometimes there may be a “shared” account, namely, an account (acc-id) with multiple owners (cust-id).  Write an SQL query to return the acc-id of all the shared accounts. You may assume that all the owners of a shared account have made withdrawals from the account. select T1.acc-id from WITHDRAW as T1, WITHDRAW as T2 where T1.cust-id <> T2.cust-id and T1.acc-id = T2.acc-id

15  Question: Consider the following schemas. CUST (cust-id, name), and WITHDRAW (w-id, cust-id, acc-id, date, amount)  Let us use the name “interesting account” to refer to the account from which the withdrawal with smallest amount was made.  Retrieve the acc-id of accounts from which withdrawals have been made, except the interesting account.

16  Answer: Consider the following schemas. CUST (cust-id, name), and WITHDRAW (w-id, cust-id, acc-id, date, amount)  Let us use the name “interesting account” to refer to the account from which the withdrawal with smallest amount was made.  Retrieve the acc-id of accounts from which withdrawals have been made, except the interesting account. select distinct acc-id from WITHDRAW where acc-id not in (select acc-id from WITHDRAW where amount = (select min (amount) from WITHDRAW))

17  Consider table: deposit (dep-id, acc-id, cust-id, amount). We want to retrieve the cust-id of the customers who deposited into two accounts with acc-id ‘A1’ and ‘A2’, respectively.  Write an SQL query with intersect. (select distinct cust-id from deposit where acc-id = ‘A1’) intersect (select distinct cust-id from deposit where acc-id = ‘A2’)  Write a nested SQL query without intersect. select distinct cust-id from deposit where acc-id = ‘A1’ and cust-id in (select cust-id from deposit where acc-id = ‘A2’) dep-idacc-idcust-idamount A112K A111K A211K A223K A332K A325K

18  Again consider table: deposit (dep-id, acc-id, cust-id, amount). We want to retrieve the cust-id of the customers who deposited into two accounts with acc-id ‘A1’ and ‘A2’, respectively.  Write an SQL query that contains only one select. select distinct T1.cust-id from deposit as T1, deposit as T2 where T1.cust-id = T2.cust-id and T1.acc-id = ‘A1’ and T2.acc-id = ‘A2’ dep-idacc-idcust-idamount A112K A111K A211K A223K A332K A325K

19  Find the ids of the accounts which have been deposited into by more than one customer.  Write a SQL query without group by : select D1.acc-id from deposit as D1, deposit as D2 where D1.cust-id <> D2.cust-id and D1.acc-id = D2.acc-id dep-idacc-idcust-idamount A112K A111K A211K A223K A332K A325K

20  Find the ids of the accounts which have been deposited into by more than one customer.  Write a SQL query with group by : select acc-id from deposit group by acc-id having count (distinct cust-id) >= 2  If there is no distinct here, A1 will also be displayed. dep-idacc-idcust-idamount A112K A111K A211K A223K A332K A325K

21  Again consider table: deposit (dep-id, acc-id, cust-id, amount). We want to retrieve the cust-id of the customers who deposited into the account with acc-id = ‘A1’ or ‘A2’ but not both.  Write an SQL query that contains only one SELECT. select cust-id from deposit where acc-id = ‘A1’ or acc-id = ‘A2’ group by cust-id having count (distinct acc-id) = 1 Indispensable! dep_idacc-idcust-idamount A112K A111K A211K A223K A332K A325K

22  deposit (dep-id, acc-id, cust-id, amount). Retrieve the cust-id of the customer who deposited the largest number of times. select cust-id from deposit group by cust-id having count (*) >= all (select count (*) from deposit group by cust-id) dep-idacc-idcust-idamount A112K A111K A211K A223K A332K A325K

23  Question: As with natural join, outer joins are not compulsory operators. That is, we can implement an outer join using “conventional” SQL.  Let us verify this for left outer join.  CS-PROF (prof-id, name)  SUPERVISION (prof-id, stu-id)  Write an alternative query that returns the same information as select prof-id, name, stu-id from CS-PROF left outer join SUPERVISION on CS-PROF.prof-id = SUPERVISION.prof-id

24  Answer:  CS-PROF (prof-id, name)  SUPERVISION (prof-id, stu-id) select prof-id, name, stu-id from CS-PROF left outer join SUPERVISION on CS-PROF.prof-id = SUPERVISION.prof-id (select T1.prof-id, name, stu-id from CS-PROF as T1, SUPERVISION as T2 where T1.prof-id = T2.prof-id) union (select prof-id, name, NULL from CS-PROF as T1 where not exists (select * from SUPERVISION as T2 where T1.prof-id = T2.prof-id))  We can see that left outer join simplifies the query significantly.

25  Question: Consider MARKS(stu-id, course-id, score)  Write a query to retrieve the stu-id of every student who scored at least 80 in all the courses s/he took, but scored less than 90 in at least one course.  Your query should not contain more than 2 select.

26  Answer: Consider MARKS(stu-id, course-id, score).  Write a query to retrieve the stu-id of every student who scored at least 80 in all the courses s/he took, but scored less than 90 in at least one course. (select stu-id from MARKS where score < 90) except (select stu-id from MARKS where score < 80)

27  Answer: Consider MARKS (stu-id, course-id, score).  Write a query to retrieve the stu-id of every student who scored at least 80 in all the courses s/he took, but scored less than 90 in at least one course. select stu-id from MARKS group by stu-id having min (score) >= 80 and min (score) < 90

28  What will happen if some of values of score are NULL in table ?  All aggregate operations except count(*) ignore tuples with null values on the aggregated attributes.