sql, selecting using condition from another table - sql

I have four tables,
game(game_id, game_name, date_of_release),
category(game_id, game_category),
company(company_id, company_name),
belongs_to(game_id, company_id)
I need to select game_id, game_name, and game_language where the company is 'Konami', I tried to do it like this
SELECT a.game_id, a.game_name, b.game_category, FROM game a
INNER JOIN category b ON a.game_id = b.game_id
INNER JOIN company c ON company_name = 'Konami'
INNER JOIN belongs_to d ON c.company_id = d.company_id
but it's selecting all the games regardless of the company name.
So what went wrong?

Your join is wrong.
SELECT
...
FROM
game a
INNER JOIN category b ON b.game_id = a.game_id
INNER JOIN belongs_to d ON d.game_id = a.game_id
INNER JOIN company c ON c.company_id = d.company_id
WHERE
c.company_name = 'Konami'

It seems that you not really understand joins. You should use them to connect tables, in you case
INNER JOIN company c ON company_name = 'Konami'
You connected row with info about Konami to all rows in previous tables. Try to think only about how you connect tables, not how you filter it while writing join clause
So, you can fix it like this: just add condition that shows how to connect tables
SELECT a.game_id, a.game_name, b.game_category
FROM game a
INNER JOIN category b ON a.game_id = b.game_id
INNER JOIN belongs_to d ON a.game_id = d.game_id
INNER JOIN company c ON c.company_id = d.company_id
WHERE company_name = 'Konami'
It's better to filter in where clause, but this will work fine too
SELECT a.game_id, a.game_name, b.game_category
FROM game a
INNER JOIN category b ON a.game_id = b.game_id
INNER JOIN belongs_to d ON a.game_id = d.game_id
INNER JOIN company c ON c.company_id = d.company_id
AND company_name = 'Konami'

Related

Column '' in field list is ambiguous. (when JOIN ON)

Column 'company_id' in field list is ambiguous
It does not seem "ambiguous", I have no idea where I should fix it:
SELECT company_id, companies.name
FROM company_contracts AS contracts
LEFT OUTER JOIN companies ON companies.id = contracts.company_id
LEFT OUTER JOIN order_logs AS logs ON companies.id = logs.company_id;
Because company_id appears on both table company_contracts and order_logs,you need to specify it
SELECT contracts.company_id,c1.name
FROM company_contracts as contracts
LEFT OUTER JOIN companies as c1 on c1.id = contracts.company_id
LEFT OUTER JOIN order_logs as logs on c1.id = logs.company_id;
You should qualify all columns names in such a query. In addition, if you really want outer joins, the second join condition should refer to the first table, not the second:
SELECT cc.company_id, c.name
FROM company_contracts cc LEFT OUTER JOIN
companies c
ON c.id = cc.company_id LEFT OUTER JOIN
order_logs ol
ON cc.company_id = ol.company_id;
Or, more likely, you want to keep all companies and the query should look like:
SELECT c.id, c.name
FROM companies c LEFT OUTER JOIN
company_contracts cc
ON c.id = cc.company_id LEFT OUTER JOIN
order_logs ol
ON c.id = ol.company_id;

retrieve people on same flight

I want to retrieve all people on same flight.
Db Model looks like:
Airplane(airplane_id),airplane_name,modelno)
Passenger (id,firstname,lastname,...)
Booking(booking_id,passenger_id,destination_id,flight_date,....)
Destinations(id,airplane_id,route,distance)
What do i need to correct in this query
SELECT * FROM Passenger as P
left join Booking as B
on P.id = B.passenger_id
left join Destinations as D
on D.id = B.destination_id
left join Airplane as A
on A.airplane_id = D.airplane_id;
How will i use where statement??
Your main issue is that you are joining on all the wrong Ids. See below for what they should be based on your column names. (If this is incorrect I would highly recommend changing your column names so they are less confusing). I have also included a sample where clause, you can put any condition you like in there though.
SELECT P.*
FROM
Passenger AS P
JOIN Booking AS B ON P.Id = B.Passenger_Id
JOIN Destinations AS D ON D.Id = B.Destination_Id
JOIN Airplane AS A ON A.Airplane_Id = D.Airplane_Id
WHERE
D.flight_date = '3/16/2021'
you can execute this query, then add a where clause to filter the results
SELECT *
FROM Airplane a
inner join Booking b on b.airplane_id = a.airplane_id
inner join Passenger p on p.id = b.passenger_id
inner join Destination d on d.id = b.destination_id

How can I connect multiple tables using an inner join?

I am trying to select and print customer_name(customer table), room_number(room table), room_rate (room table),rate_type(reservation table), checkin_date(reservation table), checkout_date(reservation table), billing_amount(billing table) from multiple tables. I tried to do an inner join but the error is 'column ambiguously defined '
SELECT *
FROM Customer c
INNER JOIN Reservation r
ON c.customer_id = r.customer_id
INNER JOIN Room s
ON s.room_id = r.room_id
INNER JOIN billing b
ON reservation_id = b.reservation_id
WHERE c.customer_name = 'John Scott';
You're missing the table alias on the left side of the last JOIN. Try this:
SELECT *
FROM Customer c
INNER JOIN Reservation r
ON c.customer_id = r.customer_id
INNER JOIN Room s
ON s.room_id = r.room_id
INNER JOIN billing b
ON r.reservation_id = b.reservation_id
WHERE c.customer_name = 'John Scott';
One problem is in your join and one in your select:
SELECT *
-------^ duplicate column names
FROM Customer c INNER JOIN
Reservation r
ON c.customer_id = r.customer_id INNER JOIN
Room s
ON s.room_id = r.room_id INNER JOIN
billing b
ON reservation_id = b.reservation_id
--------^ missing table alias
WHERE c.customer_name = 'John Scott';
If the only duplicate columns are the JOIN keys, then the USING clause (standard but not supported by all databases) is a handy way to get all the columns:
SELECT *
FROM Customer c INNER JOIN
Reservation r
USING (customer_id) INNER JOIN
Room s
USING (room_id) INNER JOIN
billing b
USING (reservation_id)
WHERE c.customer_name = 'John Scott';
When you using USING, the key columns are not duplicated with SELECT *.

Query table using inner join?

We are trying to find the names of the customers who have both a loan and an account in the same branch. Should we use inner join here? So far we have only written;
select DISTINCT customer.name
FROM Customer, Has_Loan, Branch, Has_Account
WHERE
We have tried a few different things without getting any further, so appreciate any kind of help or hint:)
You can try this :
SELECT DISTINCT Customer.Name
FROM Customer
INNER JOIN Has_Account ON Customer.Ssn = Has_Account.Assn
INNER JOIN Account ON Account.AccountNo = Has_Account.ANo
INNER JOIN Has_Loan ON Customer.Ssn = Has_Loan.Lssn
INNER JOIN Loan ON Loan.LoanNo = Has_Loan.LNo
WHERE Loan.BranchID = Account.BranchID
select c.name
from customer c
join has_loan hl on hl.lssn = c.ssn
join loan l on hl.lno = l.loanno
join has_account ha on ha.assn = c.ssn
join account a on ha.ano = a.accountno
join branch b on b.branchid = a.branchid
where l.branchid = a.branchid
group by c.name;

Join query equivalent to Not IN clause (SQL Server 2008)

I have a query with a not in clause like this:
select *
FROM COMPANY c
where c.company_id not In (SELECT SenderId
FROM CrossRef)
and c.id not in (select company_id
FROM USER)
I am wondering if there is a way to re-write that query using a left join in SQL Server 2008.
I tried the following one but it's not giving the correct result
select c.id, c.company_id
from COMPANY c
left join CrossRef cr on c.company_id != cr.senderid, COMPANY c1
left join USER u on c1.id != u.company_id
SELECT *
FROM Company C
LEFT JOIN CrossRef R ON R.SenderID = C.CompanyID
LEFT JOIN [User] U ON U.company_id = C.id
WHERE R.SenderID IS NULL
AND U.company_id IS NULL