IST 210 More SQL Todd Bacastow IST 210: Organization of Data.

Slides:



Advertisements
Similar presentations
Union, Intersection, Difference (subquery) UNION (subquery) produces the union of the two relations. Similarly for INTERSECT, EXCEPT = intersection and.
Advertisements

CSC271 Database Systems Lecture # 11.
CSC271 Database Systems Lecture # 13. Summary: Previous Lecture  Grouping through GROUP BY clause  Restricted groupings  Subqueries  Multi-Table queries.
Branch (Bno, Street, Area, City, Pcode, Tel_No, Fax_NO) Staff (Sno, FName, LName, Address, Tel_No, Position, Sex, DOB, Salary, NIN, Bno) Property_for_Rent.
Session 4 SQL Structured Query Language. SQL Modes of use –Interactive –Embedded Purpose –Create database –Create, Read, Update, Delete.
1 Minggu 4, Pertemuan 8 SQL: Data Manipulation (Cont.) Matakuliah: T0206-Sistem Basisdata Tahun: 2005 Versi: 1.0/0.0.
Chapter 6 SQL: Data Manipulation. 2 Objectives of SQL u Database language should allow user to: –create database and relation structures –perform insertion,
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.
1 Minggu 4, Pertemuan 7 SQL: Data Manipulation Matakuliah: T0206-Sistem Basisdata Tahun: 2005 Versi: 1.0/0.0.
1 Copyright © Oracle Corporation, All rights reserved. Writing Basic SQL SELECT Statements.
IFS Intro. to Data Management Chapter 6 Filtering your data.
Chapter 3 Single-Table Queries
Agenda TMA01 M876 Block 3 – Using SQL Structured Query Language - SQL A non-procedural language to –Create database and relation structures. –Perform.
Data Manipulation Using MySQL tMyn1 Data Manipulation Using MySQL Ideally, a database language should allow a user to: –Create the database and relation.
SQL: Overview SQL Overview © Pearson Education Limited 1995, 2005.
Database Systems: A Practical Approach to Design, Implementation and Management International Computer Science S. Carolyn Begg, Thomas Connolly Lecture.
1 CS 430 Database Theory Winter 2005 Lecture 12: SQL DML - SELECT.
10/15/2012ISC239 Isabelle Bichindaritz1 SQL Queries.
CSC271 Database Systems Lecture # 12. Summary: Previous Lecture  Row selection using WHERE clause  WHERE clause and search conditions  Sorting results.
About the Presentations The presentations cover the objectives found in the opening of each chapter. All chapter objectives are listed in the beginning.
2 Copyright © 2004, Oracle. All rights reserved. Restricting and Sorting Data.
SQL: Data Manipulation Presented by Mary Choi For CS157B Dr. Sin Min Lee.
Chapter 7 SQL: Data Manipulation Chapter#6 in the text book Pearson Education © 2009.
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,
SQL Data Manipulation II Chapter 5 CIS 458 Sungchul Hong.
1 General Form of the SELECT Statement SELECT [DISTINCT | ALL {* | col_expr [AS new_name] [,…]} FROMtable_name [,…] [,…] [WHERE condition] [GROUP BY col_list]
Chapter 5 SQL Data Manipulation Language Chapter 5 in Textbook.
Chapter 5 SQL: Data Manipulation © Pearson Education Limited 1995, 2005.
1 Pertemuan > > Matakuliah: >/ > Tahun: > Versi: >
Chapter 7 SQL: Data Manipulation Chapter #6 in the textbook Pearson Education © 2009.
IST 210 SQL Todd Bacastow IST 210: Organization of Data.
Copyright © 2004, Oracle. All rights reserved. Lecture 4: 1-Retrieving Data Using the SQL SELECT Statement 2-Restricting and Sorting Data Lecture 4: 1-Retrieving.
Structured Query Language
Queries SELECT [DISTINCT] FROM ( { }| ),... [WHERE ] [GROUP BY [HAVING ]] [ORDER BY [ ],...]
DATA RETRIEVAL WITH SQL Goal: To issue a database query using the SELECT command.
Chapter 8 Introduction to SQL
1 Geog 357 – Introduction to GIS The Relational Language.
1 CSCE Database Systems Anxiao (Andrew) Jiang The Database Language SQL.
A Guide to SQL, Eighth Edition Chapter Four Single-Table Queries.
Restrictions Objectives of the Lecture : To consider the algebraic Restrict operator; To consider the Restrict operator and its comparators in SQL.
More SQL (and Relational Algebra). More SQL Extended Relational Algebra Outerjoins, Grouping/Aggregation Insert/Delete/Update.
In this session, you will learn to: Query data by using joins Query data by using subqueries Objectives.
SQL and QBE Transparencies. ©Pearson Education 2009 Chapter 3 - Objectives Purpose and importance of SQL, the main language for querying relational databases.
SQL: Data Manipulation. Objectives Describe the purpose and importance of SQL. Demonstrate how to retrieve data from database using SELECT and: ▫Use compound.
SQL: Structured Query Language It enables to create and operate on relational databases, which are sets of related information stored in tables. It is.
SQL: Additional Notes. 2 Example 5.3 Use of DISTINCT List the property numbers of all properties that have been viewed. SELECT propertyNo FROM Viewing;
Chapter 6 SQL: Data Manipulation Pearson Education © 2009.
Lecture 11 SQL. Agenda SQL Select statement WHERE clause BindingSource filtering.
Concepts of Database Management, Fifth Edition Chapter 3: The Relational Model 2: SQL.
Teacher Workshop Database Design Pearson Education © 2014.
Chapter 11 SQL: Data Manipulation
Writing Basic SQL SELECT Statements
Chapter Name SQL: Data Manipulation
Chapter Name SQL: Data Manipulation
Data Manipulation Language
SQL – Data Manipulation
SQL Structured Query Language 11/9/2018 Introduction to Databases.
Chapter Name SQL: Data Manipulation Chapter #6 in the textbook
IST 210: Organization of Data
Chapter Name SQL: Data Manipulation Chapter #6 in the textbook
CPSC-310 Database Systems
Chapter Name SQL: Data Manipulation
Chapter Name SQL: Data Manipulation
Chapter Name SQL: Data Manipulation
SQL – Data Manipulation
Chapter Name SQL: Data Manipulation
Chapter Name SQL: Data Manipulation Transparencies
Chapter Name SQL: Data Manipulation Transparencies
Restricting and Sorting Data
Presentation transcript:

IST 210 More SQL Todd Bacastow IST 210: Organization of Data

IST 210 Simple Join Consider: Likes (drinker, beer) Sells (bar, beer, price) SELECT drinker, bar FROM Likes, Sells WHERE Likes.beer=Sells.beer; What’s happening here????

IST 210 Examples Employees: Employee_ID Name 01Hansen, John 02Shara, Tove 03Seven, Stephen 04Pettersen, Kari Orders: Prod_ID ProductEmployee_ID 234Printer01 657Table03 865Chair03

IST 210 Who has ordered a product, and what did they order? SELECT Employees.Name, Orders.Product FROM Employees, Orders WHERE Employees.Employee_ID=Orders.Employee_ID Result Name Product Hansen, JohnPrinter Seven, StephenTable Seven, StephenChair

IST 210 Who ordered a printer? SELECT Employees.Name FROM Employees, Orders WHERE Employees.Employee_ID=Orders.Employee_ID AND Orders.Product='Printer' Result Name Hansen, John

IST 210 Other ‘Joins’ in SQL2 R NATURAL JOIN S R CROSS JOIN S R JOIN S ON condition R OUTER JOIN S R INNER JOIN S

IST 210 Alternative JOIN Constructs SQL2 provides alternative ways to specify joins: SELECT * FROM Likes.beer JOIN Sells.beer ON Likes.beer= Sells.beer SELECT * FROM Likes JOIN Sells USING beer SELECT * FROM Likes NATURAL JOIN Sells The first produces a table with two identical beer columns, remaining two produce table with single beer column.

IST 210 Outer Join Outerjoin = natural join with dangling tuples padded with NULLs and included in the result A tuple is dangling if it doesn’t join with any other tuple AB BC ABC NULL 78 OUTERJOIN =

IST 210 Modifiers of OUTER JOIN Optional NATURAL in front Optional ON condition at end Optional LEFT, RIGHT, FULL before OUTER LEFT = Pad dangling tuples of R ONLY RIGHT = Pad dangling tuples of S ONLY

IST 210 Inner Join SELECT Employees.Name, Orders.Product FROM Employees INNER JOIN Orders ON Employees.Employee_ID=Orders.Employee_ID Name Product Hansen, JohnPrinter Seven, StephenTable Seven, StephenChair Result The INNER JOIN returns all rows from both tables where there is a match. If there are rows in Employees that do not have matches in Orders, those rows will not be listed. EXAMPLE: Who has ordered a product, and what did they order?

IST 210 Calculated Fields Query Produce a list of monthly salaries for all staff, showing the staff number, the first and last names, and the salary details. SELECT sno, fname, lname, salary/12 FROM staff; To name column, use AS clause: SELECT sno, fname, lname, salary/12 AS monthly_salary FROM staff; Monthly_salary

IST 210 Range Search Condition - BETWEEN Query: List all staff with a salary between 20,000 and 30,000. SELECT sno, fname, lname, position, salary FROM staff WHERE salary BETWEEN AND 30000;

IST 210 Range Search Condition (Cont…) BETWEEN test includes the endpoints of range. Equivalent to SELECT sno, fname, lname, position, salary FROM staff WHERE salary>=20000 AND salary <= 30000; BETWEEN does not add much to SQL's expressive power Also a negated version NOT BETWEEN.

IST 210 Set Membership - IN / NOT IN Query: List all Managers and Deputy Managers. SELECT sno, fname, lname, position FROM staff WHERE position IN ('Manager', 'Deputy');

IST 210 Pattern Matching - LIKE SQL has two special pattern matching symbols: %: sequence of zero or more characters; _ (underscore): any single character. LIKE '%Glasgow%' means a sequence of characters of any length containing 'Glasgow'. Query: Find all staff with the string 'Glasgow' in their address. SELECT sno, fname, lname, address, salary FROM staff WHERE address LIKE '%Glasgow%';

IST 210 NULL Search Condition NULL value can not be checked by comparison operators Have to test for null explicitly using special keyword IS NULL IS NOT NULL can test for non-null values. Example query: List details of all viewings on property PG4 where a comment has not been supplied. SELECT rno, date FROM viewing WHERE pno = 'PG4' AND comment IS NULL;

IST 210 ORDER BY Example Query: List salaries for all staff, arranged in descending order of salary. SELECT sno, fname, lname, salary FROM staff ORDER BY salary DESC; When no (ASC/DESC) is indicated, default order is ascending (ASC). Can sort on multiple columns. SELECT pno, type, rooms, rent FROM property_for_rent ORDER BY type, rent DESC;

IST 210 GROUP BY and HAVING Use GROUP BY clause to get sub-totals. SELECT and GROUP BY closely integrated: each item in SELECT list must be single-valued per group, and SELECT clause may only contain: Column names. Aggregate functions. Constants. An expression involving combinations of the above. If WHERE is used with GROUP BY, WHERE is applied first, then groups are formed from remaining rows satisfying predicate. Considers two nulls to be equal for purposes of GROUP BY.

IST Use of GROUP BY Example: Find number of staff in each branch and their total salaries. SELECT bno, COUNT(sno) AS count, SUM(salary) AS sum FROM staff GROUP BY bno ORDER BY bno;

IST 210 Restricted Grouping - HAVING HAVING clause is designed for use with GROUP BY clause to restrict groups that appear in final result table. HAVING filters groups. Column names in HAVING clause must also appear in the GROUP BY list or be contained within an aggregate function. Example: For each branch with more than 1 member of staff, find number of staff in each branch and sum of their salaries. SELECT bno, COUNT(sno) AS count, SUM(salary) AS sum FROM staff GROUP BY bno HAVING COUNT(sno) > 1 ORDER BY bno;

IST 210 Subquery with Aggregate Example: List all staff whose salary is greater than the average salary. SELECT sno, fname, lname, position, salary FROM staff WHERE salary > (SELECT avg(salary) FROM staff); Cannot write 'WHERE salary > avg(salary)' Instead, use subquery to find average salary (17000), and then use outer SELECT to find those staff with salary greater than Subquery SELECT list must consist of a single column name or expression, except for subqueries that use EXISTS.

IST 210 ANY/SOME and ALL If subquery preceded by ALL, condition will only be true if it is satisfied by all values produced by subquery. If subquery preceded by ANY, condition will be true if it is satisfied by any values produced by subquery. If subquery is empty, ALL returns true, ANY returns false.

IST 210 Example of ANY and ALL Example1: Find staff whose salary is larger than salary of at least 1 member of staff at branch B3. SELECT sno, fname, lname, position, salary FROM staff WHERE salary >ANY (SELECT salary FROM staff WHERE bno = 'B3'); Example 2: Find staff whose salary is larger than salary of every member of staff at branch B3. SELECT sno, fname, lname, position, salary FROM staff WHERE salary > ALL (SELECT salary FROM staff WHERE bno = 'B3');

IST 210 Sorting a join Consider: For each branch, list names of staff who manage properties. SELECT s.bno, s.sno, fname, lname, pno FROM staff s, property_for_rent p WHERE s.sno = p.sno ORDER BY s.bno, s.sno, pno;