How to combine 3 tables in SQL - sql

im using SQL and i have three tables: Owner, PetType and PetAndOwner. i want to list all the people in the owner table who own a dog. these are the tables and their attributes
https://imgur.com/a/FJRJlsU
the trouble that im having is that i cant seem to find the right code to use to be able to find out which owners have dogs because you have to get the data from the PetAndOwner table and then join it back with the other tables to print the data.
my final result should show all owners names who have a dog

There seems to be a missing table in your query, so assuming that table holds details of a pet:
SELECT Owner.FirstName,Owner.LastName,Pet.Name
FROM PetType
JOIN Pet ON Pet.PetTypeId = 1 AND PetType.PetTypeId = Pet.PetTypeId
JOIN PetAndOwner ON Pet.PetId = PetAndOwner.PetId
JOIN Owner ON Owner.ownerId = PetAndOwner.ownerId
Here is a Demo

Related

SQL select with three tables

Hi guys I'm new with databases and I'm trying to make a query where I join 3 tables. I could make it and I want to clean up the result. I want to know how can I delete the column "pin" from users table and maybe some "ids" columns.
Select * from "wish-list"
Join products
On "wish-list".id = products.holiday_id
Join users
On "wish-list".user_id = users.id
Where "wish-list".id = 1
You need to specify which columns you really need in your output. At the moment you are using
SELECT * which outputs all columns of all joined tables.
Here is what it should look like:
SELECT holiday, products.description, users.pin FROM "wish-list"
JOIN products ON "wish-list".id = products.holiday_id
JOIN users ON "wish-list".user_id = users.id
WHERE "wish-list".id = 1
It's important that you reference all columns which are not your main entity (here wish-list) with tablename.column (products.description and not only description). It will work without referencing strictly but only if the column name is unique in your query.
Furthermore you can rename columns. This is useful for example if you want to get the id's of the product table and the wish-list table.
SELECT product.id AS product_id, id AS wishlist_id FROM "wish-list"
...
Hope that helps!

Complex SQL Query with Dead Ends and 7 Tables

I was tasked with creating a complex query that incudes all of the data from all of the tables minus the Keys. I am having an issue with the dead end tables and how to circle back around to include the data of the connecting table. I need to select columns DivisionName, ProgramName, ProgramChairFName, ProgramChairLName, CourseID, OutcomeDescription from the listed tables.
SQL Diagram
The 'dead-ends' aren't really dead-ends. When you join all the tables by the appropriate keys, you'll get an assembly of the information you want.
Consider a really simple example:
table person
id name
1 Alice
table pet
id person_id animal
1 1 cat
table hobby
id person_id activity
1 1 dancing
Here, the two tables pet and hobby link to the person table via the person_id key.
In your thinking, "pet" could be considered a "dead-end" because it doesn't link to hobby. But it doesn't need to. The query:
SELECT name, animal, activity
FROM person
JOIN pet ON person.id = pet.person_id
JOIN hobby ON person.id = hobby.person_id;
creates the correct joins back to the person table. It's not a linear path (person -> pet -> hobby). The nature of the joins are specified by the "ON" part of the query. You can see this simple example works here: http://sqlfiddle.com/#!9/02c94b/1
So, in your case, you can have a series of JOINs:
SELECT [all the columns you want]
FROM Division d JOIN Program p
ON d.DivisionKey = p.DivisionKey
JOIN ProgramChairMap pcm
ON p.ProgramKey = pcm.ProgramKey
JOIN ProgramChair pc
ON pcm.ProgramChairKey = pc.ProgramChairKey
JOIN Course c
ON p.ProgramKey = c.ProgramKey
JOIN CourseOutcome co
ON c.CourseKey = co.CourseKey
JOIN Outcome o
ON co.OutsomeKey = o.OutcomeKey

SQL query to pull data from three different tables

I have to create a SQL query to list all the Nurses in the ‘Sparrow’ Wing ordered by last name, first name.
However, I need to pull Nurse_name and Nurse_surname from the Nurse table, which is linked to another table called Sister by the foreign key Sister_ID, this table is then linked to another table called Wing which has the foreign key Sister_ID.
The nurse is managed by the sister and the sister manages the wing.
Can anyone help me with an SQL query for this? As it is, I am only able to get the data from nurse and sister tables.
Since you seem to be aware that you should use the inner join to connect tables (but apparently not that the connection needs to be via the related columns) you should apply that knowledge to connect all the tables you need to answer the query.
If you start at the end result and work your way backwards you first chose the columns you need:
select Nurse.Nurse_name, Nurse.Nurse_surname
and then as they belong to the Nurse table you use that as source in the from clause
from Nurse
to get the Wing you need to connect the Sister table, but to connect that and Nurse you first need the SisterNurse table joined on the shared attribute
join SisterNurse on Nurse.Nurse_ID = SisterNurse.Nurse_ID
now you can join Sister on the attribute shared with SisterNurse
join Sister on Sister.Sister_ID = SisterNurse.Sister_Id
and finally you can join Wing
join Wing on Wing.sister_ID = Sister.Sister_ID
limit the Wings to the one names 'Sparrow'
where Wing.Wing_Name = 'Sparrow'
and order the data
order by Nurse.Nurse_surname, Nurse.Nurse_name
Put it all together and you get:
select Nurse.Nurse_name, Nurse.Nurse_surname
from Nurse
join SisterNurse on Nurse.Nurse_ID = SisterNurse.Nurse_ID
join Sister on Sister.Sister_ID = SisterNurse.Sister_Id
join Wing on Wing.sister_ID = Sister.Sister_ID
where Wing.Wing_Name = 'Sparrow'
order by Nurse.Nurse_surname, Nurse.Nurse_name
You don't give much information about the schema involved but maybe this will help:
select
n.Nurse_name
, n.Nurse_surname
, w.Wing_name
, managing_nurse.Nurse_name
, managing_nurse.surname
from
Nurse n
join Sister s on n.Sister_ID=n.Sister_ID
join Wing w on s.Sister_ID=w.Sister_ID
join Nurse managing_nurse on w.Nurse_Manager_ID=managing_nurse.Sister_ID
A simple way is to use 'union':
select * from (
select nurse_name, nurse_surname, 'nurse' as nurse_type from nurse_table
union
select nurse_name, nurse_surname, 'sister' as nurse_type from syster join nurse_table on nurse_table.syster_id = syster.syster_id
union
select nurse_name, nurse_surname, 'wing' as nurse_type from wing join syster on wing.syster_id = syster.syster_id join nurse_table on nurse_table.syster_id = syster.syster_id )
order by nurse_surname, nurse_name
Hope this help you!
P.S.
I assume that syster and wing tables have nurse_name and nurse_surname fields.
If not you have to give the same alias to all the selected columns

How should I JOIN my tables?

I'm learning C# and some SQL server, and now i am trying to get information from my small database.
I have two tables: Movie and MovieHandler.
In the table Movie, i have MovieCodeLable, which is a uniqe number, and the Title of the movie.
In the MovieHandler table, i have MovieCodeLable which is the same as in my Movie table, and i have also her the colum InStore, which is eighter 0 or 1 for not in store or in store.
I'm trying to display the Title for the movies which is not in the store. But i find it hard figure out how to join tables.
I have tried this SQL query:
SELECT Title
FROM Movie
JOIN MovieCodeLable
ON MovieHandler.MovieCodeLable
WHERE InStore = 0
Since i only get errors trying this query in Visual Studio 2012, i've probably missed something fundametal with SQL and JOINS, but i hope that someonw could make JOINS smooth as butter for me and others, struggeling to learn it.
Your JOIN is wrong and your ON clause is incomplete. The JOIN should involve the names of the 2 tables that you are joining, which in this case is Movie and MovieHandler The ON should be expression of format A = B. So your query should be:
SELECT Title
FROM Movie
JOIN MovieHandler
ON Movie.MovieCodeLable = MovieHandler.MovieCodeLable
WHERE InStore = 0
You need to specify both JOIN fields
SELECT Title
FROM Movie
JOIN MovieHandler
ON Movie.MovieCodeLable = MovieHandler.MovieCodeLable
WHERE InStore = 0
You have to do the query like this
SELECT Title
FROM Movie
JOIN MovieHandler
ON Movie.MovieCodeLable = MovieHandler.MovieCodeLable
WHERE InStore = 0
You had to complete the ON condition.You need to specify the columns to match after ON condition.Go to this link there is explanations with tables http://www.w3schools.com/sql/sql_join.asp
I do it this way in oracle. Not sure if it will work for you...
Try:
Select Mv.Title
,other_columns
from Movie Mv
,MovieHandler Mvh
Where Mv.MovieCodeLable = Mvh.MovieCodeLable
and (whatever other criteria you want
As long as MovieCodeLable is unique you're ok
Other_columns can be any column of either table format with table.column syntax (I use table shortcuts Mv and Mvh to reference the tables, I don't know if this is legal for non-oracle)

MS Access Selecting Related Rows

I have 2 tables with a many-to-any relationship. For the example we will call the tables "Guys" and Girls" There is a junction table that contains the related primary keys...who has dated who.
If I want to find all the girls that Guy 1 has dated, I do a select on the junction table selecting all girls with guys.ID. This give me a RecordSet. Now to find the names of the girls, I need to select from the girls table a row using the key from each RecordSet row.
Isn't there an easier way? Since I've defined the relationships in Access I would think that there must be a way to build a single query. How do I do that?
SELECT girls.name
FROM (guys
INNER JOIN junct ON guys.guyID = junct.guyID)
INNER JOIN girls ON junct.girlID = girls.girlID
WHERE guys.guyID = [whatever id you're looking for]