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

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>

Related

Need help trying to figure this out [closed]

Closed. This question needs debugging details. It is not currently accepting answers.
Edit the question to include desired behavior, a specific problem or error, and the shortest code necessary to reproduce the problem. This will help others answer the question.
Closed 6 years ago.
Improve this question
I have been trying to figure this out for 4 days now. I am new to this and just can't this to work like it should. Any help would be appreciated.
*Not just looking for the answer
The president of the company wants a list of all orders ever taken. He wants to see the customer name, the last name of the employee who took the order, the shipper who shipped the order, the product that was ordered, the quantity that was ordered, and the date on which the order was placed. [Hint: You will need to join the following tables: Customers, Employees, Shippers, Orders, OrderDetails, Products, and to get all of the necessary information.]
Think about what you are trying to do here. You'll need to join together multiple tables to build the desired dataset. The type of JOINs you should be using will likely associate foreign keys from one table with the primary key of another. Please review JOINs if this is not clear.
Here is an example query that may get you on the right track. The column names used will likely vary, and I'll let you figure out how to get Quantity in the dataset (the quantity is probably in OrderDetails or perhaps you need to aggregate by product ID):
SELECT Customers.name, Employees.lastName, Shippers.name, Products.name,OrderDetails.orderDate
FROM Orders
JOIN OrderDetails ON OrderDetails.id = Orders.orderDetailId
JOIN Customers ON Customers.customerId = Orders.customerId
JOIN Employees ON Employees.employeeId = Orders.employeeId
JOIN Shippers ON Shippers.Id = OrderDetails.shipperId
JOIN Products ON OrderDetails.ProductId = Products.Id;
I should note that this is formatted for T-SQL and depending on your DBMS syntax may vary.

SQL: Need to select items by different conditions [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 6 years ago.
Improve this question
My table looks like this -
Explanation
I'm working on an online shop that sells tours and spa. I'm showing all together on one page.
What I need to select?
1. All spa products (based on "Spa" column), no conditions.
2. All parent tours that have children with an upcoming date.
Meaning
Products ID 1, 4, 5.
Why?
Product 6 have a child, but from 1999. And although product 1 have one child at 2000, it has another one in 2017. All spa products are selected by default, without conditions.
I hope I made my question clear as I could. I would appreciate any help, my SQL is really bad.
Your data structure isn't great. Really, as you have a parent -> child relationship as well as special types, it would be better to have three tables e.g. Product, ProductType and ProductItem or some such nomenclature. The parent table contains the ID, name and typeId (having a flag limits you to only two types, you may chooes to have more). Your child table contains a ID, parent ID, name and date. Then you can simply use a foreign key constraint to link the two and do some simple SQL to join it all up e.g.
SELECT pt.Name, pi.Name, pi.Date from ProductItem pi
INNER JOIN Product p on p.id = pi.parentID
INNER JOIN ProductType pt on p.typeId = pt.id
WHERE pi.date > now() --The "Now" part depends on your RDBMS as it's different on different systems
AND pt.name = "TOUR"
UNION
SELECT pt.Name, p.Name, 'N/A' from Product p
INNER JOIN ProductType pt on pt.id = p.typeId
WHERE pt.name = "SPA"

ambiguous column name error [closed]

Closed. This question needs debugging details. It is not currently accepting answers.
Edit the question to include desired behavior, a specific problem or error, and the shortest code necessary to reproduce the problem. This will help others answer the question.
Closed 7 years ago.
Improve this question
I have two tables, and I want to connect the tables by CustomerID (CustomerID is same for two tables). So I used my query like:
SELECT
Cus.CustomerID
FROM
Customers AS Cus
JOIN
Payments AS Pay ON Cus.CustomerID = Pay.CustomerID
WHERE
CustomerID = 2
And it shows an error:
'CustomerID' in where clause is ambiguous
How can I resolve ambiguous column name error in a way that does not add the table name before CustomerID?
Since it doesn't know which CustomerID to use in your where clause CustomerID = 2, you should specify which to use:
SELECT
Cus.CustomerID
FROM
Customers AS Cus
JOIN
Payments AS Pay
ON
Cus.CustomerID = Pay.CustomerID
WHERE
Cus.CustomerID = 2
It doesn't matter if you use Cus.CustomerID or Pay.CustomerID since they will always be the same in your current statement (since you equal them in your join).
The ambigious part is here:
WHERE
CustomerID = 2
Both tables have this column you need to be explict either:
Cus.CustomerID
or
Pay.CustomerID
[I]n a way that does not add the table name before CustomerID
You can use a sub-query.
SELECT CustomerID
FROM
(
SELECT
Cus.CustomerID
FROM
Customers AS Cus
JOIN
Payments AS Pay
ON
Cus.CustomerID = Pay.CustomerID
) query
WHERE CustomerID = 2
Is this a good way? No. This may force the query plan to use a scan instead of a seek if CustomerID is an indexed field (which it should be since this is being joined on).

With what groupings am I supposed to think about joins? [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 7 years ago.
Improve this question
I'm looking at the example
SELECT Orders.OrderID, Customers.CustomerName, Orders.OrderDate
FROM Orders
INNER JOIN Customers
ON Orders.CustomerID=Customers.CustomerID;
from W3Schools and am wondering how I'm supposed to group the clauses in my mind. From what I understand, every SQL query returns a table, and clauses within the query may themselves return a table. So I think of the whole
Orders INNER JOIN Customers ON Orders.CustomerID=Customers.CustomerID
as being a table and I'm returning a sub-table of it by applying SELECT Orders.OrderID, Customers.CustomerName, Orders.OrderDate to it. Is that the right way to think about things?
You can think of every query as if it composes a new temporary table with the result you want. This one takes rows from Orders and from Customers, matches them according to the CustomerID field in both tables (the on clause of the join), and then returns just several fields from Orders (OrderID and OrderDate) and one field from Customers (CustomerName).
Basically, you are returning a new table containing all of the columns in your SELECT statement. If you simplify the query to:
SELECT OrderID, OrderDate
FROM Orders
You'd get a printout of the entire Orders table, but with only the two columns you specified.
With your INNER JOIN, you are filtering out the results to only include the Orders that have a CustomerID that matches a similar row in the Customers table with that same CustomerID. And with your SELECT statement, you are adding a column that shows the customer's name that is matched up with those orders.
So the ON part of the INNER JOIN is just matching customers to their respective orders. The SELECT part is where you are creating the new output table with the three columns you are interested in.
Does that help?

Get Data From A Database [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
Edit* I want to get the actual info. Right now I would get something like this:
Bob
21
1
I want to replace 21 with California and 1 with United States
If I have a database such as:
Client
Name
StateId
CountryId
Then what is the best way to get that data? Currently I have:
SELECT * FROM Client
SELECT * FROM Client
LEFT JOIN StateId,
ON Client.StateId=State.Id
SELECT * FROM Client
LEFT JOIN CountryId,
ON Client.CountryId=Country.Id
But this gives me 3 tables. Is this how database information should be read or should I get back one table that with the information in it?
You can do multiple joins in the same query. Something like:
SELECT Client.Name, State.Name, Country.Name
FROM Client
LEFT JOIN State ON Client.StateId=State.Id
LEFT JOIN Country ON Client.CountryId=Country.Id
This assume that your states in State table and countries are in Country table.
SELECT * FROM Client
SELECT * FROM Client
LEFT JOIN StateId,
ON Client.StateId=State.Id
SELECT * FROM Client
LEFT JOIN CountryId,
ON Client.CountryId=Country.Id
You can turn this into 1 query with the following
SELECT c.ClientID, c.Name, s.Name, co.Name from Client c
inner join Country co on c.Countryid = co.ID
left join state s on c.StateID = s.ID
I believe you will want an inner join actually, but this is my interpretation. Left joins will return all values in table a regardless of a value being present in table b, so if you want all people regardless of whether they have a country ID then yes a left join, but if you want all people with a country relationship and a state relationship I believe you will initially want an inner join. the second join may be inner as well, depending on if nulls are acceptable in your end table result.
Also, left joins can return some unwanted results, if a person is in multiple countries or has multiple states you could return more rows than necessary. You should establish the business rules of each table before implementing the contents of two left joins into your result set.
This may be for a class, and has no real purpose for business, but it is best not to assume and give you all possible scenarios of your result set.
What I'm doing is returning all the clients with a corresponding countryID, i don't want to return nulls here because in your next join relationship you will also return a null if country is the link between client and state.