How to query data over multiple tables in Sql [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 8 years ago.
Improve this question
I'm trying to get an extract of data using two tables in Sql. I have an AddressBook table and a companies table. The AddressBook table has a foreign key called companyid which is the primary key in the companies table. The companies table has a column called accountno. How do I lookup all the addresses on the AddressBook table and find the accountno in the companies table using the companyId?
Please let me know if you need any more info

Use the JOIN, i think you want left join. With left join you fetch the companies even if they dont have an adress, but i see you have an inner join tag so i will include that.
left join:
SELECT * FROM companies LEFT JOIN adressbook ON adressbook.companyid = companies.id
inner join:
SELECT * FROM companies INNER JOIN adressbook ON adressbook.companyid = companies.id

select *
from companies
inner join adressbook on adressbook.companyid = companies.id
if i read it correctly this is what you are looking for

Related

LEFT JOIN on column where value IS NOT NULL, otherwise join on another column [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 3 months ago.
The community reviewed whether to reopen this question last month and left it closed:
Original close reason(s) were not resolved
Improve this question
Joining 2 tables (orders, addresses)
Orders contains columns delivery_address_id (contains NULL values) and invoice_address_id (does NOT contain NULL values)
Addresses contains id column (does NOT contain NULL values)
Primarily, the LEFT JOIN must be performed on orders.delivery_address_id. However, in the case when its value is NULL in the row, perform LEFT JOIN on orders.invoice_address_id.
How do I deal with this?
I tried the operator OR but the result was not correct. I was also thinking about a CASE WHEN statement. My expectations are to get the LEFT JOIN working.
You can use the operator OR in the ON clause:
SELECT ....
FROM Orders o LEFT JOIN Addresses a
ON a.id = o.delivery_address_id
OR (o.delivery_address_id IS NULL AND a.id = o.invoice_address_id);
Or, use COALESCE():
SELECT ....
FROM Orders o LEFT JOIN Addresses a
ON a.id = COALESCE(o.delivery_address_id, o.invoice_address_id);
So, you want to join on delivery_address_id, but sometimes it's NULL, so you need invoice_address_id to be a fallback.
These situations are where the COALESCE function really shines. COALESCE(delivery_address_id, invoice_address_id) will resolve to the delivery address ID if it isn't NULL, otherwise it will resolve to the invoice address ID instead.
Thus we can achieve the join you want:
SELECT
orders.some_field,
addresses.address
FROM
orders
LEFT JOIN
addresses
ON
COALESCE(orders.delivery_address_id, orders.invoice_address_id) = addresses.id

Query for retrieving data from a bridging table of many to many relationship [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 2 years ago.
Improve this question
I have a table SQL server with two joining or bridging tables because of the many-to-many relationship.
I wonder if anyone can write the query to retrieve data or perform CRUD operations for this table that updates all these three tables.
Please check the attached Diagram particularly the table (Case, Violence_type, and Referral table in the middle where it has a bridging table).
Click to see DB diagram
Yours, omer
So you're trying to link e.g. Case to Referral via the association table Case_Referral? So what's the issue you're facing?
This is a pretty simple, straightforward SQL statement - SELECT from Case, join on Case_Referral via the case_id key, then join to Referral using the referral_id, and specify which columns from each table you need:
SELECT
c.user_name, c.date as CaseDate, c.priority, c.case_status,
r.date AS ReferralDate, r.referral_name
FROM
dbo.Case c
INNER JOIN
dbo.Case_Referral cr ON c.case_ID = cr.case_ID
INNER JOIN
dbo.Referral r ON cr.referral_ID = r.referral_ID
So what is the issue / problem you're not understanding?
You can use the same "technique" to join the other m:n relationships.

SQL can't get the accurate data [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 years ago.
Improve this question
I get trouble when getting data in SQL. How to have one table source or primary key but two different foreign key.
This is the ERD
This is the result but when i put the Departure it will be vice versa
If I'm understanding your problem correctly, it seems that you're trying to get both the "Departure" and "Arrival" values from the same join. In order to get this to work, you'd need to have two separate joins from Routes to Airports (and aliased accordingly) for each of "Departure" and "Arrival".
select date,
time,
ArrivalAirport.IATACode as Departure,
DepartureAirport.IATACode as Arrival,
FlightNumber,
Aircrafts.Name,
EconomyPrice,
Schedules.ID
from Schedules
inner join [Routes] on Schedules.RouteID = [Routes].ID
inner join Aircrafts on Schedules.AircraftID = Aircrafts.ID
inner join Airports as ArrivalAirport on ArrivalAirport.ID = [Routes].ArrivalAirportID
inner join Airports as DepartureAirport on DepartureAirport.ID = [Routes].DepartureAirportID
Note that the two instances of "Airports" joins necessitate the need to alias them accordingly so you can do what you need in the select projection.
You need to use table alias names for the table that is used twice, then join to it twice.
SELECT Routes.ID, DEP.IATACode, ARR.IATACode
FROM dbo.Routes
INNER JOIN dbo.Airports AS DEP ON DEP.ID = Routes.DepartureAirportID
INNER JOIN dbo.Airports AS ARR ON ARR.ID = Routes.ArrivalAirportID
You use the alias everywhere in the query once defined. See here for more on using an alias
Here is what I think you are looking for:
SELECT
Schedules.Date,
Schedules.Time,
DepartureAirport.IATACode AS Departure,
ArrivalAirport.IATACode AS Arrival,
Schedules.FlightNumber,
Aircrafts.Name,
Schedules.EconomyPrice,
Schedules.ID
FROM
Schedules
INNER JOIN Aircrafts ON Schedules.AircraftID = Aircrafts.ID
INNER JOIN Routes ON Schedules.RouteID = Routes.ID
INNER JOIN Airports AS DepartureAirport
ON Routes.DepartureAirportID = DepartureAirport.ID
INNER JOIN Airports AS ArrivalAirport
ON Routes.ArrivalAirportID = DepartureAirport.ID
The thing is to have two separate joins onto the Airports table so you can have two different airports. Otherwise you will have very short flights, and very dissatisfied passengers.

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.

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>