Finding Common Rows with INTERSECT

An INTERSECT operation combines the results of two queries into a single result that comprises all the rows common to both queries. Intersections have the same restrictions as unions; see “Combining Rows with UNION” earlier in this chapter.

To find common rows:

  • Type:
    								select_statement1
    INTERSECT
    select_statement2;
    
    select_statement1 and select_statement2 are SELECT statements. The number and the order of the columns must be identical in both statements, and the data types of corresponding columns must be compatible. Duplicate rows are eliminated from the result.

Listing 7.46 uses INTERSECT to list the cities in which both an author and a publisher are located. See Figure 7.46 for the result.

Listing 7.46. ...

Get SQL: Visual QuickStart Guide now with the O’Reilly learning platform.

O’Reilly members experience books, live events, courses curated by job role, and more from O’Reilly and nearly 200 top publishers.