I try to make a filter for my project. And I want to learn how can I use array values in inner join. For example I have a query like this.
SELECT Orders.OrderID, Employees.LastName, Employees.FirstName
FROM Orders
INNER JOIN Employees
ON Orders.EmployeeID = Employees.EmployeeID;
But I have multiple EmployeeIDs in array and I try to make my query like this.
SELECT Orders.OrderID, Employees.LastName, Employees.FirstName
FROM Orders
INNER JOIN Employees
ON Orders.EmployeeID = Employees.1,2,3;
this is the wrong way I know but I really don't know the right way to make this query.
No need to worry about that if there are any number of rows of employee id mapped with the orders it will return that many rows if exist, rather just use a where in clause
SELECT Orders.OrderID, Employees.LastName, Employees.FirstName
FROM Orders
INNER JOIN Employees ON Orders.EmployeeID = Employees.EmployeeID
WHERE Employees.EmployeeID IN (1,2,3);`
You can do like this,
SELECT Orders.OrderID, Employees.LastName, Employees.FirstName
FROM Orders
INNER JOIN Employees
ON Orders.EmployeeID = Employees.EmployeeID where Employees.EmployeeID IN (1,2,3);
Inner join with employee id will fetch all matching records of both orders and employees with condition of employee id and then on that where clause to fetch those records whose employee id is 1,2,3.
You could use the in operator in the on clause:
SELECT Orders.OrderID, Employees.LastName, Employees.FirstName
FROM Orders
INNER JOIN Employees
ON Orders.EmployeeID = Employees.EmployeeID and Employees.EmployeeID in (1,2,3);
Related
I am practicing MYSQL using https://www.w3schools.com/mysql/trymysql.asp?filename=trysql_func_mysql_concat which has a mock database for me to practice with an I am experimenting using the GROUP BY command I am attempting to group all employees up with all of their sales and determine, their name, their amount of sales and the product that they sold the most. I have managed to get their name and sales but not the product name. I know that extracting information with a group by is difficult and I have tried using a sub query. Is there a way to get the information.
My query is below.
SELECT
CONCAT_WS(' ',
Employees.FirstName,
Employees.LastName) AS 'Employee name',
COUNT(*) AS 'Num of sales'
FROM
Orders
INNER JOIN
Employees ON Orders.EmployeeID = Employees.EmployeeID
INNER JOIN
OrderDetails ON OrderDetails.OrderID = Orders.OrderID
INNER JOIN
Products ON Products.ProductID = OrderDetails.ProductID
GROUP BY Orders.EmployeeID
ORDER BY COUNT(*) DESC;
What this says is get orders, join employees based on orders employeeid, join the order details based on order id and join products information based on product id in the order details, then it groups them by the employee id and orders them by the number of sales an employee has made.
SELECT
concat_ws(' ',
Employees.FirstName,
Employees.LastName) as 'Employee name',
count(*) as 'Num of sales',
(
SELECT Products.ProductName
FROM Orders
INNER JOIN Employees ON Orders.EmployeeID = Employees.EmployeeID
INNER JOIN OrderDetails ON OrderDetails.OrderID = Orders.OrderID
INNER JOIN Products ON Products.ProductID = OrderDetails.ProductID
GROUP BY Orders.EmployeeID
ORDER BY count(Products.ProductName) desc
LIMIT 1
) as 'Product Name'
FROM Orders
INNER JOIN Employees ON Orders.EmployeeID = Employees.EmployeeID
INNER JOIN OrderDetails ON OrderDetails.OrderID = Orders.OrderID
INNER JOIN Products ON Products.ProductID = OrderDetails.ProductID
GROUP BY Orders.EmployeeID
ORDER BY count(*) desc;
Above is my attempt at using a sub query for the solution.
It is quite ugly, as the w3school uses still mysql 5.7
On a personal note, you should install your own server grab somewhere a database and test it there, in mysql workbench you can have many query tabs in which you can test queries , till you het the "right" result.
SELECT
CONCAT_WS(' ',
Employees.FirstName,
Employees.LastName) AS 'Employee name',
COUNT(*) AS 'Num of sales',
tn.ProductName
FROM
Orders
INNER JOIN
Employees ON Orders.EmployeeID = Employees.EmployeeID
INNER JOIN
OrderDetails ON OrderDetails.OrderID = Orders.OrderID
INNER JOIN
Products ON Products.ProductID = OrderDetails.ProductID
INNEr JOIN
(SELECT EmployeeID, p.ProductName
FROM (SELECT IF (#Eid = EmployeeID ,#rn := #rn +1, #rn := 1) rn,ProductID, sumamount
, #Eid := EmployeeID as EmployeeID
FROM
(
SELECT
EmployeeID,ProductID, SUM(Quantity) sumamount
FROM Orders o INNER JOIN OrderDetails od ON od.OrderID = o.OrderID,(SELECT #Eid := 0, #rn := 0) t1
GROUP BY EmployeeID,ProductID
ORDER BY EmployeeID,sumamount DESC ) t2 ) t3
INNER JOIN Products p ON t3.ProductID = p.ProductID
WHERE rn= 1) tn
ON Orders.EmployeeID = tn.EmployeeID
GROUP BY Orders.EmployeeID
ORDER BY COUNT(*) DESC;
In your second query you are trying to get an employee's most often sold product. But there are two mistakes in that subquery:
The subquery is invalid. You group by employee, but select a product. Which product? An employee can sell many different products. MySQL should raise a syntax error here, as all other DBMS I know of do. But you are in cheat mode. MySQL allows incorrect aggregation queries and silently applies ANY_VALUE on all columns that cannot be selected otherwise. Thus you are selecting ANY_VALUE(Products.ProductName), i.e. a product arbitrarily chosen by the DBMS. To get out of cheat mode SET sql_mode = 'ONLY_FULL_GROUP_BY';.
Then, you don't relate the subquery to your main query. So when selecting the row for, say, employee #123, your subquery still selects data for all employees in order to pick one of their products. And as this is independent from the employee in the main query, it will probably pick the same product for every other employee you are selecting, too.
Here is what the query should look like instead:
SELECT
concat_ws(' ', e.FirstName, e.LastName) as "Employee name",
count(*) as "Num of sales",
(
SELECT p2.ProductName
FROM Orders o2
INNER JOIN OrderDetails od2 ON od2.OrderID = o2.OrderID
INNER JOIN Products p2 ON p2.ProductID = od2.ProductID
WHERE o2.EmployeeID = o.EmployeeID
GROUP BY p2.ProductID
ORDER BY count(*) DESC
LIMIT 1
) as "Product Name"
FROM Orders o
INNER JOIN Employees e ON o.EmployeeID = e.EmployeeID
INNER JOIN OrderDetails od ON od.OrderID = o.OrderID
GROUP BY o.EmployeeID
ORDER BY count(*) desc;
Demo: https://dbfiddle.uk/?rdbms=mysql_8.0&fiddle=f35e96764d454a4032d7778b550fc6b4
Disclaimer: When an employee sold more than one product most often (e.g. 500 x product A, 500 x product B, 200 x product C), then one of them (A or B in the example) gets picked arbitrarily for the employee.
THE SCENARIO
Three tables:
orders
customers
delivery_addresses
THE RELATIONS
orders.customerId points to customers.id
orders.customerId points to delivery_addresses.customerId
THE GOAL
Is to select any items from orders which meet a condition of a column in either customers or delivery_addresses. In a more practical sense: i want to search for orders of customers with a certain name (or zip code or whatever) stored in either customers or delivery_addresses.
MY ATTEMPTS
I am not very good with joins and the logic behind them, but after some solid research i came up with this:
SELECT orders.* FROM orders INNER JOIN customers ON orders.customerId = customers.id INNER JOIN delivery_addresses ON orders.customerId = delivery_addresses.customerId WHERE ((customers.first_name LIKE "%max%") OR (delivery_addresses.first_name LIKE "%max%"))
This, however, does not work. I am only getting results if there is a corresponding entry in delivery_addresses, but not if there is only one in customers.
Thus, if i shorten the query like this:
SELECT orders.* FROM orders INNER JOIN customers ON orders.customerId = customers.id WHERE (customers.first_name LIKE "%max%")
I do get the correct results.
CONCLUSIONS
I am certain that i am missing a crucial point in regard to my understanding of how joins work. But i cannot figure out what it is.
Here INNER JOIN works like a filter
SELECT orders.*
FROM orders
INNER JOIN customers ON orders.customerId = customers.id
INNER JOIN delivery_addresses ON orders.customerId = delivery_addresses.customerId
WHERE ((customers.first_name LIKE "%max%")
OR (delivery_addresses.first_name LIKE "%max%"))
It could be rewritten using LEFT JOIN:
SELECT orders.*
FROM orders
INNER JOIN customers ON orders.customerId = customers.id
LEFT JOIN delivery_addresses ON orders.customerId = delivery_addresses.customerId
WHERE customers.first_name LIKE '%max%'
OR delivery_addresses.first_name LIKE'%max%
Alternatively using UNION:
SELECT orders.*
FROM orders
INNER JOIN customers ON orders.customerId = customers.id
WHERE customers.first_name LIKE '%max%'
UNION
SELECT orders.*
FROM orders
INNER JOIN customers ON orders.customerId = customers.id
INNER JOIN delivery_addresses ON orders.customerId = delivery_addresses.customerId
WHERE delivery_addresses.first_name LIKE'%max%
You can do it with LEFT joins and all the conditions in the ON clauses:
SELECT o.*
FROM orders o
LEFT JOIN customers c ON o.customerId = c.id AND c.first_name LIKE "%max%"
LEFT JOIN delivery_addresses d ON o.customerId = d.customerId AND d.first_name LIKE "%max%"
WHERE c.id IS NOT NULL OR d.customerId IS NOT NULL
You may have to add DISTINCT after SELECT if you are getting duplicate rows:
SELECT DISTINCT o.*
....................
This was probably asked a few times, but I couldn't find an answer. While doing JOIN on two tables, I encounter a problem.
First table is named "Employees" and another "Orders".
I'm trying to create a query, which will give me the output like:
order_id (from Orders) | first_name (from Employees) | last_name (from Employees)
The queries I use are:
SELECT * FROM Orders
LEFT JOIN Employees e on Orders.employeeid = e.EmployeeID;
or full join:
SELECT * FROM Orders
LEFT JOIN Employees e on Orders.employeeid = e.EmployeeID
UNION
SELECT * FROM Employees
RIGHT JOIN Orders o on Employees.EmployeeID = o.employeeid;
both work just fine, giving me the same results. Unless I select which columns I wish to extract. So query like that:
SELECT Orders.orderid, e.first_name, e.last_name FROM orders
LEFT JOIN Employees e on orders.employeeid = e.EmployeeID;
Gives me totally different results. Eg. first 100 orderids have same employee name, then another 100 different one and so on (only 4 employees overall, should be 9).
What am I doing wrong?
EDIT (screenshots added):
Orders table:
Employees table:
Output when doing full join, everything seems to be ok:
Left join (or any other join, looks the same). Some orders seem to be ommited, but overall only 4 employees are listed.
I don't know your data, but I think you want:
SELECT o.orderid, e.first_name, e.last_name FROM orders o INNER JOIN Employees e ON o.employeeid = e.employeeId
where o.employeeid != null
Order by o.orderid
It's hard to tell without seeing some data samples.
But here are a few points:
With LEFT JOIN you get 'only 4 employees overall, should be 9' -- I assume there are 5 employees that do not have any order.
In the 'full join' you get mixed columns, the correct way should be
SELECT * FROM Orders
LEFT JOIN Employees e on Orders.employeeid = e.EmployeeID
UNION
SELECT * FROM Orders
RIGHT JOIN Employees e on Orders.employeeid = e.EmployeeID
Otherwise there should be no difference in the output if you specify the columns, as opposed to *.
If
SELECT * FROM Orders
LEFT JOIN Employees e on Orders.employeeid = e.EmployeeID
is returning the correct set of results, then this should probably work:
Select orderid, FirstName, LastName from (SELECT * FROM Orders
LEFT JOIN Employees e on Orders.employeeid = e.EmployeeID LIMIT 999999) as joinedTable
SelectClassicModels.Orders.CustomerNumber,
ClassicModels.CUSTOMERS.CUSTOMERNAME,
ClassicModels.Employees.LASTNAME,
ClassicModels.Employees.firstNAME,
ClassicModels.employees.EmployeeNumber
from ClassicModels.Orders
join
ClassicModels.Customers
on ClassicModels.Orders.CustomerNumber = ClassicModels.Customers.CUSTOMERNUMBER
join
ClassicModels.EMPLOYEES
on ClassicModels.Employees.EMPLOYEENUMBER = ClassicModels.CUSTOMERS.SalesRepEmployeeNumber
Those three tables when joined work just fine, but when I try and add these modifiers, they don't work
group by ClassicModels.Orders.CustomerNumber
having count(ClassicModels.Orders.CustomerNumber) < 4
First off, welcome to StackOverflow!
I've re-formatted your SQL using my favorite tool, which I can provide a link to if you're interested. I've also added aliases to help make it more "readable". (The aliases are the lowercase bits after the table name in the FROM and JOIN clauses.)
SELECT orders.CustomerNumber,
customers.CustomerName,
employees.LastName,
employees.FirstName,
employees.EmployeeNumber
FROM CLASSICMODELS.ORDERS orders
JOIN CLASSICMODELS.CUSTOMERS customers
ON orders.CustomerNumber = customers.CustomerNumber
JOIN CLASSICMODELS.EMPLOYEES employees
ON employees.EmployeeNumber = customers.SalesRepEmployeeNumber
Now that we've got that done. Let's add your GROUP BY and HAVING clauses.
GROUP BY clauses have to have ALL of the columns used in the SELECT clause in them. (I'm not sure why. I haven't looked it up, but I just know that's how it works. :) )
SELECT orders.CustomerNumber,
customers.CustomerName,
employees.LastName,
employees.FirstName,
employees.EmployeeNumber
FROM CLASSICMODELS.ORDERS orders
JOIN CLASSICMODELS.CUSTOMERS customers
ON orders.CustomerNumber = customers.CustomerNumber
JOIN CLASSICMODELS.EMPLOYEES employees
ON employees.EmployeeNumber = customers.SalesRepEmployeeNumber
GROUP BY orders.CustomerNumber,
customers.CustomerName,
employees.LastName,
employees.FirstName,
employees.EmployeeNumber
Now that should work. Then you just have to add your HAVING clause in there.
SELECT orders.CustomerNumber,
customers.CustomerName,
employees.LastName,
employees.FirstName,
employees.EmployeeNumber
FROM CLASSICMODELS.ORDERS orders
JOIN CLASSICMODELS.CUSTOMERS customers
ON orders.CustomerNumber = customers.CustomerNumber
JOIN CLASSICMODELS.EMPLOYEES employees
ON employees.EmployeeNumber = customers.SalesRepEmployeeNumber
GROUP BY orders.CustomerNumber,
customers.CustomerName,
employees.LastName,
employees.FirstName,
employees.EmployeeNumber
HAVING COUNT(orders.CustomerNumber) < 4
I was also looking over your query, and you might get faster (and more efficient results) by using a query like this:
WITH CUSTOMERSWITHLESSTHANFOURORDERS
AS
(
SELECT CUSTOMERNUMBER
FROM CLASSICMODELS.ORDERS
GROUP BY CUSTOMERNUMBER
HAVING COUNT(CUSTOMERNUMBER) < 4
)
SELECT O.CUSTOMERNUMBER,
C.CUSTOMERNAME,
E.LASTNAME,
E.FIRSTNAME,
E.EMPLOYEENUMBER
FROM CUSTOMERSWITHLESSTHANFOURORDERS O
JOIN CLASSICMODELS.CUSTOMERS C
ON O.CUSTOMERNUMBER = C.CUSTOMERNUMBER
JOIN CLASSICMODELS.EMPLOYEES E
ON E.EMPLOYEENUMBER = C.SALESREPEMPLOYEENUMBER;
It uses what is called "common table expression" and basically just isolates a portion of the query. It could be more efficient because it's going to try and group on less data, so it could be faster. Be wary, because there's lots of "could"'s in there because I don't know how various different things are set up in your MySQL database.
Good luck!
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