Instructor: Craig Duckett. Lecture 11: Thursday, May 3 th, Set Operations, Subqueries, Views

Similar documents
Instructor: Craig Duckett. Lecture 04: Thursday, April 5, Relationships

Querying Data with Transact SQL

Instructor: Craig Duckett. Lecture 03: Tuesday, April 3, 2018 SQL Sorting, Aggregates and Joining Tables

Instructor: Craig Duckett. Lecture 14: Tuesday, May 15 th, 2018 Stored Procedures (SQL Server) and MySQL

Querying Data with Transact-SQL

CS121 MIDTERM REVIEW. CS121: Relational Databases Fall 2017 Lecture 13

MANAGING DATA(BASES) USING SQL (NON-PROCEDURAL SQL, X401.9)

Querying Data with Transact SQL Microsoft Official Curriculum (MOC 20761)

T-SQL Training: T-SQL for SQL Server for Developers

Interview Questions on DBMS and SQL [Compiled by M V Kamal, Associate Professor, CSE Dept]

Querying Microsoft SQL Server 2014

Ian Kenny. November 28, 2017

Querying Data with Transact-SQL

Course Outline. Querying Data with Transact-SQL Course 20761B: 5 days Instructor Led

Querying Data with Transact-SQL

Querying Data with Transact-SQL

20761B: QUERYING DATA WITH TRANSACT-SQL

A subquery is a nested query inserted inside a large query Generally occurs with select, from, where Also known as inner query or inner select,

In This Lecture. Yet More SQL SELECT ORDER BY. SQL SELECT Overview. ORDER BY Example. ORDER BY Example. Yet more SQL

Querying Data with Transact-SQL (20761)

$99.95 per user. Writing Queries for SQL Server (2005/2008 Edition) CourseId: 160 Skill level: Run Time: 42+ hours (209 videos)

MTA Database Administrator Fundamentals Course

Microsoft Querying Microsoft SQL Server 2014

Exam code: Exam name: Database Fundamentals. Version 16.0

Midterm Review. Winter Lecture 13

Duration Level Technology Delivery Method Training Credits. Classroom ILT 5 Days Intermediate SQL Server

Index. Bitmap Heap Scan, 156 Bitmap Index Scan, 156. Rahul Batra 2018 R. Batra, SQL Primer,

CPS221 Lecture: Relational Database Querying and Updating

Mobile MOUSe MTA DATABASE ADMINISTRATOR FUNDAMENTALS ONLINE COURSE OUTLINE

Querying Data with Transact-SQL

CPS221 Lecture: Relational Database Querying and Updating

You can write a command to retrieve specified columns and all rows from a table, as illustrated

Relational Database Development

Instructor: Craig Duckett. Lecture 02: Thursday, March 29 th, 2018 SQL Basics and SELECT, FROM, WHERE

Instructor: Craig Duckett. Lecture 07: Tuesday, April 17 th, 2018 Conflicts and Isolation, MySQL Workbench

Certification Exam Preparation Seminar: Oracle Database SQL

Querying Data with Transact-SQL

Oracle Database 10g: Introduction to SQL

20761C: Querying Data with Transact-SQL

Join (SQL) - Wikipedia, the free encyclopedia

Language. f SQL. Larry Rockoff COURSE TECHNOLOGY. Kingdom United States. Course Technology PTR. A part ofcenqaqe Learninq

Database Management System Dr. S. Srinath Department of Computer Science & Engineering Indian Institute of Technology, Madras Lecture No.

SQL Queries. for. Mere Mortals. Third Edition. A Hands-On Guide to Data Manipulation in SQL. John L. Viescas Michael J. Hernandez

QUERYING MICROSOFT SQL SERVER COURSE OUTLINE. Course: 20461C; Duration: 5 Days; Instructor-led

Introduction to Computer Science and Business

[AVNICF-MCSASQL2012]: NICF - Microsoft Certified Solutions Associate (MCSA): SQL Server 2012

Querying Microsoft SQL Server (MOC 20461C)

SQL - Data Query language

Querying Microsoft SQL Server

Querying Microsoft SQL Server

COURSE OUTLINE: Querying Microsoft SQL Server

Data about data is database Select correct option: True False Partially True None of the Above

Oracle Database 11g: SQL and PL/SQL Fundamentals

Table of Contents. PDF created with FinePrint pdffactory Pro trial version

20461D: Querying Microsoft SQL Server

CS211 Lecture: Database Querying and Updating

AVANTUS TRAINING PTE LTD

Querying Microsoft SQL Server

Chapter 2 Introduction to Relational Models

Unit Assessment Guide

Querying Microsoft SQL Server

Structured Query Language (SQL)

"Charting the Course to Your Success!" MOC D Querying Microsoft SQL Server Course Summary

Database Systems: Design, Implementation, and Management Tenth Edition. Chapter 8 Advanced SQL

20461: Querying Microsoft SQL Server

Querying Data with Transact-SQL

Oracle Syllabus Course code-r10605 SQL

Querying Microsoft SQL Server 2008/2012

Querying Microsoft SQL Server 2012/2014

Wentworth Institute of Technology COMP570 Database Applications Fall 2014 Derbinsky. Physical Tuning. Lecture 10. Physical Tuning

SQL Interview Questions

NESTED QUERIES AND AGGREGATION CHAPTER 5 (6/E) CHAPTER 8 (5/E)

SECTION 1 DBMS LAB 1.0 INTRODUCTION 1.1 OBJECTIVES 1.2 INTRODUCTION TO MS-ACCESS. Structure Page No.

Querying Microsoft SQL Server

Lecture 3 SQL - 2. Today s topic. Recap: Lecture 2. Basic SQL Query. Conceptual Evaluation Strategy 9/3/17. Instructor: Sudeepa Roy

6.830 Lecture PS1 Due Next Time (Tuesday!) Lab 1 Out end of week start early!

Microsoft Access 2016

Informatics 1: Data & Analysis

Course 20461C: Querying Microsoft SQL Server

Microsoft Access 2016

The Relational Algebra

Querying Microsoft SQL Server 2014

Wentworth Institute of Technology COMP2670 Databases Spring 2016 Derbinsky. Physical Tuning. Lecture 12. Physical Tuning

Oracle 1Z Oracle Database 12c SQL. Download Full Version :

RELATIONAL ALGEBRA II. CS121: Relational Databases Fall 2017 Lecture 3

CSC 261/461 Database Systems Lecture 5. Fall 2017

CMP-3440 Database Systems

Lab # 6. Using Subqueries and Set Operators. Eng. Alaa O Shama

20461: Querying Microsoft SQL Server 2014 Databases

normalization are being violated o Apply the rule of Third Normal Form to resolve a violation in the model

Final Exam Review 2. Kathleen Durant CS 3200 Northeastern University Lecture 23

Course Outline and Objectives: Database Programming with SQL

1 Writing Basic SQL SELECT Statements 2 Restricting and Sorting Data

CSE 565 Computer Security Fall 2018

CS317 File and Database Systems

Chapter 2. DB2 concepts

COGNOS (R) 8 GUIDELINES FOR MODELING METADATA FRAMEWORK MANAGER. Cognos(R) 8 Business Intelligence Readme Guidelines for Modeling Metadata

Relational Data Model

Ryan Stephens. Ron Plew Arie D. Jones. Sams Teach Yourself FIFTH EDITION. 800 East 96th Street, Indianapolis, Indiana, 46240

Data Base Concepts. Course Guide 2

Transcription:

Instructor: Craig Duckett Lecture 11: Thursday, May 3 th, 2018 Set Operations, Subqueries, Views 1

MID-TERM EXAM GRADED! Assignment 2 is due LECTURE 12, NEXT Tuesday, May 8 th in StudentTracker by MIDNIGHT Assignment 3 is due LECTURE 20, Tuesday, June 5 th Database Presentation is due LECTURE 20, Tuesday, June 5 th Final Exam is LECTURE 21, Thursday, June 7 th 2

3 x 150 Points (450 points Total) Assignment 1 GRADED! Assignment 2 (Stage 2): NEXT Tuesday, May 8 th Assignment 3 (Stage 3): DUE LECTURE 20 Tuesday, June 5 th Database Presentation: DUE LECTURE 20 Tuesday, June 5 th 3

Tuesday (LECTURE 10) Database Design for Mere Mortals: Chapter 7 Thursday (LECTURE 11) The Language of SQL: Chapters 9, 10 4

Mid-Term Post-Mortem Set Operations [LoSQL, Chapter 15] Subqueries [LoSQL, Chapter 14] Views [LoSQL, Chapter 13] PLEASE NOTE: I am introducing these topics now even though you haven t read about them yet in The Language of SQL book. This way I am planting seeds to let you know that this functionality is available, and to remove some of the mystery from them when you finally get to them in the reading. 5

RECOMMENDED SITE: SoloLearn http://www.sololearn.com/ 6

Mid-Term Post-Mortem 7

Mid-Term Post-Mortem Answers Ideal Field, Ideal Table Ideal Table: It represents a single subject which can be an object or an event http://www.databaseanswers.org/data_models/ Purpose of database is threefold: A repository of data The gathering of data (interface, multiple users) The collection of information from the data Extra Credit 100% + Extra Credit (my idea was that the extra credit was meant to supplement points towards 100) 100% + Extra Credit (less points available) 100% + extra credit truncated to 100 points possible 100% + Bonus question (with no possible points advertised) Bansenauer No extra credit

Set Operations 9

Set Operations Set theory is fundamental to the relational model. But whereas mathematical sets are unchanging, database sets are dynamic they grow, shrink, and otherwise change over time. This section covers the following SQL set operators, which combine the results of two SELECT statements into one result: UNION returns all the rows returned by both queries, with duplicates removed. INTERSECT returns all rows common to both queries (that is, all distinct rows retrieved by both queries). EXCEPT returns all rows from the first query without the rows that appear in the second query, with duplicates removed. These set operations are not joins, but you can mix and chain them to combine two or more tables.

Set Operations: UNION Operator The SQL UNION operator is used to combine the result sets of two or more SELECT statements. It removes duplicate rows between the various SELECT statements. Each SELECT statement within the UNION must have the same number of column fields in the result sets and all with the same data types. UNION can be useful in data warehouse applications where tables aren't perfectly normalized. A simple example would be a database having tables sales2005 and sales2006 that have identical structures but are separated because of performance considerations. A UNION query could combine results from both tables. Note that UNION does not guarantee the order of rows. Rows from the second operand may appear before, after, or mixed with rows from the first operand. In situations where a specific order is desired, ORDER BY must be used. Note that UNION ALL may be much faster than plain UNION. We will look at both of these in a moment. http://www.w3schools.com/sql/sql_union.asp http://www.w3resource.com/sql/sql-union.php http://www.techonthenet.com/sql/union.php http://www.tizag.com/sqltutorial/sqlunion.php

Difference Between SQL JOIN and a UNION Joins and Unions can be used to combine data from one or more tables. The difference lies in how the data is combined. In simple terms, joins combine data into new columns. If two tables are joined together, then the data from the first table is shown in one set of column alongside the second table s column in the same row. Unions combine data into new rows. If two tables are unioned together, then the data from the first table is in one set of rows, and the data from the second table is in another set of rows. To repeat: in order to union two tables there are a couple of requirements: The number of columns must be the same for both SELECT statements. The columns, in order, must be of the same data type.

REVIEW: JOIN Here is a visual depiction of a join. Table A and Table B s columns are combined into a single result. With a JOIN you can pick and choose what columns will be returned Each row in the result contains columns from BOTH Table A and Table B. Rows are created when columns from one table match columns from another table. This match is called the join condition. This makes joins really great for looking up values and including them in results. This is usually the result of denormalizing (reversing normalization) and involves using the foreign key in one table to look up column values by using the primary key in another.

UNION Now compare the previous depiction with that of a union. In a union each row within the result is from one table OR the other. In a union, columns are not combined to create results, rows are combined. Unions are typically used where you have two results whose rows you want to include in the same result. A use case may be that you have two tables: Teachers and Students. You would like to create a master list of names and birthdays sorted by date. To do this you can use a union to first combine the rows into a single result and then sort them. Let s now take slightly deeper look into both.

Combining Data with a Join In this section well look at the inner join. It is one of the most common forms of join and is used when you need to match rows from two tables. Rows that match remain in the result, those that don t are rejected. SELECT Employee.NationalIDNumber, Person.FirstName, Person.LastName, Employee.JobTitle FROM Employee JOIN Person ON Employee.BusinessEntityID = Person.BusinessEntityID ORDER BY Person.LastName; Notice the join condition, see how we are matching BusinessEntityID from both the Employee and Person tables. Check out that the results contains columns from both the Employee and Person tables.

Set Operations: UNION Operator Now let s take a look at UNION and how it works Note that there are two rows for Joe because those rows are distinct across their columns. There is only one row for Alex because those rows are not distinct for both columns.

Set Operations: UNION Operator UNION ALL gives different results, because it will not eliminate duplicates. Executing this statement would give these results, again allowing variance for the lack of an ORDER BY statement:

Set Operations: UNION Operator

Set Operations: INTERSECT Operator MySQL does not support the INTERSECT operator The SQL INTERSECT operator is used to return the results of two or more SELECT statements. However, it only returns the rows selected by all queries. If a record exists in one query and not in the other, it will be omitted from the INTERSECT results. Each SQL statement within the SQL INTERSECT must have the same number of fields in the result sets with similar data types. For purposes of duplicate removal the INTERSECT operator does not distinguish between NULLs. The INTERSECT operator removes duplicate rows from the final result set. The INTERSECT ALL operator does not remove duplicate rows from the final result set. http://www.techonthenet.com/sql/intersect.php http://www.tutorialspoint.com/sql/sql-intersect-clause.htm http://www.essentialsql.com/learn-to-use-union-intersect-and-except-clauses/

Set Operations: INTERSECT Operator MySQL does not support the INTERSECT operator Example : The following example INTERSECT query returns all rows from the Orders table where Quantity is between 50 and 100.

Set Operations: EXCEPT Operator MySQL does not support the EXCEPT operator Example The following example EXCEPT query returns all rows from the Orders table where Quantity is between 1 and 49, and those with a Quantity between 76 and 100. Worded another way: the query returns all rows where the Quantity is between 1 and 100, except from rows where the quantity is between 50 and 75.

Set Operations: EXCEPT Operator The following example is equivalent to the above example but without using the EXCEPT operator. Instead it utilizes a JOIN and subqueries, which we will be looking at next

Subqueries NOTE: We ll look at this a few times with different examples. We will also look at it again in greater detail once we have Microsoft SQL Server up and running. 23

SQL Subqueries Subqueries are query statements tucked inside of query statements. Like the order of operations from Algebra class, order of operations also come into play when you start to embed SQL commands inside of other SQL commands (subqueries). Let's take a look at an example involving the toys table and figure out how to select only toys with the greatest number of legs. To accomplish this, we are first going to introduce a built-in SQL function, MAX(). This function wraps around a table column and quickly returns the current maximum (max) value for the specified column. We are going to use this function to return the names of all those toys that possess the current "max" (maximum) number of legs. Query 1 Query 2 -or- SELECT MAX(numberLegs) FROM Toys -- Let s say this returns 6 SELECT * FROM Toys WHERE numberlegs = 6; -- manually put in the 6 Query with a subquery SELECT * FROM Toys WHERE numberlegs = (SELECT MAX(numberLegs) FROM Toys) http://www.w3schools.com/sql/sql_func_max.asp

SQL Subqueries A subquery is a SQL query nested inside another query. A subquery may occur in a: SELECT * FROM Toys WHERE numberlegs = (SELECT MAX(numberLegs) FROM Toys) SELECT clause FROM clause WHERE clause The subquery can be nested inside a SELECT, INSERT, UPDATE, or DELETE statement or inside another subquery. A subquery is usually added within the WHERE Clause of another SQL SELECT statement. You can use the comparison operators, such as >, <, or =. The comparison operator can also be a multiple-row operator, such as IN, ANY, or ALL. A subquery is treated as an inner query, which is a query placed as part of an outer query. The inner query executes first before its parent query so that the results of inner query can be passed to the outer query.

SQL Subqueries Basic SQL Query

SQL Subqueries

SQL Subqueries Students table

SQL Subqueries Tests table

SQL Subqueries

SQL Subqueries

SQL Subqueries

SQL Subqueries

SQL Subqueries This is what is returned by the subquery

SQL Subqueries

SQL Subqueries

SQL Subqueries

SQL Subqueries in with a list = with single row http://www.w3schools.com/sql/sql_in.asp

SQL Subqueries

Northwind Database Example Without Subquery SELECT CompanyName FROM Customers WHERE CustomerID =? SELECT CustomerID FROM Orders WHERE OrderID = 10290; [RESULT: COMMI] With Subquery SELECT CompanyName FROM Customers WHERE CustomerID = (SELECT CustomerID FROM Orders WHERE OrderID = 10290); [RESULT: Comércio Mineiro] SELECT CompanyName FROM Customers WHERE CustomerID = COMMI [RESULT: Comércio Mineiro]

SQL Subqueries W3Resource http://www.w3resource.com/sql/subqueries/understanding-sql-subqueries.php http://www.w3resource.com/sql/subqueries/single-row-subqueries.php http://www.w3resource.com/sql/subqueries/multiplee-row-column-subqueries.php http://www.w3resource.com/sql/subqueries/nested-subquerie.php

Views We ll look at VIEWs in much greater detail once we are up and running in Microsoft SQL Server 42

SQL Views SQL VIEWS are data objects, and like SQL Tables, they can be queried, updated, and dropped. A SQL VIEW is a virtual table containing columns and rows except that the data contained inside a view is generated dynamically from SQL tables and does not physically exist inside the view itself. You can add SQL functions, WHERE, and JOIN statements to a view and present the data as if the data were coming from one single table. http://www.tutorialspoint.com/sql/sql-using-views.htm http://www.w3schools.com/sql/sql_view.asp http://www.techonthenet.com/sql/views.php http://www.mysqltutorial.org/mysql-views-tutorial.aspx http://www.sqlinfo.net/sqlserver/sql_server_views_the_basics.php

SQL Views Creating a View with CREATE VIEW Think of a view as being a tailored presentation that provides a tabular window into one or more base tables. The window can display an entire base table, part of a base table, or a combination of base tables (or parts thereof). A view also can reflect the data in base tables through other views windows into windows. Generally, SQL programmers use views to present data to end-users in database applications. Views offer these advantages: Simplified data access. Views hide data complexity and simplify statements, so users can perform operations on a view more easily than on the base tables directly. If you create a complex view one that involves, say, multiple base tables, joins, and subqueries users can query this view without having to understand complex relational concepts or even knowing that multiple tables are involved. Automatic updating. When a base table is updated, all views that reference the table reflect the change automatically. If you insert a row representing a new author into the table authors, for example, all views defined over authors will reflect the new author automatically. This scheme saves disk space and prevents redundancy because, without views, the DBMS would have to store derived data to keep it synchronized.

SQL Views Creating a View with CREATE VIEW (CONTINUED) Increased security. One of the most common uses of views is to hide data from users by filtering the underlying tables. Suppose that the table employees contains the columns salary and commission. If you create a view on employees that omits these two columns but contains other innocuous columns (such as email_address), the database administrator can grant users permission to see the view but not see the underlying table, thereby hiding compensation data from the curious. Logical data independence. Base tables provide a real view of a database. But when you use SQL to build a database application, you want to present end users not the real view, but a virtual view specific to the application. The virtual view hides the parts of the database (entire tables or specific rows or columns) that aren t relevant to the application. Thus, users interact with the virtual view, which is derived from though independent of the real view presented by the base tables.

SQL Views -- TO CREATE A VIEW CREATE VIEW view [(view_columns)] AS select_statement; view is the name of the view to create. The view name must be unique within the database. view_columns is an optional, parenthesized list of one or more comma-separated names to be used for the columns in view. The number of columns in view_columns must match the number of columns in the SELECT clause of select_statement. (If you name one column this way, you must name them all this way.) If view_columns is omitted, view inherits column names from select_statement. Column names also can be assigned in select_statement via AS clauses. Each column name must be unique within the view. select_statement is a SELECT statement that identifies the columns and rows of the table(s) that the view is based on. select_statement can be arbitrarily complex and use more than one table or other views. An ORDER BY clause usually is prohibited. - - TO CREATE A VIEW CREATE VIEW CurrentBiddersID AS SELECT Bidders.name FROM Bidders WHERE Bidders.BidderID IN (SELECT Bidders.bidderID FROM CurrentBids); - - TO QUERY A VIEW SELECT * FROM CurrentBiddersID

SQL Views (We will go over these once we start using Microsoft SQL Server) When you re creating a view, some important considerations are: View names follow the same rules that table names do. View names must be unique within a schema (or database). They can not have the same name as any other table or view. The columns in a view inherit the default column names from the underlying tables. You can give view columns different names by using AS You must specify a new name for a column in a view that would have the same name as another column in the view (usually because the view definition includes a join and the columns from two or more different underlying tables have the same name). A column defined in a view can be a simple column reference, a literal, or an expression that involves calculations or aggregate functions. In some DBMSs, you must specify explicitly the name of a column in a view if the column is derived from an arithmetic expression, a built-in function, or a literal. A view column inherits the data type of the column or expression from which it is derived. You have no practical limit on the number of views that you can create. Generally, you want to create views on subsets of data that are of interest to many users. Almost any valid SELECT statement can define a view, though an ORDER BY clause usually is prohibited.

SQL Views (CONTINUED) You can nest views that is, a view s SELECT statement can retrieve data from another view. Nested views eventually must resolve to base tables (otherwise, you d be viewing nothing) The maximum number of nesting levels varies by DBMS. You can use views as a convenience to save complex queries. By saving a query that performs extensive calculations as a view, you can recalculate each time the view is queried. A view can express a query that you d otherwise be unable to run. You can define a view that joins a GROUP BY view with a base table, for example, or define a view that joins a UNION view with a base table. A view definition can t reference itself, because it doesn t exist yet. Views can display data formatted differently from those in the underlying tables. Unlike a base table, a view does not support constraints. When you define a view by using SELECT *, SQL converts the * to a list of all columns internally. This conversion occurs only once, at view creation (not at execution), so the definition of your view will not change if someone adds a column to an underlying table (by using ALTER TABLE). Because views store no data, the DBMS must execute them every time they re referenced.

SQL Views: Examples Create a view that hides the authors personal information (telephone numbers and addresses). Create a view that lists the authors who live in a city in which a publisher is located. Note that I use the column names au_city and pub_city in the view. Renaming these columns resolves the ambiguity that would arise if both columns inherited the same column name city from the underlying tables.

SQL Views: Examples Create a view that lists total revenue (= price sales) grouped by book type within publisher. This view will be easy to query later because I name the result of an arithmetic expression explicitly rather than let the DBMS assign a default name.

SQL Views: Examples Create a view that lists the last names of authors A02 and A05, and the books that each one wrote (or cowrote). Note that this statement uses a nested view: It references the view au_names created earlier.

NO ICE TODAY ASSIGNMENT 2 Work Day 52