How to summarize many to many relationship to output column in SQL - sql

I have tables books, authors and books_authors in a many to many relationship.
What I want to do is select all books AND add a column to the output with a string of the authors ids for each book. I know how to do the joins to find the authors for each book but that gives me x many rows for each book as there are authors for that book. I want one row per book. Thanks

Related

How to combine two databases?

I am asking myself how to best define a new database given two databases.
I have the following two databases
Author where I save information about authors i.e. Name, Birthyear, Country, Genre etc.
Book where I save information about the book i.e. AuthorName, ReleaseYear, Pages etc
How do I create a new database with the full information about all books and the author? I.e. a Book_Complete Database which includes Author.Name, ReleaseYear,Pages, Genre,ReleaseYear and Author.Birthyear, Author.Country?
Better to go for a single database and having 2 tables in it like
Author Table
AuthorId (PK)
Name
Birthyear
Country
Genre
Book Table
BookId (PK)
AuthorId (FK)
ReleaseYear
Pages
If you have two tables in a database you can combine them using JOIN. Here is SQLite tutorial on how to use JOIN.
https://www.sqlitetutorial.net/sqlite-join/
On the information you provided I assume you can you columns Name in table Author, and AuthorName in table Book. You can try something like this
SELECT
A.Name,
B.ReleaseYear,
B.Pages,
B.Genre,
B.ReleaseYear,
A.Birthyear,
A.Country
FROM
Author A
LEFT JOIN Book B
ON A.Name = B.AuthorName
Basically, I have these create these two tables on my app and want to store the combined version on a server. Or would you suggest another approach?
Yes we would. You should have one table per entity, i.e. one for the authors and one for the books. Thus you don't store data redundantly (e.g. the author's name or the book's title), which could otherwise lead to inconsistencies later. You can read up on database normalization here: https://en.wikipedia.org/wiki/Database_normalization or google it.
Now you must decide how to link the tables. One author can write multiple books. Can one book be written by multiple authors? Give both tables IDs you can work with (or find some natural unique key, like the ISBN for the book and maybe name + birth year for the author - the name only will probably not suffice and maybe it won't even when combined with the year).
If one book can only be written by one author, you have a 1:n relation (one book written by one author, one author writing multiple books).
author (author_id, name, birth_year, country, ...)
book (book_id, title, author_id, release_year, pages, ...)
If one book can have several authors, you have an m:n relation (one book written by multiple authors, one author writing multiple books).
author (author_id, name, birth_year, country, ...)
book (book_id, title, release_year, pages, ...)
book_author (book_id, author_id)
You can always join the tables, so as to get a combined result.

SQL sub-queries with no joins

I am really struggling with one of the questions while revising. I guess you guys can help me out.
Here I have two tables named book and branch
Branch
Book
The question is:
List the title and author of books whose sales are greater than the
average sales. For each such book, also list the difference between
its sales and the average sales. The column of differences in the
table of results should be named "Difference".
Here is what I tried
SELECT title, authorFROM book
WHERE sales > AVG (sales) ( SELECT bookNo AS Difference
FROM book
WHERE Difference= sales-AVG(sales));
You want to create a table of results with three columns: title, author and Difference(the difference between its sales and the average sales). In sql you can do math in your select expressions, so you can just add (sales-AVG(sales)) to the list of columns. To specify the name you can use the keyword AS.
SELECT title, author, (sales-AVG(sales)) AS Difference FROM book WHERE sales>AVG(sales)

Issues with Group functions

I'm working on lab question that I just cannot come up with a solution, that would allow me to figure out the books that have more then one author(See the posted question below for my comments to make sense). My mind is totally blank on it. I'm very bad with word problems. I know that I have to do a JOIN statement which I've completed and I know that I have to use the COUNT function to count the number of authors but I honestly don't know how I would go about only counting the books that have 2 authors.
Any input would be appreciated. I tried to break it down into steps but it's just that one part that I'm not grasping in my mind.
Using the correct tables in your schema, create a query using either join operation you wish that will list the book title and number of authors for all books that have been written by more than one author. Give the title column an alias of "Book Title" and the column showing the number of authors an alias of "Number of Authors".
There is a BOOKS table and a AUTHOR table that are JOINED by a BOOK_AUTHOR table by their BOOKID in BOOKS and AUTHORID in AUTHOR.
I think I'm starting to understand that I have to use a mathematical equation to figure out more then one author. I don't understand the HAVING function all too well so I'm going to do more research on this one.
You were right, you need a JOIN, a COUNT, but also an HAVING to make sure there is more then one authors that written the book :
select title as 'Book Title', count(authors) as 'Number of Authors'
from books
join authors on books.id = authors.book_id
having count(authors) > 1
group by authors;
Make sure to adapt the table names and columns to the right one, as you didn't post them.
Note that if the books's author id column has the same name as the author id, you can use the USING keyword to join. then your query would become
select title as 'Book Title', count(authors) as 'Number of Authors'
from books
join authors on using(book_id)
having count(authors) > 1
group by authors;
Note that if you want to select only books that specifically have 2 authors you can change the having clause to having count(authors) = 2. But even if you ask for that in your question, according to the exercice you pasted, you did not understand properly the question.
I finally figured out the answer with all your help. Please see below:
SELECT DISTINCT Title "Book Title", COUNT(*)"Number of Authors"
FROM BOOKS JOIN
BOOK_AUTHOR ON
BOOKS.BookId = BOOK_AUTHOR.BookId
JOIN AUTHOR ON
BOOK_AUTHOR.AuthorId = Author.AuthorId
GROUP BY Title
HAVING COUNT(*) > 1;

Linking Three Tables together

I'm creating an archive for Academic Papers. Each paper may have one author, or multiple authors. I've created the tables in the following manner:
Table 1: PaperInfo - Each row contains information on the paper
Table 2: PaperAuthor - Only Two Columns: contains PaperID, and AuthorID
Table 3: AuthorList - Contains Author Information.
There is also a Table 4 which is linked to Table 4, which contains a list of Universities which the author belongs to, but I'm going to leave it out for now in case it gets too complicated.
I wish to have a Query which will link all three tables together, and display Paper Information of the recordset in a table, with columns such as these:
Paper Title
Paper Authors
The column "Paper Authors" is going to contain more than one authors in some cases.
I've wrote the following query:
SELECT a.*,b.*,c.*
FROM PaperInfo a, PaperAuthor b, AuthorList c
WHERE a.PaperID = b.PaperID AND b.AuthorID = c.AuthorID
So far, the results I've been getting for each row is one author per row. I wish to contain more authors in one column. Can this be done in anyway?
Note: I'm using Access 2010 as my database.
In straight SQL the answer unfortunately is that it isn't possible. You would need to use a processing language in order to get the result you are after.
Since you mention you are using Access 2010 please refer to this question: is there a group_concat function in ms-access?
Particularly, read the post which points to http://www.rogersaccesslibrary.com/forum/generic-function-to-concatenate-child-records_topic16&SID=453fabc6-b3z9-34z6zb14-a78f832z-19z89a2c.html
You probably need to implement a custom function but the 2nd url does what you are looking for.
This functionality is not part of the SQL standard, but different vendors have solutions for it, see for instance Pivot Table with many to many table, MySQL pivot table.
If you know the maximum number of authors per paper (for example 3 or 4), you could get away with a triple or quadruple left join.
What you are after is an inner join.
An SQL JOIN clause is used to combine rows from two or more tables, based on a common field between them.
The most common type of join is: SQL INNER JOIN (simple join). An SQL INNER JOIN return all rows from multiple tables where the join
condition is met.
http://www.w3schools.com/sql/sql_join.asp
You may want to combine the inner join with a group to give you 1 paper to many authors in your results.
The GROUP BY statement is used in conjunction with the aggregate
functions to group the result-set by one or more columns.
http://www.w3schools.com/sql/sql_groupby.asp

Create a relation

I have no idea about how to create relation and solve their queries. I want to relation as follows:
BookAuthor(book, author, earnings)
BookReference(book, referenceBook, times)
BookReview(book, reviewer, score)
BookPublih(book, year, publisher, price, numbar)
In these database, each book may have 1 or more authors & each author may make a diff. amt. of money from that book. One book may make reference to other book.1 book may be reviewed by diff. reviewers and get diff. scores. an author could also be a reviewer & a publisher.
I want to solve following queries
Find all books published in 2003 & reviewed by both Sammer Tulpule & Hemant Mehta.
Find all the reviewers who never reviewed their own books.
Find all authors who reviewed more than 2 books written by Sita Mitra.
Find all authors who have written exactly 1 book and reviewed more than 1 book.
Find all the reviewer who reviewed every book from 'Stephen King'.
Find all books published in 1995-2000 in descending order.
I know, these is not good to find an answer, but believe me i really don't under
A solution to your problem could use these tables:
BOOKS (BookID, BookName,PubDate,PublisherPerson_ID,Price,Number) //stores each book and its data
PERSONS(PersonID, Name) //stores any people (authors, reviewers)
BOOKAUTHORS(Book_ID, Person_ID) //many to many relationship between books and authors
BOOKREVENUE(Book_ID, Person_ID, Revenue) //stores revenue for each book per author
BOOKREFERENCES(Book_ID, RefBook_ID) //many to many reference table for books
BOOKREVIEWERS(Book_ID, Person_ID, Score) //many to many relationship for book reviews
Im not going to write the queries here, but this will get you started