SQL Select statement - multiple tables allow null values - sql

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

Related

SQL question on correlated sub-queries using windowing rank functions

I have this SQL homework assignment that tells me to list all the customers and the most recent DVD which they have rented, including the title, Genre, Rating, DVD and date of rental. This can be solved via a correlated sub-query or a window rank function
Here is a screenshot of the schema:
Here is what I have tried so far:
Select
Concat(m.MemberFirstName, ' ', m.MemberLastName) as Member
, d.DvdTitle
, g.GenreName
, rt.RatingName
, r.RentalRequestDate
From
Member m
Inner Join
RentalQueue rq on m.MemberId = rq.MemberId
Inner Join
DVD d on d.DVDId = rq.DVDId
Inner Join
Genre g on g.GenreId = d.GenreId
Inner Join
Rating rt on rt.RatingId = d.RatingId
Inner Join
Rental r on r.DVDId = d.DVDId
I am not sure how I can use correlated subqueries to answer the question above as I am quite new to correlated subqueries and I would appreciate some help on this. Thanks in advance.
Try something like this, but please try to understand it too.
Select
Concat(m.MemberFirstName, ' ', m.MemberLastName) as Member
, mostrecent.*
From
Member m
CROSS APPLY
(select top (1)
d.DvdTitle
, g.GenreName
, rt.RatingName
, r.RentalRequestDate
FROM
RentalQueue rq
Inner Join
DVD d on d.DVDId = rq.DVDId
Inner Join
Genre g on g.GenreId = d.GenreId
Inner Join
Rating rt on rt.RatingId = d.RatingId
Inner Join
Rental r on r.DVDId = d.DVDId
where m.MemberId = rq.MemberId
order by r.RentalRequestDate desc
) mostrecent

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

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.

Fetching columns, 4 tables

I’ve ran in to some problems with the following SQL assignment. I’m to retrieve the ID, firstname and lastname of any member who is registered in section that contains the word ‘xyz’.
So far I’ve managed the following:
SELECT m.id, p.firstname, p.lastname FROM member m
INNER JOIN person p ON m.id = p.id
WHERE m.id IN (SELECT id FROM membersection);
How do I go forward from here? I have no idea how to retrieve the sectionid from the membersection table then fetch the section name from the section id using that ID so I can check if the section name contains the previously stated word.
member:
id
member_number
registration_date
membersection:
memberid
sectionid
person:
id
firstname
lastname
section:
id
name
Just keep joining. And in the end use LIKE to check if section.name contains 'xyz'.
SELECT m.id,
p.firstname,
p.lastname
FROM member m
INNER JOIN person p
ON p.id = m.id
INNER JOIN membersection ms
ON ms.memberid = m.id
INNER JOIN section s
ON s.id = ms.sectionid
WHERE s.name LIKE '%xyz%';
There is some ambiguity in your question with regards to how your data are structured; what are the primary and foreign keys?
But, making some assumptions, you're almost there, you can chain multiple join statements together:
select
m.id,
p.firstname,
p.lastname
from
member m
inner join person p on
m.id = p.id
inner join membersection ms on
m.id = ms.memberid
inner join section s on
ms.sectionid = s.id
where
s.name like '%xyz%'
It's not super obvious what's going on with your Data Relationships, but this would be the basic route you might want to take (LEFT JOIN as I do not know the relationship):
SELECT m.id, p.firstname, p.lastname, ms.sectionid
FROM member m
INNER JOIN person p ON m.id = p.id
LEFT JOIN section s ON m.id = s.id
LEFT JOIN membersection ms ON m.id = ms.memberid
WHERE s.name = 'xyz'

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