 |
Intersections of Records |
|
|
Intersections and Differences in Sets of Records |
|
|
|
Two of the intermediate operations used in SQL consists
of finding out whether two tables or lists have common values or records in
certain fields, or what records are in one table but not in the other.
|
|
Practical
Learning: Introducing Intersection of Records
|
|
- Open the LPM2
file. Select and copy its whole content
- Start Microsoft SQL Server and connect
- Right-click the name of the server and click New Query
- Paste the LPM2 code in the Query Editor
- To execute, right-click inside the Query Editor and click Execute
- Click inside the Query Editor and press Ctrl + A
- To see the list of tenants from the Rockville apartments and the
list of tenants from Alexandria apartments, type the following code:
USE LPM2;
GO
SELECT *
FROM Rentals.Tenants t;
GO
SELECT *
FROM Rentals.Customers c;
GO
- To execute, right-click inside the Query Editor and click Execute.
Notice that, although both tables have columns with similar values,
the orders of the columns with like-values are not the same on both
tables:

|
Intersection in Sets of
Records
|
|
If you have two tables with the same categories of
information, you may want to know what records are common in the lists.
For example, if you have two list of customers, you may want to know
whether some customers are in both tables. That operation is called an
intersection. The SQL performs it as done in algebra. That SQL operator is
called INTERSECT. Its formula is:
{ <query_specification> | ( <query_expression> ) }
INTERSECT
{ <query_specification> | ( <query_expression> ) }
The INTERSECT keyword is written
between two SQL expressions. The first expression is also referred
to as the left expression. The second expression is also referred
to as the right expression.
There are rules the expressions must follow:
 |
- Both expressions must have the same number of columns
- The data types of the columns in the same positions must
be compatible. This means that the data type of the column
ColA in position Pn in table A must be compatible
with the colum ColB in position Pn of table B. In
the same way, the data type of the column ColX in position Pn+1
in table A must be compatible with the colum ColY in position
Pn+1 of table B. Compatibilty means that both
columns can be integer-based, or decimal-based, or
string-based, or date-based
|
The columns in both expressions don't have to
have the same name.
|
Practical
Learning: Finding Records Intersections
|
|
- If you specify only one column in each expression, if a record in
the left expression and another record in the right table have the
same values, the value would appear in the result.
Click inside the
Query Editor and press Ctrl + A
- To find the intersection of records using one column, type the
following:
USE LPM2;
GO
SELECT t.TenantCode
FROM Rentals.Tenants t
INTERSECT
SELECT c.AccountNumber
FROM Rentals.Customers c;
GO
- To execute, right-click inside the Query Editor and click Execute

- The result of the INTERSECT operation consists of the names
of the columns of the first expression. Therefore, if you want to
specify an alias for a column, use the field of the first expression.
As an example, change the statement as follows:
USE LPM2;
GO
SELECT t.TenantCode [Tenant Account #]
FROM Rentals.Tenants t
INTERSECT
SELECT c.AccountNumber
FROM Rentals.Customers c;
GO
- If you specify only one column in each expression, if no record in
the left and right expression is the same, the result would be empty.
Click inside the Query Editor and press Ctrl + A
- Type the following:
USE LPM2;
GO
SELECT t.PhoneNumber
FROM Rentals.Tenants t
INTERSECT
SELECT c.Phone
FROM Rentals.Customers c;
GO
- To execute, right-click inside the Query Editor and click Execute

- If you specify more than one column in each expression, if all
columns in a certain position Pn of both expressions have
the same value, the value would appear in the result. The database
engine would check the values of columns in position Pn+1
of both expressions and those value must be the same.
Click inside
the Query Editor and press Ctrl + A
- Type the following:
USE LPM2;
GO
SELECT t.TenantCode, t.FirstName, t.MaritalStatus
FROM Rentals.Tenants t
INTERSECT
SELECT c.AccountNumber, c.FirstName, c.Status
FROM Rentals.Customers c;
GO
- To execute, right-click inside the Query Editor and click Execute

- If you specify more than one column in each expression, if a
column in position Pn of the left expression is different
from the column in the same position Pn of the right
expression, the whole result would be empty.
Click inside the Query
Editor and press Ctrl + A
- Type the following:
USE LPM2;
GO
SELECT t.TenantCode, t.FirstName, t.MaritalStatus
FROM Rentals.Tenants t
INTERSECT
SELECT c.AccountNumber, c.FirstName, c.Status
FROM Rentals.Customers c;
GO
- To execute, right-click inside the Query Editor and click Execute

- Click inside the Query Editor and press Ctrl + A
- You can use functions in the expressions. If you decide to do this
- You should (must) use the same (or compatible) function on
both expressions
- The return values of the function on both expressions would be
compared. If they are the same, the value would be included in the
result, otherwise not
To see an example, type the following:
USE LPM2;
GO
SELECT tens.TenantCode [Tenant Account #],
CONCAT(tens.LastName, N', ', tens.FirstName) [Tenant Name]
FROM Rentals.Tenants tens
INTERSECT
SELECT custs.AccountNumber,
CONCAT(custs.LastName, N', ', custs.FirstName)
FROM Rentals.Customers custs;
GO
- To execute, press F5

- To see more examples, type the following:
USE LPM2;
GO
SELECT t.TenantCode, t.LastName
FROM Rentals.Tenants t
INTERSECT
SELECT c.AccountNumber, c.LastName
FROM Rentals.Customers c;
GO
- To execute, right-click inside the Query Editor and click Execute
- Click inside the Query Editor and press Ctrl + A
- To try another example, type the following:
USE LPM2;
GO
SELECT regs.RegistrationNumber, regs.RentStartDate
FROM Rentals.Registrations regs
INTERSECT
SELECT allocs.AllocationID, allocs.DateOccupiedFrom
FROM Rentals.Allocations allocs;
GO
- To execute, right-click inside the Query Editor and click Execute
- Click inside the Query Editor and press Ctrl + A
- To see another example, type the following:
USE LPM2;
GO
SELECT regs.RegistrationNumber,
regs.TenantCode,
regs.PropertyNumber,
regs.RentStartDate
FROM Rentals.Registrations regs
INTERSECT
SELECT allocs.AllocationID,
allocs.AccountNumber,
allocs.ApartmentNumber,
allocs.DateOccupiedFrom
FROM Rentals.Allocations allocs;
GO
- To execute, right-click inside the Query Editor and click Execute

- Click inside the Query Editor and press Ctrl + A
- Instead of using the columns directly, if the expressions include
foreign keys whose values are not very clear (such as the case of
integers), you can use a join to present more meaningful values. If
you decide t doo that
- You must use the join on both expressions
- The values in the joining records would be compared and they
must be the exact same
To see an example, type the following:
USE LPM2;
GO
SELECT regs.RegistrationNumber,
CONCAT(tens.LastName, N', ', tens.FirstName) Tenant,
regs.PropertyNumber,
regs.RentStartDate
FROM Rentals.Registrations regs INNER JOIN Rentals.Tenants tens
ON regs.TenantCode = tens.TenantCode
INTERSECT
SELECT allocs.AllocationID,
CONCAT(custs.LastName, N', ', custs.FirstName),
allocs.ApartmentNumber,
allocs.DateOccupiedFrom
FROM Rentals.Allocations allocs INNER JOIN Rentals.Customers custs
ON allocs.AccountNumber = custs.AccountNumber;
GO
- To execute, right-click inside the Query Editor and click Execute

- Click inside the Query Editor and press Ctrl + A
- To see an example, type the following:
USE LPM2;
GO
SELECT regs.RegistrationNumber,
tens.FirstName,
regs.PropertyNumber,
regs.RentStartDate
FROM Rentals.Registrations regs INNER JOIN Rentals.Tenants tens
ON regs.TenantCode = tens.TenantCode
INTERSECT
SELECT allocs.AllocationID,
custs.FirstName,
allocs.ApartmentNumber,
allocs.DateOccupiedFrom
FROM Rentals.Allocations allocs INNER JOIN Rentals.Customers custs
ON allocs.AccountNumber = custs.AccountNumber;
GO
- To execute, right-click inside the Query Editor and click Execute

- Click inside the Query Editor and press Ctrl + A
- To view another example, type the following:
USE LPM2;
GO
SELECT regs.RegistrationNumber,
regs.RegistrationDate,
regs.PropertyNumber,
regs.RentStartDate
FROM Rentals.Registrations regs
INTERSECT
SELECT allocs.AllocationID,
allocs.DateAllocated,
allocs.ApartmentNumber,
allocs.DateOccupiedFrom
FROM Rentals.Allocations allocs;
GO
- To execute, press F5
- To get a more meaning display, change the SQL statement as
follows:
USE LPM2;
GO
SELECT regs.RegistrationNumber [Registration #],
Format(regs.RegistrationDate, N'dddd, MMMM dd, yyyy') [Allocated On],
N'Prop #: ' + props.PropertyNumber + N', Monthly Rent: ' + FORMAT(props.MonthlyRate, N'C') [Unit Info],
Format(regs.RentStartDate, N'y') [Property Occupied From]
FROM Rentals.Registrations regs INNER JOIN Listing.Properties props
ON regs.PropertyNumber = props.PropertyNumber
INTERSECT
SELECT allocs.AllocationID,
Format(allocs.DateAllocated, N'dddd, MMMM dd, yyyy'),
N'Prop #: ' + props.PropertyNumber + N', Monthly Rent: ' + FORMAT(props.MonthlyRate, N'C'),
Format(allocs.DateOccupiedFrom, N'y')
FROM Rentals.Allocations allocs INNER JOIN Listing.Properties props
ON allocs.ApartmentNumber = props.PropertyNumber;
GO
- To execute, press F5

- Click inside the Query window and press Ctrl + A
- To see more examples, type the following:
USE LPM2;
GO
SELECT t.TenantCode, t.LastName
FROM Rentals.Tenants t
INTERSECT
SELECT c.AccountNumber, c.LastName
FROM Rentals.Customers c;
GO
- To execute, right-click inside the Query Editor and click Execute
- To see one more example, type the following:
USE LPM2;
GO
SELECT pmts.PaymentNumber, pmts.PaymentDate,
pmts.RegistrationNumber, pmts.AmountPaid
FROM Rentals.Payments pmts
INTERSECT
SELECT recs.ReceiptID, recs.DateReceiptMade,
recs.PaymentForAllocationNumber, recs.PaymentAmt
FROM Rentals.Receipts recs
ORDER BY pmts.RegistrationNumber;
GO
- To execute, right-click inside the Query Editor and click Execute

- Click inside the Query Editor, press Ctrl + A, and press Delete
|
|