I have a database consisting of a Customer, Product, and Transaction table.
I’m trying to write an SQL statement to list all customers’ names and SSN’s for those customers who have made no transaction in the year 2000.
The TransactionDate column in the Transaction table is a Date/Time data type (e.g. 2000-12-18 00:00:00).
This is the SQL code I’ve written:
SELECT DISTINCT CustomerName, Customer.CustomerSSN
FROM Customer, Transaction
WHERE Customer.CustomerSSN=Transaction.CustomerSSN
AND YEAR(TransactionDate)<>2000;
The not equal to symbol (<>) seems to not be working for some reason. When I change it to an equal sign, it does return the correct result…
Any advice is appreciated.
I’d change the approach.
The following query doesn’t need
distinctorGROUP BYbecause none of the customer records are joined to multiple transaction records.It also works for customers who have never made Any transactions.
Finally, it uses
>= AND <rather thanYEAR()=2000. This enable an index seek rather than a full scan (assuming that you have an approriate index on the transactions table).