Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations Mike Lewis on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Returning data that exists in only one of two tables

Status
Not open for further replies.

mtl2302

Technical User
Apr 12, 2007
10
I have two tables. One that contains data for tickets sold, and another that contains all tickets sold that have been redeemed.

Each ticket has a unique identifier (the barcode).

Is there a way that I can return the data set of all tickets that have been sold, but not yet redeemed? I'm sure it is easier than I'm making it out to be by any help is welcome.
 
Yep, you sure can.

Code:
SELECT *
FROM TicketsSold
WHERE NOT EXISTS (SELECT * FROM TicketsRedeemed WHERE TicketsSold.BarCode = TicketsRedeemed.BarCode)

Denny
MCSA (2003) / MCDBA (SQL 2000)
MCTS (SQL 2005 / Microsoft Windows SharePoint Services 3.0: Configuration / Microsoft Office SharePoint Server 2007: Configuration)
MCITP Database Administrator (SQL 2005) / Database Developer (SQL 2005)

My Blog
 
alternatively
Code:
SELECT ts.*
FROM TicketsSold ts
left join TicketsRedeemed on TicketsSold.BarCode = TicketsRedeemed.BarCode
WHERE TicketsRedeemed.BarCode is null

"NOTHING is more important in a database than integrity." ESquared
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top