Intersections of Records
Intersections of Records
Intersections and Differences in Sets of Records
Introduction
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
USE LPM2; GO SELECT * FROM Rentals.Tenants t; GO SELECT * FROM Rentals.Customers c; GO
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:
|
The columns in both expressions don't have to have the same name.
Practical Learning: Finding Records Intersections |
USE LPM2; GO SELECT t.TenantCode FROM Rentals.Tenants t INTERSECT SELECT c.AccountNumber FROM Rentals.Customers c; GO
USE LPM2;
GO
SELECT t.TenantCode [Tenant Account #]
FROM Rentals.Tenants t
INTERSECT
SELECT c.AccountNumber
FROM Rentals.Customers c;
GO
USE LPM2; GO SELECT t.PhoneNumber FROM Rentals.Tenants t INTERSECT SELECT c.Phone FROM Rentals.Customers c; GO
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
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
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
USE LPM2; GO SELECT t.TenantCode, t.LastName FROM Rentals.Tenants t INTERSECT SELECT c.AccountNumber, c.LastName FROM Rentals.Customers c; GO
USE LPM2; GO SELECT regs.RegistrationNumber, regs.RentStartDate FROM Rentals.Registrations regs INTERSECT SELECT allocs.AllocationID, allocs.DateOccupiedFrom FROM Rentals.Allocations allocs; GO
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
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
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
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
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
USE LPM2; GO SELECT t.TenantCode, t.LastName FROM Rentals.Tenants t INTERSECT SELECT c.AccountNumber, c.LastName FROM Rentals.Customers c; GO
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
Difference in Sets of Records
Once again, imagine you have two tables that have the same categories of records, such as two lists of customers. You may want to know what records belong to one list and not to the other list. To assist you with this operation, Transact-SQL provides the EXCEPT operator. Its formula is the same as that of INTERSECT:
{ <query_specification> | ( <query_expression> ) } EXCEPT { <query_specification> | ( <query_expression> ) }
In this case also, the EXCEPT keyword is surrounded by two SQL expressions. The rules are the same for the intersection:
This time also, the names of the columns of the expressions can be different but columns of the first expression would be used for the final result. When creating the resulting list, the database engine compares the records of the left expression to those of the right expression:
Practical Learning: Finding the Difference in Records |
USE LPM2; GO SELECT t.TenantCode FROM Rentals.Tenants t INTERSECT SELECT c.AccountNumber FROM Rentals.Customers c; GO
USE LPM2;
GO
SELECT t.TenantCode
FROM Rentals.Tenants t
EXCEPT
SELECT c.AccountNumber
FROM Rentals.Customers c;
GO
USE LPM2; GO SELECT c.AccountNumber FROM Rentals.Customers c EXCEPT SELECT t.TenantCode FROM Rentals.Tenants t; GO
USE LPM2; GO SELECT t.TenantCode [Account #], t.FirstName [First Name] FROM Rentals.Tenants t INTERSECT SELECT c.AccountNumber, c.FirstName FROM Rentals.Customers c; GO
USE LPM2;
GO
SELECT t.TenantCode [Account #], t.FirstName [First Name]
FROM Rentals.Tenants t
EXCEPT
SELECT c.AccountNumber, c.FirstName
FROM Rentals.Customers c;
GO
USE LPM2; GO SELECT c.AccountNumber [Account #], c.FirstName [First Name] FROM Rentals.Customers c EXCEPT SELECT t.TenantCode, t.FirstName FROM Rentals.Tenants t; GO
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
USE LPM2;
GO
SELECT regs.RegistrationNumber [Registration #],
regs.RegistrationDate [Allocated On],
regs.PropertyNumber [Unit Info],
regs.RentStartDate [Property Occupied From]
FROM Rentals.Registrations regs
EXCEPT
SELECT allocs.AllocationID,
allocs.DateAllocated,
allocs.ApartmentNumber,
allocs.DateOccupiedFrom
FROM Rentals.Allocations allocs;
GO
USE LPM2; GO SELECT regs.RegistrationNumber [Registration #], Format(regs.RegistrationDate, N'D') [Allocated On], N'Prop #' + props.PropertyNumber + N', Rate: ' + FORMAT(props.MonthlyRate, N'C') + N'/Month' [Unit Info], Format(regs.RentStartDate, N'MMMM yyyy') [Property Occupied From] FROM Rentals.Registrations regs INNER JOIN Listing.Properties props ON regs.PropertyNumber = props.PropertyNumber EXCEPT SELECT allocs.AllocationID, Format(allocs.DateAllocated, N'D'), N'Prop #' + props.PropertyNumber + N', Rate: ' + FORMAT(props.MonthlyRate, N'C') + N'/Month' [Unit Info], Format(allocs.DateOccupiedFrom, N'MMMM yyyy') FROM Rentals.Allocations allocs INNER JOIN Listing.Properties props ON allocs.ApartmentNumber = props.PropertyNumber; GO
|
|||
Previous | Copyright © 2012-2022, FunctionX | Thursday 26 May 2022 | Next |
|