Get Data From A Database [closed] - sql

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.

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

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.

How to query data over multiple tables in Sql [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 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

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>

Left join multiple tables onto one table [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 9 years ago.
The community reviewed whether to reopen this question 9 months ago and left it closed:
Original close reason(s) were not resolved
Improve this question
I want to left join multiple tables to one table. The tables are themselves results of subqueries.
A classical example that comes to my mind is, I have a bunch of subqueries:
1. Subquery A gives me details of students - say table 1
2. Subquery B gives me student scores in Math - say table 2
3. Subquery C gives me student scores in English - say table 3
The tables contain scores only if the student has taken that test and the student is to be considered failed if he/she has not taken test at all (or has a score < passing score). I have student IDs (unique per person) in each table to join on.
What do I want from these? I am trying to build a dynamic query (where some parts are populated at runtime by an external mechanism) by performing some joins on these tables to give me:
1. Students who passed in both tests and corresponding scores
2. Students passed in either test, but failed (or did not take) the other test and the corresponding scores (NULL if not taken).
3. All students and their corresponding scores.
What I have on mind is left joining each score table to student profile table. How should I go about this?
Before you go ahead and suggest table 1 left join table 2 left join table 3, this structure will cause problems if, say table 2 contains a null record for a particular student (as per my knowledge). And this basically joins table 3 on table 2 and not on table 1, from my understanding, which is what I want.
PS: Feel free to suggest better ways to get what I need, if you know any.
You can create the appropriate relations by writing carefully your query:
select
t1.*, t2.foo, t3.bar
from
table1 as t1
left join table2 as t2 on t1.id = t2.id
left join table3 as t3 on t1.id = t3.id
As you can see, table3 is related to table1 (not to table2), which is what you want.