I am trying to create views of a database containing information about horses, more specifically horses, their owners, and the horse boarder. The view must join three tables. I have to create a view of "first name, last name, primary phone, and barnname". Then I have to join tables "boarder, horse, and boarder_horse" to create the relationship. I can't figure out how to connect the various tables together.
So far, this is what I have:
CREATE VIEW horse_owner
AS
SELECT b.boarder firstname, b.boarder lastname, b.boarder primaryphone,
h.horse barname
FROM boarder b
INNER JOIN horse h
ON bh.horse_id = h.id
INNER JOIN boarder_horse
ON bh.boarder_id = b.id
ORDER BY LastName DESC;
I don't understand how to correctly link the appropriate tables together.
You had order of JOINs wrong. Also, you were missing alias bh. Try:
CREATE VIEW horse_owner
AS
SELECT b.firstname, b.lastname, b.primaryphone,
h.barname
FROM boarder b
INNER JOIN boarder_horse bh
ON bh.boarder_id = b.id
INNER JOIN horse h
ON bh.horse_id = h.id
ORDER BY LastName DESC;
Related
I have a table with 3 columns in my table Travel (and of course some more):
AirportFrom,
AirportTo,
AirportFound.
The columns above display ID's from airports. In the table Airports are 2 columns AirportID and AirportName.
Instead of displaying the ID's from the airports I want to display the AirportNames.
But when I use:
SELECT id
, AirportFrom
, Airports.Airportname
, AirportTo
, Airports.Airportname
, AirportFound
, Airports.Airportname
FROM Travel
LEFT
JOIN Airports
ON AirportTo = Airports.AirportID
-- LEFT JOIN Airports ON AirportFrom = Airports.AirportID
-- LEFT JOIN Airports ON AirportFound = Airports.AirportID
It only displays the airport name of the first join in every column. I want to show the airport name for each of the 3 joins
Provide multiple aliases for joined table each time you left join, and join them as you have multiple tables:
SELECT
Travel.id,
airport_to.Airportname as to_name,
airport_from.Airportname as from_name,
airport_found.Airportname as found_name,
FROM Travel
LEFT JOIN Airports airport_to ON Travel.AirportTo = airport_to.AirportID
LEFT JOIN Airports airport_from ON Travel.AirportFrom = airport_from.AirportID
LEFT JOIN Airports airport_found ON Travel.AirportFound = airport_found.AirportID
EDIT: Replace reserved words in table aliases. Thanks for the reminder!
Your query needs table aliases (for multiple joins to the same table). Then, be sure to use qualified columns names for all columns in a query. This helps you write correct queries and it helps you and other people understand what is going on. So:
SELECT t.id, t.AirportFrom, apt.Airportname,
t.AirportTo, apf.Airportname,
t.AirportFound, apfo.Airportname
FROM Travel t LEFT JOIN
Airports apt
ON t.AirportTo = apt.AirportID LEFT JOIN
Airports apf
ON t.AirportFrom = apf.AirportID LEFT JOIN
Airports apfo
ON t.AirportFound = apfo.AirportID;
I am trying to figure out this question on a practice page online with the following tables:
Question:
For all cases in which the same customer rated the same product
more than once, and in some point in time gave it a lower rating
than before, return the customer name, the name of the product,
and the lowest star rating that was given.
I cant seem to figure out why this isnt correct - would anyone be able to help?
Here is what I have so far (without sample data):
SELECT
Customer.customer_name,
Product.product_name,
MIN(Rating.rating_stars)
FROM Rating
JOIN Product ON Rating.prod_id = Product.prod_id
JOIN Customer ON Rating.cust_id = Customer.prod_id
GROUP BY Customer.customer_name, Product.product_name
HAVING COUNT(Product.prod_id) > 1
This query will return the minimum rating stars of a product that has been reviewed more than once by the same customer, with any of the newer ratings lower than an older rating:
SELECT
r1.prod_id,
r1.cust_id,
MIN(r1.rating_star) AS min_rating
FROM
rating r1 INNER JOIN rating r2
ON r1.prod_id=r2.prod_id
AND r1.cust_id=r2.cust_id
AND r1.rating_date>r2.rating_date
AND r1.rating_star<r2.rating_star
GROUP BY
r1.prod_id,
r1.cust_id
you can then join this query with products and customers table:
SELECT
customer.customer_name,
product.product_name,
m.min_rating
FROM (
SELECT
r1.prod_id,
r1.cust_id,
MIN(r1.rating_star) AS min_rating
FROM
rating r1 INNER JOIN rating r2
ON r1.prod_id=r2.prod_id
AND r1.cust_id=r2.cust_id
AND r1.rating_date>r2.rating_date
AND r1.rating_star<r2.rating_star
GROUP BY
r1.prod_id,
r1.cust_id) m
INNER JOIN customer on m.cust_id = customer.cust_id
INNER JOIN product ON m.product_id = product.product_id
Just a few points:
You cant specify the tables in the FROM clause the sane way you specify attributes in the SELECT. You can only have a single table in the FROM, and one more for each Join you use.
SELECT a, b, c FROM a; <----------fine
SELECT a, b, c FROM a, b; <----not fine
SELECT a, b, c FROM a JOIN b; <---fine
When it comes to the tables in the FROM/JOIN, you dont use "AS" to give them an alias, just the table name followed by the alias.
FROM atable a JOIN btable b; <--This assigns alias a to "atable" and b to "btable".
You also have to specify the common attribute that the tables are going to be joined on:
customer JOIN rating ON customer.cust_id = rating.cust_id;
As for the rest you can probably work out the correct WHERE clauses to use once you have the syntax down.
I haven't been able to figure out how to make this query work.
I have a table for people and their personal data.
I have a table of let's call it houses
Let's say that the tables have these fields:
PEOPLE
id, code, name, lastname
HOUSES
id, codeowner, codeintermediate, codebuyer, area, numberofrooms
If I have three columns with a relationship with the same table (people) how can I make a LEFT JOIN work?
If owners, intermediates and buyers were separated I would use something like
"SELECT
houses.*,
owners.name AS ownersname,
intermediates.name AS intermediatesname,
buyers.lname AS buyersname
FROM houses
LEFT JOIN owners ON houses.codeowner = owners.code
LEFT JOIN intermediates ON houses.codeintermediate = intermediates.code
LEFT JOIN buyers ON houses.codebuyer = buyers.code
But how can I make this work with a single PEOPLE table? How can I use the aliases and so? Thank you beforehand!
Join the people table 3 times
SELECT
houses.*,
owners.name AS ownersname,
intermediates.name AS intermediatesname,
buyers.name AS buyersname
FROM houses
LEFT JOIN people as owners ON houses.codeowner = owners.code
LEFT JOIN people as intermediates ON houses.codeintermediate = intermediates.code
LEFT JOIN people as buyers ON houses.codebuyer = buyers.code
This is my company Table
CompanyID, CompanyName
This is my Contact Table
ContactID, ContactName, CompanyID
This is my Report Table
ReportID, ReportName
This is my ReportContact Table, Many to Many Relationship
ContactID, ReportID
I want to return all ALL my CONTACTID of 1 company, include those who are not assign to any report, I also want to return the reportID that are assign to different contacts
1 contacts can be assign to many reports
1 reports can consist of many contacts
My current SQL CODE only manage to get the 2 contactID in the ReportContactTable
SELECT rc.ContactID, rc.ReportID from contact c INNER JOIN Reportcontact rc on c.ContactID = rc.ContactID Where CompanyID=1
how can Return all the contact include those not in the reportcontact table, but get the reportID at the same times?
INNER JOIN filters out those rows that are not in ReportContact. Try to use LEFT JOIN if you want all contacts from contact table.
SELECT rc.ContactID, rc.ReportID
FROM contact c LEFT JOIN Reportcontact rc
ON c.ContactID = rc.ContactID
WHERE CompanyID = 1
I'm not 100% sure I understand what you are trying to do, but if you want all rows from the contact table then you need to use an OUTER rather than INNER join.
Try changing the word INNER to LEFT.
An INNER join ensures that rows that satisfy the join condition appear in both tables.
An OUTER join says "show me all the rows in one table, plus those that satisfy the join condition from the other table". Which table shows all the rows depends on the use of the keywords LEFT and right
a left join b on a.id = b.id will show all rows from table a plus those from b which satisfy the join condition
a right join b on a.id = b.id will show all rows from table b plus those from a which satisfy the join condition
I have this hierarchy in my database (from lowest to highest):
User => Dept => Area => Company
Now I need to make a table that shows all companies (some info about them taken directly from companies table) but the last column in the HTML table I want to be Number of users. I know I need to join the tables together and perhaps join table to itself, but how do I do this?
Each of these tables have a column linking to its parent table (except Company ofc).
JOIN the tables:
SELECT
c.companyId,
c.CompanyName,
IFNULL(COUNT(u.userID), 0) AS 'Number Of Users'
FROM Company AS c
LEFT JOIN Area AS a ON c.CompanyID = a.CompanyID
LEFT JOIN Dept AS d ON a.DeptId = d.DeptId
LEFT JOIN users AS u ON D.UserId = u.UserId
GROUP BY c.companyId,
c.CompanyName;
Note that: LEFT JOIN with IFNULL will give you those companies that has no matched rows in the other tables; with count zero in this case