SQL Structured Query Language 11/9/2018 Introduction to Databases.

Slides:



Advertisements
Similar presentations
Chapter 4: SQL  Basic Structure  Data Definition Language  Modification of the Database  Set Operations  Aggregate Functions  Null Values  Nested.
Advertisements

Ver 1,12/09/2012Kode :CCs 111,Sistem basis DataFASILKOM Chapter 3: SQL Bambang Irawan Database System Concepts, 5th Ed. ©Silberschatz, Korth and Sudarshan.
Ver 1,12/09/2012Kode :CCs 111,sistem basisdataFASILKOM Chapter 2: Relational Model Database System Concepts, 5th Ed. ©Silberschatz, Korth and Sudarshan.
CS4432: Database Systems II Query Operator & Algebraic Expressions 1.
1 Today’s Class  Relational Model  SQL CS F212 Database Systems.
E-R Diagram for a Banking Enterprise
Copyright © 2011 Pearson Education, Inc. Publishing as Pearson Addison-Wesley Chapter 5 More SQL: Complex Queries, Triggers, Views, and Schema Modification.
©Silberschatz, Korth and Sudarshan4.1Database System Concepts Ordering the Display of Tuples List in alphabetic order the names of all customers having.
Database System Concepts, 5th Ed. ©Silberschatz, Korth and Sudarshan See for conditions on re-usewww.db-book.com Chapter 3: SQL.
©Silberschatz, Korth and Sudarshan4.1Database System Concepts Chapter 4: SQL Basic Structure Set Operations Aggregate Functions Null Values Nested Subqueries.
SQL Structured Query Language Meizhen Huang. Content (4.1 – 4.4) Background Parts of SQL Basic Structure Set Operations Aggregate Functions.
Temple University – CIS Dept. CIS331– Principles of Database Systems V. Megalooikonomou Query by example (based on notes by Silberchatz,Korth, and Sudarshan.
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,
--The SQL Query Language DML--1 LIKE  LIKE allows to select character strings which have some element in common by using wild cards:  Wild cards:  “%”
Slides adapted from A. Silberschatz et al. Database System Concepts, 5th Ed. SQL - part 2 - Database Management Systems I Alex Coman, Winter 2006.
©Silberschatz, Korth and Sudarshan4.1Database System Concepts Aandachtspunten Deadlines – alles op tijd inleveren! n Lees goed wat van je verwacht wordt!
Instructor: Mohamed Eltabakh
CIS552SQL1 Data Definition Language Insertions Basic Query Structure Set Operations Aggregate Functions Null Values Nested Subqueries Derived Relations.
SQL I. SQL – Introduction  Standard DML/DDL for relational DB’s  DML = “Data Manipulation Language” (queries, updates)  DDL = “Data Definition Language”
SQL By: Toan Nguyen. Download Download the software at During the installation –Skip sign up for fast installation.
Chapter 3: SQL Data Definition Language Data Definition Language Basic Structure of SQL Basic Structure of SQL Set Operations Set Operations Aggregate.
Database System Concepts, 5th Ed. ©Silberschatz, Korth and Sudarshan See for conditions on re-usewww.db-book.com Chapter 3: SQL.
-- Introduction to database principles Maoying Wu March 11, 2013 Chapter 3: Basic SQL.
©Silberschatz, Korth and Sudarshan4.1Database System Concepts Chapter 4: SQL Basic Structure Set Operations Aggregate Functions Null Values Nested Subqueries.
SQL. Basic Structure SQL is based on set and relational operations with certain modifications and enhancements A typical SQL query has the form: select.
Chapter 3 Single-Table Queries
©Silberschatz, Korth and Sudarshan4.1Database System Concepts Null Values It is possible for tuples to have a null value for some of their attributes The.
Lecture 6 Structured Query Language SQL Lecture 6 Structured Query Language SQL Instructor: Haya Sammaneh.
3.1 Chapter 3: SQL Schema used in examples p (omit 3.8.2, , 3.11)
Basic Structure Given sets A 1, A 2,..., A n a relation r is a subset of A 1 x A 2 x... x A n Thus a relation is a set of n-tuples (a 1, a 2,..., a n )
Structured Query Language 2 Presented by: Annisa, M.Kom. Source: Database System Concepts 5 th edition.
©Silberschatz, Korth and Sudarshan2.1Database System Concepts - 5 th Edition, Oct 5, 2006 Outer Join n An extension of the join operation that avoids loss.
SQL: Data Manipulation Presented by Mary Choi For CS157B Dr. Sin Min Lee.
1 Single Table Queries. 2 Objectives  SELECT, WHERE  AND / OR / NOT conditions  Computed columns  LIKE, IN, BETWEEN operators  ORDER BY, GROUP BY,
©Silberschatz, Korth and Sudarshan5.1Database System Concepts Chapter 5: Other Relational Languages Query-by-Example (QBE)
Computing & Information Sciences Kansas State University Monday, 08 Sep 2008CIS 560: Database System Concepts Lecture 5 of 42 Monday, 08 September 2008.
Chapter 8: SQL. Data Definition Modification of the Database Basic Query Structure Aggregate Functions.
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.
Source: Database System Concepts, Silberschatz etc Edited: Wei-Pang Yang, IM.NDHU, Introduction to Database CHAPTER 5 Other Relational Languages.
1/18/00CSE 711 data mining1 What is SQL? Query language for structural databases (esp. RDB) Structured Query Language Originated from Sequel 2 by Chamberlin.
©Silberschatz, Korth and Sudarshan3.1Database System Concepts Extended Relational-Algebra-Operations Generalized Projection Aggregate Functions Outer Join.
CS 405G: Introduction to Database Systems Instructor: Jinze Liu Fall 2009.
SqlExam1Review.ppt EXAM - 1. SQL stands for -- Structured Query Language Putting a manual database on a computer ensures? Data is more current Data is.
Relational Algebra Instructor: Mohamed Eltabakh 1.
A Guide to SQL, Eighth Edition Chapter Four Single-Table Queries.
Chapter 3: Relational Model III Additional Relational Algebra Operations Additional Relational Algebra Operations Views Views.
SQL: Structured Query Language Instructor: Mohamed Eltabakh 1 Part II.
Relational Algebra Instructor: Mohamed Eltabakh 1.
More SQL: Complex Queries, Triggers, Views, and Schema Modification
Structured Query Language
More SQL: Complex Queries,
Slides are reused by the approval of Jeffrey Ullman’s
Structured Query Language (Data Manipulation Language)
Relational Model By Dr.S.Sridhar, Ph.D.(JNUD), RACI(Paris, NICE), RMR(USA), RZFM(Germany)
Relational Algebra - Part 1
Chapter 3 Introduction to SQL(3)
Schedule Today: Next After that Subqueries, Grouping and Aggregation.
Chapter 3: Relational Model III
CS 405G: Introduction to Database Systems
Chapter 3: SQL.
Lecture 4 of 42 Relational Joins Wednesday, 30 January 2008
Instructor: Mohamed Eltabakh
More SQL: Complex Queries, Triggers, Views, and Schema Modification
SQL: Structured Query Language
CMPT 354: Database System I
SQL: Structured Query Language
Schedule Today: Next And then Relational Algebra.
CSC 453 Database Systems Lecture
SQL: Structured Query Language
CS 405G: Introduction to Database Systems
Presentation transcript:

SQL Structured Query Language 11/9/2018 Introduction to Databases

Select Statements Revisited select clause ----------- attribute selection part from clause ----------- relation selection part where clause ----------- join, selection conditions part group by clause ----------- partition part having clause ----------- partition filtering part order by clause ----------- ordering rows part + Aggregates, set operations, subqueries 11/9/2018 Introduction to Databases

Introduction to Databases Banking Example branch (branch-name, branch-city, assets) customer (customer-name, customer-street, customer-only) account (account-number, branch-name, balance) loan (loan-number, branch-name, amount) depositor (customer-name, account-number) borrower (customer-name, loan-number) 11/9/2018 Introduction to Databases

Introduction to Databases Branch Table branch-name branch-city assets A Riverside $10,000 B LA $20,000 C Long Beach $15,000 D Irvine $12,000 E Pomona $7,000 F San Jose $18,000 11/9/2018 Introduction to Databases

Introduction to Databases Customer Table customer-name customer-street customer-only Joe Joe_street Y Alan Mary_street Jason Jason_street N Mary Mike Keith Keith_street 11/9/2018 Introduction to Databases

Introduction to Databases Account Table account_number branch-name balance 1 B $100 2 A $50 3 $30 4 F $120 5 $500 6 $324 11/9/2018 Introduction to Databases

Introduction to Databases Loan Table loan-number branch-name Amount 1 B $100 2 E $27 3 F $543 4 A $129 5 $26 6 $67 11/9/2018 Introduction to Databases

Introduction to Databases Depositor Table customer-name account-number Joe 1 2 Mary Keith 4 Mike 5 6 3 11/9/2018 Introduction to Databases

Introduction to Databases Borrower Table customer-name loan-number Joe 1 Jason 2 3 Keith 4 Mary 5 6 11/9/2018 Introduction to Databases

Subqueries A Select statement form appearing within another Select statement is known as a Subquery. For our purposes, we can think of the two forms as identical. A Subquery can appear in the search_condition of a WHERE clause for another Select statement. 11/9/2018 Introduction to Databases

Subquery predicates ---- IN The IN (NOT IN) predicate tests set membership. In such a case the Subquery can be regarded as returning a set of values to the outer query. Membership can be tested not only against the set provided by a Subquery but also against sets that are explicitly defined. 11/9/2018 Introduction to Databases

Subquery predicates ---- IN Example 1 “Find the account numbers opened at branches of the bank in Riverside” Select account-number From account Where branch_name IN (select branch-name from branch where branch-city=‘Riverside’) 11/9/2018 Introduction to Databases

Introduction to Databases Example 1 - Result account-number 2 3 5 11/9/2018 Introduction to Databases

Subquery predicates ---- IN Example 2 “Find the account numbers opened at A and B branches of the bank” Select account-number From account Where branch_name IN (‘A, ‘B’) 11/9/2018 Introduction to Databases

Introduction to Databases Example 2 - Result account-number 1 2 3 5 6 11/9/2018 Introduction to Databases

Subquery predicates ---- IN Example 3 “Find the account numbers opened at branches of the bank in Riverside” Select account-number From account a correlated subquery Where ‘Riverside’ IN ( select branch-city from branch b where a.branch-name=b.branch-name) 11/9/2018 Introduction to Databases

Introduction to Databases Example 3 - Result account-number 2 3 5 11/9/2018 Introduction to Databases

Subquery predicates ---- EXISTS The EXISTS predicate is TRUE if and only if the Subquery returns a non-empty set. The NOT EXISTS predicate is TRUE if and only if the Subquery returns an empty set. The NOT EXISTS can be used to implement the MINUS operator from relational algebra. 11/9/2018 Introduction to Databases

Subquery predicates ---- EXISTS Example 1 “Select all the account balances where the account has been opened in a branch in Riverside ” Select account-balance From account a Where EXISTS (select * from branch b where b.branch-city=‘Riverside’ and b.branch_name=a.branch-name) 11/9/2018 Introduction to Databases

Introduction to Databases Example 1 - Result account-number $50 $30 $500 11/9/2018 Introduction to Databases

Subquery predicates ---- EXISTS Example 2 “Select all the account balances where the account has not been opened in a branch in Riverside ” Select account-balance From account a Where NOT EXISTS (select * from branch b where b.branch-city=‘Riverside’ and b.branch_name=a.branch-name) 11/9/2018 Introduction to Databases

Introduction to Databases Example 2 - Result account-number $100 $120 $324 11/9/2018 Introduction to Databases

Subquery predicates Quantified Comparison Predicate A quantified predicate compares a simple value of an expression with the result of a Subquery. Given a comparison operation , representing some operator in the set {<, , =, <>, >, }, the equivalent predicates ‘expr  some (Subquery)’ and ‘expr  any (Subquery)’ are TRUE if and only if, for at least one element s returned by the Subquery, it is true that ‘expr  s’ is TRUE; the predicate ‘expr  all (Subquery)’ is TRUE if and only if ‘expr  s’ is TRUE for every one of the elements s of the Subquery; 11/9/2018 Introduction to Databases

Subquery predicates Quantified Comparison Predicate Example 1 “Select account numbers of the accounts with the minimum balance” Select account-balance From account Where balance <= ALL (select balance from account) 11/9/2018 Introduction to Databases

Introduction to Databases Example 1 - Result account-number 3 11/9/2018 Introduction to Databases

Subquery predicates Quantified Comparison Predicate Example 2a “Select account balances for accounts located in Riverside” Select account-balance From account Where branch-name = ANY (select branch-name from branch b where branch-city=‘Riverside’) 11/9/2018 Introduction to Databases

Subquery predicates Quantified Comparison Predicate Example 2b “Select account balances for accounts located in Riverside” Select account-balance From account Where branch-name = SOME (select branch-name from branch b where branch-city=‘Riverside’) 11/9/2018 Introduction to Databases

Introduction to Databases Example 2 - Result account-number $50 $30 $500 11/9/2018 Introduction to Databases

Set Functions in SQL SQL provides five built-in functions that operate on sets of column values in tables: COUNT(), MAX(), MIN(), SUM(), AVG(). With the exception of COUNT(), these set functions must operate on sets that consist of simple values-that is, sets of numbers or sets of character strings, rather than sets of rows with multiple values. NULL values are not counted. 11/9/2018 Introduction to Databases

Set Functions in SQL Example 1 “Select the total amount of balance of the account in branches located in Riverside” Select sum(balance) as total_amount From account a, branch b Where a.branch-city=‘Riverside’ and b.branch_name= a.branch_name 11/9/2018 Introduction to Databases

Introduction to Databases Example 1 - Result total_amount $580 11/9/2018 Introduction to Databases

Set Functions in SQL Example 2 “Select the total number of opened accounts” Select count(account-number) From account OR Select count(*) From account 11/9/2018 Introduction to Databases

Introduction to Databases Example 2 - Result count(*) $580 11/9/2018 Introduction to Databases

Groups of Rows in SQL (1) SQL allows Select statements to provide a kind of natural ‘report’ function, grouping the rows on the basis of commonality of values and performing set functions on the rows grouped: Select account-number, sum(balance) from account group by branch_name. The GROUP BY clause of the Select statement will result in the set of rows being generated as if the following loop-controlled query were being performed: FOR EACH DISTICT VALUE v OF account-number IN account Select account-number, sum(balance) from account where account-number=v END FOR 11/9/2018 Introduction to Databases

Introduction to Databases Groups of Rows in SQL (2) A set of functions occurring in the select list aggregates for the set of rows in each group and thus creates a single value for each group. It is important that all of the attributes named in the select list have a single atomic value, for each group of common GROUP BY values: Select account-number, branch-name, sum(valance) group by account-number ----------- INVALID 11/9/2018 Introduction to Databases

Groups of Rows in SQL –Example 1 “Find the total amount of money owed by each depositor” Select c.customer-name, sum(balance) From account a, customer c, depositor d Where a.account-number = d.account-number and c.customer-name = d.customer-name Group By customer-name 11/9/2018 Introduction to Databases

Introduction to Databases Example 1 - Result customer-name sum(balance) Joe $180 Mary $50 Keith $444 Mike $500 11/9/2018 Introduction to Databases

Introduction to Databases Filter grouping To eliminate rows from the result of a select statement where a GROUP BY clause appears we use the HAVING clause, which is evaluated after the GROUP BY. For example the query: Select account-branch, sum(balance) from account group by account-branch having sum(balance)>1000. Will print the account branches and total balances for every branch where the total account balance exceeds 1000. The HAVING clause can only apply tests to values that are single-valued for groups in the Select statement. The HAVING clause can have a nested subquery, just like the WHERE clause 11/9/2018 Introduction to Databases

Filter Grouping –Example 1 “Find the total amount of money owed by each depositor, for each depositor that own at least 2 accounts” Select c.customer-name, sum(balance) From account a, customer c, depositor d Where a.account-number = d.account-number and c.customer-name = d.customer-name Group By customer-name Having count(*) > 1 11/9/2018 Introduction to Databases

Introduction to Databases Example 1 - Result customer-name sum(balance) Joe $180 Keith $444 11/9/2018 Introduction to Databases

Order Results We use the ORDER BY clause when we want the output to be presented in a particular order. We provide the list of attributes to order on. 11/9/2018 Introduction to Databases

Introduction to Databases Order –Example 1 “Find the total amount of money owed by each depositor, for each depositor that own at least 2 accounts, present the results in descending order of total balance” Select c.customer-name, sum(balance) as sbalance From account a, customer c, depositor d Where a.account-number = d.account-number and c.customer-name = d.customer-name Group By customer-name Having count(*) > 1 Order By Desc sbalance 11/9/2018 Introduction to Databases

Introduction to Databases Example 1 - Result customer-name sum(balance) Keith $444 Joe $180 11/9/2018 Introduction to Databases

Quiz Both the WHERE and HAVING clauses are filtering unwanted tuples out of the result. What is the ordering of evaluation of these two clauses? Why? 11/9/2018 Introduction to Databases

Null Values We use null when the column value is either unknown or inapplicable. A comparison with at least one null value always returns unknown. SQL also provides a special comparison operator IS NULL to test whether a column value is null. To incorporate nulls in the definition of duplicates we define that two rows are duplicates if corresponding rows are equal or both contain null. 11/9/2018 Introduction to Databases

Introduction to Databases Outer Joins Let R and S two tables. The outer join preserves the rows of R and S that have no matching rows according to the join condition and outputs them with nulls at the non-applicable columns. There exist three different variants: left outer join, right outer join and full outer join. 11/9/2018 Introduction to Databases

Conceptual order in query evaluation First the relational products of the tables in the FROM clause are evaluated. From this, rows not satisfying the WHERE clause are eliminated. The remaining rows are grouped in accordance with the GROUP BY clause. Groups not satisfying the HAVING clause are then eliminated. The expressions in the SELECT list are evaluated. If the keyword DISITINCT is present, duplicate rows are now eliminated. Evaluate UNION, INTERSECT and EXCEPT for Subqueries up to this point. Finally, the set of all selected rows is sorted if the ORDER BY is present. 11/9/2018 Introduction to Databases

Introduction to Databases Questions what is the difference between: ‘select branch-name from branch b where not exists (select * from account a where a.balance = 1000 and a.branch-name=b.branch-name)’ ‘select branch-name from account a1 where not exists (select * from account a2 where a2.balance = 1000 and a1.branch-name=a2.branch-name)’ what is the result of comparing two null values using ‘=‘. How is this different in the context of duplicates? 11/9/2018 Introduction to Databases