Basic SQL (Join and Where)

Asked

Viewed 118 times

0

Good night,

I’m trying to solve a problem which is this::

Tables:

editoras (cod int primary key, nome varchar (50));

autores (cod int primary key, nome varchar (50));

livros (isbn char (13), titulo varchar (50), ano_publicacao int,
qtd_estoque int, valor decimal (10,2), id_editora int);
id_editora referencia Editoras

livros_autores (isbn char (13), id_autor int)
isbn referencia Livros
id_autor referencia Autores 

Tabela povoada

The question asks me to obtain the name(s) (s) of the author(s) who published the cheapest book. Hence, I put the following

select nome from Autores
Inner Join Livros_autores as La 
On  La.id_autor= Autores.cod
Inner Join Livros as Liv 
On La.isbn = Liv.isbn
where valor = (select min(valor) from Livros)

But the name column is not shown :( What do you think is wrong?

  • 2

    The value is not shown because it does not appear in the select field list.

  • The Join should not be by isbn? inner join livros_autores on livros.isbn = livros_autores.isbn? these fields ai of your query do not seem to exist domo the @anonimo mentioned

  • I ended up missing the question, I exchanged id for Cod, because in my query it said id was a reserved word (it was highlighted). I modified the Inner and still nothing appears.

2 answers

1


To get the cheapest ivro from each author do:

SELECT autores.nome, livros.isbn, MIN(livros.valor) 
FROM autores    INNER JOIN livros_autores ON (autores.cod = livros_autores.id_autor)
                    INNER JOIN livros ON (livros_autores.isbn = livros.isbn)
GROUP BY autores.nome, livros.isbn;

If you only want the cheapest add:

ORDER BY 3 ASC LIMIT 1;

0

Your select is not ordering the field valor, so just show the name:

select name from Authors
Inner Join Books_authors as La
On La.id_author= Authors.Cod
Inner Join Books as Liv
On La.isbn = Liv.isbn
Where value = (select min(value) from Books)

Add the field in select:

select nome, liv.valor (...)

EDIT

As mentioned in the comments, the cheapest book has no corresponding relationship in the table livros_autores, causing the condition of the inner join don’t get answered.

If you wish to display the cheapest book THAT you have author, change the subselect condition. But I do not recommend that.
I believe that the ideal is that each book must have a valid author, since there is no book without author.

  • Oh no, it’s just that I forgot to inform... la no query, I ended up putting Cod instead of id, because id was as reserved word.

  • 1

    id is not a reserved Postgresql word as you can see in the documentation: https://www.postgresql.org/docs/11/sql-keywords-appendix.html

  • Anyway, now that I understand the problem. I edited the answer, @Madsonrocha

  • She was featured kkkkk, so I thought it was reserved, sorry.

  • Still not showing, Ronaldo. The error is in Where, because when I test the other lines of code, the following table appears: Name ------- Value João --------- 60.00 José ---------- 80.00 Lúcia --------- 90.00 Maria -------- 70.00 João ---------- 90.00 Carlos ------- 80.00 José ------- 50.00

  • In fact, it shows nothing, neither name nor value. Only shows the columns.

  • If it does not show anything, edit the question and say that it does not return any record, because saying that "the X field is not selected" implies that only the field is not displayed. (and follow this to the next questions you ask)

  • Check if the book of least value has a corresponding link in the table Livros_autores, and whether it has a corresponding livros

  • Sorry for the error. Link. The author who should appear is José, all data in the table matches so that it is shown on the screen.

  • 1

    For the data that you presented in your question the lower value 40.00 of book 863 and book 863 does not appear in the table book_authors, therefore there is no record in your join that meets the criteria. José appears as the author of books 425 and 213 with values 80,00 and 50,00.

  • Exactly, this makes José have the cheapest book, since the owner of the book isbn 863 does not have in the table books_authors.

  • I’m using Inner Join to compare the values of the tables Authors, Books_authors and Books and the only data that matches is José.

  • @Madsonrocha Not really. The cheapest book remains the 40$. Remember that your subselect is (select min(valor) from livros), he considers only the value, not the relationships.

  • So how do I show the result of relationships? Taking into account the cheapest value.

  • Adding the inner join in subselect as in select

  • I think you have misunderstood how INNER JOIN works and where the WHERE clause applies. Try using the GROUP BY clause with the MIN aggregation function to the result of your junctions.

  • I will study well and correct the mistakes in the next issues. I appreciate the willingness and patience to help me <3.

Show 12 more comments

Browser other questions tagged

You are not signed in. Login or sign up in order to post.