SQL statement to get all customers with no orders - sql

I have a typical Persons table and an Orders table defined in such a way that I can do JOIN query as the following to return Orders for all Persons.
SELECT Persons.LastName, Persons.FirstName, Orders.OrderNo
FROM Persons
INNER JOIN Orders
ON Persons.id=Orders.Person_id
The question is, how do I write a statement that would return all Persons with NO Orders?
I'm using mysql.
Thank all in advance.

You may want to use LEFT JOIN and IS NULL:
SELECT Persons.LastName, Persons.FirstName
FROM Persons
LEFT JOIN Orders ON Persons.id = Orders.Person_id
WHERE Orders.Person_id IS NULL;
The result of a left join always contains all records of the "left" table (Persons), even if the join-condition does not find any matching record in the "right" table (Orders). When there is no match, the columns of the "right" table will NULL in the result set.

This should work... theres more than one way to do it.
select * from persons where person.id not in (select person_id from orders)

Just for completeness, here is the not exists version:
select * from persons p
where not exists
(select null from orders o where o.person_id = p.id)

You can use left join:
SELECT DISTINCT o.CustomerID from Orders as o
left join Customers as c
on o.CustomerID=c.CustomerID

Question
Find customers who have never made an order.
Output the first name of the customer.
Data
Two tables: Customers and Orders
SELECT first_name
from customers
WHERE first_name not in
(select first_name
from customers
join orders on customers.id=orders.cust_id)

Related

How to query for all users that bought from a store knowing their orders

Say you have 3 tables (users, products, and orders) and these tables meet the following requirements:
the product table has a storeID field
the orders table has a productID and userID field
What's the best way to query for all unique users when you only know the storeID?
All methods will give you the same results
I would prefer to use this
SELECT DISTINCT ID
FROM users u
INNER JOIN products p ON p.userID = u.ID
WHERE
p.storeID IN(SELECT storeID FROM orders)
In my opinion, best if you start your query with the smallest table, while using JOIN with large tables, so you can be more selective in the join part and would make your query elegant, and have some performance improvement.
you could be more selective by adding more conditions to the query where needed.
Could describe the columns available in your tables?
Are you able to try something like this?
select distinct userID
from store_table as a
inner join order_table as b on a.storeID=b.storeID
inner join user_table as c on b.userID=c.userID
where a.storeID='xxxxx'
Try this:
SELECT DISTINCT U.UserName
FROM Orders O
INNER JOIN Products P ON O.productID=P.productID
INNER JOIN Users U ON U.UserID=O.UserID
WHERE P.StoreID=Input_StoreID

Select rows that don't have a corresponding join in join table

I have two SQL tables - customer and widget. There's a join table, customers_widgets between them, that has two columns (customer_id and widget_id)
Is there a way I can select all the customers that aren't joined to a widget? So they have an id that doesn't appear in the customer_id column on the join table?
In general I've found NOT IN to be expensive and slow, but your mileage may vary on different RDBMS.
The two alternatives that I most often use are:
SELECT
*
FROM
customer
WHERE
NOT EXISTS (SELECT *
FROM customers_widgets
WHERE customers_widgets.customer_id = customer.customer_id
)
And...
SELECT
customer.*
FROM
customer
LEFT JOIN
customers_widgets
ON customers_widgets.customer_id = customer.customer_id
WHERE
customer_widgets.customer_id IS NULL
Try this:
SELECT customer_id
FROM customer
WHERE customer_id NOT IN (SELECT customer_id
FROM customers_widgets)
You can use an OUTER JOIN for this:
Select C.*
From customer C
Left Join customer_widgets W On C.customer_id = W.customer_id
Where W.customer_id Is Null

What is wrong with my join in this query?

Im practicing basic SQL with this site http://www.sqlishard.com/Exercise
Here is the question:
S5.0 - INNER JOIN
Now that we can pull data out of a single table and qualify column
names, let's take it a step further. JOIN statements allow us to
'join' the rows of several tables together using a condition to define
how they match one another. SELECT [columns] FROM FirstTable INNER
JOIN SecondTable ON FirstTable.Id = SecondTable.FirstTableId
Try using the INNER JOIN syntax to SELECT all columns from the
Customers and Orders tables where the CustomerId column in Orders
matches the Id column in Customers. Since both tables have an Id
column, you will need to qualify the Customers id in the WHERE clause
with either the table name or a table alias.
Here is my answer:
SELECT *
FROM Customers AS c
INNER JOIN Orders AS o ON c.ID = o.ID
WHERE o.CustomerID = c.ID
The site says im wrong? Could anyone explain where i'm going wrong?
EDIT: I see now I dont need the WHERE clause, but the question states..
you will need to qualify the Customers id in the WHERE clause with
either the table name or a table alias.
Hence my confusion. Thanks none the less.
Try this way:
SELECT c.ID,o.ID
FROM Customers AS c
INNER JOIN Orders AS o ON o.CustomerID = c.ID
or using where clause
SELECT *
FROM Customers AS c, Orders AS o
where o.CustomerID = c.ID
If you use JOIN.. ON, you do not need where clause

Deleting records that are not joined on in SQL

I have a SQL Server 2008 database. This database has two related tables: Customer and Order. My tables look like this:
Customer
--------
ID
FirstName
LastName
Order
-----
ID
CustomerID
Amount
ShipDate
I need to delete all customers that do NOT have any orders. I cannot figure out the best way to do this. Can someone tell me how to do this? the NOT part is what keeps getting me. Originally I was using "IN" but it's stumping me.
Thank you for your help!
Rather than using the IN operator, use a subquery and NOT EXISTS, something like:
DELETE Customers
FROM Customers c
WHERE NOT EXISTS (
SELECT 1
FROM Orders o
WHERE o.CustomerID = c.ID
)
DELETE Customer
WHERE ID IN
(
SELECT Customer.ID
FROM Customer
LEFT JOIN Order ON
Customer.ID = ORder.CustomerID
WHERE ORder.CustomerID IS NULL
)
Delete from Customer
WHERE ID in
(
SELECT C.ID
FROM Customer C
Left Outer Join Order O ON C.ID = O.CustomerId
WHERE O.ID IS NULL
)

Find records that do not have related records in SQL

I have 2 tables (Orders, OrderItems) that are related based on a column OrderID. I need to find all Orders that do not have any OrderItems.
We use JOIN to find related data. To find data without any related data, we can use an anti-join.
The following joins the tables, then selects those without any order items. This tends to be more efficient that a WHERE id NOT IN (...) style query.
select *
from
Orders O
left outer join OrderItems I
on I.OrderId = O.Id
where
I.Id is null
Select * From Orders Where OrderID not in (Select Distinct OrderID From OrderItems)
try with LEFT EXCEPTION JOIN
select *
from Orders
LEFT EXCEPTION JOIN OrderItems ON ...