How to do Sub Select in SQL

1

I would like to know if anyone can help me because I could not do the sub select command between two tables, could anyone help me?

I need to pull the name of the book that is in the book table together with the data in the sales table.

Table vendas :

Tablelivros:

    
asked by anonymous 27.08.2018 / 21:11

2 answers

4

Answering your question:

What you want is not subselect but rather JOIN .

Example of JOIN whereas your sale table will always have 1 book referring to the sale:

SELECT * 
FROM VENDAS VD
INNER JOIN LIVROS LV ON LV.IDLIVRO = VD.IDLIVRO

Filtering some fields:

SELECT VD.IDVENDA, LV.TITULO, LV.PRECO
FROM VENDAS VD
JOIN LIVROS LV ON LV.IDLIVRO = VD.IDLIVRO

>

Extra examples:

In order to bring ALL values of the table INNER JOIN even though did not have filled in the JOIN field in the% JOIN :

SELECT * 
FROM VENDAS VD
LEFT JOIN LIVROS LV ON LV.IDLIVRO = VD.IDLIVRO

In order to bring ALL values of the table INNER even though did not have filled in the VENDAS field in the% IDLIVRO :

SELECT * 
FROM VENDAS VD
RIGHT JOIN LIVROS LV ON LV.IDLIVRO = VD.IDLIVRO

Reference to VENDAS that would be nice to see as an example:

Select only tuples from a table with JOIN

    
27.08.2018 / 21:25
-1

The join would be a solution as mentioned above, you could also do the following:

select * from Vendas
where idlivro in (
  select idlivro from livros where idgenero = 1
);
    
27.08.2018 / 21:32