Delete rows in one table with query in another table - mysql

I have 2 tables in my database.
table customer with ID,firstName,lastName,address...
and table orders with id, idCustomer...
and I want to delete all the orders of the customers where the first name is "john"
How I write the command?
thanks

DELETE o
FROM Orders o
INNER JOIN Customer c ON o.idCustomer = c.ID
WHERE c.firstName = 'john'

Delete From
orders
Where
Orders.firstName In
(Select
firstName
From
customers
Inner Join orders On customers.ID = orders.idCustomer
Where
customers.firstName = "john")

Related

SQL query how to compare string

There are two tables:
Customers:
ID
Name
Surname
City
Orders:
OrderId
CustomerId
Purchase
Price
I'm trying to find customer id,name,surname where he hasn't Purchase "Pizza".
Any help to fix my query? I tried with cp.Purchase != "Pizza" but doesn't work
SELECT DISTINCT ID,FirstName,LastName
FROM Customers c
INNER JOIN Orders cp ON c.ID = cp.OrderID
ORDER BY c.ID
WHERE cp.Purchase LIKE '%Pizza%'
try this
select * from Customers where Id not in (
select CustomerId From Orders WHERE cp.Purchase LIKE '%Pizza%'
)
The right query is
SELECT DISTINCT c.ID, c.Name, c.Surname
FROM Customers c JOIN Orders o on c.ID = o.CustomerID
WHERE c.ID <> ALL (
SELECT c2.ID
FROM Customers c2 JOIN Orders o2 on c.ID = o2.CustomerID
WHERE o2.Purchase = 'Pizza')
This is because you are looking for who never bought a pizza, so you will select data of customers which ID never appear (<> ALL) in orders table in a record where a pizza was bought.
By the way, check also SQL base, try understand before getting the answer.

Error 1064 - In statement

I am trying to show the average price of products bought by customers from ‘Tucson’, however this query returns null even though there are two customers that have placed orders from Tuscon.
select AVG(product_price) from product where product_id in
(select product_id from orderline where order_id in
(select order_id from ordertable where cust_id in
(Select cust_id from customer where city = 'Tuscon')))
You're using fom instead of from in your query: select order_id fom ordertable where cust_id in
This should be select order_id from ordertable where cust_id in
fom is not a recognized keyword. The MySQL parser doesn't know what to do with that, so it throws an error about "invalid syntax".
Consider using join operations in place of nested IN subqueries. If we are guaranteed:
product_id is unique in product table
order_id is unique in ordertable table
cust_id is unique in customer table
then we can get an equivalent result set, the average of the price of distinct products that were ordered...
SELECT AVG(p.product_price)
FROM ( SELECT l.product_id
FROM orderline l
JOIN ordertable o
ON o.order_id = l.order_id
JOIN customer c
ON c.cust_id = o.cust_id
WHERE c.city = 'Tuscon'
GROUP BY l.product_id
) q
JOIN product p
ON p.product_id = l.product_id
If we want that "average price" of all products ordered (a different result, with the average taking into account the number of times a product was ordered...then we could use a query like this:
SELECT AVG(p.product_price)
FROM product p
JOIN orderline l
ON l.product_id = p.product_id
JOIN ordertable o
ON o.order_id = l.order_id
JOIN customer c
ON c.cust_id = o.cust_id
WHERE c.city = 'Tuscon'

SQL Querying in multiple tables

I have two tables, customers and Orders
Customers columns are
CustomerID,
Username,
Password,
Firstname,
Surname,
Email,
Mobile
Orders columns are
OrderID,
CustomerID,
Date,
Time,
Price,
Complete
I want to select all of the firstname and surnames from all the orders that have been complete. And yes It could be so that [0] = John Smith and [1] also = John Smith.
What I was thinking was
SELECT FirstName, Surname from order, customers
WHERE Complete = 'Yes' AND order.CustomerID = customer.CustomerID;
So first it looks at if the order is complete. If it is then it will look at the customer ID, then it will go to customers and get the firstname and surname of that customer then store it in the datatable.
Thank you for any help!!!
You could use EXISTS, the following query will return all customers that don't have incomplete (=0) orders:
select c.firstname, c.lastname
from customers c
where
not exists (select * from orders o
where c.customerid = o.orderid
and o.complete = 'No')
but it will also return customers that have no orders. If you want to exclude customers with no orders you could use an additional exist clause:
select c.firstname, c.lastname
from customers c
where
not exists (select * from orders o
where c.customerid = o.orderid
and o.complete = 'No')
and exists (select * from orders o where c.customerid = o.orderid)
or a group by clause:
select c.firstname, c.lastname
from customers c inner join orders o on c.customerid = o.customerid
group by c.customerid, c.firstname, c.lastname
having sum(o.complete='No') = 0
This will give you list of Firstname, Surname even though they don't have orders.
SELECT Customers.Firstname, Customer.Surname
FROM Customers, Orders
WHERE Orders.Complete = 'Yes'
LEFT JOIN Customers.CustomerID = Orders.CustomerID
I'd go with this personally:
SELECT c.Firstname, c.Surname FROM Customers c
INNER JOIN Orders o
ON c.CustomerID=o.CustomerID
WHERE o.Complete='Yes'
I like to be as explicit as possible with my queries, so anyone else who has to read my code understands the what, why and how. Though shouldn't you also select something to identify the order? Otherwise all you have is a list of names.
--Unique list of customer id, customer first name and customer surname.
SELECT DISTINCT
customers.customerid
, customers.firstname
, customers.surname
FROM orders
INNER JOIN customers
ON
customers.customerid = orders.customerid
AND orders.complete = 'Yes'
--Unique list of customer first name and customer surname, regardless
--if same names are tied to different customerid.
SELECT DISTINCT
customers.customerid
, customers.firstname
, customers.surname
FROM orders
INNER JOIN customers
ON
customers.customerid = orders.customerid
AND orders.complete = 'Yes'
Remove DISTINCT word if you want duplicates.

Sql retrieve fields without a relation in another table

I have 2 tables: Customers and Orders. A customer has many orders and an order belongs to a customer. The order can be approved (marked by a field approved_at).
I want to retrieve all the customers without any approved order. This includes customers without any order and customers with orders that aren't approved (approved_at = null).
Can I do this in a single query without subqueries?
SELECT ..., COUNT(Orders.id) AS cnt
FROM Customers
LEFT JOIN Orders ON (Customers.id = Orders.Customer_id) AND (Orders.approved_at is null)
HAVING cnt = 0
SELECT c.*
FROM Customers c
LEFT JOIN Orders o ON c.ID = o.CustomerID
WHERE o.ID IS NULL OR c.Approved_at IS NULL

How to create SQL subquery ON JOIN using multiple tables

I have the folowing tables.
ORDER
OrderNumber
CustomerNumber
EmployeeNumber
OrderDate
CUSTOMER
CustomerNumber
Name
Address
EMPLOYEE
EmployeeNumber
Name
Address
ORDERDETAIL
OrderNumber
Qty
Description
Price
Let say ORDERDETAIL table has 10 records
I would like to write a query that will return 10 records from ORDERDETAIL table to include Employee name, employee address, customer name, customer address and and order Date.
I know that I could write a query and use INNER JOIN to get the info from ORDER table, but how do you create the rest of query to get the info from the CUSTOMER and EMPLOYEE tables.
SELECT *
FROM OrderDetail D
INNER JOIN Order O
ON D.OrderNumber = O.OrderNumber;
Just add some more joins...
SELECT *
FROM OrderDetail D
JOIN Order USING (OrderNumber)
JOIN Customer USING (CustomerNumber)
JOIN Employee USING (EmployeeNumber)
You might want to re-order the JOINs in order to have the smallest tables first, as this could provide you with some performance boost (depending on your server's version, the most recent will optimize the join for you and might actually execute the joins in the "probably best" way).
Also, in the MySQL dialect at least, JOIN implicitly expands to INNER JOIN, and writing
A JOIN B USING (COL)
is equivalent to writing
A JOIN B ON (A.COL = B.COL)
SELECT *
FROM OrderDetail D
INNER JOIN Order O ON D.OrderNumber = O.OrderNumber
INNER JOIN Eployee E on O.EployeeNumber = E.EployeeNumber
INNER JOIN Customer C on O.CustomerNumber = C.CustomerNumber
if you have foreign key reference between
ORDER.EmployeeNumber and EMPLOYEE.EmployeeNumber
ORDER.CustomerNumber and CUSTOMER.CustomerNumber
then try this
SELECT
E.name AS employeeName,
E.Address AS employeeAddress,
C.name AS customerName,
C.Address AS customerAddress.
O.OrderDate
FROM OrderDetail D
INNER JOIN Order O ON D.OrderNumber = O.OrderNumber
INNER JOIN EMPLOYEE E ON E.EmployeeNumber = 0.EmployeeNumber
INNER JOIN CUSTOMER C ON C.CustomerNumber= 0.CustomerNumber
LIMIT 0,10