Installment Numbers 6 Answers to Puzzle Corner Problems (Installment Numbers1-5) 會研所 黃潔.

Slides:



Advertisements
Similar presentations
SQL: The Query Language Part 2
Advertisements

A Guide to SQL, Seventh Edition. Objectives Use joins to retrieve data from more than one table Use the IN and EXISTS operators to query multiple tables.
Copyright © 2011 Pearson Education, Inc. Publishing as Pearson Addison-Wesley Chapter 5 More SQL: Complex Queries, Triggers, Views, and Schema Modification.
CS4432: Database Systems II Query Operator & Algebraic Expressions 1.
SQL Subqueries Objectives of the Lecture : To consider the general nature of subqueries. To consider simple versus correlated subqueries. To consider the.
INFS614, Fall 08 1 Relational Algebra Lecture 4. INFS614, Fall 08 2 Relational Query Languages v Query languages: Allow manipulation and retrieval of.
Copyright © 2011 Pearson Education, Inc. Publishing as Pearson Addison-Wesley Chapter 5 More SQL: Complex Queries, Triggers, Views, and Schema Modification.
Introduction to Structured Query Language (SQL)
CMPT 354, Simon Fraser University, Fall 2008, Martin Ester 52 Database Systems I Relational Algebra.
Introduction to Structured Query Language (SQL)
Nov 24, 2003Murali Mani SQL B term 2004: lecture 12.
1 Views. 2 What are views good for?(1) Simplifying complex queries: we saw one example. Here is another example that allows the user to "pretend" that.
Slides adapted from A. Silberschatz et al. Database System Concepts, 5th Ed. SQL - part 2 - Database Management Systems I Alex Coman, Winter 2006.
Instructor: Mohamed Eltabakh
Database Systems More SQL Database Design -- More SQL1.
Introduction to Structured Query Language (SQL)
Chapter 6 SQL: Data Manipulation Cont’d. 2 ANY and ALL u ANY and ALL used with subqueries that produce single column of numbers u ALL –Condition only.
2.3 More on Solving Linear Equations
Copyright © Cengage Learning. All rights reserved.
Database System Concepts, 6 th Ed. ©Silberschatz, Korth and Sudarshan See for conditions on re-usewww.db-book.com Chapter 3: Introduction.
Chapter 3 Single-Table Queries
SQL Unit 5 Aggregation, GROUP BY, and HAVING Kirk Scott 1.
Relational Query Languages. Languages of DBMS  Data Definition Language DDL  define the schema and storage stored in a Data Dictionary  Data Manipulation.
CSE314 Database Systems More SQL: Complex Queries, Triggers, Views, and Schema Modification Doç. Dr. Mehmet Göktürk src: Elmasri & Navanthe 6E Pearson.
IFS180 Intro. to Data Management Chapter 9 – Outer Joins.
Advanced SQL Murat Kantarcioglu Adapted from Silberchatz et al. slides.
1 CS 430 Database Theory Winter 2005 Lecture 12: SQL DML - SELECT.
SQL: Data Manipulation Presented by Mary Choi For CS157B Dr. Sin Min Lee.
Chapter 6 SQL: Data Manipulation (Advanced Commands) Pearson Education © 2009.
1 Single Table Queries. 2 Objectives  SELECT, WHERE  AND / OR / NOT conditions  Computed columns  LIKE, IN, BETWEEN operators  ORDER BY, GROUP BY,
Using Special Operators (LIKE and IN)
Dr Gordon Russell, Napier University Unit SQL 2 - V2.0 1 SQL 2 Unit 1.3.
VICTORIA UNIVERSITY OF WELLINGTON Te Whare Wananga o te Upoko o te Ika a Maui SWEN 432 Advanced Database Design and Implementation Exam and Lecture Overview.
Oracle DML Dr. Bernard Chen Ph.D. University of Central Arkansas.
Nitin Singh/AAO RTI ALLAHABAD 1 SQL Nitin Singh/AAO RTI ALLAHABAD 2 OBJECTIVES §What is SQL? §Types of SQL commands and their function §Query §Index.
6 1 Lecture 8: Introduction to Structured Query Language (SQL) J. S. Chou, P.E., Ph.D.
BIS Database Systems School of Management, Business Information Systems, Assumption University A.Thanop Somprasong Chapter # 7 Introduction to Structured.
Chapter 2 Section 3 Copyright © 2008 Pearson Education, Inc. Publishing as Pearson Addison-Wesley.
Installment 8 Empty Bags and Identity Crises 報告人: 李興漢.
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.
NULL VALUES CHAPTER 5 (6/E) CHAPTER 8 (5/E) 1. LECTURE OUTLINE  Dealing with null values Three-valued logic Effects in WHERE clauses IS NULL Effects.
1 CS 430 Database Theory Winter 2005 Lecture 5: Relational Algebra.
INSTALL NUMBER 12 Answers to Puzzle Corner Problems (Installment Number 7-11) 中正大學資管所 碩一 郭 溥 淵.
SqlExam1Review.ppt EXAM - 1. SQL stands for -- Structured Query Language Putting a manual database on a computer ensures? Data is more current Data is.
A Guide to SQL, Eighth Edition Chapter Five Multiple-Table Queries.
© 2002 by Prentice Hall 1 Structured Query Language David M. Kroenke Database Concepts 1e Chapter 3 3.
Temporary SQL Tables Los Angeles Pierce College Computer Science 560.
A Guide to SQL, Eighth Edition Chapter Four Single-Table Queries.
Thinking in Sets and SQL Query Logical Processing.
7 1 Database Systems: Design, Implementation, & Management, 7 th Edition, Rob & Coronel 7.6 Advanced Select Queries SQL provides useful functions that.
Day 5 - More Complexity With Queries Explanation of JOIN & Examples Explanation of JOIN & Examples Explanation & Examples of Aggregation Explanation &
Installment 17 Toil and Trouble 資管所 研一 涂延坤.
Why Three-Valued Logic is a Mistake ~by 丁碩賢 ( ) 2001/10/17.
Concepts of Database Management, Fifth Edition Chapter 3: The Relational Model 2: SQL.
IFS180 Intro. to Data Management Chapter 10 - Unions.
More SQL: Complex Queries, Triggers, Views, and Schema Modification
More SQL: Complex Queries,
PL/SQL LANGUAGE MULITPLE CHOICE QUESTION SET-1
SQL: Advanced Options, Updates and Views Lecturer: Dr Pavle Mogin
Chapter 2 Section 3.
SQL Structured Query Language 11/9/2018 Introduction to Databases.
Instructor: Mohamed Eltabakh
Computer Science 210 Computer Organization
Chapter 4 Summary Query.
More SQL: Complex Queries, Triggers, Views, and Schema Modification
Section 4 - Sorting/Functions
Chapter 2 Section 3.
Linear Algebra Lecture 6.
Copyright © 2008 Pearson Education, Inc
2-2 Logic Part 2 Truth Tables.
Presentation transcript:

Installment Numbers 6 Answers to Puzzle Corner Problems (Installment Numbers1-5) 會研所 黃潔

The Median Problem Source: “ Theory Is Practical! ” Problem statement: write a sequence of SQL statements to compute the median of the values in column T.C.

Solution: INSERT INTO T1 (C) SELECT T.C FROM T UNION ALL SELECT T.C FROM T ; INSERT INTO T2 (C) SELECT DISTINCT T1.C FROM T1 WHERE ( SELECT COUNT ( * ) FROM T ) <= ( SELECT COUNT ( * ) FROM T1 X WHERE X.C>=T1.C ) AND ( SELECT COUNT ( * ) FROM T ) <= (SELECT COUNT ( * ) FROM T1 X WHERE X.C <= T1.C ) ; INSERT INTO T3 ( M ) SELECT AVG ( T2.C ) FROM T2 ;

Explanation (1/2) We can avoid unknown whether column T.C contains an odd or even number. The purpose of the first INSERT above is to create a table T1 that is a “ doubled up ” version of table T.

Explanation (2/2) The second and third INSERTs make use of the following simple theorem. Theorem: S = x [0] ≦ x [1] ≦ … ≦ x [2n-1] The median M= ( x [n-1] + x [n] ) /2

Comments (1/2) Ignore the possibility that the given sequence might contain nulls. Omitted the necessary CREATE TABLEs for the intermediate and final result tables T1, T2, and T3. Using the SQL/92 dialect of SQL.

Comments (2/2) Allowing tables to include duplicate rows. What happens if table t is empty? What is the median of an empty sequence of value ? The answer clearly is undefined.

The Identity Problem Source: ” The Importance of Closure ” Problem statement: What is the identity in the relational model with respect to join and Cartesian product?

The Winners Problem Source: “ What ’ s in a Name? ” Problem statement: given the relation EDT { E, D, T, … } PRIMARY KEY { D,T } Use relational algebra operators to produce the relation RESULT{ E, N } PRIMARY KEY { E }

Solution T1:= SUMMARIZE EDT BY (E,D)ADD COUNT AS CT; /* T1 {E, D, CT} - E occurs CT times on D */ T2:= SUMMARIZE T1 BY (D) ADD MAX(CT) AS CT; /* T2{D, CT} – CT is max no. of times any E occurs on D */ T3:= T1 JOIN T2; /* T3{E, D, CT} – E occurs CT times on D and no distinct */ /* E ’ occurs more than CT times on D */ T4:= SUMMARIZE T3 BY (E) ADD COUNT AS N; /* T4{E, N } – N is no. of days on which e is a winner */

Comments Broken the overall problem into a sequence of separate relational assignments. A good implementation of the relational algebra would not require any such declarations. Here is an SQL analog of the foregoing solution.

The Three-valued Logic Problem Source: “ Why Three – Valued Logic Is a Mistake ” Problem statement: the three-valued logic (3VL) and real world answers to the following pseudoSQL query: SELECT E# FROM EMP WHERE MAYBE (D# = ‘ D1 ’ ) ;

Solution The 3VL answer is “ E1 ”. The real – world answer, by contrast, is no employee numbers at all. The expression in the WHERE clause must evaluate to false in the real world. 3VL does not match reality.

Operators of Two-Valued Logic Source: “ Nothing in Excess ” Problem statement: The four monadic and 16 dyadic operators of two-valued logic can all be formulated in terms of suitable combinations of NOT and either AND or OR.

Solution Let f(A) be the monadic operator Let f(A,B) be the dyadic operator

The median problem revisited

The winners problem revisited

2VL operators revisited 2VL operators can all be formulated in terms of the single logical operator “ NOR ”