Creating an INNER JOIN from 2 tables IF condition is met - sql

I have 2 tables, Products and Orders. If somebody creates an order, I want Product_Name01 from Orders to be filled in by the Product_Name from Products.
enter image description here
The code below does not work. What am I doing wrong?
SELECT * FROM Sandbox.dbo.orders o
SELECT * FROM Sandbox.dbo.Products p
INNER JOIN o -- is this the target table?
WHERE EXISTS (
SELECT Product_Name01
FROM o
WHERE b.Product_ID01 = o.Product_ID )

I think the inner join already "checks" if both tables have matching records.
"The INNER JOIN keyword selects records that have matching values in both tables."- - https://www.w3schools.com/sql/sql_join_inner.asp
So your query should probably just look like this:
SELECT * FROM Sandbox.dbo.orders o
INNER JOIN Sandbox.dbo.Products p
ON b.Product_ID01 = o.Product_ID;

Related

How to avoid the "ambiguous" error message when using joins to create several tables

I'm trying to use the following code to create a list of customers and their brands that they buy. The brands table has the brand name and customer_id is in the customers table. To link them I have to get the brand_id and receipt_id linked together via the receipts table (connects to customers table) and receipt_item_details1 table (connects to brands table).
So, receipt_item_details1 table (has brand_id column to then connect to brands table) and new table customer_receipts (created by first inner most subquery) are trying it to be linked by receipt_id. I'd like to show the customer_id column when I build my table joining these two table (an original: receipt_item_details1 joined to a new table: customer_receipts).
ISSUE: I keep getting the following error. how do Infix it and also list the brands?
"column reference "customer_id" is ambiguous
LINE 3: ...pts.receipt_id, receipt_item_details1.receipt_id, customer_r.."
SELECT customer_brandids.brand_id, brands.brand_id, customer_brandids.customer_id, brands.brand_name
FROM
(SELECT customer_receipts.receipt_id, receipt_item_details1.receipt_id, customer_receipts.customer_id, receipt_item_details1.brand_id
FROM
(SELECT receipts.customer_id, customers.customer_id, receipts.receipt_id
FROM receipts
INNER JOIN customers
ON receipts.customer_id = customers.customer_id) AS customer_receipts
INNER JOIN receipt_item_details1
ON customer_receipts.receipt_id = receipt_item_details1.receipt_id) AS customer_brandids
INNER JOIN brands
ON customer_brandids.brand_id = brands.brand_id
Your inner subselect
(SELECT receipts.customer_id, customers.customer_id
generates a result with two columns named customer_id. So your next higher subselect cannot differ between both columns if you reference customer_id
You should give one or both an alias:
(SELECT receipts.customer_id as r_customer_id,
customers.customer_id as c_customer_id
Then your next higher query can call
SELECT customer_receipts.c_customer_id...
So first step of solving the problem:
SELECT
customer_brandids.brand_id,
brands.brand_id,
customer_brandids.c_customer_id, --> reference alias
brands.brand_name
FROM
(SELECT
customer_receipts.receipt_id as c_receipt_id, --> same problem
receipt_item_details1.receipt_id as r_receipt_id,
customer_receipts.c_customer_id, --> reference alias
receipt_item_details1.brand_id
FROM
(SELECT
receipts.customer_id as r_customer_id, --> here was the problem
customers.customer_id as c_customer_id,
receipts.receipt_id
FROM receipts
INNER JOIN customers
ON receipts.customer_id = customers.customer_id) AS customer_receipts
INNER JOIN receipt_item_details1
ON customer_receipts.receipt_id = receipt_item_details1.receipt_id) AS customer_brandids
INNER JOIN brands
ON customer_brandids.brand_id = brands.brand_id
Addionally:
You don't need to take both columns (e.g. of receipt_id) because of the INNER JOIN it is ensured that both columns have the same value
You can use aliases to shorten your query.
You don't need to create a subquery for each join. Just join.
All in all, this should do the same:
SELECT b.brand_id, c.customer_id, b.brand_name
FROM receipts r
INNER JOIN customers c ON r.customer_id = c.customer_id
INNER JOIN receipt_item_details1 rid ON r.receipt_id = rid.receipt_id
INNER JOIN brands b ON b.brand_id = rid.receipt_id
demo: db<>fiddle
Do not use nested selects when it is not necessary, try to use joins, and query will be more simple and will look something like this
select * from receipts
join customers on receipts.customer_id = customers.customer_id
join receipt_item_details1 on receipts.receipt_id = receipt_item_details1.receipt_id
join brands on receipt_item_details1.brand_id = brands.brand_id
Instead of asterisk you can define columns you want to get

Joining 3 tables in sql-server

I have three tables in sql-server like table A table B table C.
How can I join 3 tables as expressed in the image below?
More information needed to give you a correct piece of code, but from the image you need LEFT JOINs.
(ID's have been presumed)
SELECT *
FROM Customers c
LEFT JOIN Items i ON c.iid = i.id
LEFT JOIN Sales s ON c.sid = s.id
It's never too late :)
Most probably you'll need this:
select ...
from customers
full outer join (items inner join sales on (xxx)) on (xxx)

How do i apply inner join or left join to my query?

This is my query :
select p.cod_produs, p.title, c.category as cname, p.description, p.short_desc, p.img1, p.price, p.qty, p.isActive, p.disponibilitate
from tblproducts p
inner join tblcategory c on p.cat_id = c.id
I have two tables : tblcategory & tblproducts. In my tblcategory i have an attribute cat_id. There are possibilities when my product does not have any categories assigned, in that case the cat_id's value is 0. My query returns all of the products which have categories. So , I want to create a query which returns me all of the products with categories and without(return 0 ). How can i do that ? thx
Inner Join : The INNER JOIN keyword selects all rows from both tables as long as there is a match between the columns in both tables. i.e. Only if both tables have the matching column, it is added to the result.
Left Join : The LEFT JOIN keyword returns all rows from the left table (table1), with the matching rows in the right table (table2). The result is NULL in the right side when there is no match. I think this is what you need (from what I interpreted from your question).
Read more on joins here

SQL the column exists more than once

This is working fine
SELECT i.*,o.*,p.*
FROM orders o
INNER JOIN oitems i
ON i.orderid = o.orderid
LEFT OUTER JOIN products p
ON i.catalogid = p.catalogid
however i want to perform a nested select as an example this is giving the coulmn x exists more than once
SELECT AA.*,
FROM (
SELECT i.*,o.*,p.*
FROM orders o
INNER JOIN oitems i
ON i.orderid = o.orderid
LEFT OUTER JOIN products p
ON i.catalogid = p.catalogid ) AA
i know the second example makes no sense , but i need another select with groupping, is there a way to fix the coulm exists more than once error without having to specify the column names in the select statement?
By using *, you are getting the same column more than once in your output. To avoid, this, specifically state the columns you want returned, instead.
The culprits are likely orderid and catalogid which both exist in more than one table, but there may be others.
You have mention column names specifically. I can see that you are using "*". you have to use like select i.columnName,o.columnName etc

Joining multiple tables and getting multiple attributes from one of them

I'm trying to join multiple tables together for building a report. The report lists a course, revisions made to it, and who requested, made and approved the revisions.
Under requested, made an approved, the values are employee numbers. I'm trying to join my innerjoined table above, with the Employee table so I can list the names (not just employee numbers) of those that requested, made and approved revisions.
This is what I have which I know is totally wrong.
SELECT *
FROM Courses
INNER JOIN CourseRevisions ON CourseRevisions.PELID = Courses.PELID
INNER JOIN CourseGroups ON CourseGroups.CourseGroupID = Courses.CourseGroupID
INNER JOIN [dbo].[OPG_Employees] ON OPG_Employees.EmployeeID = CourseRevisions.UpdatedBy
AND OPG_Employees.EmployeeID = CourseRevisions.ApprovedBy
AND OPG_Employees.EmployeeID = CourseRevisions.RequestedBy
This only returns a single result which just happens to have the same employee ID listed for all 3 (Requested, Approved and Updated)
How would i get it so I can get the table result for individual employees in each?
You have to join to the OPG_Employees table once for each field, i.e. 3 times in the example above. One INNER JOIN to it for UpdatedBy, one INNER JOIN for ApprovedBy, one INNER JOIN for RequestedBy.
Something like so:
SELECT *
FROM Courses
INNER JOIN CourseRevisions ON CourseRevisions.PELID = Courses.PELID
INNER JOIN CourseGroups ON CourseGroups.CourseGroupID = Courses.CourseGroupID
INNER JOIN [dbo].[OPG_Employees] empUpdatedBy ON empUpdatedBy.EmployeeID = CourseRevisions.UpdatedBy
INNER JOIN [dbo].[OPG_Employees] empApprovedBy ON empApprovedBy.EmployeeID = CourseRevisions.ApprovedBy
INNER JOIN [dbo].[OPG_Employees] empRequestedBy ON empRequestedBy.EmployeeID = CourseRevisions.RequestedBy
You need a separate join for each employee being referenced:
SELECT *
FROM Courses INNER JOIN
CourseRevisions
ON CourseRevisions.PELID = Courses.PELID INNER JOIN
CourseGroups
ON CourseGroups.CourseGroupID = Courses.CourseGroupID INNER JOIN
[dbo].[OPG_Employees] UpdateEmp
ON UpdateEmp.EmployeeID = CourseRevisions.UpdatedBy INNER JOIN
[dbo].[OPG_Employees] ApprovedEmp
on OPG_ApprovedEmp.EmployeeID = CourseRevisions.ApprovedBy INNER JOIN
[dbo].[OPG_Employees] RequestedEmp
on RequestedEmp.EmployeeID = CourseRevisions.RequestedBy
Your original formulation required that all three ids be exactly the same.