Here is my sample data structure and sample data. What I am trying to accomplish here is to not show customers who have a subscriber record existing with a type of 'Subscriber'. You will see in the data set that Eli Manning has two subscription records. One is of type 'Owner' and the other is of type 'Subscriber'. So, he should not be in my results because there is an 'SUBSCRIBER' record instance. Odell Beckham Jr. has only one record with type of 'Owner', so he should be showing.
I have tried to use this query, but the results return customer Saquan Barkley. You will see that this customer has a 'Subscriber' record in the Subscribers table, so my sql is not working as expected. Any help would be much appreciated.
My Query:
select distinct
a.customer_id,
a.fst_name,
a.last_name,
a.email,
b.subscription_type
from
customers a,
subscriptions b
where
a.customer_id <> (select customer_id from subscriptions
where subscription_type <> 'SUBSCRIBER')
AND b.subscription_type <> 'SUBSCRIBER'
order by customer_id asc;
Tables & Data:
DROP TABLE CUSTOMERS;
DROP TABLE SUBSCRIPTIONS;
CREATE TABLE "CUSTOMERS"
( "FST_NAME" VARCHAR2(50 BYTE),
"LAST_NAME" VARCHAR2(100 BYTE),
"CUSTOMER_ID" NUMBER NOT NULL ENABLE,
"EMAIL" VARCHAR2(150 BYTE),
CONSTRAINT "CUSTOMERS_PK" PRIMARY KEY ("CUSTOMER_ID"));
CREATE TABLE "SUBSCRIPTIONS"
( "ID" NUMBER NOT NULL ENABLE,
"CUSTOMER_ID" NUMBER NOT NULL ENABLE,
"SUBSCRIPTION_TYPE" VARCHAR2(20 BYTE),
"SERIAL_NUMBER" VARCHAR2(50 BYTE),
CONSTRAINT "SUBSCRIPTIONS_PK" PRIMARY KEY ("ID"));
INSERT INTO customers (fst_name, last_name, customer_id, EMAIL)
VALUES ('Eli', 'Manning', '1', '[email protected]');
INSERT INTO customers (fst_name, last_name, customer_id, EMAIL)
VALUES ('Odell', 'Beckham Jr.', '2', '[email protected]');
INSERT INTO customers (fst_name, last_name, customer_id, EMAIL)
VALUES ('Saquan', 'Barkley', '3', '[email protected]');
INSERT INTO customers (fst_name, last_name, customer_id, EMAIL)
VALUES ('Evan', 'Engram', '4', '[email protected]');
INSERT INTO customers (fst_name, last_name, customer_id, EMAIL)
VALUES ('Nate', 'Solder', '5', '[email protected]');
INSERT INTO customers (fst_name, last_name, customer_id, EMAIL)
VALUES ('Patrick', 'Omameh', '6', '[email protected]');
INSERT INTO subscriptions (id, customer_id, subscription_type, serial_number)
VALUES ('1', '1', 'SUBSCRIBER', 'ASDF1234556');
INSERT INTO subscriptions (id, customer_id, subscription_type, serial_number)
VALUES ('2', '1', 'OWNER', 'ASDF1234556');
INSERT INTO subscriptions (id, customer_id, subscription_type, serial_number)
VALUES ('3', '2', 'OWNER', 'ASDF987657');
INSERT INTO subscriptions (id, customer_id, subscription_type, serial_number)
VALUES ('4', '3', 'SUBSCRIBER', 'ASDF11223344');
COMMIT;
JOINsyntax. Easier to write (without errors), easier to read (and maintain), and easier to convert to outer join if needed.from customers a, subscriptions bisfrom customers a cross join subscriptions bessentially. You are applying no criteria on how to join the two tables (such asa.customer_id = b.customer_id). So you are combining all customers with all subscriptions. You really shouldn't use this syntax. It was replaced with explicit joins in standard SQL 1992 (though it took Oracle nine years to finally adopt this in Oracle 9i).DISTINCTis typical sign for badly written queries. Instead of applying it right away, think about what makes its use necessary.b.subscription_typein your output? This can of course lead to multiple rows for the same customer. If you need it, wouldn't you rather like a list (a comma separated string with the types) and only one row per customer? If you don't need it, then you don't have to join with the subscriptions table at all.