SQL Fundamentals Set Operators: UNION,

SQL Fundamentals Set Operators:  UNION, SQL Fundamentals Set Operators:  UNION, - Start

Added : 2018-03-08 Views :30K

Download Presentation

SQL Fundamentals Set Operators: UNION,




Download Presentation - The PPT/PDF document "SQL Fundamentals Set Operators: UNION," is the property of its rightful owner. Permission is granted to download and print the materials on this web site for personal, non-commercial use only, and to display it on your personal computer provided you do not modify the materials and that you retain all copyright notices contained in the materials. By downloading content from our website, you accept the terms of this agreement.



Presentations text content in SQL Fundamentals Set Operators: UNION,

Slide1

SQL Fundamentals

Set Operators: UNION,INTERSECT,EXCEPT

1

Microsoft Enterprise Consortium

Microsoft Enterprise Consortium:

http://enterprise.waltoncollege.uark.edu

Microsoft Faculty Connection/Faculty Resource Center

http://www.facultyresourcecenter.com

Slide2

What you’ll need …

Log in to MEC for this lesson and into MSSMS (Microsoft SQL Server Management Studio).Be sure to select your account ID under Database in the Object Explorer pane, similar to the example shown here.

If there is something besides the topics for this

preentation that you’re not familiar with, please review earlier lessons in this SQL Fundamental series.2

Slide3

SET OPERATORS

There are 3 set operator commands:UNIONINTERSECT

EXCEPTUNION combines two or more data sets.

3

A

B

+

A and B

=

Slide4

Set Operators: UNION

UNION: If the data sets overlap (have common data), the duplicates are removed.

UNION compatible rule: This rule applies to all 3 set operators. The # of columns must be the same in both data sets.The column data types must be in the same sequence and compatible. Example: Column 2 in both data sets must be have the same data type.

4

A

B

+

A and B

=

Slide5

Set Operators: INTERSECT

INTERSECT: If the data sets overlap, only the overlap is in the result.

5

A

B

+

=

Slide6

Set Operators: EXCEPT

EXCEPT: If the data sets overlap, only the portion not in common with the second data set is in the result.

6

A

B

+

=

Slide7

UNION example – p1

List students who got 90 or higher on

their CONTRIBUTE evaluation item (they were evaluatees).

select stdid, stdfname, stdlname, eval_item_ID, scorefrom students, evaluations, eval_items_scoreswhere stdid = evaluateeIDand evaluations.eval_ID = eval_items_scores.eval_IDand eval_item_ID

=

'CONTRIBUTE'

and score >=

90

;

The output has 14 rows.

7

Slide8

UNION example – p2

List students who got 90 or higher on

their RELIABLE evaluation item (they were evaluatees).

select stdid, stdfname, stdlname, eval_item_ID, scorefrom students, evaluations, eval_items_scoreswhere stdid = evaluateeIDand evaluations.eval_ID = eval_items_scores.eval_IDand eval_item_ID

=

'RELIABLE'

and score >= 90;

The output

has

11

rows.

8

Slide9

UNION example – p3

UNION the lists to combine the output.

select stdid, stdfname, stdlname, eval_item_ID, scorefrom students, evaluations, eval_items_scores

where stdid = evaluateeIDand evaluations.eval_ID = eval_items_scores.eval_IDand eval_item_ID = 'CONTRIBUTE'and score >= 90UNION

select stdid, stdfname, stdlname, eval_item_ID, score

from students, evaluations, eval_items_scores

where stdid = evaluateeID

and evaluations.eval_ID = eval_items_scores.eval_ID

and eval_item_ID = 'RELIABLE'

and score >= 90;

9

Slide10

UNION example – p4

Remove the evaluation item ID column. This will reduce the list because some students were listed twice—once for CONTRIBUTE and once for RELIABLE.

The output drops to 20 rows.select stdid, stdfname, stdlname,

eval_item_ID, scorefrom students, evaluations, eval_items_scoreswhere stdid = evaluateeIDand evaluations.eval_ID = eval_items_scores.eval_IDand eval_item_ID = 'CONTRIBUTE'

and score >= 90

UNION

select stdid, stdfname, stdlname,

eval_item_ID

, score

from students, evaluations, eval_items_scores

where stdid = evaluateeID

and evaluations.eval_ID = eval_items_scores.eval_ID

and eval_item_ID = 'RELIABLE'

and score >= 90;

10

Slide11

INTERSECT example

Change UNION to INTERSECT. Now, we see which students got 90 or higher in both CONTRIBUTE and RELIABLE>

select stdid, stdfname, stdlname, score

from students, evaluations, eval_items_scoreswhere stdid = evaluateeIDand evaluations.eval_ID = eval_items_scores.eval_IDand eval_item_ID = 'CONTRIBUTE'and score >= 90

INTERSECT

select stdid, stdfname, stdlname, score

from students, evaluations, eval_items_scores

where stdid = evaluateeID

and evaluations.eval_ID = eval_items_scores.eval_ID

and eval_item_ID = 'RELIABLE'

and score >= 90;

11

Slide12

EXCEPT example

Now, use EXCEPT. We see which students got 90 or higher for CONTRIBUTE but didn’t for RELIABLE.

select stdid, stdfname, stdlname, score

from students, evaluations, eval_items_scoreswhere stdid = evaluateeIDand evaluations.eval_ID = eval_items_scores.eval_IDand eval_item_ID = 'CONTRIBUTE'and score >= 90EXCEPT

select stdid, stdfname, stdlname, score

from students, evaluations, eval_items_scores

where stdid = evaluateeID

and evaluations.eval_ID = eval_items_scores.eval_ID

and eval_item_ID = 'RELIABLE'

and score >= 90;

12

Slide13

What was covered …

Set OperatorsUNIONINTERSECT

EXCEPTUNION compatible rule

13

Slide14

Resources

http://enterprise.waltoncollege.uark.edu/mec.aspMicrosoft Faculty Connection—Faculty Resource Center

http://www.facultyresourcecenter.com/Microsoft Transact-SQL Referencehttp://msdn.microsoft.com/en-us/library/aa299742(v=SQL.80).aspx

AdventureWorks Sample Databasehttp://msdn.microsoft.com/en-us/library/ms124659%28v=sql.100%29.aspx14


About DocSlides
DocSlides allows users to easily upload and share presentations, PDF documents, and images.Share your documents with the world , watch,share and upload any time you want. How can you benefit from using DocSlides? DocSlides consists documents from individuals and organizations on topics ranging from technology and business to travel, health, and education. Find and search for what interests you, and learn from people and more. You can also download DocSlides to read or reference later.
Youtube