15 views

Uploaded by Cesar Chavez

- Building Financial Data Mode - Lab#31Working with Informatica Functions
- informatica faq
- oracle_book
- Microsoft Visual Basic 6 (Database Programming)
- Informatica 8.6 PC_86_DesignerGuide
- CSDP
- Temporal Logic in Information Systems
- SQL Queries funda
- vbnet4
- Chap3 Practice Key
- Oracle Financials Interface
- IBPS Specialist IT Officer Previous Paper
- DBMS(Database Management System)
- Yahaya Project Report2
- Normalization Nota.pdf
- Online Placement System Report.doc
- Openobject Memento
- mca304
- 13
- 2 - 2 - Motivating Relational Algebra (8-57)

You are on page 1of 24

By

Gregory Larsen

2011/11/16

Presented by

Alex Albergo

The Series

• This article is part of the Stairway Series: Stairway to T-SQL DML

• The Stairway Series will provide you with a basic understanding of how to

work with data from SQL Server tables, using SQL Server’s Transact-SQL

(T-SQL) dialect. DML is the Data Manipulation Language, and is the aspect

of the language dealing with the data. It includes the statements SELECT,

INSERT, UPDATE and DELETE. This Stairway will as also provide some

history of the SQL language and some general concepts about set theory.

Each level will build upon the prior level, so by the time you are finished you

will have a good understanding of how to select and modify data from SQL

Server.

• A relational database contains tables that relate to each other by key

values. When querying data from these related tables you may choose to

select data from a single table or many tables. If you select data from many

tables, you normally join those tables together using specified join criteria.

The concepts of selecting data from tables and joining tables together is all

about managing and manipulating sets of data. In Level 4 of this Stairway I

will be exploring the concepts of set theory and mathematical operators to

join, merge, and return data from multiple SQL Server tables.

What is a table really?

• A table is just a bunch of rows that contain

values in one or more columns.

• A table can be thought of as a set containing

zero, one, or many records.

– Each row in a table is considered a member of the

table set.

– The concept of sets and members of sets are the

basic elements of set theory.

– By performing set operations, such as intersection

and union against different sets (or against different

tables in a SQL Server database) we are able to

retrieve the data we need

Basic Set Theory

• In the late 1800’s Georg Cantor formally defined a set. He defined a

set as “any collection into a whole M of definite and separate objects

m.”, where the separate objects m are elements of the set M. .

• Cantor’s work eventually grew into what we now called the set

theory.

• Edgar Codd used the concept of set theory to explain the

relationships between data in database tables, which formed the

basis of our all familiar relational database.

• The relational database concept that Edgar Codd developed was

just a projection of the mathematical concepts of set theory for

retrieving data stored in a database.

• By better understanding set theory, we can better understand and

manipulate our data that is stored in our relational databases

Basic Set Theory continued

• In addition to a set, there is also the concept of a

subset.

• A subset is just a collection of members of a set

where the number of members in the subset is

less than the number members of the whole set.

• A subset can be thought of as a partial list of

members of a set.

• In SQL Server, a subset of a table would be a

set of rows, where the total number of rows in

the subset has fewer rows than the original table

Basic Set Theory continued

• To retrieve data for your application you are

typically doing more than just retrieving a set of

records from a single SQL Server table.

• You will commonly need to join one set with

another set, and then finally use some parts of

that joined set to support the data that your

application might need.

• To explore the idea of joining sets together a

little more I will be looking at a concept known as

Venn diagrams.

Venn Diagrams

• A Venn diagram is a graphical representation of

how two or more sets intersect each other. A

Venn diagram makes it easy to visualize and

describe the different parts of intersecting sets.

by John Venn, although he never called it a

Venn diagram. Figure 1 shows a simple Venn

diagram that represents the intersection of two

sets, ‘Set A’ and ‘Set B’.

Simple Venn diagram that

represents two intersecting sets

Figure 1: Venn diagram

• Set A is represented by the light blue circle, and

Set B is represented by the red circle.

• Here we have labeled the different parts of the

joined set with 1, 2, and 3, as well as shaded

each part with a different color.

– The light blue part labeled ‘1’, represent the members

in Set A that do not match, or intersect with any

members in Set B.

– The purple part, labeled ‘2’, contains the members of

Set A that intersect, or have the matching members in

Set B.

– The red part labeled ‘3’ is the part of Set B that has no

matching, or intersecting members with Set A.

• A Venn diagram can be used to visually

represent how all kinds of different sets

might intersect. Figure 2 shows a Venn

diagram that represents where Set B is

contained within Set A, so B is a subset of

A.

Venn diagram showing the

Intersection of 3 Sets

• Understanding of the different parts of

intersecting sets as represented using

Venn diagrams can give you a better

sense for how joined sets can be used to

support retrieving data from relational

databases.

Mathematical Operators

• You might remember INTERSECT, and

UNION operators from your mathematical

studies of algebra.

• These mathematical operators have been

around since set theory was introduced,

and they not only apply to joining sets, but

they also apply to joining SQL Server

tables.

Set A and Set B intersects on

members that have matching

values

Set A and Set B intersections

• You can visually see the different parts of

the two intersecting sets.

– Part 1 consists of members with a values of 1

through 4,

– Part 2 members have values of 5 or 6,

– Part 3 contains members with the values of 7

and 8.

The INTERSECT operator

• The INTERSECT operator combines two sets together

based on the common members between the two sets

• The result of the INTERSECT operation is a list of the

common members between the two intersecting sets.

• Using the INTERSECT operator to return the members

in part 2 of the Venn diagram, except the members will

be distinct (duplicates will be removed), so the result set

only returns a single row for every unique member value.

Removing the duplicates is something that the

INTERSECT operator does automatically.

SQL command

• USE tempdb;

• GO

• SELECT Member FROM [SET A]

• INTERSECT

• SELECT Member FROM [SET B];

Output of intersection

• You can see how only two rows are

returned, and the values returned are

those distinct values that are common

between tables [SET A ]and [SET B].

• Member

• ------

• 5

• 6

The UNION operator

• The UNION operator brings together all

three different parts (1, 2, and 3) from the

Venn diagram.

• It returns every member value in both

[SET A] and [SET B] and de-duplicates the

members while the union operation is

performed

SQL Command

• SELECT Member FROM [SET A]

• UNION

• SELECT Member FROM [SET B];

When this code is run

• You can see all the member values are returned, but all the

duplicate member values in the two different sets have been

removed.

• Member

• ------

• 1

• 2

• 3

• 4

• 5

• 6

• 7

• 8

UNION ALL operator

• It is possible to retain the duplicate values

while the union operation is performed

against two sets.

• This can be done by using the UNION ALL

operator.

• The ‘ALL’ part of the operator tells SQL

Server to not remove the duplicates.

SQL command

• SELECT Member FROM [SET A]

• UNION ALL

• Member

• ------

• 1

• 1

• 2

• 3

• 4

• 4

• 5

• 6

• 6

• 5

• 5

• 6

• 7

• 7

• 8

Set Theory and SQL Server

• Set theory, which has been since the late

1800’s, laid the groundwork for selecting

information out of SQL Server databases.

• With the use of Venn diagrams, we are able to

show visually how two or more sets intersect.

• Knowing how our 19th-century mathematicians

defined and joined sets is useful in helping us

understand how to retrieving data from SQL

Server tables using the INTERSECT, UNION,

UNION ALL

Thank you and I hope this helps.

Alex Albergo

Director of programming

Passport Software, Inc.

- Building Financial Data Mode - Lab#31Working with Informatica FunctionsUploaded byAmit Sharma
- informatica faqUploaded byraghu_ram_32
- oracle_bookUploaded bywaitout
- Microsoft Visual Basic 6 (Database Programming)Uploaded byAbdul Majeed Khan
- Informatica 8.6 PC_86_DesignerGuideUploaded byJimmy
- CSDPUploaded byRichard Schwarz
- Temporal Logic in Information SystemsUploaded bySamer Abu Gahgah
- SQL Queries fundaUploaded byJohn
- vbnet4Uploaded byeshamu
- Chap3 Practice KeyUploaded bykrishnanand
- Oracle Financials InterfaceUploaded byTony Tang
- IBPS Specialist IT Officer Previous PaperUploaded byUday Kumar
- DBMS(Database Management System)Uploaded byPardeep Vats
- Yahaya Project Report2Uploaded byYahaya Kareem
- Normalization Nota.pdfUploaded byzaaziz
- Online Placement System Report.docUploaded byBharat Ahuja
- Openobject MementoUploaded byaryienne
- mca304Uploaded bykola0123
- 13Uploaded byNishki Gejmer
- 2 - 2 - Motivating Relational Algebra (8-57)Uploaded byRahulsinghoooo
- Lec16.pptUploaded bysachin
- Prompt.docxUploaded byvincent tekey
- A Tutorial on Database ConceptsUploaded byaggelikib
- Oracle SQL37Uploaded bySumit K
- SQL 2Uploaded byShanmuka Sreenivas
- Data Base FAQUploaded byanandkrishna2006
- openerptechnicalmementov0-6-4a4-1-101110035939-phpapp01Uploaded byRicardo Valencia
- Introduction to SQLUploaded byAjay Kumar
- ComE 223 (Slide 2).pdfUploaded byMariel Asunsion
- htgilh5yuUploaded bypspk143143

- Best Practices for Dealing With Phishing and Ransomware.pdfUploaded byMasterPirate
- Dissecting Shit TestsUploaded byGlenn M. Eades
- Tutorial de GITUploaded byEduardo Galindo Franco
- What You Need to Know About Unity 5 [eBook]Uploaded byCesar Chavez
- Executive Guide to Apache and HadoopUploaded byClivison César
- Cracking Security MisconceptionsUploaded byCesar Chavez
- Developing Modern Applications With ScalaUploaded byCesar Chavez
- Webmin User GuideUploaded byAbe Li
- ConCurrency Patterns David KocherUploaded byCesar Chavez
- Apache_Tomcat_Handbook.pdfUploaded byErnstjr Montes
- How to Build and Scale With MicroservicesUploaded byCesar Chavez
- Gradle-Build-Automation-Handbook.pdfUploaded byCesar Chavez
- E Poster Rafael Lovato 02 v101Uploaded byCesar Chavez
- E Poster Rafael Lovato 01 v101Uploaded byCesar Chavez
- E Book What is Jiu Jitsu v101Uploaded byCesar Chavez
- rmiUploaded byCesar Chavez

- TcodeUploaded byPrakash Praky
- Conference SVM ClassifierUploaded byAdhiyaman Pownraj
- Transporting Table Space 001Uploaded bysaikatKasba
- data_analyst_CV_template[1].pdfUploaded bysrikoptom
- Configure BIND as a Private Network DNS Server on Ubuntu 14Uploaded byashbless
- SAP ABAP Internal TablesUploaded byroysmoss
- 2017-6-6Uploaded byPRUEBA 54
- Forestic CommandsUploaded byec_tester
- 4D 2003 LanguageUploaded byEduardo Alcivar
- DS1621 Temperature SensorUploaded byUsman Malik
- 002 Questions Performance and System TuningUploaded byR D Sandeep
- Custom Document Um Application Code ReviewUploaded byG OD YO
- BCS-053Uploaded bymanish.cdma
- Lab 4 ReportUploaded byJorge Berumen
- gdbUploaded byPhuong Tran
- D56497GC10_agUploaded byedenfray
- &&&, ;;, And Other HieroglyphicsUploaded byRajashekar Gatla
- An Introduction to Apache HiveUploaded bySemtech Solutions Ltd
- How to Store File Incoming as XSTRING in SAPUploaded byjagadeeswar
- Forensic ToolkitUploaded byclu5t3r
- an introduction to as400 system operationsUploaded byRahul
- 7 IDT LOV and Paramater Objects.pdfUploaded byvishnurk
- Section 2 QuizUploaded byAlina Mirt
- chap12_SUploaded byImran Shaukat
- Can Oracle GoldenGate Be Used to Replicate SAP Data (Doc ID 1269452.1)Uploaded byYasir
- 130552025 InsightIQ User Guide 2 0Uploaded byGaurav Sachan
- g2bsc Lmtct User GuideUploaded byVijay Verma
- MS-Dos CommandUploaded byanonymouslk
- Android InitUploaded byappleapps
- Linux Professional Institute - LPIC 101 & 102Uploaded byAlireza Aminzadeh