Advanced SQL for Decision Support ISYS 650. Set Operators Union Intersect Difference Cartesian product.

Slides:



Advertisements
Similar presentations
© Abdou Illia MIS Spring 2014
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.
TURKISH STATISTICAL INSTITUTE 1 /34 SQL FUNDEMANTALS (Muscat, Oman)
Database Management Systems 3ed, R. Ramakrishnan and J. Gehrke1 SQL: Queries, Programming, Triggers Chapter 5 Modified by Donghui Zhang.
SQL Sub (or Nested ) Query. Examples Q: Find students whose GPA is below the average. –The criteria itself requires a SQL statement. –SELECT * FROM student.
Set operators (UNION, UNION ALL, MINUS, INTERSECT) [SQL]
Chapter 11 Group Functions
Querying Database ISYS 363. Basic Query Language Operations Selection Projection Join Aggregates: Max, Min, Sum, Avg, Count –Totals and SubTotals –GroupBy.
Decision Support and Data Warehouse. Decision supports Systems Components Data management function –Data warehouse Model management function –Analytical.
SQLPlus Commands. Oracle Account Server: libra.sfsu.edu Telnet: libra.sfsu.edu How to use Oracle: –
Structured Query Language Review ISYS 650. Language Overview Three major components: –Data definition language, DDL Create, Drop and Alter Tables or Views.
1 Minggu 4, Pertemuan 8 SQL: Data Manipulation (Cont.) Matakuliah: T0206-Sistem Basisdata Tahun: 2005 Versi: 1.0/0.0.
Database Systems: Design, Implementation, and Management Eighth Edition Chapter 8 Advanced SQL.
Using Relational Databases and SQL Steven Emory Department of Computer Science California State University, Los Angeles Lecture 5: Subqueries and Set Operations.
Basic SQL Select Commands. Basic Relational Query Operations Selection Projection Natural Join Sorting Aggregation: Max, Min, Sum, Count, Avg –Total –Sub.
Querying Database ISYS 363. Basic Query Language Operations Selection Projection Join Aggregates: Max, Min, Sum, Avg, Count –Totals and SubTotals –GroupBy.
Introduction to Access BUS 782. Access Objects Tables –Open –Design –New –Wizard Queries Forms Reports Pages.
Relational Algebra. Set operations: Union, intersection, difference, Cartesian product Relational operations: Selection, projection, join, division.
Querying Database. Basic Query Language Operations Selection Projection Join Aggregates: Max, Min, Sum, Avg, Count –Totals and SubTotals –GroupBy Calculated.
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.
Data Warehousing. On-Line Analytical Processing (OLAP) Tools The use of a set of graphical tools that provides users with multidimensional views of their.
Relational Operators, SQL, and Access Query ISYS 562.
Database Systems: Design, Implementation, and Management Tenth Edition Chapter 8 Advanced SQL.
Introduction to Databases Chapter 7: Data Access and Manipulation.
Relational Query Operators (ii). Other Joins Theta join: Apply any specified condition on the product of two relations. Example: BoyStudent: BID, Bname,
SQL advanced select using Oracle 1 7. Multiple Tables: Joins and Set Operations 8. Subqueries: Nested Queries.
ITBIS373 Database Development
Oracle Database Administration Lecture 2 SQL language.
1 CS 430 Database Theory Winter 2005 Lecture 12: SQL DML - SELECT.
INLS 623– S QL Instructor: Jason Carter. SQL SELECT DISTINCT SELECT DISTINCT column_name, column_name FROM table_name ;
Chapter 6 SQL: Data Manipulation (Advanced Commands) Pearson Education © 2009.
Joins & Sub-queries. Oracle recognizes that you may want data that resides in multiple tables drawn together in some meaningful way. One of the most important.
Data Warehousing.
Oracle DML Dr. Bernard Chen Ph.D. University of Central Arkansas.
11 Copyright © Oracle Corporation, All rights reserved. Creating Views.
1 Agenda – 10/24/2013 Answer questions from lab on 10/22. Present SQL View database object. Present SQL UNION statement.
Database Programming Sections 11 & 12 – Creating, and Managing Views, Sequences, Indexes, and Synonymns.
Information Technologies and Microsoft SQL Server Day 2 by Alper Özpınar
6 1 Lecture 8: Introduction to Structured Query Language (SQL) J. S. Chou, P.E., Ph.D.
MIS 3053 Database Design & Applications The University of Tulsa Professor: Akhilesh Bajaj RM/SQL Lecture 5 © Akhilesh Bajaj, 2000, 2002, 2003, All.
Introduction to Access ISYS 363. Creating a New Database MS Office button/New –Blank database –New database name and location.
Relational Query Operators (Algebra). Relational Query Operators Set operations: Union, intersection, difference, Cartesian product Relational operations:
Structured Query Language Introduction. Basic Select SELECT lname, fname, phone FROM employees; Employees Table LNAMEFNAMEPHONE JonesMark SmithSara
AL-MAAREFA COLLEGE FOR SCIENCE AND TECHNOLOGY INFO 232: DATABASE SYSTEMS CHAPTER 7 (Part II) INTRODUCTION TO STRUCTURED QUERY LANGUAGE (SQL) Instructor.
SQL advanced select using Oracle 1. 2 Select Simple –data from a single table Advanced –data from more tables join sub-queries.
Lecture 8 – SQL Joins – assemble new views from existing tables INNER JOIN’s The Cartesian Product Theta Joins and Equi-joins Self Joins Natural Join.
Querying Database ISYS 363.
Copyright © 2011 Pearson Education, Inc. Publishing as Pearson Addison-Wesley Chapter 6 The Relational Algebra and Relational Calculus.
SQL SELECT Getting Data from the Database. Basic Format SELECT, FROM WHERE (=, >, LIKE, IN) ORDER BY ; SELECT LastName, FirstName, Phone, City FROM Customer.
A Guide to SQL, Eighth Edition Chapter Five Multiple-Table Queries.
Introduction to Access BUS 782. Creating a New Database MS Office button/New –Blank database –New database name and location.
Database Management Systems 3ed, R. Ramakrishnan and J. Gehrke1 Database Management Systems Chapter 5 SQL.
Data Warehousing.
1 SQL – IV Grouping data from tables in SQL –The concept of grouping –GROUP BY clause –HAVING Clause –Determining whether values are unique –Group by using.
Introduction to Access ISYS 363. Access Objects Tables –Open –Design –New –Wizard Queries Forms Reports Pages.
SQL Miscellaneous Topics. Views A database view is: – a virtual or logical table based on a query. – a stored query. CREATE VIEW viewname AS query; –CREATE.
SQL advanced select using Oracle 1 Multiple Tables: Joins and Set Operations Subqueries: Nested Queries.
Query Processing – Implementing Set Operations and Joins Chap. 19.
SQL BY MR.PRASAD SAWANT INDIA Introduction to SQL What is SQL? –When a user wants to get some information from a database file, he can issue a query.1.
Introduction to Access ISYS 363. Access Objects Tables –Open a table: Double click the table name –Home/View: Datasheet view Design view Queries Forms.
Set Operators. Union Intersect Difference Cartesian product.
7 1 Database Systems: Design, Implementation, & Management, 7 th Edition, Rob & Coronel 7.6 Advanced Select Queries SQL provides useful functions that.
1 Ch. 11: Grouping Things Together  ANSI standard SQL Group functions: AVG, COUNT, MAX, MIN, STDDEV, SUM, VARIANCE  Others: 8i: GROUPING (used with CUBE.
SQL: Structured Query Language It enables to create and operate on relational databases, which are sets of related information stored in tables. It is.
CS3220 Web and Internet Programming More SQL
Chapter Name SQL: Data Manipulation
Querying Database ISYS 363.
Chapter 8 Advanced SQL.
Database Systems: Design, Implementation, and Management Tenth Edition
CSC 453 Database Systems Lecture
Presentation transcript:

Advanced SQL for Decision Support ISYS 650

Set Operators Union Intersect Difference Cartesian product

Union Set1={A, B, C} Set2={C, D, E} Union: Members in Set 1 or in Set 2 –Set1 U Set 2 = {A, B, C, D, E}

Intersect Members in Set 1 and in Set 2 –Set1 ∩ Set2={C}

Difference Set1={A, B, C} Set2={C, D, E} Set1 – Set2: Members in Set1 but not in set2 = {A,B} Set2 – Set1:Members in Set2 but not in set1 = {D, E} Set1-Set2 ≠ Set2 – Set1 Logical operator: NOT

Files as Sets Business students’ file: BusSt Science student’s file: SciSt –BusSt U SciSt: –BusSt ∩ SciSt –BusSt – SciSt Spring 06 Student file: S06St Fall 06 Student file: F06St –S06St – F06St –F06St – S06St

Union Compatibility Two relations that have the same number of attributes and same type of attributes. Union, Intersect and difference operators require the two relations to be union compatible.

Union Compatibility Examples File 1: –SID – 9 characters –Sname – 25 characters File 2: –SSN – 9 characters –Ename – 25 characters File 3: –Ename – 25 characters –EID – 9 characters File 1 and file 2 are union compatible; file 1 and file 3 are not; file 2 and file 3 are not.

Product Set1 = {a, b, c} Set2 = {X, Y, Z} Set1 X Set2 = {aX, aY aZ, bX, bY, bZ, cX, cY, cZ}

Faculty File: FIDFname F1Chao F2Smith Student File: SIDSname FID S1PeterF1 S2PaulF2 S3SmithF1 Faculty X Student:

SQL Set Operators Union compatible Union: –(SELECT * FROM table1) UNION (SELECT * FROM table2); Intersect: (SELECT * FROM table1) INTERSECT (SELECT * FROM table2); Minus: (SELECT * FROM table1) MINUS (SELECT * FROM table2);

Find students taking 263 and acct 101 SELECT sid FROM registration WHERE cid='ISYS263' INTERSECT SELECT sid FROM registration WHERE cid='acct101‘; Note 1: This condition is always false: –SELECT sid FROM registration –WHERE cid='ISYS263‘ AND cid='acct101‘; Note 2: How to get student name? –SELECT sid,sname FROM student –WHERE sid IN ( ….);

Use IN to do intersect SELECT sid,sname FROM student WHERE sid IN ( SELECT sid FROM registration WHERE cid='ISYS263‘) AND sid IN (SELECT sid FROM registration WHERE cid='acct101‘);

Use NOT IN to do difference Q: Display faculty’s name and phone if the faculty does not advise any student. –SELECT fid, fname FROM faculty –WHERE fid NOT IN (SELECT DISTINCT fid FROM student);

Cartesian Product SELECT fields FROM table1, table2; SELECT * FROM student, faculty; table name alias: Ex: SELECT aid,sname,s.fid,fname FROM student s, faculty f; Take the product of a table itself. –SELECT * FROM emp e1, emp e2; –SELECT * FROM student s1, student s2;

CROSS JOIN = Product SELECT * FROM student CROSS JOIN course;

Access Demo How to do product with Access? Union? Access query wizards: –Find duplicates query. –Find unmatched query

SQL GROUPING SETS GROUPING SETS –SELECT CITY,RATING,COUNT(CID) FROM CUSTOMERS –GROUP BY GROUPING SETS(CITY,RATING,(CITY,RATING),()) – ORDER BY CITY; Note: Compute the subtotals for every member in the GROUPING SETS. () indicates that an overall total is desired.

Results CITY Rating COUNT(CID) CHICAGO A 1 CHICAGO B 2 CHICAGO 3 LOS ANGELES A 1 LOS ANGELES C 1 LOS ANGELES 2 SAN FRANCISCO A 2 SAN FRANCISCO B 1 SAN FRANCISCO 3 A 4 8 CITY R COUNT(CID) B 3 C 1

SQL CUBE Perform aggregations for all possible combinations of columns indicated. –SELECT CITY,RATING,COUNT(CID) FROM CUSTOMERS –GROUP BY CUBE(CITY,RATING) –ORDER BY CITY, RATING;

Results CITY Rating COUNT(CID) CHICAGO A 1 CHICAGO B 2 CHICAGO 3 LOS ANGELES A 1 LOS ANGELES C 1 LOS ANGELES 2 SAN FRANCISCO A 2 SAN FRANCISCO B 1 SAN FRANCISCO 3 A 4 B 3 CITY R COUNT(CID) C 1 8

SQL ROLLUP The ROLLUP extension causes cumulative subtotals to be calculated for the columns indicated. If multiple columns are indicated, subtotals are performed for each of the columns except the far-right column. –SELECT CITY,RATING,COUNT(CID) FROM CUSTOMERS – GROUP BY ROLLUP(CITY,RATING) – ORDER BY CITY, RATING;

Results CITY Rating COUNT(CID) CHICAGO A 1 CHICAGO B 2 CHICAGO 3 LOS ANGELES A 1 LOS ANGELES C 1 LOS ANGELES 2 SAN FRANCISCO A 2 SAN FRANCISCO B 1 SAN FRANCISCO 3 8

Views A database view is: – a virtual or logical table based on a query. – a stored query. CREATE VIEW viewname AS query; –CREATE VIEW femalestudent AS – SELECT * FROM student WHERE sex=‘f’; CREATE OR REPLACE VIEW femalestudent AS SELECT * FROM student WHERE sex=‘f’;

READ ONLY Views CREATE VIEW viewname AS query –WITH READ ONLY; Ex: –CREATE VIEW readEmp –AS (SELECT * FROM emp) –WITH READ ONLY;

ROWNUM Field & Top n Analysis ROWNUM field is a pseudocolumn that applies to every table and view. Use ROWNUM to do Top n Analysis: –Select students with the best 3 GPA Create a view order by GPA, then select from view with rownum <=3. Or using InLineView

InLine View When a multiple-column subquery is used in the FROM clause of an outer query, it basically creates a temporary table that can be referenced by other clauses of the outer query. The temporary table is called InLine view.

Select from InLine View SELECT GPAGroup, Count(SID), avg(gpa) FROM (SELECT SID,GPA, CASE WHEN gpa < 2.0 THEN 'Poor' WHEN gpa < 3.0 THEN 'Good' ELSE 'Excellent' END AS GPAGroup, FROM student) GROUPBY GPAGroup; Note: Calculated field cannot be used in the Where or Group By clause. Select eid,ename, salary*.1 As Tax From Employee Where Tax > 1000; ---- This will cause error.

InLine View &Top (Last) n Analysis Find students with the top 3 GPA. Can we do: –SELECT * FROM student ORDER BY GPA desc WHERE ROWNUM<=3; ? --- No! SELECT * FROM (SELECT * FROM student ORDER BY GPA DESC) WHERE ROWNUM<=3; Note: Use the ROWNUM of the InLineView.

Using RANK() for Top N select first_name, last_name from ( SELECT first_name, last_name, RANK() OVER (ORDER BY salary DESC) sal_rank FROM employees ) WHERE sal_rank <= 10; RANK calculates the rank of a value in a group of values. The return type is NUMBER.

Pivot Table Demo: # of customer by City and Rating select city, rating, count(*) cnt from customer group by city, rating ; select city, max( decode( Rating, 'A', cnt, 0 ) ) A, max( decode( Rating, 'D', cnt, 0 ) ) D, max( decode( Rating, 'Z', cnt, 0 ) ) Z from ( select city, rating, count(*) cnt from customer group by city, rating ) group by City;

Indexes Field declared as PRIMARY KEY will have an index. CREATE INDEX indexname –ON tablename (column names separated by commas); –Ex: CREATE INDEX fkFID ON student (fid); DROP INDEX indexname;