SQL Data Manipulation II Chapter 5 CIS 458 Sungchul Hong.

Slides:



Advertisements
Similar presentations
SQL. More than 100 DBMS support SQL –Used by DBAs and application programmers –Structured Query Language or SEQUEL –ORACLE-> relation database based on.
Advertisements

CSC271 Database Systems Lecture # 11.
Chapter 6 SQL: Data Manipulation Pearson Education © 2009.
CSC271 Database Systems Lecture # 13. Summary: Previous Lecture  Grouping through GROUP BY clause  Restricted groupings  Subqueries  Multi-Table queries.
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.
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.
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 Pertemuan > > Matakuliah: >/ > Tahun: > Versi: >
Chapter 5 SQL Data Manipulation Language Chapter 5 in Textbook.
Chapter 5 SQL: Data Manipulation © Pearson Education Limited 1995, 2005.
Bayu Adhi Tama, ST., MTI. Introduction Relational algebra and relational calculus are formal languages associated with the relational.
Advanced Database Systems
1 Pertemuan > > Matakuliah: >/ > Tahun: > Versi: >
Chapter 5 SQL: Data Manipulation. 2 Chapter - Objectives u Purpose and importance of SQL. u How to retrieve data from database using SELECT and: –Use.
Chapter 7 SQL: Data Manipulation Chapter #6 in the textbook Pearson Education © 2009.
Chapter 5 Relational Algebra and Relational Calculus Pearson Education © 2009.
IST 210 SQL Todd Bacastow IST 210: Organization of Data.
Queries SELECT [DISTINCT] FROM ( { }| ),... [WHERE ] [GROUP BY [HAVING ]] [ORDER BY [ ],...]
Chapter 8 Introduction to SQL
Chapter 5 Relational Algebra Pearson Education © 2014.
CSC271 Database Systems Lecture # 8. Summary: Previous Lecture  Relation algebra and operations  Selection (Restriction), projection  Union, set difference,
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.
SQL : Data Manipulation Pertemuan 09 s/d 12 Matakuliah: M0564 /Pengantar Sistem Basis Data Tahun : 2008.
IST 210 More SQL Todd Bacastow IST 210: Organization of Data.
Chapter 6 SQL – Data Manipulation Pearson Education © 2014.
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.
Insert, Update, and Delete Statements DBMS Course.
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.
Teacher Workshop Database Design Pearson Education © 2014.
Chapter 11 SQL: Data Manipulation
Relational Algebra Lecture 2.
SQL : Data Manipulation Pertemuan 07 s/d 08
Tahun : <<2007>> Versi : <<2/1>>
Chapter Name SQL: Data Manipulation
Chapter Name SQL: Data Manipulation
Data Manipulation Language
CS311 Database Management system
SQL – Data Manipulation
Relational Algebra and Relational Calculus
SQL Pertemuan 6 9/17/2018 Sistem Basis Data.
Introduction to SQL Chapter 3.
Chapter Name SQL: Data Manipulation Chapter #6 in the textbook
SQL-2 Week 9-11.
Chapter Name SQL: Data Manipulation Chapter #6 in the textbook
Chapter Name SQL: Data Manipulation
Chapter Name SQL: Data Manipulation
Chapter Name SQL: Data Manipulation
Chapter 4 Summary Query.
SQL – Data Manipulation
Chapter Name SQL: Data Manipulation
The Relational Algebra
Chapter Name SQL: Data Manipulation Transparencies JOINS
Chapter Name SQL: Data Manipulation Transparencies
Chapter Name SQL: Data Manipulation Transparencies
Chapter Name SQL: Data Manipulation
Presentation transcript:

SQL Data Manipulation II Chapter 5 CIS 458 Sungchul Hong

Logical Operator AND, OR, NOT AB A AND BA OR BNOT A FFFFT TFFTF FTFTT TTTTF

Logical Operator DeMorgan’s Law – NOT (A AND B)  – NOT (A OR B)  – NOT (A AND B AND C) 

Row Selection (WHERE clause) Comparison Range Set membership Pattern match Null

Comparison Search Condition SELECT staffNo, fName, lName, position, salary FROM Staff WHERE salary > 10000;

SQL Comparison Operators = equals <> is not equal to < is less than > is greater than <= is less than or equal to >= is greater than or equal to

Order of Evaluation An expression is evaluated left to right Subexpressions in brackets are evaluated first. NOTs are evaluated before ANDs and Ors ANDs are evaluated before ORs

Compound Comparison Search SELECT * FROM Branch WHERE city = ‘London’ OR city = ‘Glasgow’;

Range Search Condition BETWEEN / NOT BETWEEN SELET staffNo, fName, lName, position, salary FROM Staff WHERE salary BETWEEN AND 30000; SELET staffNo, fName, lName, position, salary FROM Staff WHERE salary >= AND salary <= 30000;

Set Membership Search Condition IN / NOT IN SELECT staffNO, fName, lName, positon FROM Staff WHERE position IN (‘Manager’, ‘Supervisor’); SELECT staffNO, fName, lName, positon FROM Staff WHERE position =‘Manager’ OR position = ‘Supervisor’;

Pattern Match Search LIKE / NOT LIKE –% (* in MS): wildcard eg) LIKE ‘H*’ –_: single character eg) LIKE ‘H___’ –If the search string can include the pattern-matching character itself, then –LIKE ’15#%’ ESCAPE ‘#’ –SELECT ownerNo, fName, lName, address, telNO –FROM PrivateOwner –WHERE address LIKE ‘*Glasgow*’;

NULL Search Condition IS NULL / IS NOT NULL SELECT clientNO, viewDate FROM Viewing WHERE propertyNO = ‘PG4’ AND comment IS NULL; *

Sorting Results ORDER BY column identifiers ( ASC or DESC) SELECT staffNO, fName, lName, salary FROM Staff ORDER BY salary DESC;

Multiple Column Ordering SELECT properyNO, type, rooms, rent FROM PropertyForRent ORDER BY type, rent DESC;

Aggregate Functions COUNT: numeric/non-numeric –COUNT(*): counts all the rows –DISTINCT: do not count duplicate values SUM: numeric AVG: numeric MIN: numeric/non-numeric MAX: numeric/non-numeric

COUNT SELECT COUNT(*) AS count FROM PropertyForRent WHERE rent > 350; SELECT COUNT (DISTINCT propertyNO) AS count FROM Viewing WHERE viewDate BETWEEN ‘1-May-01’ AND ’31- May-01’;

Count & SUM SELECT COUNT (StaffNo) AS count, SUM(salary) AS sum FROM Staff WHERE position = ‘Manager’;

MIN, MAX, and AVG SELECT MIN(salary), MAX(salary), AVG(salary) AS avg FROM Staff;

Grouping GROUP BY In SELECT list, only column names, aggregate functions, constants, and expression of previous combination. All column names in the SELECT list must appear in the GROUP BY clause unless the name is used only in an aggregate function. Each item in the SELECT list must be single- valued per group.

GROUP BY Find the number of staff working in each branch and the sum of their salaries. SELECT branchNO, COUNT(staffNo) AS count, SUM(salary) AS sum FROM Staff GROUP BY branchNO ORDER BY branchNO; SELECT branchNO, (SELECT COUNT (staffNo) AS count FROM Staff s WHERE s.branchNo = b.branchNO), (SELECT SUM(salary) AS sum FROM Staff s WHERE s.branchNo = b.branchNo) FROM Branch b ORDER BY branchNO;

GROUP BY The result of GROUP BY branchNOstaffNosalary B003 B005 B007 SG37 SG14 SG5 SL21 SL41 SA COUNT(staffNo)SUM(salary)

GROUP BY branchNOstaffNosalary B003 B005 B007 SG37 SG14 SG5 SL21 SL41 SA branchNOstaffNosalary B003 B005 B007 SG37 SG14 SG5 SL21 SL41 SA bs

GROUP BY & HAVING For each branch office with more than one member of staff, find the number of staff working in each branch and the sum of their salaries. SELECT branchNo, COUNT(staffNo) AS count, SUM (salary) AS sum From Staff GROUP BY branchNO HAVING COUNT(staffNO)>1 ORDER BY branchNO;

Subqueries Inner SELECT statement Subquery or nested query SELECT, INSERT, UPDATE, and DELETE A scalar subquery returns a single column and a single row. A row subquery returns multiple columns, but again only a single row. A table subquery returns on or more columns and multiple rows.

Subquery with Equality List the staff who work in the branch at ‘163 Main St’ SELECT staffNO, fName, lName, position FROM Staff WHERE branchNO = (SELECT branchNO FROM Branch WHERE street = ‘163 Main St’);

Subquery with an Aggregate Function List all staff whose salary is greater than the average salary, and show by how much their salary is greater than the average. SELECT staffNO, fName, lName, position, salary – (SELECT AVG (salary) FROM Staff) AS salDiff FROM Staff WHERE salary > (SELECT AVG(salary) FROM Staff);

Subquery Rules The ORDER BY clause may not be used in a subquery. The subquery SELECT list must consist of a single column name or expression, except for subqueries that use the keyword EXISTS. BY default, column names in a subquery refer to the table name in the FROM clause of the subquery. It is possible to refer to a table in a FROM clause of an outer query by qualifying the column name. When a subquery is one of the two operands involved in a comparison, the subquery must appear on the right- hand side of the comparison.

Nested subqueries: Use of IN List the properties that are handled by staff who work in the branch at ‘163 Main St’. SELECT propertyNo, street, city, postcode, type, rooms, rent FROM PropertyForRent WHERE staffNo IN (SELECT staffNO FROM Staff WHERE branchNO = (SELECT branchNO FROM Branch WHERE street = ‘163 Main St’));

Use of ANY/SOME Find all staff whose salary is larger than the salary of at least one member of staff at branch B003. SELECT staffNO, fName, lName, position, salary FROM Staff WHERE salary > SOME (SELECT salary FROM Staff WHERE branchNo = ‘B003’);

Use of ALL Find all staff whose salary is larger than the salary of every member of staff at branch B003. SELECT staffNO, fName, lName, position, salary FROM Staff WHERE salary > ALL (SELECT salary FROM Staff WHERE branchNo =‘B003’);

Multi-Table Queries List the names of all clients who have viewed a property along with any comment supplied. SELECT c.clientNo, fName, lName, proertyNo, comment FROM Client c, Viewing v WHERE c.clientNo = v.clientNo

Database Updates INSERT UPDATE DELETE

INSERT INSERT INTO Staff VALUES (‘SG16’, ‘Alan’, ‘Brown’, ‘Assistant’, ‘M’, DATE ‘ ’, 8300, ‘B003’)

UPDATE UPDATE Staff SET salary = salary*1.03

DELETE DELETE FROM Viewing WHERE propertyNo = ‘PG4’;