Join on non unique column [closed] - sql

Closed. This question is not reproducible or was caused by typos. It is not currently accepting answers.
This question was caused by a typo or a problem that can no longer be reproduced. While similar questions may be on-topic here, this one was resolved in a way less likely to help future readers.
Closed 2 years ago.
Improve this question
I have two tables in DB and I want to select and join some data from these table.
The first table has some customers:
customer
id
Dave
1
Tom
2
The second table has a list of products and a column that indicates which customer bought that Product:
id
product
isin
customer id
1
PC
XV452889
1
2
phone
VN865232
2
3
laptop
PL201325
1
I tried INNER JOIN in order to get as output a table that lists for each product that have been bought, who was its customer.
Desired output:
id
product
isin
customer id
customer
1
PC
XV452889
1
Dave
2
phone
VN865232
2
Tom
3
laptop
PL201325
1
Dave
I tried inner join but the answer is empty, its like you cant join two table on a non-unique column. it has been two days I try to solve it.
select table2.product, table2.customer_id
from table2
inner join table1 on table1.id = table2.customer_id;
Hers is the query I am running on similar tables (allocations table is equivalent to the second table of product above and orders is equivalents to customer table):

You can join the customer.id on product.customer_id:
SELECT p.*, c.customer
FROM products p
JOIN customer c on p.customer_id = c.id

THANK YOU ALL FOR YOUR ANSWERS about he join!!!
I found the error in the query, it was missing ',' after orders.instructions...
too long queries ):

Related

how to decide which can be taken as first table and second table? [closed]

Closed. This question needs details or clarity. It is not currently accepting answers.
Want to improve this question? Add details and clarify the problem by editing this post.
Closed 5 days ago.
This post was edited and submitted for review 4 days ago and failed to reopen the post:
Original close reason(s) were not resolved
Improve this question
To find the salesorders for all productsID along with productsID and name?
I wrote this cause the sales table also has productsID column.
select A.salesorderID, A.productsID, B.Name
from Production.productsID AS A
LEFT JOIN sales.salesorderID AS B
ON A.productsID = B.proudctsID
But the answer I saw is
select A.productsID, A.Name, B.salesorderID
from Production.productsID AS A
LEFT JOIN sales.salesorderID AS B
ON A.productsID = B.productsID
The decision is made based on how you want your output. The purpose of a left join is to join a table that has values you want to see to another table that MIGHT have values you want to see. The order is logical depending on which values you DON'T want to be left out of the query.
So, lets say I have a table of People with a relationship to a table of Pets.
People
ID
Name
1
John
2
Mike
3
Sue
Pets
ID
PersonID
Name
1
1
Foodo
2
2
Barrky
My organization is having a dog walking event and we want to invite all of our people. Simple, right? Done.
SELECT * FROM People;
ID
Name
1
John
2
Mike
3
Sue
But wait, its a dog walking event, so I want to include the name of these peoples Pets on the invite. No big deal.
SELECT People.*, Pets.Name AS PetName FROM People INNER JOIN Pets on Pets.PersonID = People.ID;
Result:
ID
Name
PetName
1
John
Foodo
2
Mike
Barrky
Bam, now I have invites for all of my people and their pets.
But wait! The director doesn't want to limit this to ONLY people with pets. Anyone is invited! Its a walk-a-thon and after all, not ONLY people with pets can walk and donate money to my cause. So now, I want to get ALL of my people along with their Pet's name if they have one. THIS is how you decide which order the left join should be.
SELECT People.*, Pet.Name
FROM People LEFT OUTER JOIN
Pets ON Pet.PersonID = People.ID;
Now my results are the below.
ID
Name
PetName
1
John
Foodo
2
Mike
Barrky
3
Sue
NULL
I'm guessing/assuming that B.Name is not the same as A.Name. A.Name is presumably the name of the Product (as it is coming from the Product table) whereas B.Name is the name of something in the sales order

Need some assistance with 2 SQL queries [closed]

Closed. This question needs to be more focused. It is not currently accepting answers.
Want to improve this question? Update the question so it focuses on one problem only by editing this post.
Closed 6 years ago.
Improve this question
I need some help with my exam, there are 2 questions but I don't know how to join them and which SQL statements I need.
Write a SQL statement which displays the employee id, last name,and salary of any employee whose salary is above the price of the most expensive product.
Write a SQL statement which displays the customer id of customers who purchased the product with product_id 1 but so far never purchased the product with product_id 3.
It would be nice if somebody explain me the solution.
With the understanding this is a practice exam, and that you don't have access to the tables/values....
You really should show what you've tried... but since you're new...
Question 1:
Return employeeID, last name and salary of all employees who have a salary greater than the price of the most expensive product (like how they avoided using the word max there...)
So this uses a subquery to get the max price from products and simply compares salary against it.
SELECT Employee_ID, Last_Name, Salary
FROM employees
WHERE salary > (Select max(price) from Products);
Question 2:
Generate two sets one for product_ID of 1 one for product_ID of 3 and make sure those with 1 are not in the group with 3.
and this uses a correlated subquery (notice how the PUR alias is in the subquery? that's why it's correlated)
So the first query gets all the customers buying product 1. The 2nd query finds all the customers buying product 3. and since we want only those customers who bought 1 not 3, we correlate the queries using not exists and we have our answer.
Exists - usually fastest as it can early exit the subquery once a single record for a customer is found.
SELECT Distinct PUR.Customer_ID
FROM purchases PUR
WHERE PRODUCT_ID = 1
and not exists (SELECT 1
FROM Purchases PUR2
WHERE Product_ID = 3
and PUR.Customer_ID = PUR2.Customer_I
There are 2-3 ways of doing this. I find the above the most efficient in most cases. However you could also do it by... a self join; or using an not IN statement
Self LEFT join (works great if you need data from multiple tables
We self join on purchases but filter each table instance to be for a specific product. we use left join as we want all records with product Id 1 and where a match is found in p2, we want to EXCLUDE those records so if the customer_ID is null that means they have no product 3 purchases
SELECT Distinct P1.customer_ID
FROM Purchases P1
LEFT JOIN Purchase P2
on P1.customer_Id = P2.customer_ID
and P2.Product_ID = 3
WHERE P1.Product_ID = 1
and P2.customer_ID is null
not in (usually slowest but works fine if subquery is for a VERY Small datasets)
SELECT Distinct PUR.Customer_ID
FROM purchases PUR
WHERE PRODUCT_ID = 1
and customer_ID not in (SELECT Customer_Id
FROM Purchases
WHERE Product_ID = 3)
Notice that neither of the 1st two answers uses a join; though a correlated subquery is close.

Select Max in a table to get name in other table SQL [closed]

Closed. This question needs details or clarity. It is not currently accepting answers.
Want to improve this question? Add details and clarify the problem by editing this post.
Closed 8 years ago.
Improve this question
I have 2 tables
Players
Pnr
Pname
Padress
Pcity
Tickets
Tnr
Pnr
Date
Costs
I want to get the name of the player with the highest Ticket Cost from the database with Select. And I want to know who has got a Ticket in May. I also want to know who has never got a Ticket.
How do I get each of these?
Pnr is the primary key of Players and is connected with Pnr from Tickets
I've tried
SELECT MAX(Costs) from Players, Tickets
Where max()
I hope i understand correctly. Try the following query. I think it will still have some errors, but you can comment them here and I will try to correct them.
select top 1 Pname from Players
inner join Tickets on Players.Pnr = Tickets.Pnr
where Date > 1.05.2014 and Date <31.05.2014
order desc by Tickets.Costs
For highest ticket cost:
SELECT P.*,T.Tnr,T.Date,T.cost
FROM Players P JOIN
Tickets T ON T.Pnr=B.Pnr
WHERE T.Cost= SELECT MAX(Cost) from Tickets
For player who never got a ticket:
SELECT P.*
FROM Players P LEFT JOIN
Tickets T ON T.Pnr=B.Pnr
WHERE T.Tnr IS NULL
AND T.Pnr IS NULL
AND T.Date IS NULL
AND T.Costs IS NULL

How do you join three tables in Oracle? [closed]

Closed. This question does not meet Stack Overflow guidelines. It is not currently accepting answers.
This question appears to be off-topic because it lacks sufficient information to diagnose the problem. Describe your problem in more detail or include a minimal example in the question itself.
Closed 9 years ago.
Improve this question
I'm having a hard time joining three separate tables in Oracle. I'm never joined three tables before so I'm not well versed. My theory is below:
SELECT customer_num WHEN customer_num IS 104 -198, order_num
FROM orders INNER JOIN items
ON order_num, stock_num
INNER JOIN stock
ON stock_num, description
Essentially, I'm trying to start with the ORDERS table and pull the customer number (customer_num) specifically customer number 104 -108 and the order_num from the orders table. Then attach the orders table to the Items table and attach the order_num and stock_num, and lastly attach the stock table and pull out the stock_num and description.
Your syntax is all over the place, and wouldn't work for a two-table join, or even querying a single table. Not sure where you got WHEN from, or the order of your clauses. Please review the SQL reference to see how to join and how to filter. Anyway...
You seem to want something like this:
SELECT o.customer_num, o.order_num, i.stock_num, s.description
FROM orders o
INNER JOIN items i ON i.order_num = o.order_num
INNER JOIN stock s ON s.stock_num = i.stock_num
WHERE o.customer_num BETWEEN 104 AND 198;
The WHERE clause is being applied to the orders table to restrict which customers' orders you get. I've assumed from your description that the orders and items tables have a common order_num column you can join on; and that the items and stock tables have a common stock_num column you can join on.
As OldProgrammer said, it would be helpful to include your table schemas in the question so assumptions don't need to be made, and showing sample data and expected output for that data would clarify what you're trying to do.
It should look something like this (based on the limited information about the schema)...
SELECT
O.CUSTOMER_NUM,
O.ORDER_NUM,
S.STOCK_NUM,
S.DESCRIPTION
FROM
ORDERS O,
ITEMS I,
STOCK S
WHERE
I.CUSTOMER_NUM >= 104
AND I.CUSTOMER_NUM <= 198
AND O.ORDER_NUM = I.ORDER_NUM
AND I.STOCK_NUM = S.STOCK_NUM
The syntax for a JOIN is:
TableExpression [ INNER ] JOIN TableExpression
{
ON booleanExpression | USING clause
}
The booleanExpression is what the tables will be linked by. So, in your example, something like this:
SELECT customer_num WHEN customer_num IS 104 -198, order_num
FROM orders INNER JOIN items
ON orders.<some_column> = items.<some_column>
INNER JOIN stock
ON items.<some_column> = stock.<some_column>

Inner join 2 tables one to many 2 where clauses [closed]

Closed. This question does not meet Stack Overflow guidelines. It is not currently accepting answers.
Questions asking for code must demonstrate a minimal understanding of the problem being solved. Include attempted solutions, why they didn't work, and the expected results. See also: Stack Overflow question checklist
Closed 9 years ago.
Improve this question
I have 2 tables:
OrderDetail
OrderMaster
Both have a column named SalesOrder.
OrderDetail table has multiple rows per unique SalesOrder.
OrderMaster table has one row per unique SalesOrder.
OrderDetail has a column named LineType.
OrderMaster has a column named OrderStatus.
I want to select all records from OrderDetail that have a LineType of "1" AND whose matching SalesOrder line in the OrderMaster table has a OrderStatus column value of "4".
In plain English, orders with a Status 4 are open and ready to ship, LineType value of 1 means the Detail Line is a product code.
How should this query be structured? It's going into VS 2008 (VB).
I can give you some sql:
SELECT d.*
FROM OrderDetails d
INNER JOIN OrderMaster m ON m.SalesOrder = d.SalesOrder
WHERE d.LineType = 1 and m.OrderType = 4
How you'll use that from VB.Net depends on a number of things that weren't included with your question.