retrieve people on same flight - sql

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

Related

How to inner join with multiple tables for 3NF Normalization in SQL

I am trying to create normalization 3 nf (normal form) in this database. However, when I execute the query, the table is empty. As you will see, this is my table and diagram.
Here is the relationship that we want to 3NF
Torder->(Customer-Food-Carrier-Waiter)-(Ternary Relationship)-(Customer_id,Food_id,Carrier_id,Waiter_id)
Here is my query
SELECT
Customers.ID AS CustomerID,
Food.ID AS FoodID,
Carrier.ID AS CarrierID,
Waiter.ID AS WaiterID,
tOrder.ID AS TORDERID
FROM
tOrder
INNER JOIN
Customers ON Customers.ID = tOrder.Customer_id
INNER JOIN
Food ON Food.ID = tOrder.Food_id
INNER JOIN
Carrier ON Carrier.ID = tOrder.Carrier_id
INNER JOIN
Waiter ON Waiter.ID = tOrder.Waiter_id
ORDER BY tOrder.ID;
Clearly, you have an issue where some of the tables are empty or the ids do not match. You can use LEFT JOIN to keep all orders and see what is happening:
FROM tOrder o LEFT JOIN
Customers c
ON c.ID = o.Customer_id LEFT JOIN
Food f
ON f.ID = o.Food_id LEFT JOIN
Carrier ca
ON ca.ID = o.Carrier_id LEFT JOIN
Waiter w
ON w.ID = o.Waiter_id
If there are no matches, then the orders are still in the results, with NULL for the values in the table with no matches.
Note that this also introduces table aliases, so the query is easier to write and to read.

Joining tables and finding values that do not exist

I am having some issues with joining tables to get null values, and I can't find what I am doing wrong.
The case: I am trying to make a cinema system, where I have made entities that match the cinema.
I have a Hall, Row and Seat table, and a Show table that holds the value for movies and what hall it will be played in. To bond everything together, I have made a Reservation table that is keeping track of what seats to that specific show is taken.
My entities look like this:
My problem: I am trying to fetch all free seats for the show, I can get all seats for the show, but when I try to add the Reservation to get the free ones I get no records.
My query that is able to fetch all seats:
SELECT show.id AS ShowID,
seat.id AS SeatID,
seat.rowid AS RowID,
show.hallid AS HallId,
reservation.seatid AS Expr1
FROM show
INNER JOIN hall
ON show.hallid = hall.id
FULL OUTER JOIN seat
ON hall.id = seat.hallid
LEFT OUTER JOIN reservation
ON reservation.showid = show.id
WHERE ( show.id = 1 )
AND ( reservation.seatid IS NULL )
ORDER BY reservation.showid,
rowid
You need INNER joins between Show, Hall, Row and Seat and a LEFT join to Reservation, so you can filter out the matched rows:
SELECT s.Id AS ShowID, t.Id AS SeatID, t.RowId AS RowID, s.HallId
FROM Show s
INNER JOIN Hall h ON h.Id = s.HallId
INNER JOIN Seat t ON t.HallId = h.Id
INNER JOIN Row w ON w.HallId = h.Id AND w.Id = t.RowId
LEFT JOIN Reservation r ON r.ShowId = s.Id AND r.HallId = h.Id AND r.SeatId = t.Id AND r.RowId = w.Id
WHERE (s.Id = 1) AND (r.SeatId IS NULL)
Replace:
INNER JOIN Hall ON Show.Id = Hall.Id FULL OUTER JOIN
With:
INNER JOIN Hall ON Show.HallId = Hall.Id FULL OUTER JOIN
While it might not be the full answer to your question, i think this might cause issues for you too.

How to Join 3 Tabels in Sqlserver

I tried to join 3 tables all together but when I execute the query only the first row on the table is displayed. The table consists of many rows. How can I display multiple rows?
This is the code that I tried
SELECT
l.id,m.name,b.bookname,l.issuedate,l.returndate
FROM lend l
INNER JOIN member m
on l.memberid = m.id
INNER JOIN books b ON b.id = l.bookid
Member table
enter image description here
Books table
enter image description here
lend table
enter image description here
data i need to join
ID membername Bookname issue_date return_date
This is my first post.
It is possible that one of the other tables that you have joined to does not contain the other 2 records. In order to see all records from [lend], you would do this:
SELECT l.id, m.name, b.bookname, l.issuedate, l.returndate
FROM lend l
LEFT OUTER JOIN member m ON l.memberid = m.id
LEFT OUTER JOIN books b ON b.id = l.bookid
I am not sure which table contains the records that you are interested in, but select from only that table first. Check the record count. Then LEFT OUTER JOIN to one more table. Execute. Check record count. Keep going. That way you know which table join has affected the record count.
if you want all records of all tables:
SELECT
l.id,m.name,b.bookname,l.issuedate,l.returndate
FROM lend l
FULL OUTER JOIN member m
on l.memberid = m.id
FULL OUTER JOIN books b ON b.id = l.bookid
if you wand only records that are related with lend:
SELECT
l.id,m.name,b.bookname,l.issuedate,l.returndate
FROM lend l
left JOIN member m
on l.memberid = m.id
left JOIN books b ON b.id = l.bookid

SQL LEFT JOIN for joining three tables but one with to exclude content

I have 3 tables
STUDENTS
FEES_PAID
SUSPENDED
I want to get the details of the students who have paid the fees but not from SUSPENDED.
SELECT
ID
FROM
STUDENTS s
LEFT JOIN
SUSPENDED p ON s.ID = p.ID
INNER JOIN
FEES_PAID f ON f.ID = s.ID
WHERE
s.ID IS NULL
Unfortunately this does not work. Can any one suggest an efficient query?
Thanks in advance
You need to check if the second table is missing from the LEFT JOIN. So, you need to look at a column in that table. Change the WHERE to:
WHERE p.ID IS NULL
Alternatively, use NOT EXISTS:
SELECT s.ID
FROM STUDENTS s INNER JOIN
FEES_PAID f
ON f.ID = s.ID
WHERE NOT EXISTS (SELECT 1 FROM SUSPENDED p WHERE s.ID = p.ID);
Note that for both these queries, you will need to qualify the ID in the SELECT to specify the table where it comes from.
This should work:
SELECT
s.ID
FROM
STUDENTS s
LEFT JOIN
SUSPENDED p
ON s.ID=p.ID
INNER JOIN
FEES_PAID f
ON f.ID= s.ID
WHERE
p.ID IS NULL

SQL Select statement - multiple tables allow null values

I do realise that problem I am facing is not a rocket science but still, I did not find any information about fixing this.
I have multiple tables in my database (PSQL) I want to create a select query to make a reporting function for my app.
Here is my query:
select
s.id, s.name, st.name, p.firstname || ' ' || p.lastname,
f.name, f.store_date, bdt.name, bd.comment
from
system s, systemstatus st, role w, person p, file f,
documenttype bdt, document bd
where
w.system_id = s.id and
p.id = w.person_id and
st.id = s.status_id and
bd.system_id = s.id and
bd.file_id = f.id and
bd.type_id = bdt.id and
bd.role_id = w.id;
Query works I get 300 rows fully filled with values I am searching for. Problem is that I have about 1000 rows in System Table. It is possible that there is no Person or Document which could be linked with particular System.
I would like to see all rows that are in my System table (I mean about 1000), and when I can't link Person or Document with System I want the field to be null ( now it is not shown at all)
Main part of the answer is - you need left outer join.
Additional parts of the answer - use ANSI join syntax and format your queries:
select
s.id, s.name, st.name, p.firstname || ' ' || p.lastname, f.name,
f.store_date, bdt.name, bd.comment
from system as s
left outer join systemstatus as st on st.id= s.status_id
left outer join role as w on w.system_id = s.id
left outer join person as p on p.id = w.person_id
left outer join document as bd on bd.system_id = s.id and bd.role_id = w.id
left outer join documenttype as bdt on bdt.id = bd.type_id
left outer join file as f on f.id = bd.file_id
Always remember that somebody will read your code someday (may be it will be future you :) ) - so readability counts!
You are connecting your tables in query via "where" clause, which is equal to inner join. Instead you should left join Person and Document tables to System table, e.g.:
select *
from system s
left join role w on w.system_id = s.id
left join person p on p.id = w.person_id